$(document).ready (function(){

$(".pageRow.images .column .imgHover").bind("touchend", expandViewerShow);

			
			isAndroid = navigator.userAgent.match(/Android/i) != null;
			isWebOS = navigator.userAgent.match(/webOS/i) != null;
			isiPad = navigator.userAgent.match(/iPad/i) != null;
			isiPhone = navigator.userAgent.match(/iPhone/i) != null;
			isiPod = navigator.userAgent.match(/iPod/i) != null;
			ishp = navigator.userAgent.match(/hp-tablet/i) != null;
		
			if( isiPad || isiPhone || isiPod || isAndroid || isWebOS ){ 
			
						$(".pageRow.images .column").bind("touchstart", imageOver);
						$(".pageRow.images .column").bind("touchend", imageOut); 	
						$(window).bind("scroll", moveLoader);
			
			}else{
				
						$(".pageRow.images .column").bind("mouseover", imageOver);
						$(".pageRow.images .column").bind("mouseout", imageOut); 
					
			}
		
$(".pageRow.images .column").bind("click", expandViewerShow);
$("#headerContainer").find("a").bind("mouseover", aMenuOver).bind("mouseout", aMenuOut);				
$(".column").find("a").bind("mouseover", aMainOver).bind("mouseout", aMainOut);
$("#expandViewer").bind("click", expandViewerHide);	
$("#expandViewerClose").bind("click", expandViewerHide);



if (typeof document.body.style.opacity == "string" ){ //check for browser opacity support
$("#headerSubMenuContainer").css({"opacity" : "0"});
}

///LOAD MENU

	
	if (typeof document.body.style.opacity == "string" ){ //check for browser opacity support
	
		$("#headerSubMenuContainer").animate(
		{"opacity" : "1"},
		{	easing: 'linear',
			duration: 800
		});
	
	
	}
	
	var i = 0;
	menuLoad(i);	
	
///LOAD MENU


setup_form();






// //////////////////////////////////////////////////////////////////////////////////////////////// function clipBody//
clipBody();
$(window).resize(clipBody);
function clipBody(){
	
	//Do clip function if window is 1024 wide or less
	if( $(window).width() <= 1024){
			
			//Window size height - less than 688 and not the home page
			if($(window).height() <= 688 && typeof $("body").find("#front-page-gallery").css("") == "undefined"){
				
				$("html, body").height(530);
				
			}else{
				
			//Window size height - more than 688 or fixed mobile device
			
			//fixed mobile device
				if( isiPad || isiPhone || isiPod || isAndroid || isWebOS ){ 
				
					contentHeight = $(".column").eq(1).height() + parseInt($(".column").eq(1).css("padding-top"));
				
			//If the text goes further than the side image height	
					if( contentHeight > $("#sideImg").height() ){
					
					//If the text goes further than 1024 image size is 100%	
						if(contentHeight > 1024){
							
							$("html, body").css({"height" : "100%"});	
					
					//otherwise image height and text height are the same	
						}else{
							
							$("html, body").css({"height" : contentHeight + parseInt( $(".column").eq(1).css("margin-bottom")) + "px"});
						
						}
				
			//if text doesnt go further than image height, image height is window height - 160px for header, leaving image to fill the side, to bottom	
					}else{
						
						$("html, body").height( $(window).height() - 160);
					}
				
				}else{
			//Window size height - more than 688
					$("html, body").height( $(window).height() - 160);
					
				}
				
			}
			
			
	
	//Don't do clip function if window is more than 1024 wide	
	}else{
	
	$("html, body").css({"height" : "100%"});	
		
	}
}
// //////////////////////////////////////////////////////////////////////////////////////////////// function clipBody//







///LOAD FRONT IMAGES

loadCount = 0;	
totalFrontPageImages = $("#loader").find("img").length;
$("#loader").find("img").each(function(loadCount){
	
	$(this).load(function(){
		
		loadCount ++;
	
		
		if(loadCount === totalFrontPageImages){
			$("#loader").fadeOut(400);
			$("#front-page-gallery").fadeIn(400, function(){
			playFrontPageGallery();											  
			});
			
		}		
					   
	}).error(function(){
		
		loadCount ++;
	
		
		if(loadCount === totalFrontPageImages){
			$("#loader").fadeOut(400);
			$("#front-page-gallery").fadeIn(400, function(){
			playFrontPageGallery();											  
			});
			
		}		
					   
	});
	
	

});
//////////////////


///INIT GRID LOAD

var gridItemsTotal = $("#page5columns .column img").length;
var gridCount = 0;
var columnHtmlSRC = new Array();
$("#page5columns .column img").each(function(i){
	$(this).parent().css({"display" : "none"});
	$(this).css({"opacity" : "1"});
	columnHtmlSRC[i] = $(this).parent().html();	
	$(this).parent().html("");
});
//alert (gridItemsTotal + " " + columnHtmlSRC);
LoadGrid(gridCount, gridItemsTotal, columnHtmlSRC);

///
					


});
// //////////////////////////////////////// Load Events ////////////////////////


function LoadGrid(gridCount, gridItemsTotal, columnHtmlSRC){
	
	if(gridCount < gridItemsTotal){
	$("#page5columns .column").eq(gridCount).html(columnHtmlSRC[gridCount]).find("img").load(function(){
			$(this).parent().fadeIn(1000);
			gridCount++;
			LoadGrid(gridCount, gridItemsTotal, columnHtmlSRC);
					
	}).error(function(){
			$(this).parent().fadeIn(1000);
			gridCount++;
			LoadGrid(gridCount, gridItemsTotal, columnHtmlSRC);	
	});
	
	}else if(gridCount === gridItemsTotal){
		$("#grid-loader").fadeOut(400);
	}

}


function playFrontPageGallery(){

	$("#front-page-gallery").find("div").eq(2).delay(3000).animate({"opacity" : "0"}, 1000, function(){
		
		$("#front-page-gallery").find("div").eq(0).before( $("#front-page-gallery").find("div").eq(2) ).css({"opacity" : "1"});
		playFrontPageGallery();
	
	});		

}








function menuLoad(i){


		var menuLength = $("#headerSubMenuContainer").find("a").length;						
		
		if(i < menuLength){
			
			
			
				$("#headerSubMenuContainer").find("a").eq(i).animate({"color" : "#B59F82"},
				
					{	easing: 'linear',
						duration: 100,
						complete: function(){
						
								$(this).animate({"color" : "#5A4E43"},
				
								{	easing: 'linear',
									duration: 200,
									complete: function(){
										
										
										if ($(this).css("color") == "rgb(181, 159, 130)"){
											
											$(this).css({"color": "rgb(90, 78, 67)"});
											
										}
										
										
									} // Dim Complete
								
								});
								
							menuLoad(i);
								
							} // Bright Complete
					
					});
					
			
			
			
			i++
			
			
		}
		

	

}//function menuLoad//











// //////////////////////////////////////////////////////////////////////////////////////////////// function setup form//
function setup_form(){
	


$("#STORE").click(function(){
			$("#nom-store-select").fadeIn(500, function(){
				$("#page2columns,  #nom-store-select .selectStoreClose").bind("click", function(){
					$("#nom-store-select").fadeOut(500, function(){
						$("#page2columns").unbind("click");
						});
					});
			});
			
				   
});


$("#nom-store-select .nom-store-select-column").find("a").click(function(event){											   
event.preventDefault();
$("#STORE").val( $(this).text() );
$("#nom-store-select").fadeOut(500);
});

$("#STORE").keydown(function() {return false});





$("#privacy-link").click(function(event){
								  
		event.preventDefault();
			
			$("#privacy").fadeIn(500, function(){
				$("#page2columns,  #privacy .selectStoreClose").bind("click", function(){
					$("#privacy").fadeOut(500, function(){
						$("#page2columns").unbind("click");
						});
					});
			});
			
				   
});


	
	
	
	
	
$("#name").focus();

/*
$(":input, input[type=text]").attr("type", "text").each(
							 
function(){
	
	$(this).val( $(this).attr("placeholder") );
	
});




$(":input, input[type=text]").attr("type", "text").focus(

		function(){
			
			if( $(this).val() != $(this).attr("placeholder") ) {
				return false;							  
			}else{
				
				$(this).val("");
			
			}
				
		}
							 
);


		
$(":input, input[type=text]").attr("type", "text").focusout(

		function(){
			
			if( $(this).val() === "") {
				$(this).val( $(this).attr("placeholder") ) ;						  
			}else{
				
				return false;
			
			}
				
		}
							 
);	*/

$("[placeholder]").each(
							 
function(){
	
	$(this).val( $(this).attr("placeholder") ).blur();
	
});




$("[placeholder]").focus(

		function(){
			
			if( $(this).val() != $(this).attr("placeholder") ) {
				return false;							  
			}else{
				
				$(this).val("");
			
			}
			
			
	
			if (this.name == "STORE"){
			
			$("#STORE").click();
			
			}
	
		
				
		}
							 
);


		
$("[placeholder]").focusout(

		function(){
			
			if( $(this).val() === "") {
				$(this).val( $(this).attr("placeholder") ) ;						  
			}else{
				
				return false;
			
			}
				
		}
							 
);	


	
}
// //////////////////////////////////////////////////////////////////////////////////////////////// function setup form//










// //////////////////////////////////////////////////////////////////////////////////////////////// function aMenuOver//
function aMenuOver(){
$(this).animate({"color" : "#B59F82"}, {
	easing: 'linear',
	duration: 200,
	queue: false
});
}// //////////////////////////////////////////////////////////////////////////////////////////////// function aMenuOver//


// //////////////////////////////////////////////////////////////////////////////////////////////// function aMenuOut//
function aMenuOut(){
$(this).animate({"color" : "#5A4E43"}, {
	easing: 'linear',
	duration: 200,
	queue: false
});
}// //////////////////////////////////////////////////////////////////////////////////////////////// function aMenuOut//









// //////////////////////////////////////////////////////////////////////////////////////////////// function aMenuOver//
function aMainOver(){
$(this).animate({"color" : "#B59F82"}, {
	easing: 'linear',
	duration: 200,
	queue: false
});
}// //////////////////////////////////////////////////////////////////////////////////////////////// function aMenuOver//


// //////////////////////////////////////////////////////////////////////////////////////////////// function aMenuOut//
function aMainOut(){
$(this).animate({"color" : "#84745F"}, {
	easing: 'linear',
	duration: 200,
	queue: false
});
}// //////////////////////////////////////////////////////////////////////////////////////////////// function aMenuOut//












// //////////////////////////////////////////////////////////////////////////////////////////////// function imageOver//
function imageOver(){

$(this).find(".imgHover").show();
$(this).find(".imgHover").stop().animate({"opacity" : "1"}, {
	easing: 'linear',
	duration: 200,
});

}// //////////////////////////////////////////////////////////////////////////////////////////////// function imageOver//


// //////////////////////////////////////////////////////////////////////////////////////////////// function imageOut//
function imageOut(){

$(this).find(".imgHover").stop().animate({"opacity" : "0"},{
	easing: 'linear',
	duration: 200,
	complete: function(){
	$(this).hide();
	}
});	

}// //////////////////////////////////////////////////////////////////////////////////////////////// function imageOut//















// //////////////////////////////////////////////////////////////////////////////////////////////// function expandViewerShow//
function expandViewerShow(){

		if( isiPad || isiPhone || isiPod || isAndroid || isWebOS || ishp ){ 

					$("#expandViewer").css({
					"top" : $(window).scrollTop(),
					"height" : $(window).attr("innerHeight")
					});
			
					//Bind resize function for Mobile Device
					$(window).bind("scroll", panViewScroll);

			}
			
		
		$("#expandViewerInner").html("");
						
			
// /////////////////////////////////////////////////// LOAD IMAGE AND LABEL DATA ///////////////////////////////////////////// 

		var thisImage = $(this).find("img");
		
		//	Get product info
		
		//Check if items and price data exist
		if(thisImage.attr("items") != undefined || thisImage.attr("prices") != undefined){
			
				items = thisImage.attr("items").split(", ");
				price = thisImage.attr("prices").split(", ");
				//alert (items.length + " " + items + " " + price.length + " " + price);
				
			}else{
				
				items = "";
				price = "";
				
		}
		
			collectionslabelhtml = "";
			for (i = 0; i < items.length; i++){  collectionslabelhtml += "<li>" + (i + 1) + " - <span>" + items[i] + "</span> " + price[i] + "</li>"; }
			$("#expandViewerLabelInner").find("ul.collectionInfo").html(collectionslabelhtml);		
			
			
		//Check if press data exist
		if(thisImage.attr("press") != undefined){
			
			press = thisImage.attr("press");
				
			presslabelhtml = "<li><span>" + press + "</span></li>";
			$("#expandViewerLabelInner").find("ul.pressInfo").html(presslabelhtml);		
				
		}
		
		
	
		

// /////////////////////////////////////////////////// LOAD IMAGE AND LABEL DATA ///////////////////////////////////////////// 			
			
			
			
$("#expand-loader").show();
$("#expandViewer").show();

$("#expandViewer").stop().animate({"opacity" : "1"}, 400, function(){
		
			if( isiPad || isiPhone || isiPod || isAndroid || isWebOS || ishp ){ 
			
				//Touchevents
				//alert ("mobile");
				$("#expandViewer").bind("touchstart touchmove touchend gesturechange", panExpandViewerOnTouch);
				$("#headerContainer").css({"opacity" : "0.2"});
				$("#page5columns").css({"opacity" : "0.2"});
			
			}else{
		
				//Bind mouse move effect for expandviewer
				$("#expandViewer").bind("mousemove", panExpandViewer);
			}
			
			
		lowResSrc = thisImage.attr("src").split("/");
		$("#expandViewerInner").html("<img src='../includes/images/hi-res/" + lowResSrc[4] + "' />");
		
		$("#expandViewerInner img")
		.load(function(){
			$(this).fadeIn(1200);
			$("#expand-loader").fadeOut(400);
		}).error(function(){
			$(this).fadeIn(1200);
			$("#expand-loader").fadeOut(400);
		});
			
			
			
	
	});


}// //////////////////////////////////////////////////////////////////////////////////////////////// function expandViewerShow//













// //////////////////////////////////////////////////////////////////////////////////////////////// function expandViewerHide//
function expandViewerHide(){


		
			if( isiPad || isiPhone || isiPod || isAndroid || isWebOS || ishp ){ 
			
				//Touchevents
				//alert ("mobile");
				$("#headerContainer").css({"opacity" : "1"});
				$("#page5columns").css({"opacity" : "1"});	
			
			}else{

				//Unbind mouse move effect for expandviewer
				$("#expandViewer").unbind("mousemove", panExpandViewer);
				
			}
					
$("#expandViewer").stop().animate({"opacity" : "0"}, 1000, function(){	
		$("#expandViewer").hide();	
		$("#expandViewerInner img").css({"display" : "none"});	
	});	
$("#expandViewerInner").stop();


//Unbind resize function for Mobile Device
$(window).unbind("scroll", panViewScroll);



}// //////////////////////////////////////////////////////////////////////////////////////////////// function expandViewerHide//










// //////////////////////////////////////////////////////////////////////////////////////////////// function panExpandViewer//
function panExpandViewer(e){
	
	
	var viewerMaxScrollDistance = (		$("#expandViewerInner").attr("scrollHeight")	);
	
	var viewerHeight = $(this).height() - 40;
	var viewerMousePosY = e.pageY - $(this).offset().top - 20;
	var mousePosPercent = Math.floor(	(viewerMousePosY / viewerHeight)*100	);
	
	var viewerScrollToPos = (mousePosPercent / 100) * (viewerMaxScrollDistance - viewerHeight) + "px";

	

	$("#expandViewerInner").stop().animate({"scrollTop": viewerScrollToPos}, {easing: 'easeOutCirc', duration:2500});
	
	

}// //////////////////////////////////////////////////////////////////////////////////////////////// function panExpandViewer//










// //////////////////////////////////////////////////////////////////////////////////////////////// function panViewScroll//
function panViewScroll(){

	//Resize for Mobile Device
	$("#expandViewer").css({
		"top" : $(window).scrollTop(),
		"height" : $(window).attr("innerHeight")
	});

}// //////////////////////////////////////////////////////////////////////////////////////////////// function panViewScroll//










function panExpandViewerOnTouch(e){

if (e.type == "touchstart"){

	mouseDownY = ($("#expandViewer").height() - event.targetTouches[0].pageY - $("#expandViewer").offset().top) - $("#expandViewerInner").scrollTop();
	//$("#expandViewer").bind("touchmove", panExpandViewerOnTouch);
	//event.preventDefault();

}

if (e.type == "touchmove"){

	mouseMoveY = $("#expandViewer").height() - event.targetTouches[0].pageY - $("#expandViewer").offset().top - mouseDownY;
	event.preventDefault();	
	
	$("#expandViewerInner").scrollTop(mouseMoveY);

}

if (e.type == "touchend"){

	event.preventDefault();
	//$("#expandViewerInner").css({"opacity" : "0.2"});
	//$("#expandViewer").unbind("touchmove");
	
}


if (e.type == "gesturechange"){

	expandViewerHide();
	
}


//add easing??



}//function panExpandViewerOnTouch//


function moveLoader(){
	
	$("#grid-loader").css({"top" : $(this).scrollTop()  + ($(this).height() / 2) + "px"});

	
}
