$(document.window).addEvent('domready',function(){
	
	addEventHandlers();
	init();
	(function(){addImgEvents()}).delay(100);
	
	//var interval = checkHash.periodical(100);

});

var currentHash = "";
function checkHash(){
	if(currentHash != getHash().toString()){
		//console.log('current hash = '+currentHash+' | getHash = '+ getHash());
		currentHash = getHash().toString();
		init();
	}
}

function init(){
	
	if(getHash().toString().contains('%27')){
		window.location = "#"+getHash().replaceAll('%27',"'");
	}
	
	var navTargets = JSON.decode(getHash().toString().replaceAll("'",'"'));
	if(!navTargets){return null;}
	youAreHere(navTargets);
	$('nav').getElements('a.current').each(function(item,index){	
		var js = item.getProperty('href').replace('javascript:','');
		
		var result = $try(function(){
		    eval(js);	
		});
 
			
	});
	
	(function(){addImgEvents()}).delay(200);
	
	(function(){
		if(navTargets.img){
			insert_image(navTargets.img,"content");
		}
	}).delay(200);
	
	
	if(
		navTargets.main == 'contact.php' ||
		navTargets.main == 'brochures.html' ||
		navTargets.main == 'thankyou.html' ||
		navTargets.main == 'employment.html' ||
		navTargets.main == 'newsletters.html'
	){	
	(function(){
		$('content').load(navTargets.main);
		$('pictures').load('empty.html');
		}).delay(100);		
	}
}

function addEventHandlers(){
	$(document.body).getElements('#nav a').each(function(item,index){
		item.addEvents({
			'click':function(e){				
				var targetsArr = item.getProperty('href').split("'");
				var navTargets = {id:targetsArr[1],main:targetsArr[3],bottom:targetsArr[5]};								
				window.location = "#"+JSON.encode(navTargets).replaceAll('"',"'");				
				youAreHere(navTargets);
				(function(){addImgEvents()}).delay(200);
			}
		});
		//insert_image("img/portfolioimages/fullsize/portst3.jpg", "content")	
		$('contactLink').removeEvents('click');
		$('contactLink').addEvents({'click':function(e){
			e.stop();
			//javascript:wrapHttpRequest({url:'contact.php',target:document.getElementById('content'),callback:contactHttpHandler});
			(function(){
				$('content').load('contact.php');			
				$('pictures').load('empty.html');
				 window.location = "#{'main':'contact.php','bottom':'empty.html'}";
			 }).delay(100);
		}});
		
		$('brochuresLink').removeEvents('click');
		$('brochuresLink').addEvents({'click':function(e){
			e.stop();
			//javascript:wrapHttpRequest({url:'brochures.html',target:document.getElementById('content'),callback:contactHttpHandler});
			(function(){
				$('content').load('brochures.html');	
				$('pictures').load('empty.html');
				 window.location = "#{'main':'brochures.html','bottom':'empty.html'}";
			 }).delay(100);
		}});
		
		$('employmentLink').removeEvents('click');
		$('employmentLink').addEvents({'click':function(e){
			e.stop();
			//javascript:wrapHttpRequest({url:'employment.html',target:document.getElementById('content'),callback:contactHttpHandler});
			(function(){
				$('content').load('employment.html');
				$('pictures').load('empty.html');
				window.location = "#{'main':'employment.html','bottom':'empty.html'}";
			}).delay(100);
		}});
		
		$('newslettersLink').removeEvents('click');
		$('newslettersLink').addEvents({'click':function(e){
			e.stop();
			//javascript:wrapHttpRequest({url:'newsletters.html',target:document.getElementById('content'),callback:contactHttpHandler});
			(function(){	
				$('content').load('newsletters.html');
				$('pictures').load('empty.html');
				window.location = "#{'main':'newsletters.html','bottom':'empty.html'}";
			}).delay(100);
		}});
		
		
		
	});	
	
	$('contactLink').addEvents({
		'click':function(e){				
				var targetsArr = this.getProperty('href').split("'");
				//console.log(targetsArr);
				var navTargets = {main:targetsArr[1],bottom:'empty.html'};								
				window.location = "#"+JSON.encode(navTargets).replaceAll('"',"'");				
				youAreHere(navTargets);
				$('pictures').load('empty.html');
			}
	});
	
	$('logoLink').addEvents({
		'click':function(e){	
			var navTargets = {main:'home.html',bottom:'homebottom.html'};	
			$('pictures').load('homebottom.html');
			window.location = "#"+JSON.encode(navTargets).replaceAll('"',"'");				
			youAreHere(navTargets);
		}
	});
	
	//addImgEvents();
}

function addImgEvents(){
	$('pictures').getElements('img').addEvents({
		'click':function(e){
			
			if(this.getParent().get('tag') == 'a'){
				(function(){init()}).delay(10);
				return;
			}
			//console.log('clicked an image in the bottom area');
			
			var navTargets = JSON.decode(getHash().replaceAll("'",'"')) || {};	
			var onclick = this.getProperty('onclick').toString();
			if(!onclick){return;}		
			
			var targetsArr = onclick.replace('"',"'").replaceAll('"',"'").split("'");			
			
			//console.log(targetsArr);
			
			navTargets.img = targetsArr[1];			
			window.location = "#"+JSON.encode(navTargets).replaceAll('"',"'");
		}
	});
}



function youAreHere(navTargets){	
	$('nav').getElements('a').removeClass('current');
	$('nav').getElements('a').addClass('notCurrent');
	
	//not current
	$('nav').getElements('a').setStyles({'text-decoration':'none','font-weight':'normal','color':'#94BD88'});
	
	$try(function(){
	   $(navTargets.id).removeClass('notCurrent');
	  $(navTargets.id).addClass('current');	
	   
	   //current
	   $(navTargets.id).setStyles({'text-decoration':'underline','font-weight':'bolder'	,'color':'#FFF4B7'});
	}, function(){
	    //no dice
	});	
	
	$('nav').getElements('a.current').each(function(item,index){	
		$try(function(){
			item.getParent('li').getParents('li').getFirst('a').removeClass('notCurrent');
		    item.getParent('li').getParents('li').getFirst('a').addClass('current');			 
			item.getParent('li').getParents('li').getFirst('a').setStyles({'text-decoration':'underline','font-weight':'bolder','color':'#FFF4B7'});
	
		}, function(){			 
			//item.getParent('li').getParent('ul').getParent('div').getFirst('li').getFirst('a').addClass('current');
		   // console.log(item.getParent('li').getParents('li, ul').getFirst('a'));
		});
	});
}

function getHash() {
	var hash = window.location.hash;
	hash = hash.substring(1); 
	if(hash.length > 0){
		return hash;
	}else{
		return false;
	}
}

//the following is from http://snipplr.com/view.php?codeview&id=3141
String.prototype.replaceAll = function(pcFrom, pcTo){
	var i = this.indexOf(pcFrom);
	var c = this;
	
	while (i > -1){
		c = c.replace(pcFrom, pcTo); 
		i = c.indexOf(pcFrom);
	}
	return c;
}

