$(document).ready(function()
{
   // sidenav expand and collapse
   //$(".sidenavbody").hide();
	
	$(function() {
		var zIndexNumber = 1000;
		$('.stack').each(function() {
			$(this).css('zIndex', zIndexNumber);
			zIndexNumber -= 10;
		});
	});
  
  $("div[id^='collectionmod_']").mouseenter(function() {
																	  
		$("#collectionmodimg_wrapper").stop(true,true).fadeOut(500).load("/cust/products/ajaxhosts/get_collection_img.asp", { 'collection_id': $(this).attr("rel") } ).fadeIn(700);
	});
  
  $(".sidenavhead").click(function() {
   		$(this).next(".sidenavbody").slideToggle(600);
   });
   // product page tabs
   $("#tab_outfit").click(function() {
   		tab_outfit();
   });
   $("#tab_reviews").click(function() {
   		tab_reviews();
   });
	$("#homepage_image").delay(250).fadeIn(1000);
   // 
	LaunchFancyBox();
	
	$("#y1").click(function() {
		swaphistory("1");								
	});
	$("#y2").click(function() {
		swaphistory("2");								
	});
	$("#y3").click(function() {
		swaphistory("3");								
	});
	$("#y4").click(function() {
		swaphistory("4");								
	}); 
	$("h1").each(function() {
							$(this).html('<div class="h1_innerwrapper"><img align="left" class="h1img" src=\"/assets/images/h1/'+$(this).html().substr(0,1)+'.png\" /><span>' + $(this).html().substr(1)+"</span></div>").wrap('<span class="h1_wrapper" />');	 
								 });
	//$("h1").html('<div class="h1_innerwrapper"><img align="left" class="h1img" src=\"/assets/images/h1/'+$(this).html().substr(0,1)+'.png\" /><span>' + $(this).html().substr(1)+"</span></div>").wrap('<span class="h1_wrapper" />');
	
});

function swaphistory(x) {
	$("table[id^='ty']").css('display','none');
	$("#ty"+x).css('display','block');
	$("#gpimg").attr('src','/assets/images/history/'+x+'.jpg');
	$("div[id^='y']").css('background-color','#fff');
	$("#y"+x).css('background-color','#efefef');
}

function LaunchFancyBox() { 
	if ($("#sizechartlink").length > 0) {
		$("#sizechartlink").fancybox({'showCloseButton':true,'hideOnOverlayClick':false,'width':500,'height':400});
	}
}

// tab_outfit - gets called when you click on the outfit tab on the product details page ***************************************
function tab_outfit() {
	 if ($("#tab_outfit").attr("class") == 'taboff') {
		  $("#tab_outfit").removeClass('taboff').addClass('tabon');
		  $("#tab_reviews").removeClass('tabon').addClass('taboff');
		  $("#reviews").css('display','none');
		  $("#relatedproducts").fadeIn(500).css('display','block');
	   }
}
// tab_reviews - gets called when you click on the reviews tab on the product details page *************************************
function tab_reviews() {
	 if ($("#tab_reviews").attr("class") == 'taboff') {
		 $("#tab_reviews").removeClass('taboff').addClass('tabon');
		 $("#tab_outfit").removeClass('tabon').addClass('taboff');
		 $("#relatedproducts").css('display','none');
		 $("#reviews").fadeIn(500).css('display','block');
	  }
}

// swaps out the main image and thumbnails on the product page ******************************************************************
function switchcolors(productcolor_id) {
	$("#productcolor_id").val(productcolor_id);
	$("#mainimgwrapper").fadeOut(500).load("/cust/products/ajaxhosts/get_main_img.asp", { 'productcolor_id': productcolor_id } ).fadeIn(700);
	$("#imgThumbs").fadeOut(500).load("/cust/products/ajaxhosts/get_thumbs.asp", { 'productcolor_id': productcolor_id } ).fadeIn(700);
	$("#relatedproducts").fadeOut(500).load("/cust/products/ajaxhosts/get_related.asp", { 'productcolor_id': productcolor_id } ).fadeIn(700);
	$("#available_sizes").fadeOut(500).load("/cust/products/ajaxhosts/get_sizes.asp", { 'productcolor_id': productcolor_id } ).fadeIn(700); 
	$("#selectedcolor").fadeOut(500).load("/cust/products/ajaxhosts/get_color_name.asp", { 'productcolor_id': productcolor_id } ).fadeIn(700); 
	$("#price").fadeOut(500).load("/cust/products/ajaxhosts/get_price.asp", { 'productcolor_id': productcolor_id } ).fadeIn(700); 
	tab_outfit();
}

// attaches magnify script to product image *************************************************************************************
function magnify() {
	$('#mainimg').loupe({
  	width: 200, // width of magnifier
  	height: 200, // height of magnifier
  	loupe: 'loupe' // css class for magnifier
	});
}

// switches out the main image on the product details page.  called when user clicks thumbnail **********************************
function switchProductImg(i) {
	$("#mainimgwrapper").fadeOut(500).css('display','hidden').load("/cust/products/ajaxhosts/get_main_img.asp", { 'img': i } ).css('display','hidden').fadeIn(700);
}

// makes sure that the user selects a size and qty before adding something to the cart.
function validateAddToCart() {
	e=0;
	if ($('#qty').val() == 0) {
		$("#qtyerror").fadeOut(500).html("Please specify a quantity").fadeIn(500);
		e = 1;
	}
	if ($('#size_id').val() == 0) {
		$("#sizeerror").fadeOut(500).html("Please specify a size").fadeIn(500);
		e = 1;
	}
	if (e==0) { return true; } else { return false; }
}

// removes items from carts. 
function updateCart() {
	numrows = $("tr[id^='cartrow_']").length;
	c=0;
	$("input[name='remove']").each( function () {
		if (this.checked) { 
			c++;
			$("#cartrow_"+this.value).css('background-color','#46B5DC').load("/cust/shopping_cart/ajaxhosts/removeitemfromcart.asp", { 'id': this.value } ).fadeOut(500);
			setTimeout('$(\'#cartrow_'+this.value+'\').remove();',500);			
		}
	});
	// if cart is now empty, clear the table, else reset the price total
	if (numrows==c) {
		 setTimeout('$(\'#cart\').remove();',500);
		 setTimeout('$(\'#emptycart\').fadeIn(500).html("Your cart is empty");',500);
	}
	else {
		setTimeout('updateTotal();',600);
	}
}

function updateTotal() {
	total = 0
	$(".price").each( function () {
		total += parseInt(this.innerHTML);	
	});
	$('#total').html('$'+total);
}

