var SITE_URL = "http://www.kjbromell.co.uk";
function getPageSize(){

	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;

	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		//xScroll = document.body.scrollWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.scrollWidth;
		//windowHeight = document.body.offsetHeight;
		windowHeight = document.documentElement.scrollHeight;
		//windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	if (navigator.appName == "Microsoft Internet Explorer")
		{yposition = document.body.scrollTop;}
	else {yposition = window.pageYOffset;}

	if (navigator.appName == "Microsoft Internet Explorer")
		{xposition = document.body.scrollLeft;}
	else {xposition = window.pageXOffset;}	

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight, xposition, yposition);
	return arrayPageSize;
}
function init() 
{
	if (arguments.callee.done) return;
	arguments.callee.done = true;

	      //DOM has now loaded!
	      //alert("hi");
		FullScreenContainer();

};

	/* for Mozilla */
if (document.addEventListener) 
{
	document.addEventListener("DOMContentLoaded", init, false);
}

	/* for Internet Explorer */
	/*@cc_on @*/
	/*@if (@_win32)
                document.write("<script defer src="+SITE_URL+"/ie_onload.js><"+"/script>");
        /*@end @*/

	/* for other browsers */
	window.onload = init;
	window.onresize = function()
	{
		FullScreenContainer();
	}

	function FullScreenContainer()
	{
		var arrSizes = getPageSize();

		
		//alert("called");
	       //Does the container Exists?!!1
		if(document.getElementById("contentarea"))
		{
			//Doe the height of the container equal less than the screen height.!!
//			document.title = "Screen "+arrSizes[3]+" Div Height "+document.getElementById("container").offsetHeight+"";
			//alert("Screen "+arrSizes[3]+" Div Height "+document.getElementById("pagecontainer").offsetHeight);

			//Clear height!
			document.getElementById("contentarea").style.height = "";

			if (document.getElementById("contentarea").offsetHeight<arrSizes[3])
			{
				//alert("Expand");
				document.getElementById("contentarea").style.height = ""+arrSizes[3]+"px";
			}
		}
	}


//CONTACT PAGE RELATED JAVASCRIPT
/////////////////////////////////////

function validate_contact_form()
{
	errstr = "";
	errcount = 0;

	//Check name
	if(document.getElementById("contact_name").value.length < 1)
	{
		errstr += "You must enter a name before submitting the contact form\n";
		errcount++;
	}

	//Check email
	email = document.getElementById("contact_email").value;
	
	if(email.length < 1)
	{
		errstr += "You must enter a contact email address before submitting the contact form\n";
		errcount++;
	}
	else
	{
		if(!validEmail(email))
		{
			errstr += "You must enter a valid email address before submitting the contact form\n";
			errcount++;
		}
	}

	//Check query text
	if(document.getElementById("contact_query").value.length < 1)
	{
		errstr += "You must enter some text in the query area before submitting the contact form\n";
		errcount++;
	}

	if(errcount == 0)
	{
		document.getElementById("contact_form").submit();
	}
	else
	{
		alert(errstr);
	}
}

function validEmail(email)
{
	var rPattern = "^[^0-9][A-z0-9-_]+([.][A-z0-9_-]+)*[@][A-z0-9_-]+([.][A-z0-9_-]+)*[.][A-z]{2,4}$";
	var re = new RegExp(rPattern);

	if (email.match(re)) 
	{
		return true;
	} 
	else 
	{
		return false;
	}
}


var out_fade =
{
fadeout : function (id,curropacity,opacityToFade,bHidden)
	  {
		  var opacity = curropacity;
		  if(bHidden)
		  {
			  document.getElementById(id).style.display='';
		  }
		  this.fadeLoop(id, opacity,opacityToFade); // remember its not  "->" function name.. its "."
	  },
fadeLoop : function (id, opacity,opacityToFade)
	  {
		  var object = document.getElementById(id);
		  if (opacity <= opacityToFade)
		  { // just encase i forget 99% due to a firefox bug..
			  out_fade.setOpacity(object, opacity);
			  opacity += 15;
			  window.setTimeout("out_fade.fadeLoop('" + id + "', " + opacity + "," + opacityToFade + ")", 30);
		  }
		  else
		  {
			  ajaxrunning=false;
		  }
	  },
setOpacity : function (object, opacity)
	  {
		  object.style.filter = "alpha(style=0,opacity:" + opacity + ")";	// IE
		  object.style.KHTMLOpacity = opacity / 100;				// Konqueror
		  object.style.MozOpacity = opacity / 100;					// Mozilla (old)
		  object.style.opacity = opacity / 100;					// Mozilla (new)
	  }
}


var in_fade =
{
fadeout : function (id,curropacity,opacityToFade,bHide)
	  {
		  var opacity = curropacity; 
		  this.fadeLoop(id, opacity,opacityToFade,bHide); // remember its not  "->" function name.. its "."
	  },

fadeLoop : function (id, opacity,opacityToFade,bHide)
	  {
		  var object = document.getElementById(id);
		  if (opacity >= opacityToFade)
		  {
			  in_fade.setOpacity(object, opacity);
			  opacity -= 15;
			  window.setTimeout("in_fade.fadeLoop('" + id + "', " + opacity + "," + opacityToFade + ","+ bHide +")", 30);
		  }
		  else
		  {
			  if(bHide)
			  {
				  object.style.display='none';
			  }
		  }
	  },
setOpacity : function (object, opacity)
	  {
		  object.style.filter = "alpha(style=0,opacity:" + opacity + ")";	// IE
		  object.style.KHTMLOpacity = opacity / 100;				// Konqueror
		  object.style.MozOpacity = opacity / 100;					// Mozilla (old)
		  object.style.opacity = opacity / 100;					// Mozilla (new)
	  }
}

function zoomImage(imgsrc,caption,path,w,h)
{

  //document.getElementById("page").style.display = 'none';
//  document.getElementById("msh-contentarea").style.width = '100%';

	imgsrc++;
	if(imgsrc<10)
	{
		imgsrc = "0" + imgsrc;
	}

	var imgurl = SITE_URL + path + imgsrc + ".jpg";

	//alert(imgurl);

	document.getElementById("zoombox").style.left = 50 + "%";
	document.getElementById("zoombox").style.top = 50 + "%";
	document.getElementById("zoombox").style.display = "block";

	//document.getElementById("zoomcaption").innerHTML = caption;
	//w = document.getElementById("zoomimg").width;
	w = w + 10;
	document.getElementById("zoombox").style.width = w + "px"//w  + 10 + "px";

	//h = document.getElementById("zoomimg").height;
	h = h + 30; 
	document.getElementById("zoombox").style.height = h + "px";
	document.getElementById("zoomimg").style.marginLeft = "5px";
	document.getElementById("zoomimg").style.marginTop = "5px";


	//setOpacity(document.getElementById("fadebox"),30);
	document.getElementById("fadebox").style.width = "100%";

	var arrPageData = getPageSize();
	h2 = arrPageData[1];
	w2 = arrPageData[0];
	xpos = arrPageData[4];
	ypos = arrPageData[5];


//	document.getElementById("zoombox").style.marginTop = ypos + (-1 * (h/2)) + "px";
//	document.getElementById("zoombox").style.marginLeft = xpos + (-1 * (w/2)) + "px";
	document.getElementById("zoombox").style.marginTop = ypos -200 + "px";
	document.getElementById("zoombox").style.marginLeft = xpos -160 + "px";

	document.getElementById("fadebox").style.height = h2 + "px";
	document.getElementById("fadebox").style.width = w2 + "px";
	document.getElementById("fadebox").style.display = "";

	//document.getElementById("zoombox").style.marginLeft =  + "px";
	//document.getElementById("zoombox").style.marginTop = (-200) + "px";		

	out_fade.fadeout("fadebox",0,20,true);
	out_fade.fadeout("zoombox",0,200,true);

        //Load Image
        var loader = new ImageLoader(imgurl);
        loader.loadEvent = function(url, image)
        {
                //document.getElementById("zoomimg").style.display='block';
                //document.getElementById("fadebox").style.display='block';
                document.getElementById("zoomimg").src = loader.url; 
        }
        loader.load();
}

function hideImage()
{
	document.getElementById("zoomimg").src = SITE_URL + "/images/blank.jpg";
	in_fade.fadeout("fadebox",20,0,true);
	in_fade.fadeout("zoombox",100,0,true);
	
	//document.getElementById("fadebox").style.display='none';
	//document.getElementById("zoombox").style.display='none';
        
}

//Event Listener!!!
function addListener(element, type, expression, bubbling)
{
          bubbling = bubbling || false;
          if(window.addEventListener)	
          { // Standard
                    element.addEventListener(type, expression, bubbling);
                    return true;
          } 
          else if(window.attachEvent) 
          { // IE
                    element.attachEvent('on' + type, expression);
                    return true;
          } 
          else 
          {
                return false;
          }
}

//Image Object
var ImageLoader = function(url)
{
          this.url = url;
          this.image = null;
          this.loadEvent = null;
};

ImageLoader.prototype = 
{
          load:function()
          {
          
            this.image = document.createElement('img');
            var url = this.url;
            var image = this.image;
            var loadEvent = this.loadEvent;
            
            addListener(this.image, 'load', function(e)
            {
                      if(loadEvent != null)
                      {
                                loadEvent(url, image);
                      }
            }, false);
            this.image.src = this.url;
            
          },
          getImage:function()
          {
                return this.image;
          }
};
