$(document).ready(function(){
	//if there is the color dropdown on this page
	if($("#colorSelect").length>0){
		//if the value is already set, then get the available sizes
		if($("#colorSelect").val()!=""){
			getSizes();
		}
		//when select is changed, get new value and make ajax call to get available sizes
		$("#colorSelect").change(function(){
			getSizes();
		 });
	}
	
	if($(".photoGalleryLink").length>0){
		$(".photoGalleryLink").css("cursor", "pointer");
		$(".photoGalleryLink").click(function(){
			var galleryId = $(this).parent()[0].id.substring(8);			
			var xmlFile = "/galleryXML.php?g="+galleryId;
			
			var scrOfX = 0, scrOfY = 0;
			if( typeof( window.pageYOffset ) == 'number' ) {
				//Netscape compliant
				scrOfY = window.pageYOffset;
				scrOfX = window.pageXOffset;
			} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
				//DOM compliant
				scrOfY = document.body.scrollTop;
				scrOfX = document.body.scrollLeft;
			} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
				//IE6 standards compliant mode
				scrOfY = document.documentElement.scrollTop;
				scrOfX = document.documentElement.scrollLeft;
			}
			
			$("body").append("<div id='lightboxBack' style='display:none;'></div>");
			if($("body").height()>$(window).height()){
				height = $("body").height()+65;
			}else{
				height=$(window).height();
			}
			
			$("#lightboxBack").width($(window).width());
			$("#lightboxBack").height(height);
			$("#lightboxBack").css({
				backgroundColor: "#000000",
				zIndex:	"100",
				position: "absolute",
				top: "0px",
				left: "0px",
				opacity: 0.8
			});
			
			

			$("#lightboxBack").fadeIn(300, function(){
				//get slide show to display in the box				 
				$(".hiddengallery").css({
					backgroundColor: "#FFFFFF",					
					width: "750px",
					height: "566px",
					position: "absolute",
					zIndex:	"102",
					margin: (scrOfY-245)+"px 0 0 -375px",
					left: "50%",
					top: "50%"
				});
				
				$("#hiddengallery_"+galleryId).fadeIn(300);
			});
			
			$("#lightboxBack").unbind("click", exitLightBox);
			$("#lightboxBack").bind("click", exitLightBox);
		});
	}	
});

function exitLightBox(){
	$(".hiddengallery").fadeOut(300, function(){						   
		$(".hiddenvideo").fadeOut(300);
		$("#lightboxBack").fadeOut(600, function(){
			$("#lightboxBack").remove();
		 });
   });
}

function openVideo(){
	var scrOfX = 0, scrOfY = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	}
	
	$("body").append("<div id='lightboxBack' style='display:none;'></div>");
	if($("body").height()>$(window).height()){
		height = $("body").height()+65;
	}else{
		height=$(window).height();
	}
	
	$("#lightboxBack").width($(window).width());
	$("#lightboxBack").height(height);
	$("#lightboxBack").css({
		backgroundColor: "#000000",
		zIndex:	"100",
		position: "absolute",
		top: "0px",
		left: "0px",
		opacity: 0.8
	});

	$("#lightboxBack").fadeIn(300, function(){
		//get slide show to display in the box				 
		$(".hiddenvideo").css({
			background: "none",					
			width: "475px",
			height: "395px",
			position: "absolute",
			overflow: "hidden",
			zIndex:	"102",
			margin: (scrOfY-200)+"px 0 0 -265px",
			left: "50%",
			top: "50%"
		});
		$(".hiddenvideo").fadeIn(300);
	});
	$("#lightboxBack").unbind("click", exitLightBox);
	$("#lightboxBack").bind("click", exitLightBox);	
}

function getSizes(){
	cid = $("#colorSelect").val();
	pid = $("#productId").val();
	$.ajax({
		type: "POST",
		url: "/scripts/getSizes.php",
		data: "cid="+cid+"&pid="+pid,				
		success: function(msg){
			if(msg.length>0){
				$("#sizeSelect").html(msg);
			}else{
				$("#sizeSelect").html('<option value="" style="font-style:italic">-First Select Color-</option>');						
			}
		}
	});		
}

function sameAddress(which){
	if(which.checked == true){
		$("input[name='ship_name']").val($("input[name='first_name']").val()+" "+$("input[name='last_name']").val());
		$("input[name='ship_company']").val($("input[name='company']").val());
		$("input[name='ship_address1']").val($("input[name='address1']").val());
		$("input[name='ship_address2']").val($("input[name='address2']").val());
		$("input[name='ship_city']").val($("input[name='city']").val());
		$("select[name='ship_state_id']").val($("select[name='state_id']").val());
		$("input[name='ship_zip']").val($("input[name='zip']").val());
		$("input[name='ship_phone']").val($("input[name='phone']").val());
	}
}

function validateAddToCart(){
	var errors = 0;
	var errorMsg = "Please fix the following:\n\n";
	
	if($("#colorSelect").val() == ""){errors++; errorMsg+="-Choose a color.\n";}
	if($("#sizeSelect").val() == ""){errors++; errorMsg+="-Choose a size.\n";}
	if($("#quantityInput").val()<1){errors++; errorMsg+="-Quantity must be greater than 0.\n";}
	
	if(errors>0){
		alert(errorMsg);	
	}else{
		$("#addToCart")[0].action="/shop_item.php";
		$("#addToCart")[0].submit();
	}
}

function showSizeChart(){
	$("body").append("<div id='sizeChart'><div id='sizeChartTop'><span id='exitButton'></span></div><div id='sizeChartContent' align='center'><img src='/images/sizechart.png' width='771px' height='812px' alt='' /></div><div id='sizeChartBottom'></div></div>");
	$("#sizeChart").fadeIn(300);
	
	
	$("#exitButton").unbind('click', exitSizeChart);
	$("#exitButton").bind('click', exitSizeChart);	
}
function exitSizeChart(){
	$("#sizeChart").fadeOut(300);	
}

function showForm(which){
	$("#link_"+which).siblings().css("fontWeight", "normal");
	$("#link_"+which).css("fontWeight", "bold");	
	$(".hiddenForm").slideUp(400);
	$("#"+which).slideDown(600);
}