
$(document).ready(function(){


if (navigator.userAgent.indexOf('iPhone') != -1 || navigator.userAgent.indexOf('iPad') != -1) {
/// IPOD IPAD BINDINGS ///

		// STOP TOUCH EVENTS //
		$(document).bind("touchmove", function(e){
			e.preventDefault();							
		}).bind("gesturechange", function(e){
			e.preventDefault();							
		});
		// STOP TOUCH EVENTS //
		
		
		
		// BIND TOUCH-SCROLLING FOR THE PORTFOLIO //
		$("#portfolio").bind("touchstart touchmove touchend", portfolioScrolling);
		// BIND TOUCH-SCROLLING FOR THE AUX PAGES //
		$("#items").bind("touchstart touchmove touchend", itemsScrollable);
		// BIND TOUCH-SCROLLING FOR THE CONTACT OVERLAY //
		$("#contact-employ").bind("touchstart touchmove touchend", contactScrollable);
		// BIND TOUCH HIGHLIGHT TEXT //
		$(".section hgroup, .project, nav a").live("touchstart touchend", function(e){
																
			if (e.type === "touchstart"){
				
				$(this).css({"color" : "#fff"}).find("h1, h2, h3").css({"color" : "#fff"});
				
			}
			
			if (e.type === "touchend"){
				
				$(this).css({"color" : "#a8a8a8"}).find("h1, h2, h3").css({"color" : "#a8a8a8"});
				
			}	
		});
		// BIND EVENT FOR SCREEN ROTATE //
		$(window).bind("orientationchange", function(){								 
			$(".projects-inner").css({"left" : "0px", "top" : "0px"});
			hideURLbar();
			
			 if (navigator.userAgent.indexOf('iPhone') != -1) {
				//Resize Gallery on iPhone//
				$(document).find(".gallery:visible").each(function(){
					gallery = $(this);
					galleryImageWidth = gallery.find("img").width();
					galleryImageMargin = parseInt(gallery.find("img").css("margin-left"));
					numberOfImages = gallery.find("img").length;	
					gallery.find(".gallery-inner").width(320 + (galleryImageWidth * numberOfImages) + (galleryImageMargin * numberOfImages));
					gallery.find(".gallery-inner").stop().animate({"left" : "0px", "top" : "0px"}, 500);
				});
			 } //Resize Gallery on iPhone//
			 
		});
		
}else{
/// DESKTOP BINDINGS ///

		//
		// CSS BACKGROUND PROPERTY FOR DESKTOP
		//
		$("body").css({"background-attachment" : "fixed"});

		$(window).bind("resize", function(){	
			
			setLeftScroll();
			
			//
			// MAKE SURE SCROLL HANDLE STAYS ON SCREEN WHEN WINDOW SIZE REDUCED
			//
			
			$('.handle2, .handle').each(function(){
										
					if($(this).offset().left + 32 > $(window).width()){
						
						$(this).css({"left" : $(window).width() - 32 + "px"});
						
					}
										
			});
				
			
		//
		// RESCALE CLOSE BUTTONS ON WINDOW RESIZE
		//
			
		$("#portfolio").find(".section:not(.open)").each(function(){
				var closeButton = $("#close-button-width");
				var moveRight = parseInt( closeButton.css("margin-right") );
				var makeWidth = parseInt( closeButton.css("width") );
				 
				$(this).find(".section-open-close").css({"margin-right" : moveRight});
				$(this).find(".section-open-close").css({"width" : makeWidth});
		});
		
		$("#portfolio").find(".section.open").each(function(){
				var closeButton = $("#close-button-width");
				var makeWidth = parseInt( closeButton.css("width") );
			
						$(this).find(".section-open-close").css({
									"width" : (makeWidth * 2) + "px",
									"background-position" : "-" + (makeWidth * 2) + "px 0px"}
						);
		});	
		
		
		
		
		}); //RESIZE FUNCTION

}
/// DESKTOP & iDEVICE BINDINGS ///

	// BIND EVENT TO SHOW AND HIDE THE IPHONE MENU //
	$("#iphoneNav .menu").bind("click", menuShow);
	// BIND EVENT TO SHOW AND HIDE CONTACT OVERLAY //
	$(".contact-button").bind("click", contact);
	
	//
	// POPUP CLOSE BUTTON - NOT FOR IPHONE
	//
	
if (navigator.userAgent.indexOf('iPhone') == -1){$("#portfolio-popup-close").bind("click", closeImagePopUp);}

	


});

///////////////////////// END OF DOCUMENT READY //////////////////////////////////////////////////////////////



/*------------------------------------------------------------------------------------------------------------
**
**	MISC BASIC FUNCTIONS ---
**
**------------------------------------------------------------------------------------------------------------
*/

//
// SHOW IPHONE MENU
//

function menuShow(e){
	
	var iMenu = $(this).parent().find("ul");
	e.preventDefault();
	
	if (iMenu.is(':hidden') === true){
		$(this).find("div").css({"background" : "url(../images/ix.png) repeat-y 0px -10px"});
		iMenu.show();
		
	}else{
		$(this).find("div").css({"background" : "url(../images/ix.png) repeat-y 0px 0px"});
		iMenu.hide();
		$("#contact-employ").hide();
	}
}


//
// SHOW CONTACT SCREEN OVERLAY
//

function contact(e){
	var contact = $("#contact-employ");
	e.preventDefault();
	if (contact.is(':hidden') === true){
		contact.show();	
		contact.find("div").css({"top" :  "0px"});
	}else{
		contact.hide();
	}
}


//
// OPEN AND CLOSE SECTION EVENT
//

function sectionOpenClose(){
	//
	// IF DESKTOP > SCROLL BACK TO TOP SO SECTION ALIGN WITH HEADER
	// ** DESKTOP ONLY ** 
	if (navigator.userAgent.indexOf('iPhone') == -1 && navigator.userAgent.indexOf('iPad') == -1){
	$("body, html").animate({scrollTop : "0"}, 500);
	}
		
	$(this).css({"background" : "none"});	
	
	// VARIABLES
	
	var selectedSection = $(this).parent();
	var closeButton = $("#close-button-width");
	var moveRight = parseInt( closeButton.css("margin-right") );
	var makeWidth = parseInt( closeButton.css("width") );
	
	
		
					if(selectedSection.parent().hasClass("scrolling")){
					
					return;
						
					}else{
					
					
										if (selectedSection.hasClass("open") === true){
													
													//
													// IF SECTION IS ALREADY OPEN
													//
													
													/*alert (selectedSection.hasClass("open"));*/
													
													// CHECK IF THE GALLERY VIEWER IS OPEN, IF IT IS - CLOSE IT OR OTHERWISE, CLOSE THE SECTION
													if (selectedSection.find(".gallery").css("display") === "block"){
		
														galleryClose(selectedSection.find(".gallery"));
		
	
													}else{
																if (navigator.userAgent.indexOf('iPhone') != -1 || navigator.userAgent.indexOf('iPad') != -1) {
																
																		$("#portfolio").find(".section.open .projects").css({"height" : "0px"});
																		$("#portfolio").find(".section.open .section-open-close").css({
																						"margin-right" : moveRight + "px",
																						"width" : makeWidth + "px",
																						"background-position" : "0px 0px"}
																		);	
																
																}else{
																	
																	
																	$("#portfolio").find(".section.open .projects").css({"height" : "0px"});
																	$("#portfolio").find(".section.open .section-open-close").css({
																						"margin-right" : moveRight + "px",
																						"width" : makeWidth + "px",
																						"background-position" : "0px 0px"}
																	);	
																
																	$("#portfolio").animate({"top" :  "0px"}, 500);
																
																}
																$(document).find(".open").removeClass("open").find("");
													}
													
												
										
										}else{
											
	
													//
													// IF ANOTHER SECTION IS CLICKED
													//
											
													/*alert (selectedSection.hasClass("open"));*/
													
													//CLOSE GALLERY ON SECTION OPEN
													galleryClose(selectedSection.find(".gallery"));
													
													selectedSection.find(".projects").css({"height" : "0px"});
													selectedSection.find(".section-open-close").css({
																			"margin-right" : moveRight + "px",
																			"width" : makeWidth + "px",
																			"background-position" : "0px 0px"}
													);	
													
														
													$("#portfolio").find(".section.open .projects").css({"height" : "0px"});
													$("#portfolio").find(".section.open .section-open-close").css({
																				"margin-right" : moveRight + "px",
																				"width" : makeWidth + "px",
																				"background-position" : "0px 0px"}
													);	
													
													$(document).find(".open").removeClass("open");
													var sectionHeight = selectedSection.height() + 1;
													var scrolll = -1 * (selectedSection.index() * sectionHeight) - sectionHeight;	
												
													loadSectionsInner(this);
													
													
													//
													// CHECK THE WIDTH OF PROJECTS CONTAINER AND RESET SCROLL
													// ** DESKTOP ONLY ** 
													if (navigator.userAgent.indexOf('iPhone') == -1 && navigator.userAgent.indexOf('iPad') == -1){
					
														setLeftScroll();
													
													}
													
													selectedSection.addClass("open");
												
													$("#portfolio").animate({"top" :  scrolll + "px"}, 500, function(){
																																													 
															selectedSection.find(".projects").css({"height" : "360px"});
															selectedSection.find(".section-open-close").css({
																				"margin-right" : "8px",
																				"width" : (makeWidth * 2) + "px",
																				"background-position" : "-" + (makeWidth * 2) + "px 0px"}
															
															);	
															
															//TURN ON HASH CHANGE
															$(window).bind("hashchange", hashFunction);
																																														 
													});
													
													//TURN OF HASH CHANGE BEFORE CHANGEING THE HASH VIA CLICK
													$(window).unbind("hashchange", hashFunction);
													window.location.hash = $(this).attr("id").replace("hash", "");
													
											
										}
										
					}
	
						
}


//
// OPEN AND CLOSE GALLERY EVENT
//

function galleryOpen(){
	
	if ($(this).parent().hasClass("scrolling")){
	return;
	}else{
	var sectionGalleryViewer = $(this).parent().parent().parent().find(".gallery");
	sectionGalleryViewer.fadeIn(400);
	loadProjectGalleryInner(this);
	resetGallery(sectionGalleryViewer);
	
	//
	// CHECK THE WIDTH OF PROJECTS CONTAINER AND RESET SCROLL
	//
	setLeftScroll();	
	
	
	//
	// BIND TOUCH / CLICK TO OPEN UP POP UP FOR LARGE IMAGE
	//
	
	
	if (navigator.userAgent.indexOf('iPad') != -1) {
	// 	IPAD IPHONE //
		sectionGalleryViewer.find(".gallery-inner").find("img").bind("touchend", openImagePopUp);
	}else if (navigator.userAgent.indexOf('iPhone') == -1 ){
	// 	DESKTOP //
		sectionGalleryViewer.find(".gallery-inner").find("img").bind("click", openImagePopUp);
		
	}
	
	
	}
	
}

function galleryClose(gallery){
	gallery.fadeOut(400).find(".gallery-inner").empty();	
	
	//
	// FADE BUTTON ON GALLERY CLOSE - IF THE SECTION IS OPEN
	//
	if(gallery.parent().hasClass("open") === true){
	gallery.parent().find(".section-open-close").hide().fadeIn(300);
	}
	
}

/*------------------------------------------------------------------------------------------------------------
*/



function makeProjectSlider(currentSection){
	
	
	currentSection.find(".slider").slider({
										  
					start: function(event, ui){
						$(this).addClass("scrolling");
						
					},
												  
					slide: function(event, ui){
						
									currentSection.find('.handle').css({"left" : ($(this).width() - 32) * (ui.value / 100) +"px"})
			
									moveToLeft = Math.abs( (currentSection.find(".projects-inner").width() - currentSection.width()) * (ui.value / 100));
						
									currentSection.find(".projects-inner").css({"left" : moveToLeft * -1 +"px"});
					},
					
					stop: function(event, ui){
						
						$(this).removeClass("scrolling");
						
					}				
	});
	
	
}

function makeGallerySlider(currentSection){
	
	
	currentSection.find(".slider2").slider({
										  
					start: function(event, ui){
						$(this).addClass("scrolling");
					},
												  
					slide: function(event, ui){
						
									currentSection.find('.handle2').css({"left" : ($(this).width() - 32) * (ui.value / 100) +"px"})
			
									moveToLeft = Math.abs( (currentSection.find(".gallery-inner").width() - currentSection.width()) * (ui.value / 100));
						
									currentSection.find(".gallery-inner").css({"left" : moveToLeft * -1 +"px"});
					},
					
					stop: function(event, ui){
						
						$(this).removeClass("scrolling");
						
					}				
	});
	
	
}

function setLeftScroll(){
	
	
	$(".projects").each(function(){
		if( $(this).width() > ($(this).find("div").eq(0).width() + $(this).find("div").eq(0).offset().left)){
			$(this).find(".slide-container").hide();
				//RESET PROJECTS LEFT AND SCROLL
				$(this).find(".slider").slider("value", 0);
				$(this).find(".projects-inner").stop().animate({"left":"0px"}, 400);
				$(this).find(".handle").stop().animate({"left":"0px"}, 400);
			
			
		}else if( $(this).width() < $(this).find("div").eq(0).width()){
			$(this).find(".slide-container").show();
		
		}
								 
	});
	
	$(".gallery").each(function(){
		if( $(this).width() > ($(this).find("div").eq(0).width() + $(this).find("div").eq(0).offset().left)){
			$(this).find(".slide-container2").hide();
				//RESET GALLERY LEFT AND SCROLL
				resetGallery($(this));
			
		}else if( $(this).width() < $(this).find("div").eq(0).width()){
			$(this).find(".slide-container2").show();
		
		}
								 
	});	
	
	
}

function resetGallery(galleryToReset){
	
			galleryToReset.find(".slider2").slider("value", 0);
			galleryToReset.find(".gallery-inner").stop().animate({"left":"0px"}, 400);
			galleryToReset.find(".handle2").stop().animate({"left":"0px"}, 400);
	
}

function closeImagePopUp(){
	
	$("#portfolio-popup").fadeOut(400, function(){
												
		$(this).find("#portfolio-popup-inner").html("");										
	});
}

function openImagePopUp(){
	
	if ($(this).parent().hasClass("scrolling")){
		return;
	}else{
	
	var popup_image_data = $(this).attr("src"), //GET THE IMAGE SOURCE
		lastSlash = popup_image_data.lastIndexOf("/"), //FIND WHERE THE PATH ENDS
		image_name = popup_image_data.slice(lastSlash); //GET THE FILE NAME
		image_path = popup_image_data.slice(0, lastSlash);//GET THE PATH
		popup_html = "<img src='" + image_path + "/large" + image_name + "' />"; //PATH + large/ + FILE NAME
		
		
	$("#portfolio-popup-loader").show();
	$("#portfolio-popup").fadeIn(400, function(){
											   
											   
			$("#portfolio-popup-inner").html(popup_html);
			popupImageLoad($(this).find("#portfolio-popup-inner").find("img"));
						   
	});
	
	}// IF SCROLLIMG
	

}


function popupImageLoad(viewer){
	
	
	viewer.load(function(){
	
			$(this).fadeIn(400);
			$("#portfolio-popup-loader").fadeOut(800);				 
						 
	});
	
}


