$(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();

 $("div#productimage a").click(
  function() {
   if (typeof($(this).attr("href")) != "undefined") {
    loadImage($(this).attr("href"));
   }//if
   return false;
  },
  function () {}
 );
});

function loadImage (url) {
 showOverlayDiv();
 showLoadingImage();

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

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

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

  $(imageContainer).append(imgImage);

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

  $("#largeimagecontainer").css("position", "absolute");
  $("#largeimagecontainer").css("z-index", "3");

  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();

  //$("#largeimagecontainer").click(hideImage);
 });

 imgPreload.src = url;
}//function

function hideImage () {
 $("div#largeimagecontainer").remove();
 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", "1");

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

 /*
 $("div#overlay").click(function () {
  hideLoadingImage();
  hideOverlayDiv();
 });
 */
 $("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 doRequiredLabels() {

 //Remove all existing markers
 $('span.required').remove();

 $('form:not(.notrequired) div.required label').each( function() {
  if (! $(this).hasClass('inline')) {
   $(this).prepend('<span class="required">* </span>');
  }//if

 });

}//function

doAlternateRowHighlights = function() {

	//Remove any existing alternate classes
	$('div#content table.alternate tbody tr').removeClass('alternate');

	//Apply alternate class to alternate table rows
	$('div#content table.alternate tbody tr:odd').addClass('alternate');

}//function


$(document).ready(function() {
 
 //Do required labels
 doRequiredLabels();

 //Add to favourites
 var newLI = $('<li id="ctaFavourites"><a id="link-fav" href="javascript:void(false)">Add to Favourites</a></li>');
 $('li#ctaPrint').before(newLI);

 if (window.sidebar) {
   $('a#link-fav').click(function() {window.sidebar.addPanel(document.title,self.location,'')});

 } else if (window.external) {
  $('a#link-fav').click(function() {window.external.AddFavorite(self.location,document.title)});

 } else if (window.opera) {
  $('a#link-fav').click(function() {
       var e = document.createElement('a');
       e.setAttribute('href',self.location);
       e.setAttribute('title',document.title);
       e.setAttribute('rel','sidebar');
       e.click();
    });

 }//if

 if (typeof _gaq != 'undefined') {
  $('a#link-fav').click( function() { _gaq.push(['_trackEvent', 'Actions', 'Click', 'Add to favourites']); });
  $('#ctaPrint a').click( function() { _gaq.push(['_trackEvent', 'Actions', 'Click', 'Print page']); });
 }//if

 //Fix links to bookmarks that aren't preceded with a url
 $('a[href^="#"]').each( function(index) {
  $(this).attr('href', jQuery.url.attr('path') + $(this).attr('href'));
 });
 
 if ($('#form-clearbasket').length == 1) {
  $('#form-clearbasket').submit( function() {
   return confirm('Are you sure you want to empty your basket?');
  });
 }//if
 
 //Sort out height of footer boxes
 if ($('#footer .box').length > 0) {
  var intHeight = 0;
  $('#footer .box').each( function() {
   if ($(this).height() > intHeight) {
    intHeight = $(this).height();
   }//if
  });
  
  $('#footer .box').each( function() {
   $(this).height(intHeight);
  });
  
 }//if
 
 //Do enlarged images
 if ($('#productImage a img').length > 0) {
  $('#productImage a').click( function() {
   loadImage($(this).attr('href'));
   return false;
  });
 }//if

 //Fade stuff in footer
 if ($('#feedbackSnippets li').length > 1) {
  $('#feedbackSnippets').innerfade({
   speed: 1000,
   timeout: 6000,
   type: 'sequence',
   containerheight: $('#feedbackSnippets').parent('div.box').height() + 'px'
  });
 }//if

 //Fade stuff on home page
 if ($('#homeAwards img').length > 1) {
  $('#homeAwards').innerfade({
   speed: 1000,
   timeout: 4500,
   type: 'sequence',
   containerheight: $('#homeAwards').height + 'px'
  });
 }//if

 function hideFeedback(strSelector) {
  $(strSelector).each( function(i, val) {
   if (i > 2) {
    $(this).hide();
   }//if
  });  
 }//function

 function showFeedback(strSelector) {
  $(strSelector).each( function() {
   $(this).show();
  });  
 }//function

 if ($('div.collapse div.quote').length > 3) {
  $('div.quote:eq(3)').before('<p><a href="javascript:void(false)" id="showMoreQuotes">Click for more Reviews ></a></p>');
  $('a#showMoreQuotes').toggle( function() {
   showFeedback('div.quote');
   $(this).text('Click for fewer Reviews >');
  }, function() {
   hideFeedback('div.quote');
   $(this).text('Click for more Reviews >');
  });
  hideFeedback('div.quote');
 }//if
 
 if ($('div.collapse div.review').length > 3) {
  $('div.review:eq(3)').before('<p><a href="javascript:void(false)" id="showMoreReviews">Click for more Reviews ></a></p>');
  $('a#showMoreReviews').toggle( function() {
   showFeedback('div.review');
   $(this).text('Click for fewer Reviews >');
  }, function() {
   hideFeedback('div.review');
   $(this).text('Click for more Reviews >');
  });
  hideFeedback('div.review');
 }//if
 
 if ($('#addNewAddress').length > 0) {
  $('a#addNewAddress').toggle( function() {
   $('form#formAddNewAddress').show();
  }, function() {
   $('form#formAddNewAddress').hide();
  });
 }//if
 
 if ($('#awardWinning').length > 0) {
  $('#awardWinning').hover( function() {
   $(this).addClass('hover');
  }, function() {
   $(this).removeClass('hover');
  });
 }//if
 
 //Do AJAX stuff for payment methods
 if ($('form.formCheckoutSummary').length > 0) {
  $('form.formCheckoutSummary input.submit').click( function() {
   $.post('ajax/doPaymentMethods.php', $(this).parents('form').serialize());
  });
 }//if
 
 function doDeliveryBillingAddresses() {
  if ($('input#radio-AddressSame1').attr('checked')) {
   $('fieldset#deliveryAddress').hide();
  }//if
  if ($('input#radio-AddressSame2').attr('checked')) {
   $('fieldset#deliveryAddress').show();
  }//if  
 }//function
 
 //Helper for delivery address / billing address
 if ($('input#radio-AddressSame1').length > 0) {
  $('input#radio-AddressSame1').click( doDeliveryBillingAddresses );
  $('input#radio-AddressSame2').click( doDeliveryBillingAddresses );
  doDeliveryBillingAddresses();
 }//if

 function doDeliveryRecipient() {
  if ($('input#text-DeliveryAddressRecipient').length > 0) {
   if ('' == $('input#text-DeliveryAddressRecipient').val()) {
    $('input#text-DeliveryAddressRecipient').val( $('input#text-UserFirstName').val() + " " +  $('input#text-UserSurname').val() );
   }//if
  }//if
 }//function

 if ($('input#text-UserSurname').length > 0) {
  $('input#text-UserSurname').change( doDeliveryRecipient );
 }//if
 
});
