/*The standard Javascript functions used on most pages*/
/*Run this function when the window loads*/
$(document).ready(function(){
	/********************************************************************************************************/
	/******************************************Catalogue item hover******************************************/
	/********************************************************************************************************/
	//When an element with the class "catalogueItem" is hovered over
	$(".catalogueItem").hover(function() {		
		//Find the element with the class "catalogueButton" within the hovered element and toggle its class
		$(this).find(".catalogueButton").toggleClass("catalogueButtonHover");
	});
	
	/********************************************************************************************************/
	/*****************************************Alternative item hover*****************************************/
	/********************************************************************************************************/
	//When an element with the class "productGroupAlternativeItemsLink" is hovered over
	$(".productGroupAlternativeItemsLink").hover(function() {		
		//Find all elements with the class "productGroupAlternativeItemsImage" and hide them
		$(".productGroupAlternativeItemsImage").hide();
		//Find the previous link element which contains an element with the class "productGroupAlternativeItemsImage"
		$(this).prev("a").find(".productGroupAlternativeItemsImage").show();
	});
	
	/********************************************************************************************************/
	/***********************************************Buy popup************************************************/
	/********************************************************************************************************/
	/*Function to show popups*/
	function showPopup(popupID) {
		/*Set the ID of the popup to show*/
		var popupInnerContent = "#" + popupID;
		
		/*Show the selected popup content, the outer container is still hidden so the popup is not shown yet, however, the inner content must be shown here to ensure that the height of the outer container is calculated correctly*/
		$(popupInnerContent).show();
		
		/*The width and height of the popup*/
		var popupWidth = $("#standardPopupOuter").width();
		var popupHeight = $("#standardPopupOuter").height();
		/*The horizontal and vertical position of the popup, set to be in the center of the screen*/
		var popupHorizontalPosition = ($(window).width() - popupWidth) / 2+$(window).scrollLeft();
		var popupVerticalPosition = ($(window).height() - popupHeight) / 2+$(window).scrollTop();
		/*Position the popup in the middle of the screen*/
		$("#standardPopupOuter").css("top", popupVerticalPosition + "px").css("left", popupHorizontalPosition + "px");
		
		/*Set the default opacity to 0 for IE (filter) and other browsers (opacity)*/
		$("#standardPopupOverlay").css("filter","alpha(opacity = 0)").css("opacity","0")
	
		/*Set the height and width of the overlay to the size of the document (screen). Width is set in the CSS file to 100%*/
		$("#standardPopupOverlay").css("height",$(document).height());
		/*Show the overlay and fade it in to half opacity*/
		$("#standardPopupOverlay").show().fadeTo("slow", 0.5);
		/*Fade in the popup*/
		$("#standardPopupOuter").fadeIn("slow");
	}
	/*Function to hide popups*/
	function hidePopup() {
		/*Fade out the popup, overlay and inner popup containers*/
		$("#standardPopupOuter,#standardPopupOverlay,.standardPopupInner").fadeOut("slow");
		/*Remove any classes applied to the outer popup container*/
		$("#standardPopupOuter").removeClass();
	}
	
	/*When a popup link is clicked*/
	$(".standardPopupLink").click(function() {
		/*Call the showPopup function passing in the name of the link (which is used to identify the popup ID)*/
		showPopup($(this).attr("name"));
		/*Return false to stop the link from following the destination URL*/
		return false;
	});
	/*When the popup close button or overlay is clicked*/
	$(".standardPopupCloseButton,#standardPopupOverlay").click(function() {
		/*Call the hidePopup function*/
		hidePopup();
		/*Return false to stop the link from following the destination URL*/
		return false;
	});
});
