$(document).ready(function(){

	/*A little hand holding for IE
	************************************/
	if(!Array.indexOf){
		Array.prototype.indexOf = function(obj){
			for(var i=0; i<this.length; i++){
				if(this[i]==obj){
					return i;
				}
			}
			return -1;
		}
	}

	/*Remove all unwanted elements
	************************************/
	$('h2').addClass("hidden");
	$('a.goToTop').addClass("hidden");
	
	/*Useful variables
	************************************/
	var header=document.getElementById('header');
	var theNavs=header.getElementsByTagName('ul');
	var expanderArray=new Array();
	var linkArray=new Array();
	var is_open="home";
	
	/*Expander object
	************************************/
	var Expander=function(theDiv){
		this.theDiv=theDiv;
		this.reveal=function(){
			is_open=this;
			$("#"+theDiv).stop().slideDown(400);
		}
		this.close=function(that){
			$("body").addClass(that.theDiv);
			
			if((that.theDiv=="people")||(that.theDiv=="nature")||(that.theDiv=="abstract")||(that.theDiv=="other")){
				$("body").addClass("home");
			}else{
				$("body").removeClass("home");
			}
			
			
			function doReveal(){
				that.reveal();
			}
			$("body").removeClass(theDiv);
			$("#"+theDiv).stop().slideUp(400, function () { if(that!="dontyoudaredoanything"){
				var timer=setTimeout(function(){doReveal()}, 250);
			} });
			
		}
	}
	
	/* Fill link array with all the links.
	** Instantiate expander objects which expand the divs to which the links, err, link.
	** Fill expander array with the objects.
	** These two arrays are the same size as each other so a link can be related to an expander object by its position in the array
	************************************/
	for(var i=0; i<theNavs.length; i++){
		var theAnchors=theNavs[i].getElementsByTagName('a');
		for(var j=0;j<theAnchors.length;j++){
			if((theAnchors[j].getAttribute('href')).substring((theAnchors[j].getAttribute('href')).indexOf('#'))!="#top"){
				linkArray.push(theAnchors[j]);
				var theHref=theAnchors[j].getAttribute('href');
				$(theHref.substring(theHref.indexOf('#'))).addClass("hidden");
				expanderArray.push(new Expander(theHref.substring(theHref.indexOf('#')+1)));
			}
		}
	};
	
	/* onClick events to start revealing the divs
	************************************/
	for(var k=0;k<linkArray.length;k++){
		linkArray[k].onclick=function(){
			var expandThis=expanderArray[linkArray.indexOf(this)];
			if(is_open=="home"){
				$("body").addClass(expandThis.theDiv);
				if((expandThis.theDiv!="people")&&(expandThis.theDiv!="nature")&&(expandThis.theDiv!="abstract")&&(expandThis.theDiv!="other")){
					$("body").removeClass("home");
				}
				
				expandThis.reveal();				
			}else{
				if(is_open==expandThis){
					return false;
				}else{
					is_open.close(expandThis);
					
				}
			}
			return false;
		}
	};
	
	$('#homeLink').click(function(){
		if(is_open=="home"){
			return false;
		}else{
			is_open.close("dontyoudaredoanything");
			$("body").addClass("home");
			is_open="home";
			return false;
		}
	})

});
