// sets thumbs to landscape if needed
function setup_images() {
	mainImgWidth = $("#detailPage #mainImg img").width();
	mainImgHeight = $("#detailPage #mainImg img").height();
			
	
	var difference = 400 - mainImgHeight;
	if (mainImgHeight < 400 && mainImgHeight < mainImgWidth && difference > 30) {		
		$("#detailPage").addClass("landscape");
	}
	
	if (mainImgWidth == 340 && mainImgHeight == 317) {
		$("#detailPage").addClass("square").removeClass("landscape");
	}
	
	if ($("#detailPage").not(".landscape").not(".square") && mainImgWidth < 400 && mainImgWidth < mainImgHeight) {
		$("#mainImg").width(mainImgWidth + 10);
		var wDifference = 400 - mainImgWidth;
		$("#detailPage .detailText").width(370 + wDifference);
		$("#detailPage .detailImages").width(525 - wDifference);
	}
	
	if($("#detailPage").is(".landscape") && mainImgWidth < 400) {
		
		$("#detailThumbs").width(mainImgWidth + 10);
		$("#detailThumbs .padding").width(mainImgWidth);
		$("#detailThumbs .sliderNext").css("left", mainImgWidth);
		$("#mainImg").width(mainImgWidth + 10);
		var wDifference = 400 - mainImgWidth + 100;
		$("#detailPage .detailText").width(370 + wDifference);
		$("#detailPage .detailImages").width(525 - wDifference);
		
		$("#detailPage").addClass("smallWidth");	
	}					
}


$(window).load(function() {
	
	
	// all functions executable on detail page start here!!!
	
		
		$(".detailImages a").fancybox();
		$("#productPhoto a").fancybox();
		$("#additionalPhotoContain a").fancybox();
		// NEED TO CONDENSE ALL THESE STATEMENTS, ASAP!!

	    
        // determines if thumbs should be landscape
        setup_images();

		//thumbnail slider for detail page
			var position = 0; // position starts at 0
			
			
			
			
			if ($("#detailPage").is(".landscape")) { 
				var itemSize = 95;
				var setSize = 380;
				var itemCount = $("#detailThumbs a").size();
				if ($("#detailPage").is(".smallWidth")) {	
						var itemSize = 95;
						var thumbSetWidth = mainImgWidth + 10;
						var setSize = parseInt(thumbSetWidth / itemSize);
						setSize = setSize * itemSize;
						var itemCount = $("#detailThumbs a").size();
					
				}
				var sliderSize = parseFloat(itemCount) * parseFloat(itemSize);
					var size = $("#detailThumbs img").size();
					
				$("#detailThumbs .wrap").width(sliderSize);
					if ( size == 0 ) {
						$("#detailThumbs").addClass("none");
					}
			}
		
	
			else if($("#detailPage").not(".landscape") && mainImgHeight < 400){
				
				$("#detailPage").addClass("smallHeight");
				$("#detailThumbs").height(mainImgHeight + 10);
				$("#detailThumbs .padding").height(mainImgHeight);
				$("#detailThumbs .sliderNext").css("top", mainImgHeight);
				var itemSize = 90;
				thumbSetHeight = mainImgHeight + 10;
				var setSize = parseInt(thumbSetHeight / itemSize);
				setSize = setSize * itemSize;
				var itemCount = $("#detailThumbs a").size();
				var sliderSize = parseFloat(itemCount) * parseFloat(itemSize);
				$("#detailThumbs .wrap").height(sliderSize);
				
				var size = $("#detailThumbs img").size();

				if ( size == 0 ) {
					leftWidth = $(".detailImages").width();
					rightWidth = $(".detailText").width();

					$("#detailThumbs").addClass("none");
					$(".detailImages").width(leftWidth - 110);
					$(".detailText").width(rightWidth + 110);
				}


			}
			else {
				var itemSize = 90;
				var setSize = 360;
				var itemCount = $("#detailThumbs a").size();
				var sliderSize = parseFloat(itemCount) * parseFloat(itemSize);
				$("#detailThumbs .wrap").height(sliderSize);
				
				var size = $("#detailThumbs img").size();

				if ( size == 0 ) {
					leftWidth = $(".detailImages").width();
					rightWidth = $(".detailText").width();

					$("#detailThumbs").addClass("none");
					$(".detailImages").width(leftWidth - 110);
					$(".detailText").width(rightWidth + 110);
				}


			}
			
						
			// show or hide slider nav based on position of current slide set.
			
			function displayNav() {
					var diff = sliderSize + position;
					if (diff <= setSize) {
						$("#detailThumbs .sliderNext").hide();	
					}
					else {
						$("#detailThumbs .sliderNext").show();	
					}

					if(position == 0) {
						$("#detailThumbs .sliderPrev").hide();	
					}

					else {
						$("#detailThumbs .sliderPrev").show();
					}

			}
			
			displayNav();
			
			
			// slide on nav click 
			
			$("#detailThumbs .sliderNav").click(function(){
				if ($(this).is(".sliderNext")) {
					position = position - setSize;
				}
				
				if ($(this).is(".sliderPrev")) {
					position = position + setSize;
				}
					if ($("#detailPage").is(".landscape")) {

						$("#detailThumbs .wrap").animate({
							left: position	}, 1000);
					}

					else {

						$("#detailThumbs .wrap").animate({
							top: position	}, 1000);
					}

					displayNav();
	
			});

			$("#itemDetail").delay(4000).css("visibility","visible");

});




