$(document).ready(function(){
	
	//loadImages($("#cases img"), $("#masterProgress"));
	
	//change when move
	var siteURL = "http://www.byjohn.se/";
	
	var cases = $("#cases a");
	var galleryNav = $;
	
	var description = "";
	var pageColor = "";
	
	var bodyScroll = 0;
	var documentTitle = document.title;
	var titleArray = documentTitle.split("-");
	
	
	var animDur = 800;
	cases.mouseover(function(){
		
		var currentIndex = $(this).index();
		
		$("span", this).animate({color: eval("caseColor"+currentIndex)}, { queue:false});
		$(this).animate({backgroundColor: eval("caseColor"+currentIndex)}, { queue:false});
	});
	
	cases.mouseout(function(){
			$("span", this).animate({color: "#00f9ff"}, { queue:false });
		    $(this).animate({ backgroundColor: "#00f9ff" }, { queue:false });
	});

	//AJAX
	
	var newHash = window.location.toString().substring(window.location.toString().indexOf("#")+1);
	
	
	cases.click(function(){
		bodyScroll = $("body").scrollTop();
		//remeber to check this if AJAX doen't work after move to online
		var where_is_hash= $(this).attr("href");
		var mytool_array=where_is_hash.split("/");
		
		newHash = mytool_array[3];
		
		window.location.hash = newHash;

		return false;

	});
	
	$(window).bind("hashchange", function(){
		
		$("#caseOverlay").css({background: "#181818 url("+siteURL+"wp-content/themes/Portfolio 2010/img/preloader.gif) center fixed no-repeat"});
		
		
		//alert("hash change");			
		//change this URL when moveBy
		
		newHash = window.location.toString().substring(window.location.toString().indexOf("#")+1);
		
		changeTitle("Loading");
		
		
		if(window.location == siteURL+"#" || window.location == siteURL){
			changeTitle("Home");
			$("#caseOverlay").fadeOut("fast", function(){
				$("body").css({overflow: "auto"});
			});
		}
		
		else{
		
			$("#caseOverlay").fadeIn("fast");
			
			$("body").css({overflow: "hidden"});
			
			$("#caseWrapper").empty();
			$("#caseWrapper").load(siteURL + newHash + " #caseContent", function(){
				
				//When case is loaded
				
				$("#caseOverlay").css({background: "#181818"});
				
				changeTitle($("h2#title").html());
				
				//change pageTitle
				/*
				var titleArray = documentTitle.split("-");
				var newTitle = $("h2#title").html();
				
				document.title = newTitle +" - "+ titleArray[1];
				*/
				
				var caseTop = $("#caseTop");
				description = $("#description");
				var caseNav = $("#caseNav a");
				pageColor = $("h2#title").css('color');
								
				//description.toggle();
				//description.animate({height: "toggle"});

				$(".category-work").css({borderBottom: pageColor + "solid 1px"});
				
				transIn();
				
				function transIn(){
					caseTop.css({opacity: 0, marginTop: 55});
					caseTop.animate({opacity: 1, marginTop: 40});
				}
								
				$(".close").click(function(){
					changeTitle("Home");
					window.location = siteURL+"#";
					//$(window).trigger("hashchange");
					$("body").scrollTop(bodyScroll);

					return false;
				});
				
				//Loading Navigate between cases
				caseNav.click(function(){
					//remeber to check this if AJAX doen't work after dircetory change
					var where_is_hash= $(this).attr("href");
					var mytool_array=where_is_hash.split("/");
					
					newHash = mytool_array[3];
					
					window.location.hash = newHash;
					
					return false;
				});

				//Case Navigation
				$("span.generalText").css({opacity: 0});
				if($("img.prev").length == 0 || $("img.next").length == 0){
					$("span.generalText").css({left: "25px"});
				}
				else{
					$("span.generalText").css({left: "0px"});
				}
				
				caseNav.mouseover(function(){
					$("span.generalText", this).animate({opacity: "1"},{duration: 200, queue:false});
				});
				
				caseNav.mouseout(function(){
					$("span.generalText", this).animate({opacity: "0"},{duration: 200, queue:false});
				});
					
				//CaseNavigation END
				
				
				//Gallery START
				galleryNav = $("#galleryNav a");
				galleryNav.hide();
				
				var images= $("#gallery .gallery-item");
				var zCount = images.length;
				
				images.click(function(){return false;});
						
				images.css({position: "absolute"});
				galleryNav.filter(":first").css({background: pageColor, borderColor: pageColor});
				galleryNav.filter(":first").addClass("selected");
				
				images.filter(":eq(0)").css({zIndex: "100"});
				images.css({zIndex: "0"});
				
				//Images in correct z-order
				
				for(var i = 0; i <= images.length-1; i++){
					images.filter(":eq("+i+")").css({zIndex: images.length-i});
				}
				
				
				images.hide();
				loadImages(images, $("#progressbar"));
				
				
				galleryNav.filter(":last").remove();
				galleryNav.click(function(){
					
					
					if($(this).hasClass("selected")){
						
					}
					
					else{
						galleryNav.removeClass("selected");
						var currentIndex = $(this).index();
						var relatedImg = images.filter(":eq("+currentIndex.toString()+")");
				
						galleryNav.css({background: "none"});
						$(this).css({background: pageColor, borderColor: pageColor});
					
						//images.css({zIndex: "0"});
					
						relatedImg.css({zIndex: zCount++});
						relatedImg.toggle();
						relatedImg.fadeIn("100000");
					}
					
					$(this).addClass("selected");
					
					return false;
				});
				
				//Gallery END
				
				//Loading actions ends here

			});

		}
	});
	
	if(newHash != siteURL){
		$("#caseOverlay").css({display: "block"});
		$(window).trigger("hashchange");
	}
	
	
	//Preloading
	
	//$("#portfolio").toggle();
	var loadCount = 0;
	
	$("#cases img").load(function(){
		loadCount++;
		//alert(loadCount);

		
	});
	
	function loadImages(images, loadingBar){
		
		ghostImage = images.filter(":last");
		ghostImage.show();
		
		ghostImage.css({position: "absolute", zIndex: 1000, opacity: .9});
		
		//images.fadeIn();
		//total and loaded images
		var totImg = images.length;
		var lodImg = 0;
		
				
		$("#images").toggle();	
		
		
		$("#gallery img").load(function(){
			lodImg++;
			percent = lodImg/totImg;
						
			loadingBar.css({backgroundColor: pageColor});
			
			var updatePercent = percent*100;
			//alert(images.length);
			
			loadingBar.animate({width : updatePercent+"%"}, {queue: false});
			
			//WRITES PERCENT IN PROGRESSBAR
				//loadingBar.html(Math.round(updatePercent)+"%");
			
			//alert("pecent: "+updatePercent+" loaded: "+lodImg+" totImg "+totImg);
			
			if (updatePercent == 100){
					loadingBar.html("");
					loadingBar.animate({width:"100%"}).fadeOut(	function(){
					//images.filter(":eq("+currentIndex.toString()+")");
					allDone();				

				});
			}
			
		});
		
		function allDone(){
			if(images.length == 2){}
			else{galleryNav.fadeIn();}
			
			images.filter(":first").fadeIn();
			ghostImage.animate({opacity: 0}, function(){
				ghostImage.remove();
				images.show()
			});
		}

	}
	
	
	//Menu
	
/*******

	***	Anchor Slider by Cedric Dugas   ***
	*** Http://www.position-absolute.com ***
	
	Never have an anchor jumping your content, slide it.

	Don't forget to put an id to your anchor !
	You can use and modify this script for any project you want, but please leave this comment as credit.
	
*****/

	$(".menu a").anchorAnimate();
	
	//Mail form
	
	$("#submit_btn").click(function(){
		
		var mailForm = document.contact;
		
		if(mailForm.email.value == ""){
			displayMessage("Please enter your E-mail address");
		}
		
		else if(validateMail(mailForm.email.value)){
			displayMessage("Invalid E-mail address");
		}
		
		else if(mailForm.message.value == ""){
			displayMessage("Make sure to enter a message before sending");
		}
		
		else{
			//alert(mailForm.email.value);
			
			$.post("http://www.byjohn.se/wp-content/themes/Portfolio%202010/contactForm/sendForm.php", {email: mailForm.email.value, message: mailForm.message.value}, function(data){
			
				displayMessage("I will get back to you as soon as possible.");
				
			});
		}
		
		return false;
	});
	
	function displayMessage(message){
		$("#sendingForm").css({display: "block"});
		$("#sendingForm").css({background: "#181818"});
		$("#sendingForm").html("<p style='text-align:center; padding-top: 100px;'>"+message+"</p>");
		$("#sendingForm p").hide();
		$("#sendingForm p").fadeIn(function(){
			$("#sendingForm").delay(2500).fadeOut();
		});
	}
		
	function validateMail(mail) {
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		
		if(reg.test(mail) == false) {
			//alert('Invalid Email Address');
			return true;
	   }
	}
	
	
	//Change Title
	function changeTitle(caseTitle){
		document.title = caseTitle +" - "+ titleArray[1];
	}
	
});
