/* Section 1: Variables for scrollbars */
var number_of_related_products=0;
var related_product_height=100;
var related_products_margin=0;
var related_products_maximum_margin=0;

var position_count=0;
var max_position_count=0;
var direction=-1;
var animation=null;

function adjustMenu() {
	related_products_margin=related_products_margin+(direction*2);
	if (related_products_margin<0) { related_products_margin=0; direction=direction*-1; }
	if (related_products_margin>related_products_maximum_margin) { related_products_margin=related_products_maximum_margin; direction=direction*-1; }
	$('#related_products ul li#related_product_0 img').css({ marginTop: (related_products_margin*-1)+5 });
} //end function adjustMenu()

function fastAdjustMenu() {
	related_products_margin=related_products_margin+(direction*5);
	if (related_products_margin<=0) { related_products_margin=0; $('#related_products_up').css({opacity: 0.3}); } else { $('#related_products_up').css({opacity: 1}); }
	if (related_products_margin>=related_products_maximum_margin) { related_products_margin=related_products_maximum_margin; $('#related_products_down').css({opacity: 0.3}); } else { $('#related_products_down').css({opacity: 1}); }
	$('#related_products ul li#related_product_0 img').css({ marginTop: (related_products_margin*-1)+5 });
} //end function adjustMenu()

/* Section 2: Buy Now button */
		function ___getPageSize() {
			var xScroll, yScroll;
			if (window.innerHeight && window.scrollMaxY) {	
				xScroll = window.innerWidth + window.scrollMaxX;
				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;
				yScroll = document.body.offsetHeight;
			}
			var windowWidth, windowHeight;
			if (self.innerHeight) {	// all except Explorer
				if(document.documentElement.clientWidth){
					windowWidth = document.documentElement.clientWidth; 
				} else {
					windowWidth = self.innerWidth;
				}
				windowHeight = self.innerHeight;
			} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
				windowWidth = document.documentElement.clientWidth;
				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 = xScroll;		
			} else {
				pageWidth = windowWidth;
			}
			arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
			return arrayPageSize;
		};
		function ___getPageScroll() {
			var xScroll, yScroll;
			if (self.pageYOffset) {
				yScroll = self.pageYOffset;
				xScroll = self.pageXOffset;
			} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
				yScroll = document.documentElement.scrollTop;
				xScroll = document.documentElement.scrollLeft;
			} else if (document.body) {// all other Explorers
				yScroll = document.body.scrollTop;
				xScroll = document.body.scrollLeft;	
			}
			arrayPageScroll = new Array(xScroll,yScroll);
			return arrayPageScroll;
		};

function hideBuyNowForm() {
	$('#popup_form').css({ display: "none" });
	$('#basket_overlay').css({ display: "none" });
	return false;
} //end function

function showBuyNowForm(buyNowClicked) {
	//set Variables
	var productId=buyNowClicked.id.replace("product_","");
	$("#product_to_add").attr("value",productId);

	var productName=$("#product_title_"+productId).text();
	$("#popup_product_name").text(productName);

	// Get page size
	var arrPageSizes = ___getPageSize();
	$('#basket_overlay').css({
				backgroundColor:	"#000",
				opacity:			"0.1",
				width:				arrPageSizes[0],
				height:				arrPageSizes[1]
			}).fadeIn();
	// Get page scroll
	var arrPageScroll = ___getPageScroll();
	$('#popup_form').css({
				top:	arrPageScroll[1] + (arrPageSizes[3] / 10),
				left:	arrPageScroll[0]
			}).show();
	//$('#basket_overlay').css({ display: "block" });
			$(window).resize(function() {
				// Get page sizes
				var arrPageSizes = ___getPageSize();
				// Style overlay and show it
				$('#basket_overlay').css({
					width:		arrPageSizes[0],
					height:		arrPageSizes[1]
				});
				// Get page scroll
				var arrPageScroll = ___getPageScroll();
				// Calculate top and left offset for the jquery-lightbox div object and show it
				$('#popup_form').css({
					top:	arrPageScroll[1] + (arrPageSizes[3] / 10),
					left:	arrPageScroll[0]
				});
			});
	return false;
} //end function

function setUpBuyNowForm() {
	$('body').append('<div id="basket_overlay"></div><form id="popup_form" method="post" enctype="multipart/form-data"><div><p><strong id="popup_product_name">Product</strong> <br />has been added to your basket</p><p><input type="hidden" name="quantity" value="1" /><input id="product_to_add" type="hidden" name="product_to_add" value="0" /><input class="button add" type="submit" name="add_to_basket" value="View Basket" /></p><p><input class="button add" type="submit" name="add_to_basket" value="Continue Shopping" /></p><p><input class="button cancel" type="submit" name="cancel" id="cancel" value="Undo" /></p></div></form>');
	$("#cancel").click( function() { return hideBuyNowForm(); } )
	$(".buy_now").click( function() { return showBuyNowForm(this); } )
	$('#popup_form').css({ display: "none" });
	$('#basket_overlay').css({ display: "none" });
} //end setUpBuyNowForm()

/* Section 3: Image Zoom on Hover */
function createImageZoomBox() {
	var imageSource=$("img#main_product_image").attr("src").replace("/new-products-main/","/new-products/");
	//$("#main_product_image").replaceWith('<div id="main_product_image_div">&nbsp;</div>');
	$("#main_product_image").before('<div id="main_product_image_div">&nbsp;</div>');
	$("#main_product_image_div").css("background", "#fff url("+imageSource+") top left no-repeat");
	$("#main_product_image_div").css("visibility", "hidden");

	//on hover, change the background
	/*$("#main_product_image_div").parent().hover(function () {
		$("#main_product_image_div").css("visibility", "visible");
    }, function () {
		$("#main_product_image_div").css("visibility", "hidden");
    });
	$("#main_product_image_div").parent().mousemove(function (event) {
		var offset = $("#main_product_image_div").offset();
		var x = (event.pageX - offset.left)/$("#main_product_image_div").outerWidth();
		var y = (event.pageY - offset.top)/$("#main_product_image_div").outerHeight();
		//change these for calculations later?
		var zoomedImageWidth=750;
		var zoomedImageHeight=525;
		var smallImageWidth=382;
		var smallImageHeight=268;
		var widthDifference=zoomedImageWidth-smallImageWidth;
		var heightDifference=zoomedImageHeight-smallImageHeight;
		var newXPos=0-widthDifference*x;
		var newYPos=0-heightDifference*y;
		//$("#main_product_image_div").html("Moving! X: "+x+" Y: "+y);
		$("#main_product_image_div").css("background-position", newXPos+"px "+newYPos+"px");
	});*/

	//$("#main_product_image").parent().parent().siblings().css("visibility","hidden"); //works
	//$("#main_product_image").parent().parent().siblings().hover(function () {
	$("#product_images a.detail").hover(function () {
		//var image=$(this).children("img:last");
		//var imageSource=image.attr("src").replace("/new-products-thumbs/","/new-products/").replace("/new-products-main/","/new-products/");
		var imageSource=$(this).attr("href");
		$("#main_product_image_div").css("background", "#fff url("+imageSource+") top left no-repeat");
		$("#main_product_image_div").css("visibility", "visible");
		var overlayText=$(this).attr("title");
		if (overlayText!='') {
			$("#main_product_image_div").html('<strong>'+overlayText+'</strong>');
		} else {
			$("#main_product_image_div").html('');
		} //end if (overlayText!=='')
    }, function () {
		//var imageSource=$("#main_product_image").attr("src").replace("/new-products-main/","/new-products/");
		//$("#main_product_image_div").css("background", "#fff url("+imageSource+") top left no-repeat");
		$("#main_product_image_div").css("visibility", "hidden");
    });
	//$("#main_product_image").parent().parent().siblings().mousemove(function (event) {
	$("#product_images a.detail").mousemove(function (event) {
		var offset = $(this).offset();
		var x = (event.pageX - offset.left)/$(this).outerWidth();
		var y = (event.pageY - offset.top)/$(this).outerHeight();
		//change these for calculations later?
		var zoomedImageWidth=750;
		var zoomedImageHeight=525;
		var smallImageWidth=382;
		var smallImageHeight=268;
		var widthDifference=zoomedImageWidth-smallImageWidth;
		var heightDifference=zoomedImageHeight-smallImageHeight;
		var newXPos=0-widthDifference*x;
		var newYPos=0-heightDifference*y;
		//$("#main_product_image_div").html("Moving! X: "+x+" Y: "+y);
		$("#main_product_image_div").css("background-position", newXPos+"px "+newYPos+"px");
	});
	$("#product_images a.colour").hover(function () {
		var imageSource=$(this).attr("href").replace("/new-products/","/new-products-main/");
		$("#main_product_image_div").css("background", "#fff url("+imageSource+") top left no-repeat");
		$("#main_product_image_div").css("visibility", "visible");
		var overlayText=$(this).attr("title");
		if (overlayText!='') {
			$("#main_product_image_div").html('<strong>'+overlayText+'</strong>');
		} else {
			$("#main_product_image_div").html('');
		} //end if (overlayText!=='')
    }, function () {
		$("#main_product_image_div").css("visibility", "hidden");
    });
} //end createImageZoomBox()

/* Section 4: Colour Swatches */
var number_of_colour_swatches=0;
var colour_swatch_width=99;
var colour_swatches_margin=0;
var colour_swatches_maximum_margin=0;

var colour_swatches_position_count=0;
var colour_swatches_max_position_count=0;
var colour_swatches_direction=-1;
var colour_swatches_animation=null;

function colour_swatches_adjustMenu() {
	colour_swatches_margin=colour_swatches_margin+(colour_swatches_direction*2);
	if (colour_swatches_margin<=0) {
		colour_swatches_margin=0;
		colour_swatches_direction=colour_swatches_direction*-1;
		$('#previous_colours').css({opacity: 0.3});
	} else {
		$('#previous_colours').css({opacity: 1});
	} //end if (colour_swatches_margin<=0)
	if (colour_swatches_margin>colour_swatches_maximum_margin) { colour_swatches_margin=colour_swatches_maximum_margin; colour_swatches_direction=colour_swatches_direction*-1; }
	$('ul#product_images li#colour_swatches div ul').css({ marginLeft: ((colour_swatches_margin*-1)+5)+'px' });
} //end function adjustMenu()

function colour_swatches_fastAdjustMenu() {
	colour_swatches_margin=colour_swatches_margin+(colour_swatches_direction*5);
	if (colour_swatches_margin<=0) {
		colour_swatches_margin=0;
		colour_swatches_direction=colour_swatches_direction*-1;
		clearInterval(colour_swatches_animation);
		$('#previous_colours').css({opacity: 0.3});
	} else {
		$('#previous_colours').css({opacity: 1});
	} //end if (colour_swatches_margin<=0)
	if (colour_swatches_margin>=colour_swatches_maximum_margin) {
		colour_swatches_margin=colour_swatches_maximum_margin;
		colour_swatches_direction=colour_swatches_direction*-1;
		clearInterval(colour_swatches_animation);
		$('#next_colours').css({opacity: 0.3});
	} else {
		$('#next_colours').css({opacity: 1});
	} //end if (colour_swatches_margin<=0)
	$('ul#product_images li#colour_swatches div ul').css({ marginLeft: ((colour_swatches_margin*-1)+5)+'px' });
} //end function adjustMenu()


//if the document is ready, apply all functions
$(window).load(function() {
//$(document).ready(function() {
	number_of_related_products=$('#related_products li').length;
	if (number_of_related_products>4) {
		related_products_maximum_margin=(number_of_related_products-4)*related_product_height;
		/*animation=setInterval("adjustMenu()",25);
		$("#related_products ul").hover( function() {
			clearInterval(animation);
		}, function() {
			adjustMenu();
			animation=setInterval("adjustMenu()",25);
		}); //end $("#nearest_store_postcode")*/
        $('#related_products_up').css({opacity: 0.3});

		$("#related_products_up").hover( function() {
			direction=-1;
			clearInterval(animation);
			animation=setInterval("fastAdjustMenu()",25);
		}, function() {
			clearInterval(animation);
			//animation=setInterval("adjustMenu()",25);
		}); //end $("#nearest_store_postcode").click

		$("#related_products_down").hover( function() {
			direction=1;
			clearInterval(animation);
			animation=setInterval("fastAdjustMenu()",25);
		}, function() {
			clearInterval(animation);
			//animation=setInterval("adjustMenu()",25);
		}); //end $("#nearest_store_postcode").click
	} //end if (number_of_related_products>4)

	//apply buy now form
	setUpBuyNowForm();

	//set up new zoomer
	createImageZoomBox();

	//colour_swatches
	number_of_colour_swatches=$('ul#product_images li#colour_swatches div ul li').length;
    if (number_of_colour_swatches>3) {
		colour_swatches_maximum_margin=(number_of_colour_swatches)*colour_swatch_width-346;
        $('ul#product_images li#colour_swatches div ul').css({width: ((number_of_colour_swatches)*colour_swatch_width)+'px'});
        $('#previous_colours').css({opacity: 0.3});
		$("#previous_colours").hover( function() {
			colour_swatches_direction=-1;
			clearInterval(colour_swatches_animation);
			colour_swatches_animation=setInterval("colour_swatches_fastAdjustMenu()",25);
		}, function() {
			clearInterval(colour_swatches_animation);
		}); //end $("#previous_colours").click

		$("#next_colours").hover( function() {
			colour_swatches_direction=1;
			clearInterval(colour_swatches_animation);
			colour_swatches_animation=setInterval("colour_swatches_fastAdjustMenu()",25);
		}, function() {
			clearInterval(colour_swatches_animation);
		}); //end $("#next_colours").click
    } else {
        $('#next_colours').css({opacity: 0.3});
        $('#previous_colours').css({opacity: 0.3});
    } //end if (number_of_colour_swatches>3)
});
