/* ------------------------------------

By Hamilton Pytluk
ham@85digital.com

Free to use, but please keep this
credit line :)

------------------------------------ */

$(document).ready(function(){

	$("a").attr("tabIndex","-1").attr("onfocus","this.blur()");
	$(".linkout").attr("target","_blank");
	
	var duroptions = { "duration" : 900, "easing" : "easeInOutExpo" };
					
	// Primary Layout
	function quickbox_default(){ $("#quick-box").animate({ "top" : "0px" }, duroptions); }				
	function fills_default() { $("#fills").css("height","540px").animate({ "top" : "540px" }, duroptions); }			
	function main_default(x) { $("#main-page").delay(x).css("margin-top","0px").fadeIn(600); }			
	function footer_default(x) { $("#footer").delay(x).fadeIn(600);	}			
	function underover_default(){ $("#under, #over").animate({ "top" : "0px" }, duroptions);}
	function dots_default(){ $("#feature-dots").animate({ "top" : "260px" }, duroptions); }			
	function feature_default(x){ 
		$("#feature #over").css("z-index","3000"); 
		$("#feature").delay(x).animate({ "height" : "540px" }, duroptions); 
	}	
	
	function portfolio_default(x){ $("#portfolio-page").delay(x).animate({ "top" : "1200px" }, duroptions).fadeOut(300); }
	function jill_default(x){ $("#jill-page").delay(x).animate({ "top" : "1200px" }, duroptions).fadeOut(300); }
	function blog_default(x){ $("#blog-page").delay(x).animate({ "top" : "1200px" }, duroptions).fadeOut(300); }
	function view_default(x){ $("#view").animate({ "height" : "1200px"}, duroptions) }
			
	// Secondary Layout
	function quickbox_second(x){ $("#quick-box").delay(x).animate({ "top" : "-400px" }, duroptions); }	
	function main_second(x){ $("#main-page, #footer").fadeOut(300); }	
	function underover_second(x){ $("#under, #over").delay(x).animate({ "top" : "140px" }, duroptions); }
	function dots_second(x){ $("#feature-dots").delay(x).animate({ "top" : "-300px" }, duroptions); }			
	function fills_second(x){ $("#fills").delay(x).animate({ "top" : "140px" }, duroptions); }
	function feature_second(x){ 
		$("#feature #over").css("z-index","-10000");
		$("#feature").delay(x).animate({"height" : "540px" }, duroptions); 
	}
	
	
	function view_jill(x){ $("#view").animate({ "height" : "1000px"}, duroptions) }
	function view_port(x){ $("#view").animate({ "height" : "800px"}, duroptions); }
			
	// Blog Layout
	function main_blog(){ $("#main-page, #footer").fadeOut(300); }
	function feature_blog(x){ $("#feature").delay(x).animate({ "height" : "1800px" }, duroptions); }
	function underover_blog(x){ $("#under, #over").delay(x).animate({ "top" : "1800px" }, duroptions); }
	function dots_blog(x){ $("#feature-dots").delay(x).animate({ "top" : "-300px" }, duroptions); }
	function quickbox_blog(x){ $("#quick-box").delay(x).animate({ "top" : "1800px" }, duroptions); }
	function fills_blog(x){ $("#fills").delay(x).animate({ "top" : "1800px" }, duroptions); }	
	function blog_show(x){ $("#blog-page").fadeIn(0).delay(x).animate({ "top" : "-1815px" }, duroptions); }	
	function view_blog(x){ $("#view").animate({ "height" : "1500px"}, duroptions) }
			
	// Portfolio
	function portfolio_show(x){ $("#portfolio-page").delay(x).animate({ "top" : "-385px" }, duroptions).fadeIn(300); }	
	
	// Jill
	function jill_show(x){ $("#jill-page").delay(x).animate({ "top" : "-385px" }, duroptions).fadeIn(300); }	
	
	// Get Hastag
	var starting = window.location.hash;
	if(starting == '') { window.location.hash = "#!/home"; }
	if(starting == "#!/portfolio"){
		main_second();
		$(".sort-isedit").addClass("on");
		set_pagination("#editorial-flow");
		underover_second(900);
		dots_second(900);
		quickbox_second(900);
		fills_second(900);
		portfolio_show(900);	
	}	
	if(starting == "#!/jill"){
		main_second();
		underover_second(900);
		dots_second(900);
		quickbox_second(900);
		fills_second(900);
		jill_show(900);	
	}	
	if(starting.indexOf("#!/blog") >= 0 ){
		main_blog();
		portfolio_default(900);
		feature_blog(900);
		underover_blog(900);
		dots_blog(900);
		quickbox_blog(900);
		fills_blog(900);	
		blog_show(900);	
		view_blog();
	}		
	
	// So you can't keep on clickin' (later on)
	var running = false;
	
	$(".menulink").click(function(){
		
		// Get current location
		var at = window.location.hash;
	
		// Move the view to the top of the page
		$("html, body").delay(300).animate( { "scrollTop" : "0px" }, { "duration" : "300", "easing" : "easeInOutExpo" } );
			
		if(!running) {
			
			// Turn off clicks
			running = true;	
		
			// What did we click?			
			var goto = "#!/"+$(this).attr("class").replace(/ menulink/, '');	
								
			// Checks	
			if((goto == "#!/home") && (at == "#!/jill" || at == "#!/portfolio")) {			
				feature_default();			
				quickbox_default();
				portfolio_default(0);
				jill_default(0);
				fills_default();
				main_default(300);
				footer_default(300);
				underover_default();
				dots_default();
				view_default();
				running = false;				
			}	
					
			if((goto == "#!/jill") || (goto == "#!/portfolio")) {						
					
				if(at == "#!/home" || at.indexOf("#!/blog") >= 0) {
					main_second();
					underover_second(300);
					dots_second(300);
					quickbox_second(300);
					fills_second(300);
					
					if(goto == "#!/portfolio") {
						portfolio_show(300);
						view_port();
					}
					if(goto == "#!/jill") {
						jill_show(300);
						view_jill();
					}
				}
				
				if(at == "#!/jill" && goto =="#!/portfolio") {
					$(".sort-isedit").addClass("on");
					set_pagination("#editorial-flow");
					jill_default(0);
					portfolio_show(0);	
					view_port();				
				}
				
				if(at == "#!/portfolio" && goto =="#!/jill") {
					portfolio_default(0);			
					jill_show(0);	
					view_jill();	
				}
									
				running = false;
			}
			
			// FOR THE BLOG
			if(goto.indexOf("#!/blog") >= 0) {
				main_blog();
				portfolio_default(300);
				jill_default(300);
				feature_blog(300);
				underover_blog(300);
				dots_blog(300);
				quickbox_blog(380);
				fills_blog(300);	
				blog_show(300);
				view_blog();
				running = false;
			}
			
			if((goto == "#!/home") && (at.indexOf("#!/blog") >= 0)) {
				blog_default();
				quickbox_default();
				fills_default();
				main_default(0);
				footer_default(0);
				underover_default();
				dots_default();
				feature_default(0);
				view_default();
				running = false;	
			}
			
			if((goto == "#!/jill" || goto == "#!/portfolio") && (at.indexOf("#!/blog") >= 0)) {	
				blog_default(100);
				main_second();
				underover_second(0);
				dots_second(0);
				quickbox_second(0);
				fills_second(0);
				feature_second(300);	
						
				running = false;	
				if(goto == "#!/jill") {
					view_jill();	
				}
				if(goto == "#!/portfolio") {
					view_port();	
				}
			}
			
			// Dunno what to do with these
			if(goto == at) { running = false; }
							
		}
		
	});
	
	// Default Spread Tab
	$(".isall").addClass("on");
	
	// Zoom rollover	
	$("#portfolio-spread ul li").hover(function () {
    	$("img",this).attr("src","wp-content/themes/jill-theme/img/portfolio-shade-over.gif");
  	}, function () {
    	$("img",this).attr("src","wp-content/themes/jill-theme/img/portfolio-shade.gif");
	});
		
	// Sorting things out
	$("#portfolio-menu ul li").click(function(){
		
		$("#portfolio-menu ul li").removeClass("on");
		$(this).addClass("on");
		
		var todo = $(this).attr("class").replace(/sort-/, '').replace(/ on/, '');	
		if(todo == "isall") { $("li.portrait, li.structure, li.editorial").animate({ opacity: 1.0 }, 500); }
		
		// if(todo == "isedit") { $("li.portrait, li.structure").animate({ opacity: 0.2 }, 500); $("li.editorial").prependTo("#portfolio-spread ul:first-child").animate({ opacity: 1.0 }, 500); }
		// if(todo == "isport") { $("li.editorial, li.structure").animate({ opacity: 0.2 }, 500); $("li.portrait").prependTo("#portfolio-spread ul:first-child").animate({ opacity: 1.0 }, 500); }
		// if(todo == "isstru") { $("li.editorial, li.portrait").animate({ opacity: 0.2 }, 500); $("li.structure").prependTo("#portfolio-spread ul:first-child").animate({ opacity: 1.0 }, 500); }
		
		if(todo == "isedit") { set_pagination("#editorial-flow"); $("#portrait-flow, #structure-flow").fadeOut(500, function(){ $("#portfolio-hold").animate({"top":"0px"}, 10); $("#editorial-flow").fadeIn(500); } ); }
		if(todo == "isport") { set_pagination("#portrait-flow"); $("#editorial-flow, #structure-flow").fadeOut(500, function(){ $("#portfolio-hold").animate({"top":"0px"}, 10); $("#portrait-flow" ).fadeIn(500); } ); }
		if(todo == "isstru") { set_pagination("#structure-flow"); $("#editorial-flow, #portrait-flow").fadeOut(500, function(){ $("#portfolio-hold").animate({"top":"0px"}, 10); $("#structure-flow").fadeIn(500); } ); }
		
	});	
	
	// image popups
	var maxnum = $("#portfolio-spread ul").children().length + 1;
	var curr_image = 0;

	$("#fresh-work-image img").click(function(){
		$("#blowup").attr("src","wp-content/themes/jill-theme/img/fresh-work-image-f.jpg").attr("class","999");	
		$("#box").slideDown(600, "easeOutExpo");
		$("#box-in").delay(300).slideDown(600, "easeOutExpo");
		$("#img-prev, #img-next").css("display","none");
		$("#rewrap").css("width","600");
		$("#freshworkblurb").css("display","block");
	});
	
	$("#portfolio-spread .item").click(function(){
		
		maxnum = $(this).parent().parent().children().size() + 1;
		curr_image = $(this).parent().parent().children().index($(this).parent()) +1;
		
		//alert(maxnum + " - " + curr_image);
	
		var fullimg = "wp-content/themes/jill-theme/img/gallery/" + $(this).parent().attr("id") + "-f.jpg";
		var imgid = $(this).parent().attr("class").replace(/editorial /, '').replace(/portrait /, '').replace(/structure /, '').replace(/piece-/, '');
		
		// $("html, body").delay(600).animate( { "scrollTop" : "0px" }, { "duration" : "100", "easing" : "easeInOutExpo" } );
		$("#blowup").attr("src",fullimg).attr("class",imgid).fadeOut(0);		
		$("#box").slideDown(600, "easeOutExpo");
		$("#box-in").delay(300).slideDown(600, "easeOutExpo");
		$("#img-prev, #img-next").css("display","block");
		$("#rewrap").css("width","840");
		$("#freshworkblurb").css("display","none");
		
	});
	
	$("#img-wrap img, #img-copy p span").click(function() {
		$("#box").delay(300).slideUp(600, "easeOutExpo");
		$("#box-in").slideUp(600, "easeOutExpo");
	});
	
	// Image prev/next
	//var maxnum = $("#portfolio-spread ul").children().length;
	//maxnum++;
	
	$("#portfolio-spread ul li").each(function(counter) { $(this).addClass("piece-"+(counter+1)); });
	
	$("#img-next").click(function() {
		var next = $("#blowup").attr("class");
		next++;
		curr_image++;
		
		//alert(next + " : " + curr_image + " / " + maxnum);
		
		//if(next >= maxnum) {
		if(curr_image >= maxnum) {
			$("#box").delay(300).slideUp(600, "easeOutExpo");
			$("#box-in").slideUp(600, "easeOutExpo");
		} else {
			
			$("#box-in").fadeOut(300, function(){
			
				var nextimg = "wp-content/themes/jill-theme/img/gallery/" + $(".piece-" + next).attr("id") + "-f.jpg";	
				$("#blowup").attr("src",nextimg).attr("class",next);
				$("#box-in").delay(600).fadeIn(300);	
				
			});		
		
		}
		
	});
	
	$("#img-prev").click(function() {
		var prev = $("#blowup").attr("class");
		prev--;
		curr_image--;
		
		//if(prev == 0 || prev == 998) {
		if(curr_image == 0 || curr_image == 998) {
			$("#box").delay(300).slideUp(600, "easeOutExpo");
			$("#box-in").slideUp(600, "easeOutExpo");
		} else {
			
			$("#box-in").fadeOut(300, function(){
			
				var previmg = "wp-content/themes/jill-theme/img/gallery/" + $(".piece-" + prev).attr("id") + "-f.jpg";	
				$("#blowup").attr("src",previmg).attr("class",prev);
				$("#box-in").delay(600).fadeIn(300);	
				
			});		
		
		}
		
	});
	
	// Blog
	$("#blog-right ul li").each(function(counter) { $(this).attr("id","post-list-"+(counter+1)); });
	$("#blog-left div").each(function(counterb) { $(this).attr("class","post-list-"+(counterb+1)); });
	$("#post-list-1").addClass("current-post");
	
		//onload
		var thisisid = $(".current-post").attr("id");
		$("#blog-left ."+thisisid).fadeIn(0);		
		
			//onclick
			$("#blog-right ul li").click(function(){				
				
				var comingfrom = $(".current-post").attr("id");
				$("#blog-right ul li").removeClass("current-post");
				
				var goingto = $(this).attr("id");
					$("#blog-left ."+comingfrom).fadeOut(100);
					$("#blog-left ."+goingto).delay(120).fadeIn(100);	
					$("#"+goingto).addClass("current-post");
				
			});
			
	// Porfolio Rows
	$("#portfolio-button ul li").click(function(){
		
		if(!running) {
			
			var rowto = $(this).attr("id").replace(/port-row-/,'');
			
			if(rowto == 1) { $("#portfolio-hold").animate({ "top" : "-5px" }, duroptions); }
			if(rowto == 2) { $("#portfolio-hold").animate({ "top" : "-400px" }, duroptions); }
			if(rowto == 3) { $("#portfolio-hold").animate({ "top" : "-796px" }, duroptions); }
			if(rowto == 4) { $("#portfolio-hold").animate({ "top" : "-1192px" }, duroptions); }
			if(rowto == 5) { $("#portfolio-hold").animate({ "top" : "-1588px" }, duroptions); }
			if(rowto == 6) { $("#portfolio-hold").animate({ "top" : "-1978px" }, duroptions); }
			if(rowto == 7) { $("#portfolio-hold").animate({ "top" : "-2376px" }, duroptions); }
			if(rowto == 8) { $("#portfolio-hold").animate({ "top" : "-2770px" }, duroptions); }
			if(rowto == 9) { $("#portfolio-hold").animate({ "top" : "-3166px" }, duroptions); }
			
			$("#portfolio-button ul li.on").removeClass("on"); $("#port-row-"+rowto).addClass("on");
			
			running = false;	
			
		}
		
	});
	
	function set_pagination(arg_id){
		var total_pages = Math.ceil($(arg_id).children().size() / 15);
		var curr_page = 0;
		$("#portfolio-button ul li.on").removeClass("on"); $("#port-row-1").addClass("on");
		$("#portfolio-button ul li").each(function(){
			if(++curr_page <= total_pages){
				$(this).css("display","block");	
			}else{
				$(this).css("display","none");			
			}
		});
	}
			
	$(".contact-button-go").click(function(){ 	
		$("iframe").attr("src","/wp-content/themes/jill-theme/contact.php");
		$("#contact-form").fadeIn(300);	
	});	
	
	$("#contact-form-close").click(function(){ 	
		$("#contact-form").fadeOut(300);	
	});		
	
	$(".facebook-comment-boxes > div").each(function(counter) { $(this).addClass("fb-comments-"+(counter+1)); });
	
	$("#facebook-comment-button").click(function(){
		$("#facebook-comment-box").fadeIn(600, function(){
			var atthispost = $(".current-post").attr("id").replace(/post-list-/,'');
			$(".fb-comments-"+atthispost).css("display","block");
		});
	});
	
	$("#facebook-comment-box-close").click(function(){
		$("#facebook-comment-box").fadeOut(600);
		$(".fb-comments-1, .fb-comments-2, .fb-comments-3, .fb-comments-4, .fb-comments-5").css("display","none");
	});
			
			
});


