/**
 * @author jmdigiac
 */

/*
 * jQuery BBQ: Back Button & Query Library - v1.2.1 - 2/17/2010
 * http://benalman.com/projects/jquery-bbq-plugin/
 * 
 * Copyright (c) 2010 "Cowboy" Ben Alman
 * Dual licensed under the MIT and GPL licenses.
 * http://benalman.com/about/license/
 */
(function($,p){var i,m=Array.prototype.slice,r=decodeURIComponent,a=$.param,c,l,v,b=$.bbq=$.bbq||{},q,u,j,e=$.event.special,d="hashchange",A="querystring",D="fragment",y="elemUrlAttr",g="location",k="href",t="src",x=/^.*\?|#.*$/g,w=/^.*\#/,h,C={};function E(F){return typeof F==="string"}function B(G){var F=m.call(arguments,1);return function(){return G.apply(this,F.concat(m.call(arguments)))}}function n(F){return F.replace(/^[^#]*#?(.*)$/,"$1")}function o(F){return F.replace(/(?:^[^?#]*\?([^#]*).*$)?.*/,"$1")}function f(H,M,F,I,G){var O,L,K,N,J;if(I!==i){K=F.match(H?/^([^#]*)\#?(.*)$/:/^([^#?]*)\??([^#]*)(#?.*)/);J=K[3]||"";if(G===2&&E(I)){L=I.replace(H?w:x,"")}else{N=l(K[2]);I=E(I)?l[H?D:A](I):I;L=G===2?I:G===1?$.extend({},I,N):$.extend({},N,I);L=a(L);if(H){L=L.replace(h,r)}}O=K[1]+(H?"#":L||!K[1]?"?":"")+L+J}else{O=M(F!==i?F:p[g][k])}return O}a[A]=B(f,0,o);a[D]=c=B(f,1,n);c.noEscape=function(G){G=G||"";var F=$.map(G.split(""),encodeURIComponent);h=new RegExp(F.join("|"),"g")};c.noEscape(",/");$.deparam=l=function(I,F){var H={},G={"true":!0,"false":!1,"null":null};$.each(I.replace(/\+/g," ").split("&"),function(L,Q){var K=Q.split("="),P=r(K[0]),J,O=H,M=0,R=P.split("]["),N=R.length-1;if(/\[/.test(R[0])&&/\]$/.test(R[N])){R[N]=R[N].replace(/\]$/,"");R=R.shift().split("[").concat(R);N=R.length-1}else{N=0}if(K.length===2){J=r(K[1]);if(F){J=J&&!isNaN(J)?+J:J==="undefined"?i:G[J]!==i?G[J]:J}if(N){for(;M<=N;M++){P=R[M]===""?O.length:R[M];O=O[P]=M<N?O[P]||(R[M+1]&&isNaN(R[M+1])?{}:[]):J}}else{if($.isArray(H[P])){H[P].push(J)}else{if(H[P]!==i){H[P]=[H[P],J]}else{H[P]=J}}}}else{if(P){H[P]=F?i:""}}});return H};function z(H,F,G){if(F===i||typeof F==="boolean"){G=F;F=a[H?D:A]()}else{F=E(F)?F.replace(H?w:x,""):F}return l(F,G)}l[A]=B(z,0);l[D]=v=B(z,1);$[y]||($[y]=function(F){return $.extend(C,F)})({a:k,base:k,iframe:t,img:t,input:t,form:"action",link:k,script:t});j=$[y];function s(I,G,H,F){if(!E(H)&&typeof H!=="object"){F=H;H=G;G=i}return this.each(function(){var L=$(this),J=G||j()[(this.nodeName||"").toLowerCase()]||"",K=J&&L.attr(J)||"";L.attr(J,a[I](K,H,F))})}$.fn[A]=B(s,A);$.fn[D]=B(s,D);b.pushState=q=function(I,F){if(E(I)&&/^#/.test(I)&&F===i){F=2}var H=I!==i,G=c(p[g][k],H?I:{},H?F:2);p[g][k]=G+(/#/.test(G)?"":"#")};b.getState=u=function(F,G){return F===i||typeof F==="boolean"?v(F):v(G)[F]};b.removeState=function(F){var G={};if(F!==i){G=u();$.each($.isArray(F)?F:arguments,function(I,H){delete G[H]})}q(G,2)};e[d]=$.extend(e[d],{add:function(F){var H;function G(J){var I=J[D]=c();J.getState=function(K,L){return K===i||typeof K==="boolean"?l(I,K):l(I,L)[K]};H.apply(this,arguments)}if($.isFunction(F)){H=F;return G}else{H=F.handler;F.handler=G}}})})(jQuery,this);
/*
 * jQuery hashchange event - v1.2 - 2/11/2010
 * http://benalman.com/projects/jquery-hashchange-plugin/
 * 
 * Copyright (c) 2010 "Cowboy" Ben Alman
 * Dual licensed under the MIT and GPL licenses.
 * http://benalman.com/about/license/
 */
(function($,i,b){var j,k=$.event.special,c="location",d="hashchange",l="href",f=$.browser,g=document.documentMode,h=f.msie&&(g===b||g<8),e="on"+d in i&&!h;function a(m){m=m||i[c][l];return m.replace(/^[^#]*#?(.*)$/,"$1")}$[d+"Delay"]=100;k[d]=$.extend(k[d],{setup:function(){if(e){return false}$(j.start)},teardown:function(){if(e){return false}$(j.stop)}});j=(function(){var m={},r,n,o,q;function p(){o=q=function(s){return s};if(h){n=$('<iframe src="javascript:0"/>').hide().insertAfter("body")[0].contentWindow;q=function(){return a(n.document[c][l])};o=function(u,s){if(u!==s){var t=n.document;t.open().close();t[c].hash="#"+u}};o(a())}}m.start=function(){if(r){return}var t=a();o||p();(function s(){var v=a(),u=q(t);if(v!==t){o(t=v,u);$(i).trigger(d)}else{if(u!==t){i[c][l]=i[c][l].replace(/#.*/,"")+"#"+u}}r=setTimeout(s,$[d+"Delay"])})()};m.stop=function(){if(!n){r&&clearTimeout(r);r=0}};return m})()})(jQuery,this);


$(document).ready(function()
{
	
var IsIE = false;
<!--
if (navigator.appName=="Microsoft Internet Explorer") {
	IsIE=true;
	$("#footer").css("color","red").html("You are browsing with Internet Explorer, which, unfortunately, doesn't (yet) get along with this website. Any other browser should work. <a href='http://www.google.com/chrome/'>Chrome</a> and <a href='http://www.apple.com/safari/download/'>Safari</a> are best.");
}
//-->

var imageset;
var footer_on;
var footer_off=false;

function hide_footer(){
	footer_on=false;
	$("#footer").animate({opacity:"0"},"fast", function(){
		$("#footer").hide();
		});
}

function show_footer(){
	if ((imageset||footer_on)&&!footer_off){
		footer_on=true;
		if (IsIE) {
			$("#footer").css("color","red").load("IE.html");
		} 
		else if (imageset){
			$("#footer").load("navigation.html");
			window.setTimeout(hide_footer,4000);
		}
		else {
			$("#footer").load("copyright.html");
			//window.clearTimeout();
		}
		$("#footer").show(function(){
			$("#footer").animate({opacity:"0.7"},"fast");
		});
		
	}
	else {hide_footer();}
	
}

function fade_title(){
//fade-in title page elements
	$('#img-viewer').animate({opacity: 1}, "normal", function(){
		$("#loading").css("visibility", "hidden");
	})
	show_footer();
}

//bbq 1
	//var cache={'':$('.bbq-default')};
//var pagetype;

function scratch(x){
	if (x.parent().siblings(".submenu_item").is(":not(.visited)")) {}
	else {
		x.parent().siblings(".menu_item").addClass("visited");
	}
}

function set_title(A){
	$('a.menulink').each(function(index) {
		var title=$(this).text();
		var href=$(this).attr("href");
		
		if (href==location.hash){
			$(this).parent().addClass("visited");
			scratch($(this));
			if (A==""){
				document.title = title + ' > Joel Di Giacomo';
			}
			else {
				document.title = title + ' > ' + A +  ' > Joel Di Giacomo';
					
			}
				
		}
  	}); 
}

function load_page(page){
	$('#img-viewer').animate({opacity: 0}, "fast", function(){
		$("#loading").css("visibility","visible").animate({opacity:1}, "fast");
		$('#img-viewer').load(page, function(){
			if (imageset) {
				$("#controller").jFlow({
					slides: "#slides",
					width: "900px",
					height: "600px",
					duration: 500
				});
			}
			$("#loading").animate({opacity:0}, "fast").css("visibility", "hidden");
			$('#img-viewer').animate({opacity: 1}, "normal");
		});
		show_footer();
	});
}

    $(window).bind('hashchange', function(e){
        var url = e.fragment;
		var pagetype=url.slice(0, 4);
		var category;
		
		//Title page or Define "page" variable
		if (url=='') {
			var page="title.html";
			var pagetype="title"
			document.title = 'Joel Di Giacomo';
			hide_info();
			footer_on=true;
			imageset=false;
			$("#menu ul li.menu_item").show("fast").nextAll().hide("fast");
			$("#info,#home").hide("fast");
			$('#img-viewer').animate({opacity: 0}, "normal", function(){
	  			$('#img-viewer').load(page, function(){
					fade_title();
				});
			});
		}
		else {
			var page=url+".html";
			footer_on=false;
		}	
		
		if (pagetype=="arch") {
			//var pagename=url.slice(5);
			var category="Architecture";
			var info = "info-" + url.slice(5) + ".html";
			$("#info,#home").show("fast");
			//hide all menu items except 1st menu header
			$("[id!='menu_item1']&&.menu_item, .submenu_item").hide("fast");
			if ($("#menu_item1").is(":not(:visible)")) {
				$("#menu_item1").show("fast");
			}
			$('#info-content').load(info);
			imageset=true;
			load_page(page);
		}
		else if (pagetype=="phot") {
			var category="Photography";
			//hide all menu items except 2nd menu header	
			$("[id!='menu_item2']&&.menu_item, .submenu_item, #info").hide("fast");
			$("#home").show("fast");
			hide_info();
			if ($("#menu_item2").is(":not(:visible)")) {
				$("#menu_item2").show("fast");
			}
			imageset=true;
			load_page(page);
		}
		else if (pagetype=="film") {
			//var pagename=url.slice(5);
			var category="Film";
			$("#menu ul li.menu_item").show("fast").nextAll().not('.submenu_item3').hide("fast");
			$('.submenu_item3').show("fast");
			$("#info,#home").hide("fast");
			hide_info();
			imageset=false;
			load_page(page);
		}
		else if (pagetype=="guid"||pagetype=="sket") {
			//var pagename=url;
			var category='';
			var info = "info-" + url + ".html";
			//hide all menu items except 5th menu header	
			$("[id!='menu_item5']&&.menu_item, .submenu_item, #info").hide("fast");
			$("#info,#home").show("fast");
			if ($("#menu_item5").is(":not(:visible)")) {
				$("#menu_item5").show("fast");
			}
			$('#info-content').load(info);
			imageset=true;
			load_page(page);
		}
		else if (pagetype=="link"||pagetype=="para"||pagetype=="proc") {
			//var pagename=url;
			var category='';
			$("#menu ul li.menu_item").show("fast").nextAll().not('.submenu_item5').hide("fast");
			$('.submenu_item5').show("fast");
			$("#info,#home").hide("fast");
			hide_info();
			imageset=false;
			load_page(page);
		}	
		else if (pagetype=="soun") {
			//var pagename=url;
			var category='Sound';
			$("#menu ul li.menu_item").show("fast").nextAll().not('.submenu_item4').hide("fast");
			$('.submenu_item4').show("fast");
			$("#info,#home").hide("fast");
			hide_info();
			imageset=false;
			load_page(page);
		}
		set_title(category);				
    });

	$(window).trigger('hashchange');

$('#menu').animate({opacity: 1}, "slow");

//hide submenu items
	$("#menu ul li.menu_item").nextAll().hide();
	
//display active
	//$("#menu ul").each(function(){
	//$(this).find("li.active").prevAll().nextAll().show();
	//});
	
//slide toggle menu items
	$("#menu ul li.menu_item").click(function(){
	$(this).nextAll().slideToggle("fast");
	});

//hide submenu items
	$("#menu ul li.submenu_item").click(function(){
		if ($(this).find("a").attr("href")==location.hash&&imageset){	
			menu_collapse($(this));
			if (location.hash.slice(1, 5) == "arch"||location.hash.slice(1, 5) == "guid") {
				$("#info").show("fast");
			}
		}	
	});

function menu_collapse(x){
	x.parent().children().first().addClass('current');
	$(".submenu_item,.menu_item").not('.current').hide("fast");
	$(".menu_item").removeClass('current');
	$("#home").show("fast");
}

//hide footer
	$("#footer").click(function(){
		footer_off=true;
		hide_footer();
	});

/*hide copyright tag
	$("#copyright").click(function(){
	$(this).animate({opacity:"0"},"fast", function(){
		$(this).hide();
		});
	});	
*/

//display "menu" and "info" on hover
	$("#home").hover(function () {
		$(this).append("<span> Menu</span>");
		}, 
  		function () {
    	$(this).find("span:last").remove();
  		});
	$("#info").hover(function(){
			$(this).append("<span> Info</span>");
		}, 
  		function () {
			$(this).find("span:last").remove();
		/*if ($("#info-content").is(':hidden')) {
			$(this).append("<span> Info</span>");
			}
		}, 
  		function () {
		if ($("#info-content").is(':hidden')) {
			$(this).find("span:last").remove();
			}*/
  		});

//display menu headers when home button clicked	
	$("#home").click(function(){
		$("#menu .menu_item").show("fast");
		$("#menu ul li.menu_item").nextAll().hide();
		$("#home, #info").hide("fast");
		hide_info();
		//$('#img-viewer').load('title.html'); //load title image when home button clicked
	});	

//add visited class to external links
	$("a.external").click(function(){
		$(this).parent().addClass("visited");
		scratch($(this));
	});
	
//display and hide info when clicked
	$("#info").click(function(){
		toggle_info();
	});

//hide info content when clicked
	$("#info-content").click(function(){
		hide_info();
	});

var menuwidth1 = $("#menu").css("width");
	menuwidth2 = menuwidth1.slice(0, 3)*2;
	
function hide_info(){
	$("#info").find("span:last").remove();
	if ($("#info-content").is(':visible')) {
		$("#info-content").hide("fast", function(){
			$("#menu").animate({width: menuwidth1}, "fast");
		});
	}	
}
		
function toggle_info(){
	if ($("#info-content").is(':hidden')) {
		$("#menu").animate({width:menuwidth2},"fast", function(){
			$("#info-content").show("fast");
		});
	}
	else  {
		hide_info();
	}
}

//keyboard navigation
	$(document.documentElement).keyup(function (event) {
  	if (event.keyCode == 37) {
    	$(".jFlowPrev").click();
  	} else if (event.keyCode == 39) {
		$(".jFlowNext").click();
  	}
	});

	
});
