$(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();
	
	if ($("#contest_form").length > 0) {
		$("#contest_form").submit(function() {
			$(".req").removeClass('invalid');
			e=0;
			$('#dob').val($('#birth_year').val()+'-'+$('#birth_month').val()+'-'+$('#birth_day').val());
			if ($("#first_name").val() == "") {  e=1; $("#first_name").addClass("invalid"); }
			if ($("#last_name").val() == "") {  e=1; $("#last_name").addClass("invalid"); }
			if ( !isEmail2($("#email").val())) {  e=1; $("#email").addClass("invalid"); }
			if ( !$('#agree').attr('checked') ) { e=2; }
			if (e==0) { 
				$.post('/cust/contests/ajaxhosts/enter.asp',$('#contest_form').serialize(),function() {
					$('#contest_form_wrapper').html('<div id="error_div" style="display:block">You are entered to win.<br><br>We will be contacting the winner soon. Be sure to check your email to see if it\'s you!<br><br>We will be posting information about the winner(s) on our website as well as our <a href="http://www.facebook.com/4allbyjofit" target="_blank">Facebook</a> and <a target="_blank" href="http://twitter.com/4allbyjofit">twitter</a> pages. Fan or follow us to see the results.</div>');
																														 });
			}
			else if (e==2) { $('#error_div').html('You must agree to contest rules and regulations to enter.').show();	 }
			else {
				$('#error_div').html('Please complete all required fields.').show();	
			}
			return false;
		});
	}
	
});

function LaunchFancyBox() { 
	if ($("#sizechartlink").length > 0) {
		$("#sizechartlink").fancybox({'showCloseButton':true,'hideOnOverlayClick':false,'width':690,'height':150});
	}
}

// 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.replace("$",""));	
	});
	$('#total').html('$'+total);
}


