// JavaScript Document

	
	// Function to load in First Picture in list on Product Detail Page
	function initFirstPic() {
		if ($('div.product-slide-views div.items ul li').length > 1){
			var initSrc = $('div.product-slide-views div.items ul li a:first').find('img').attr('src');
			var newInitSrc = initSrc.replace("67x64", "408x395");
			var newZoomSrc = initSrc.replace("67x64", "800x600");
			$('#product-main-img img#main-prod-img').attr('src', newInitSrc);
			$('#product-main-img a.zoom').attr('href', newZoomSrc);
		};
	};
	
	// method for drop down validation
	jQuery.validator.addMethod( 
		"selectNone", 
		function(value, element) { 
			if (element.value == "none") 
			{ 
				return false; 
			} 
			else return true; 
		}, 
		"Please select a colour." 
	); 
	
	function disableAddToCart() {		
		var str = $("select#1 option:selected").attr('class');
		if( str == "out-of-stock" ){
			$("#product-det-add-form input#btn_add").fadeOut();
			$("#product-det-add-form select#1").addClass('out-of-stock');
			$(".product-det-outofstock").fadeIn();
		}
		else
		{
			$("#product-det-add-form input#btn_add").fadeIn();
			$("#product-det-add-form select#1").removeClass('out-of-stock');
			$(".product-det-outofstock").fadeOut();
		}
	};
	


	
	$(document).ready(function(){
	   
				
		// Set up Prod Det Tabbed Content Items
		$(".product-det-tabs").hide(); //Hide all content
		$("ul#product-det-tabs-nav li:first a").addClass("active").show(); //Activate first tab
		$(".product-det-tabs:first").show(); //Show first tab content
		
		//Product Detail tabs
		$("ul#product-det-tabs-nav li").click(function() {
	
			$("ul#product-det-tabs-nav li a").removeClass("active"); //Remove any "active" class
			$(this).children('a').addClass("active"); //Add "active" class to selected tab
			$(".product-det-tabs").hide(); //Hide all tab content
	
			var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
			$(activeTab).fadeIn(1250); //Fade in the active ID content
			return false;
		});
		
		// Set up Prod Variation Tabs (View & Colour)
		$(".product-variation-tabs").hide(); //Hide all content
		$("ul#product-main-nav li:first a").addClass("active").show(); //Activate first tab
		$(".product-variation-tabs:first").show(); //Show first tab content
		
		//Product Detail tabs
		$("ul#product-main-nav li").click(function() {
	
			$("ul#product-main-nav li a").removeClass("active"); //Remove any "active" class
			$(this).children('a').addClass("active"); //Add "active" class to selected tab
			$(".product-variation-tabs").hide(); //Hide all tab content
	
			var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
			$(activeTab).fadeIn(1250); //Fade in the active ID content
			
			// Load the first image in list when changing nav
			var navItem = $(this).find('a').attr("href").replace(/#/,'');
						
			var src = $('div.product-slide-' + navItem + ' div.items ul li a:first').find('img').attr('src');
			var newSrc = src.replace("67x64", "408x395");			
			$('#product-main-img img#main-prod-img').attr('src', newSrc).hide().fadeIn(1250);
			
			
			
			return false;
		});
		
		
		// Product Scroll Bar
		$.easing.custom = function (x, t, b, c, d) {
			if ((t/=d/2) < 1) return c/2*t*t*t + b;
			return c/2*((t-=2)*t*t + 2) + b;
		}
	
		$(function() { 
		// initialize scrollable  for product sliders
			$("div.product-slide-views, div.product-slide-colours").scrollable({
					next: 'a.prodSlideNext',
					prev: 'a.prodSlidePrev',
					easing: 'custom',
					items: '.items',
					size: 2,
					keyboard: true,
					clickable: false,
					speed: 1000					
			});
		
		});
		
		
		//Remove initial val from qty field	
		$("#product-det-add-form #txt_qty").click(function() {	
			$(this).val('');
		});		
		
		
		// validation handler for add to cart + Wishlist form
		//$("#product-det-add-form").validate({		
//			focusCleanup: false
//		});
		
		$("form#out-of-stock-form, form#sendfriend-form, #cee_ajax .form").validate({		
			focusCleanup: false
		});
		
		// Add to car form validation
		$("#product-det-add-form").validate({ 
			rules: {
					1: {
						selectNone: true
						},
					quantity: {
							required: true,
      						min: 1,
							number:true
						}
					}
		}); 
		
		// Add to cart action
		var options = {
			success: showResponse,
			url: '/shop/cart/add/ajax/'
		};
		
		function showResponse(responseText, statusText, xhr, $form) {
			eval('var response='+responseText);
			if (response.results='Success') { //assume error handled by javascript				

				$("div#add-to-cart img").attr("src", "/site_media/img/added-to-cart-a.gif");
				$("div#add-to-cart img").attr("usemap", "#cartAddedMap");
				
				$('div#nav-rhs ul li.cart').animate({
					opacity: 0.25
				}, 1000, function() {
					// Animation complete.
				});
				
				window.setTimeout(function() {
					$('div#nav-rhs ul li.cart').animate({
						opacity: 1			
					}, 1000, function() {
						$("#cart_count").html(response.cart_count+" ITEMS");
					});
					
				}, 400);
				
				window.setTimeout(function() {					
					$("div#add-to-cart").fadeOut(750);
					$("div#add-to-cart img").removeAttr("usemap")
				}, 3000);
				
				//$("form#product-det-add-form input#btn_add").attr("disabled", "disabled");
				
			}
		};

		
		// Close add to cart overlay
		$("#cee_closeBtn").click(function(){
			$("div.overlay").fadeOut(500);
	 	});
		
		
				
		$("#product-det-add-form #btn_add").click(function() {
														   
			// Set Add to cart overlay to centre, and overlay correct height
			//var docHeight = $(document).height();
//			var winHeight = $(window).height();
//			var winWidth = $(window).width();
//			var boxWidth = 180;
//			var boxHeight= 47;
//			var left = ( winWidth / 2 ) - (boxWidth / 2 );
//			var top = ( winHeight / 2 ) - (boxHeight / 2 );
//	
//			$("div.overlay").css('height', docHeight);
//			$("div#cart-added").css('left', left);
//			$("div#cart-added").css('top', top);
			
			
			if ($("#product-det-add-form").valid())
			{
				//$("form#product-det-add-form input#btn_add").attr("disabled", "disabled");
				$("div#add-to-cart img").attr("src", "/site_media/img/add-to-cart-a.gif");
				$("div#add-to-cart").fadeIn(100);
				$('#product-det-add-form').ajaxForm(options);				
			}			
			
		});				
		
		// Load first picture on Load
		initFirstPic();
		
		// Set up load Main image on thumbnail click
		$("div.product-slide-views div.items ul li a, div.product-slide-colours div.items ul li a").click(function() {
			var src = $(this).find('img').attr('src');
			var newSrc = src.replace("67x64", "408x395");
			var newZoomSrc = src.replace("67x64", "800x600");
			$('#product-main-img img#main-prod-img').attr('src', newSrc).hide().fadeIn(1250);
			$('#product-main-img a.zoom').attr('href', newZoomSrc);
			return false;			
		});
		
		// Product Zoom
		$("a.zoom").fancybox({
			'overlayShow' : false,
			'hideOnContentClick': true,
			'zoomSpeedIn' : 900,
			'zoomSpeedOut' : 500,			
			'easingIn' : 'easeOutBack',
			'easingOut' : 'easeInBack',
			'overlayShow' : true,
			'overlayOpacity' : 0.7
		});
		
		//Disable Arrows if less than 4 items	
		
		if($("#views div.items ul li").length < 6)
		{			
			$(this).find('div#views').find('a.prodSlideNext').addClass('disabled');			
		}
		else
		{
			$(this).find('div#views').find('a.prodSlideNext').removeClass('disabled');
		}
		
		//Disable Arrows if less than 4 items		
		if($("#colours div.items ul li").length < 6)
		{			
			$(this).find('div#colours').find('a.prodSlideNext').addClass('disabled');			
		}
		else
		{
			$(this).find('div#colours').find('a.prodSlideNext').removeClass('disabled');
		}
		
		
		// Initiate share module tooltip
		$("#share ul li a img[title]").tooltip({ 
			tip: "#shareTip",
			position: "bottom right", 
			effect: 'fade',
			offset: [2, -24]
		});
		
		// Email Friend Overlay
		$(".ceebox").ceebox({titles:false, overlayColor:"#666", overlayOpacity:0.7 });
		
		// Disable "select colour" option for non ie6, ie7 browsers
		$("#product-det-add-form select#1").click(function(){
			$("#select-option").attr("disabled", "disabled");			
		});
		
		// Default to select colour if page refreshed
		$("#select-option").attr("selected", "selected");
		
		
		// Disable Add To Cart Button if colour is out of stock
		$("#product-det-add-form select#1").change(function(){
			disableAddToCart();
		});
		


							   
	});
