jQuery.preloadImages = function() {
 for(var i = 0; i<arguments.length; i++) {
  jQuery("<img>").attr("src", arguments[i]);
 }//for
}//function

$(function() {
 imgLoadingImage = document.createElement("img");
 $(imgLoadingImage).attr("src", "images/loading.gif");
 $(imgLoadingImage).attr("alt", "Loading...");
 $(imgLoadingImage).attr("width", "16");
 $(imgLoadingImage).attr("height", "16");
 $(imgLoadingImage).attr("id", "loadingimage");
 $("body").append(imgLoadingImage);
 hideLoadingImage();
});

function loadImage (url, strBackgroundColor, intBorderWidth) {

 if (typeof strBackgroundColor == 'undefined' ) strBackgroundColor = '#112d80';
 if (typeof intBorderWidth == 'undefined' ) intBorderWidth = '1';

 showOverlayDiv();
 showLoadingImage();

 imgPreload = new Image();
 $(imgPreload).load(function () {
  arrPageSize = getPageSize();
  arrPageScroll = getPageScroll();

  imageContainer = document.createElement("div");
  $(imageContainer).attr("id", "largeimagecontainer");

  imgClose = document.createElement("img");
  $(imgClose).attr("src", "images/close.gif");
  $(imgClose).attr("id", "closebutton");
  $(imgClose).attr("width", "14");
  $(imgClose).attr("height", "14");
  $(imgClose).attr("alt", "Close");
  $(imgClose).attr("border", "0");
  $(imgClose).click(hideImage);

  imgImage = document.createElement("img");
  $(imgImage).attr("src", url);
  $(imgImage).attr("alt", "");
  $(imgImage).attr("width", imgImage.width);
  $(imgImage).attr("height", imgImage.height);

  $(imageContainer).append(imgImage);
  $(imageContainer).append(imgClose);

  $("#largeimagecontainer").hide();
  $("body").append(imageContainer);

  $("#largeimagecontainer").css("position", "absolute");
  $("#largeimagecontainer").css("z-index", "300");
  $("#largeimagecontainer").css("background-color", strBackgroundColor);
  $("#largeimagecontainer").css("border", intBorderWidth + "px solid #ffffff");

  if ($.browser.msie) {
   if ((arrPageSize[2] < arrPageSize[0])) {
    imagecontainertop = (((arrPageSize[3] - imgImage.height - 22) / 2) + 9);
   } else {
    imagecontainertop = (((arrPageSize[3] - imgImage.height - 22) / 2));
   }//if

   if ((arrPageSize[3] < arrPageSize[1])) {
    imagecontainerleft = (((arrPageSize[2] - imgImage.width - 22) / 2) + 9);
   } else {
    imagecontainerleft = (((arrPageSize[2] - imgImage.width - 22) / 2));
   }//if
  } else {
   imagecontainertop = (((arrPageSize[3] - imgImage.height - 22) / 2) - 9);
   imagecontainerleft = (((arrPageSize[2] - imgImage.width - 22) / 2) - 9);
  }//if

  if (imagecontainertop < 0) { imagecontainertop = 0; }//if
  if (imagecontainerleft < 0) { imagecontainerleft = 0; }//if

  $("#largeimagecontainer").css("top", arrPageScroll[1] + imagecontainertop + "px");
  $("#largeimagecontainer").css("left", arrPageScroll[0] + imagecontainerleft + "px");

  $("#largeimagecontainer").show();
  hideLoadingImage();
 });

 imgPreload.src = url;
}//function

function hideImage () {
 $("div#largeimagecontainer").remove();
 hideOverlayDiv();
}//function

function showElement(elemID) {
 showOverlayDiv();
 showLoadingImage();

 arrPageSize = getPageSize();
 arrPageScroll = getPageScroll();

 var objElement = $('#' + elemID);

 if ($.browser.msie) {
  if ((arrPageSize[2] < arrPageSize[0])) {
   objElementTop = (((arrPageSize[3] - objElement.height()) / 2) + 9);
  } else {
   objElementTop = (((arrPageSize[3] - objElement.height()) / 2));
  }//if

  if ((arrPageSize[3] < arrPageSize[1])) {
   objElementLeft = (((arrPageSize[2] - objElement.width()) / 2) + 9);
  } else {
   objElementLeft = (((arrPageSize[2] - objElement.width()) / 2));
  }//if
 } else {
  objElementTop = (((arrPageSize[3] - objElement.height()) / 2) - 9);
  objElementLeft = (((arrPageSize[2] - objElement.width()) / 2) - 9);
 }//if

 if (objElementTop < 0) { objElementTop = 0; }//if
 if (objElementLeft < 0) { objElementLeft = 0; }//if

 objElement.css("top", arrPageScroll[1] + objElementTop + "px");
 objElement.css("left", arrPageScroll[0] + objElementLeft + "px");

 objElement.show();
 hideLoadingImage();
}//function

function hideElement(elemID) {
 var objElement = $('#' + elemID);
 objElement.hide();
 hideOverlayDiv();
}//function

function showLoadingImage () {
 arrPageSize = getPageSize();
 arrPageScroll = getPageScroll();

 $("#loadingimage").css("position", "absolute");
 $("#loadingimage").css("z-index", "2");
 $("#loadingimage").css("top", (arrPageScroll[1] + ((arrPageSize[3] - 16) / 2)) + "px");
 $("#loadingimage").css("left", (arrPageScroll[0] + ((arrPageSize[2] - 16) / 2)) + "px");
 $("#loadingimage").show();
}//function

function hideLoadingImage () {
 $("#loadingimage").hide();
}//function

function showOverlayDiv () {
 arrPageSize = getPageSize();

 overlayDiv = document.createElement("div");
 $(overlayDiv).attr("id", "overlay");
 $(overlayDiv).css("height", arrPageSize[1] + "px");
 $(overlayDiv).css("position", "absolute");
 $(overlayDiv).css("top", "0");
 $(overlayDiv).css("left", "0");
 $(overlayDiv).css("width", "100%");
 $(overlayDiv).css("z-index", "100");

 $("body").append(overlayDiv);

 $("select").hide();
}//function

function hideOverlayDiv () {
 $("div#overlay").remove();
 $("select").show();
}//function

// Core code from - quirksmode.org
function getPageScroll(){
	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {
		yScroll = document.body.scrollTop;
	}//if

	arrayPageScroll = new Array('', yScroll)
	return arrayPageScroll;
}

// Core code from - quirksmode.org
// Edit for Firefox by pHaez
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) {
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else {
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}//if

	var windowWidth, windowHeight;
	if (self.innerHeight) {
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) {
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}//if

	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}//if

	if(xScroll < windowWidth){
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}//if

	arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight)
	return arrayPageSize;
}

function doGoogleMap() {
 if (self.GBrowserIsCompatible) {
  var map = new GMap2(document.getElementById("googlemap"));
  map.removeMapType(G_HYBRID_MAP);
  map.addControl(new GLargeMapControl());
  map.addControl(new GMapTypeControl());
  map.setMapType(G_NORMAL_MAP);
  map.enableScrollWheelZoom();

  map.setCenter(new GLatLng(52.819519, -1.766278), 13);
  marker = new GMarker(new GLatLng(52.819519, -1.766278));
  GEvent.addListener(marker, "click", function() {
   marker.openInfoWindowHtml("<span class=\'mapinfo\'>Constructive Marketing Limited<br />Unit 2 Lakes Court<br />Lancaster Park<br />Needwood<br />Burton on Trent<br />DE13 9PD</span>");
  });
  map.addOverlay(marker);

 }//if
}//function

$(document).ready(function() {

 if ($('div#googlemap').length == 1) {
  doGoogleMap();
 }//if

 //Image fade
	if ($('div#panel div.body').length > 0) {
 	$('div#panel div.body').innerfade({
 		speed: 3200,
 		timeout: 20000,
 		type: 'sequence',
 		containerheight: '112px'
 	});
	}//if

	//Replace email placeholders with real email links
	$('a.email').each( function() {
	 $(this).attr('href', 'mailto:contact@constructive-marketing.co.uk');
	 $(this).text('contact@constructive-marketing.co.uk');
	});

 //Email to a friend stuff
 $('div#panel a#sendtofriend').click( function() {
  showElement('panelFriend');
  return false;
 });

 $('div#panelFriend img.closebutton').click( function() {
  hideElement('panelFriend');
 });

 $('div#panelFriend form').submit( function() {

 	strErrors = '';

 	if ($('input#input-YourName').val() == '') {
 		strErrors += "- your name must be completed\r\n";
 	}//if

 	if ($('input#input-YourEmail').val() == '') {
 		strErrors += "- your email must be completed\r\n";
 	}//if

 	if ($('input#input-FriendsName').val() == '') {
 		strErrors += "- your friend's name must be completed\r\n";
 	}//if

 	if ($('input#input-FriendsEmail').val() == '') {
 		strErrors += "- your friend's email must be completed\r\n";
 	}//if

 	if (strErrors != '') {
 		alert('There were problems with the form:' + "\r\n\r\n" + strErrors);
 		return false;
 	}//if

 	return true;

 });

});