function presentContent(div_num) {
	function wrapped(){		
		if(div_num < fade_in_divs.length){
			setTimeout(function(){$(fade_in_divs[div_num][0]).fadeIn('slow', presentContent(div_num + 1))}, fade_in_divs[div_num][1]);
		} else {
			if (firstrun) {
				replaceLinks();
				firstrun = false;
		}}
	}
	return wrapped;
}

fade_in_divs = new Array(["#head", 50], ["#slogan", 1000], ["#hello_txt", 4000], ["#footer", 0], ["#here_you_are", 100]);

arrow_positions = new Array();
arrow_positions["index.html_ajax"] = 0;
arrow_positions["about.html_ajax"] = 55;
arrow_positions["work.html_ajax"] = 125;
arrow_positions["contact.html_ajax"] = 185;

/* When lectures added these are the coordinates:
arrow_positions["lectures.html_ajax"] = 125;
arrow_positions["work.html_ajax"] = 185;
arrow_positions["contact.html_ajax"] = 240;
*/



cites = ['<table align="center" border="0" style="border-collapse:collapse;"><tr><td><span class="large">&ldquo;</span></td><td><span class="large"><span class="red">Simplicity</span> is the ultimate sophistication</span></td><td><span class="large">&rdquo;</span></td></tr><tr><td></td><td style="text-align:right; font-variant:small-caps;">Leonardo da Vinci</td><td></td></tr></table>',
'<table align="center" border="0" style="border-collapse:collapse;"><tr><td><span class="large">&ldquo;</span></td><td><span class="large">form ever <span class="red">follows</span> function</span></td><td><span class="large">&rdquo;</span></td></tr><tr><td></td><td style="text-align:right; font-variant:small-caps;">Louis Sullivan</td><td></td></tr></table>'];


function shiftCurrentPage(s){
	document.getElementById("index.html_ajax").className="";
	document.getElementById("about.html_ajax").className="";
//	document.getElementById("lectures.html_ajax").className="";
	document.getElementById("work.html_ajax").className="";
	document.getElementById("contact.html_ajax").className="";
	document.getElementById(s).className="current";
	$("#here_you_are").animate({marginLeft:arrow_positions[s]},500);
}

function replaceContent(s){
	shiftCurrentPage(s);
	$("#content").fadeOut('fast', function(){$("#content").load(s, function(){
		$("#content").fadeIn('slow', replaceLinks);
		pageTracker._trackPageview(s);
	})})
}

function replaceLinks(){
	$("a").each(function(){
		if((!this.rel) && (this.href.indexOf("#")==-1)){
			// this.href = 'javascript:replaceContent("'+this.href + "_ajax"+'");';
			var parts = this.href.split("/");
			this.href = "#" + parts[parts.length-1]+"_ajax";
		}
	});
	
	//enable scoller
    $("div.scrollable").scrollable({size: 2});
    //enable lightboxed items
    $('a[rel=lightbox]').lightBox();
}

// on anchor change call new page with animation
function anchorChange() {
    if (old_anchor!=location.hash) {
    	window.clearInterval(sloganChangeTimer);
    	if(location.hash==""){
			location.reload();
	   	}else{
	        old_anchor = location.hash;
	        replaceContent(location.hash.substring(1, location.hash.length));
	        if(location.hash=="#index.html_ajax"){
				fade_in_divs = new Array(["#slogan", 0], ["#hello_txt", 0]);
				window.setTimeout("presentContent(0)()", 1000);
				sloganChangeTimer = window.setInterval("changeSlogan()", 15000);
				citesPosition = 0;
			};
       	}
    }
}

function replaceSloganContent(){
	if(citesPosition<cites.length - 1){
		citesPosition +=1;
	} else {
		citesPosition=0;
	}
	var temp_slogan = document.getElementById("slogan");
	if (temp_slogan != null) {
			document.getElementById("slogan").innerHTML = cites[citesPosition];
			$("#slogan").fadeIn(2000);
		};
}

function changeSlogan(){
	var sloganElement = document.getElementById("slogan");
	if(sloganElement!=undefined){
		$("#slogan").fadeOut(2000, replaceSloganContent);
	}
}

citesPosition = 0;

isIE6 = /msie|MSIE 6/.test(navigator.userAgent);


//  Add the javascript enabled css to start 
  var newSS=document.createElement('link');
  newSS.rel='stylesheet';
  newSS.href='./css/enable_script.css';
  newSS.type='text/css';
  document.getElementsByTagName("head")[0].appendChild(newSS);




window.onload = function(){
		
		if(!isIE6){
			sloganChangeTimer = window.setInterval("changeSlogan()", 15000);
			
			$("#loader").ajaxStart(
				function(){
					this.style.display = "block";
				});
			$("#loader").ajaxStop(
				function(){
					this.style.display = "none";
				});
			firstrun = true;
			if(location.hash=="#index.html_ajax"){
				location.hash="";
			}
			if(location.hash){
				replaceContent(location.hash.substring(1, location.hash.length));
				fade_in_divs = new Array(["#head", 0], ["#footer", 0], ["#here_you_are", 0]);
			}
			presentContent(0)();
			old_anchor = location.hash;
			
			//prefatch images
			
			     // set image list
			     var images = [ "./css/images/nav_active.png",
			     				"./css/images/here_you_are_arrow.png",
			     				"./css/images/arrow_right.png",
			     				"./css/images/arrow_left.png",
			     				"./images/portrait.png",
			     				"./images/old_phone.png",
			     				"./images/webdesigndevelopment.png",
			     				"./images/work/celodoro.jpg",
								"./images/work/soultea.jpg",
			     				"./images/work/polaris.png",
			     				"./images/work/botgard.jpg",
			     				"./images/work/ja.png",
			     				".images/work/olmarmusic.jpg"
			     				];
			     var cached_images = new Array();
			     // start preloading
			     var i;
			     var current_Image = new Image();
			     for(i=0; i<=images.length-1; i++)
			     {
			     	  current_Image.src = images[i];
			          cached_images.push(current_Image);
			     }

			window.setInterval('anchorChange()', 200);
		}
	};
