var lastAddress="";
var timeoutInt;
var isSliding = false;
var initialized = false;
function init(){
	// not good as 1st child. expands on roll over and collapses when trying to select items below.
	// $("#col-nav>ul>li:first-child").hover(a_hoverHandler, a_hoverOutHandler);
	
	if ($.browser.msie && $.browser.version <= 6) return;
	
	$("a").click(a_clickHandler);
	
	if(initialized) return;
		
	$.address.init(address_initHandler);
	$.address.change(address_changeHandler);
	
	initialized = true;
}


function a_hoverHandler(event){
	var $a = $(this).children("a");
	var attr =  $a.attr("href");
	if(attr=="/project/"){
		if(!isSliding) {
			isSliding = true;
			if(timeoutInt) clearTimeout(timeoutInt);
			timeoutInt = setTimeout('$("#col-nav>ul>li>ul").slideDown(500, function(){ isSliding = false })', 100);
		}
	}
}
function a_hoverOutHandler(event){
	var $a = $(this).children("a");
	var attr =  $a.attr("href");
	if(attr=="/project/" && $.address.pathNames()[0]!="project"){
		if(!isSliding) {
			isSliding = true;
			if(timeoutInt) clearTimeout(timeoutInt);
			timeoutInt = setTimeout('$("#col-nav>ul>li>ul").slideUp(500, function(){ isSliding = false })', 500);
		}
	}
}

function a_clickHandler(event){
	var $a = $(this);
	var attr =  $a.attr('href');
	
		
	var href = this.href;
	var hostname = location.protocol+"//"+location.hostname;
	if(attr.indexOf(location.protocol+"//"+location.hostname) == 0){
		attr = attr.substring(hostname.length);
	}
	if(attr.indexOf("mailto:")==0 || attr.indexOf("/contact")>=0 || attr.indexOf("http:")==0){
		return true;
	}
	//Workaround for PDF open link	
	if(attr =="/pdfs/esplanadearticle.pdf")
	{		
		var arr = attr.split($.address.baseURL());
		
		var attr_path = arr[arr.length-1];
		attr_path = (attr_path.indexOf('/#')==0) ? attr_path.substr(1) : attr_path;		
		
		n = attr_path.substr(indexOfHash+1);
		
		var val = unescape($.address.value());
		
		var hash = $.address.hash();
		if(hash=='' &&  val.lastIndexOf('#')>=0)
		{
			hash = val.substr( val.lastIndexOf('#'), val.length+1 );
		}
		if(hash!='')
		{
			val = val.substr(0,val.lastIndexOf('#'));
		}
		var jumptolink = $('a[name='+n+']');
		
		return true;
	}
	$.address.value(attr);
	return false;
}

function address_initHandler(event) {
	if(location.pathname != event.path){
		$("#content-window > div").hide();
		
	}
}
function address_changeHandler(event) {
	//if(location.pathname != event.path){
		address_load(event.path);
	//}
}
function address_load(path) {
	$.post(path, getHandler);
}
function getHandler(data, textStatus) {	
	$.address.title($(data).parent().find("title").text());
	document.title = $(data).parent().find("title").text();
	
	var loadedWrapperDiv = $(data).parent().find("div");
	var projectsUL = $("#col-nav>ul>li>ul");
	
	$("#col-right").html($("#col-right", loadedWrapperDiv).html());	
	
	var currentPathNames = $.address.pathNames();
	
	var animateThumbs = true;
	if(lastAddress.length>0){
		var lastPathNames = lastAddress.substring(1,lastAddress.length-1).split("/");
		
		animateThumbs = (lastPathNames[0]!=currentPathNames[0] 
							|| (lastPathNames[0]==currentPathNames[0]  && lastPathNames[1]!=currentPathNames[1] && currentPathNames[0]!="about"));
		if(lastPathNames[0]=="project" && currentPathNames[0]!="project"){
			projectsUL.slideUp(500, function(){ 
												$("#col-nav").html($("#col-nav", loadedWrapperDiv).html());
												init();
										});
		} else if(lastPathNames[0]!=currentPathNames[0] && currentPathNames[0]=="project"){
			
			var wasHidden = projectsUL.is(":hidden");
			$("#col-nav").html( $("#col-nav", loadedWrapperDiv).html() );
			if(wasHidden){
				projectsUL = $("#col-nav>ul>li>ul");
				projectsUL.hide();
				projectsUL.slideDown(500);
			}
			
		} else {
			$("#col-nav").html( $("#col-nav", loadedWrapperDiv).html() );
		}
		
	} else {
		$("#col-nav").html($("#col-nav", loadedWrapperDiv).html());
		if(currentPathNames[0]=="project"){
			projectsUL = $("#col-nav>ul>li>ul");
			projectsUL.hide();
			projectsUL.slideDown(1000);
		}
	}
	// init();
	if(animateThumbs) $("#secondary-nav").fadeOut(500);
	// if(animateThumbs) { $("#secondary-nav").animate({opacity:0}, 250, "linear", function() { $(this).find("ul").html( $("#secondary-nav>ul", loadedWrapperDiv).html() ); $(this).animate({opacity:1},250); init(); }); }
	$("#content-window>div").fadeOut(500, 
		function(){
			$("#content-window").html( $("#content-window", loadedWrapperDiv).html() );			
			$("#secondary-nav").html( $("#secondary-nav", loadedWrapperDiv).html() );
			$("#content-window > div").hide(0);
			$("#wrapper").attr("class", loadedWrapperDiv.attr("class"));
			$("#content-window>div").fadeIn(500);
			if(animateThumbs) $("#secondary-nav").fadeIn(500);
			if($.address.path()=="/contact/"){
				ltkForm_init();
			}
			init();
		});

	lastAddress = $.address.value();
}

var defaultNameValue = "Your name";
var defaultEmailValue = "Email";

function ltkForm_init(){
	if( document.ltkForm.senderName.value=="" ){
		document.ltkForm.senderName.value = defaultNameValue;
	}
	if( document.ltkForm.senderEmail.value=="" ){
		document.ltkForm.senderEmail.value = defaultEmailValue;
	}
}

function clearDefaultValues(){
	if( document.ltkForm.senderName.value==defaultNameValue ){
		document.ltkForm.senderName.value = "";
	}
	if( document.ltkForm.senderEmail.value==defaultEmailValue ){
		document.ltkForm.senderEmail.value = "";
	}
	return true;
}

$(document).ready(init);
