$(document).ready(function() {
    
	var options =
	{
		zoomWidth: 481,
		zoomHeight: 460,
		xOffset: 8,
		yOffset: 36,
		preloadText: '<img src="/static/img/loading-zoom-logo.png" width="90" height="43">',
        title: false

	}
	
	$(function() {
		$(".jqzoom").jqzoom(options);
	});

	/**
	 * load function
	 */
	$.load = function () {
		/*Rounded Corners for Sort */
		$("#brandMainCont #sort").corner("3px;");

		/*Product Page*/
			
		/*Rounded corners for rating box*/
				
		$("#productDesc .rateCont, #productView .moreTab .header, #detailsTab li").corner("5px; top;");
				
		/*Remove last line */
		$(".myOrderRow:last-child").css({background:"none"});
					
		/*Rounded corners for overall Rating */	
		$(".overallRate").corner("5px;");
		
		//setting timer..
		timer = 1;
		
		//Display the default image.
		$("img.altImage:first").click();
		
		
		
		if($("li.in").length == 1){
			// if all skus are out of stock but at least one preorderable
			$("li.in:first").click();
    	}
		else{
			// change the price
	    	$("span#listPrice").text($("li.sku:first").attr("listPrice"));
	    	
	    	if($("li.sku:first").attr("salePrice") != "$0.00 AUD"){
	    		$("span#listPrice").parent().addClass("strike");
	    		$("span#salePrice").text($("li.sku:first").attr("salePrice"));
	    		$("span#saving").text($("li.sku:first").attr("saving"));
	    		
	    		$("span.sale").parent().show();
	    		$("span.sale").show();
	    		$("#productView .productViewImg .sale").show();
	    		$("a#currencyConverter").attr("amount",$("li.sku:first").attr("salePrice"));
	    	}
	    	else{
	    		$("span#listPrice").parent().removeClass("strike");
	    		$("span.sale").parent().hide();
	    		$("#productView .productViewImg .sale").hide();
	    		$("a#currencyConverter").attr("amount",$("li.sku:first").attr("listPrice"));
	    	}
		}
		
		//if all skus are out of stock
		/* #24129, return a warning message for the customer.
		 * if($("li.in").length == 0){
			$("div#addCartBtn").removeClass("btRdGreen");
			$("div#addCartBtn").addClass("btRdGrey");
			$("div#addCartBtn").unbind("click");
		}*/
		
		$("div#threeDFrame").bind('mousemove', $.moveGesture);
		
		$("a.fancyBoxZoom").attr("href", $("a.fancyBoxZoom").attr("page") + '?giftListId=' + $("span#giftListId").attr("giftListId"));
	}
	
	/**
	 * Enable to show a popup for the video link.
	 */
	$("div.viewExtra div.viewVideo").popupWindow({ 
		centerBrowser:1,
		scrollbars:1,
		windowURL:$("div.viewExtra div.viewVideo").attr("link")
	});
	
	$("div.viewExtra div.view3d_big").click(function(){ 
			$("div#threeDFrame").show();
			$("a#largeImage").hide();
			$.loadThreeDView();
	});
	
	$.loadThreeDView = function(){
		$("div#threeDFrame").empty();
		
		// Load 3D view..
		$("div#threeDFrame").prepend("<div id='product-wait-three-d'/>");
		
		var threeDViewUrl = $("div#threeDFrame").attr("threeDViewUrl");
		
		$("div#threeDFrame").load("/browse/contents/product/threeDView.jsp",{threeDViewUrl:threeDViewUrl}, function() {});
	};
	
	$.moveGesture = function(e){
		if($("div#threeDFrame").attr("mouseIsDown") == undefined || $("div#threeDFrame").attr("mouseIsDown") == false){
			$("#threeDCursorImage").show();
		}
		
		$("#threeDCursorImage:visible").css("top", e.pageY- $("div#threeDFrame").offset().top - 17);
		$("#threeDCursorImage:visible").css("left", e.pageX - $("div#threeDFrame").offset().left - 16);
	};
	
	$("div#threeDFrame").mouseleave(function(){
		$("#threeDCursorImage").hide();
	});
	
	$("div#threeDFrame").mousedown(function(e){
		$("#threeDCursorImage").hide();
	});
	
	$("div#threeDFrame").mouseup(function(e){
		if(e.target == $("div#threeDFrame")){
			$("#threeDCursorImage").show();
			$("#threeDCursorImage:visible").css("top", e.pageY- $("div#threeDFrame").offset().top - 17);
			$("#threeDCursorImage:visible").css("left", e.pageX - $("div#threeDFrame").offset().left - 16);
		}
		else{
			$("#threeDCursorImage").hide();
		}
	});
	
	$("input#qty").blur(function(){
		var currentValue = parseInt($(this).val(), 10);
		var maxQt = parseInt($("input#qty").attr("maxQt"), 10);
		
		if ((isNaN(currentValue)) || (currentValue < 1)) {
			$(this).val("1");
			$("input.quantity").val("1");
		}
		else if(currentValue > maxQt){
			$(this).val(maxQt);
			$("input.quantity").val(maxQt);
		}
	});
	 
	
    /**
     * Event mouseover & mouseout on div#shareProduct
     */
    $("div#shareProduct").mouseover(function(e) {
    	clearTimeout(timer);
     	$(".share").show('slow');
    
    }).mouseout(function() {
    	timer = window.setTimeout("$('.share').hide('slow')", 500);
    	
    });
 
    /**
     * Event mouseover & mouseout on div.share
     */
    $("div.share").mouseover(function(e) {
    	clearTimeout(timer);
    }).mouseout(function() {
    	timer = window.setTimeout("$('.share').hide('slow')", 500);
    });
 
    
    function hideHeaderSlot(param){
		if(param == 'true'){
			$("#headerSlot1").hide();
			$("#headerSlot2").hide();
		}else{
			$("#headerSlot1").show();
			$("#headerSlot2").show();
		}
	}
    
    /**
	 * Choose GiftList list popup setup
	 */
	$("div#sizeChartBtn a").fancybox({
		'overlayOpacity'	:	0.45,
        'overlayColor'		:	'#000',
		'zoomSpeedIn'		:	500,
        'zoomSpeedOut'		:	500,
		'frameWidth'		:	750,
		'frameHeight'		:	500,
		'hideOnContentClick': false,
		'callbackOnClose'	: function(){
			hideHeaderSlot('false');
		},
		'callbackOnShow':function(){				
			hideHeaderSlot('true');
		}
    });
    
    /**
     * Event click on sku sizes that are in stock.
     */
    $("li.in").click(function(){
    	// put this sku selected
    	$("li.in,li.out").removeClass("selected");
    	$(this).addClass("selected");
    	
    	// change the preorder status
    	if($(this).attr("preorder") == "true"){
    		$("span.preorder").show();
    	}
    	else{
    		$("span.preorder").hide();
    	}
    	
    	// change the price
    	$("span#listPrice").text($(this).attr("listPrice"));
    	
    	if($(this).attr("salePrice") != "$0.00 AUD"){
    		$("span#listPrice").parent().addClass("strike");
    		$("span#salePrice").text($(this).attr("salePrice"));
    		$("span#saving").text($(this).attr("saving"));
    		
    		$("span.sale").parent().show();
    		$("span.sale").show();
    		$("#productView .productViewImg .sale").show();
    		$("a#currencyConverter").attr("amount",$(this).attr("salePrice"));
    	}
    	else{
    		$("span#listPrice").parent().removeClass("strike");
    		
    		$("span.sale").parent().hide();
    		$("#productView .productViewImg .sale").hide();
    		$("a#currencyConverter").attr("amount",$(this).attr("listPrice"));
    	}
    	
    	//change values in all forms
    	$("form input.skuId").val($(this).attr("skuId"));
    });
    
   /* $("li.out").mouseenter(function(){
    	var index = $("li.sku").index(this);
    	var start = ((index / 11) * 11);
    	var end = start + 11;
    	$("li.sku").slice(start, end).height("44px");
    });
    
    $("li.out").mouseleave(function(){
    	$("li.sku").height("17px");
    });*/
    
    /**
     * Event keyup on qty field
     */
    $("input#qty").keyup(function(){
    	//change value in addToCart Form for quantity
    	$("input.quantity").val($(this).val());
    	
    });
    
    /**
     * Event Click on addToFavourites
     */
    $("div#addToFavBtn").click(function(){
    	$("form#addToFavForm").submit();
    });
    
    /**
     * Event Click on addToFavourites
     */
    $("div#addToGiftListBtn").click(function(){
    	$("#addToGiftListForm").submit();
    });
        
    /**
     * Event click on alternate images to switch the main image
     */
    $("img.altImage").click(function(){
    	$("div#threeDFrame").hide();
		$("a#largeImage").show();
    	
    	$("div.productViewImg img").attr("src", $(this).attr("largeImage"));
    	$("div.productViewImg a").attr("href", $(this).attr("extraLargeImage"));
    });
    
    $("img.relatedProduct").click(function(){
		var productId = $(this).attr("id");
		window.location = "product.jsp?productId=" + productId;
	});

    /**
     * Event click on div#detailsTab ul li
     */
	$("div#detailsTab ul li").click(function(){
		// hide content review...
		$("div#detailsTab div.content:eq(0)").show();
		$("div#detailsTab div.content:eq(1)").hide();
		
		var index = $("div#detailsTab ul li").index(this);
		
		$("div#detailsTab ul li").removeClass("active");	
		$(this).addClass("active");
		
		$("div#detailsTab div.content:eq(0) > span").addClass("displayNone");
		$("div#detailsTab div.content:eq(0) > span:eq(" + index + ")").removeClass("displayNone");
		
		
	});

	/**
	 * Event click on a[name='allReviewsLink']
	 */
	$("a[name='allReviewsLink']").click(function(){
		$.loadReviewSection(2);
	});
	
	/**
	 * Event focus on input[name='qty']
	 */
	$("input[name='qty']").focus(function(){
		$(this).select();
	});
	
	/** 
	 * load review section.. on detail tabs..
	 */
	$.loadReviewSection = function (reviewId){
		
		//
		$("div#detailsTab div.content:eq(0)").hide();
		$("div#detailsTab div.content:eq(1)").show();
		
		$("div#detailsTab div.content:eq(1)").empty();
		
		// Load Images..
		$("div#detailsTab div.content:eq(1)").prepend("<div id='detailsReview-wait-images'/>");
		
		$("div#detailsTab div.content:eq(1)").load("/browse/contents/product/productReviews.jsp",{reviewId : reviewId}, function() {

			/*Rounded Cornersfor Review tabs*/
			$(".sortReview li a").corner("2px;");
		
		}); 
		
	}
	
	/* Show shareProduct icons */
	$("#shareProduct").hover(function() {
		$(".share").show('slow');
	});
	
	$("a.fancyBoxZoom").click(function(){
		$("a.fancyBoxZoom").attr("href", $("a.fancyBoxZoom").attr("page") + '?giftListId=' + $("span#giftListId").attr("giftListId"));
	});
	
	/**
	 * Choose GiftList list popup setup
	 */
	$("a.fancyBoxZoom").fancybox({
		'overlayOpacity'	:	0.45,
        'overlayColor'		:	'#000',
		'zoomSpeedIn'		:	500,
        'zoomSpeedOut'		:	500,
		'frameWidth'		:	250,
		'hideOnContentClick': false
    });
	
	/**
	 * SendToFriend popup setup
	 */
	$("a.fancyBoxZoomSendFriend").fancybox({
		'overlayOpacity'	:	0.45,
        'overlayColor'		:	'#000',
		'zoomSpeedIn'		:	500,
        'zoomSpeedOut'		:	500,
		'frameWidth'		:	480,
		'frameHeight'		:	360,
		'hideOnContentClick': false
	});
	
	$("a.fancyBoxZoomUCC").click(function(){
		$("a.fancyBoxZoomUCC").attr("href", $("a.fancyBoxZoomUCC").attr("page") + '?amount=' + $("a#currencyConverter").attr("amount"));
	});
	
	
	 $("a.fancyBoxZoomUCC").fancybox({
		 'overlayOpacity'	:	0.45,
	     'overlayColor'		:	'#000',
		 'zoomSpeedIn'		:	500,
         'zoomSpeedOut'		:	500,
		 'frameWidth'		:	700,
		 'frameHeight'		:	325,
		 'hideOnContentClick': false

     });

	
	$.load();
	$("li.out").slice(0, 11).height("44px");
	$('li.out').css("background-image", "url(/static/img/backgrounds/bg_sizeOutOver.gif)");
	$('#productDesc #sizeChartOneSize li.out').css("background-image", "url(/static/img/backgrounds/bg_sizeOutOver-OneSize.gif)");
	$('li.out').css("color","black");
	
	/*Big Wishlist Button*/	
	$(".btRdGrey40px").corner("3px;");
	
});	
	
