var currentBg	= 0;
var phighlights = new Array('#929292','#00A0FB','#FB9B00','#FC136C','#8B00FB');
var urlName		= new Array(null,'welkom','profiel','werkzaamheden','contact');
var titles		= new Array(null,'Welkom','Profiel','Werkzaamheden','Contact');
var pageTypes	= new Array(null, 0		 , 0	   , 0	           , 0	     );
var page		= 0;
var currentP	= 1;
var Qlocation	= "";
var setHTML		= false;
var spinning	= false;
var scroller;
var is_chrome	= navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
var magicStory;

window.addEvent('domready', function() {

	// check for no JS version
	Qlocation	= ""+window.location;
	Qlocation	= Qlocation.split('#');

	if(Qlocation.length < 2){
		window.location = '/#'+window.location.href.replace(/http\:\/\/jessedegger\.nl/gi,'').replace(/http\:\/\/www\.jessedegger\.nl/gi,'');
	}

	// prepare every background for fading
	for(var i = 0; i < 7; i++){
		changeBackground(i);
	}

	changeBackground(0);

	// preload all pages
	for(var i = 1; i < urlName.length; i++){
		//$('pageTemp_'+i).load('pages/'+i+'.html');
		new Request.JSON({url: 'request.php?p='+i, onSuccess: updateHTMLDivs}).send();

	}
	// build menu items
	$$('.item').each(function(object){
		
		// add onclick event
		object.addEvent('click',openPage);

	});

	// delete SEO url's
	$$('.item a').each(function(object){
		
		// add onclick event
		object.href = '#'+object.href.replace(/http\:\/\/jessedegger\.nl/gi,'').replace(/http\:\/\/www\.jessedegger\.nl/gi,'');

	});

	// make very highlight from SEO to span
	$('header').innerHTML  ='<h1><span class="highlight">J</span>esse <span class="highlight">D</span>egger</h1>';
	$('a-href-1').innerHTML='<span class="highlight">Wel</span>kom';
	$('a-href-2').innerHTML='<span class="highlight">Pro</span>fiel';
	$('a-href-3').innerHTML='<span class="highlight">Werk</span>zaamheden';
	$('a-href-4').innerHTML='<span class="highlight">Con</span>tact';

	// show scroller
	$$('.scrollarea').setStyle('display','inline');

	// set right container dimensions
	if($$('.realcontent')){
		$$('.realcontent').setStyle('width','430px');
		$$('.realcontent').setStyle('height','255px');
		$$('.realcontent').setStyle('overflow-x','visible');
	}

	scroller = new MooScroller($E('div.about'), $E('.scrollHandle'), {
		scrollSteps: 20,

		scrollLinks: {
			forward: $E('div.scrollForward'),
			back: $E('div.scrollBack')
		}
	});

	// make lightbox
	new Lightbox($$('.photo-image a'));
	
	// check for magic stories
	if($('magic_story_end')){
		$('magic_story_end').fade('out');
	}

	seekToRequestPage();
	
});


updateHTMLDivs = function(JSON){
	$('pageTemp_'+JSON.p).set('html',JSON.html);

	page = JSON.p;

	if(setHTML == page){
		changeBackground(page);

		if(!spinning){

			spinning = true;

			if(page != currentP){
				var fadeAwayText = new Fx.Tween('scrollerMain', {property: 'opacity'});
				fadeAwayText.start(1,0).chain(
					function(){
						$('scrollerMain').set('html',JSON.html);

						// set right container dimensions
						if($$('.realcontent')){
							$$('.realcontent').setStyle('width','430px');
							$$('.realcontent').setStyle('height','255px');
							$$('.realcontent').setStyle('overflow-x','visible');
						}

						// make lightbox
						new Lightbox($$('.photo-image a'));

						// set title
						document.title = 'Jesse Degger - '+titles[setHTML];
						favicon.change('/images/icons/fav_'+setHTML+'.ico'); if(is_chrome) { window.location = window.location; }

						$$('.highlight').tween('color', phighlights[setHTML]);

						currentP = page;
						
						// check special page
						//alert('SLOW LOAD: '+JSON.p);
						if(pageTypes[JSON.p] == 0){
							$$('.scrollarea').fade('in');
							scroller.update(); $('scrollhandle').setStyle('top','0px'); $('scrollerMain').scrollTo(0,0);
						}else{
							$$('.scrollarea').fade('out');
							makeMagicStory.delay(500);;
						}
						
						pageTracker._trackPageview("/"+setHTML+"/"+urlName[setHTML]+".html");
						this.start(0,1);

					},
					function(){ spinning = false; }
				);
			}else{
				spinning = false;
				changeBackground(page);
				$$('.highlight').tween('color', phighlights[setHTML]);
			}

		}else{
			updateHTMLDivs(JSON);
		}
	}
}

seekToRequestPage = function(){
	Qlocation	= ""+window.location;
	Qlocation	= Qlocation.split('#');

	if(Qlocation[1]){
		var pageQ 	= Qlocation[1].split('/');

		if(pageQ[1]){
			var rPage	= pageQ[1];

			if($('pageTemp_'+rPage)){

				if(! $('pageTemp_'+rPage).get('html') == ""){

					if(currentP != rPage){
						var fadeAwayText = new Fx.Tween('scrollerMain', {property: 'opacity'});
						fadeAwayText.start(1,0).chain(
							function(){

								$('scrollerMain').set('html', contentHTML);

								// set right container dimensions
								if($$('.realcontent')){
									$$('.realcontent').setStyle('width','430px');
									$$('.realcontent').setStyle('height','255px');
									$$('.realcontent').setStyle('overflow-x','visible');
								}

								// make lightbox
								new Lightbox($$('.photo-image a'));

								// set title
								document.title = 'Jesse Degger - '+titles[rPage];
								favicon.change('/images/icons/fav_'+rPage+'.ico'); if(is_chrome) { window.location = window.location; }


								$$('.highlight').tween('color', phighlights[rPage]);

								currentP = rPage;
							
								// check special page
								//alert(currentP +" ("+pageTypes[currentP]+")");
								if(pageTypes[currentP] != 1){
									scroller.update(); $('scrollhandle').setStyle('top','0px'); $('scrollerMain').scrollTo(0,0);
									$$('.scrollarea').fade('in');
									//alert(true);
								}else{
									$$('.scrollarea').fade('out');
									makeMagicStory.delay(500);;
								}
						
								pageTracker._trackPageview("/"+rPage+"/"+urlName[rPage]+".html");
								this.start(0,1);

							}
						);
					}else{
						changeBackground(rPage);
					}
				}else{
					setHTML = rPage;
				}
			}else{
				openPageByID(1);
			}
		}else{
			openPageByID(1);
		}
	}else{
		openPageByID(1);	
	}
}

openPageByID = function(id){
	ppage = id;

	if(ppage != currentP){

		if(! $('pageTemp_'+ppage).get('html') == ""){
			window.location = "#/"+ppage+"/"+urlName[ppage]+".html";
	
			changeBackground(ppage);

			if(ppage != currentP){
				var fadeAwayText = new Fx.Tween('scrollerMain', {property: 'opacity'});
				fadeAwayText.start(1,0).chain(
					function(){
						$('scrollerMain').set('html',$('pageTemp_'+ppage).get('html'));

						// set right container dimensions
						if($$('.realcontent')){
							$$('.realcontent').setStyle('width','430px');
							$$('.realcontent').setStyle('height','255px');
							$$('.realcontent').setStyle('overflow-x','visible');
						}

						// make lightbox
						new Lightbox($$('.photo-image a'));

						// set title
						document.title = 'Jesse Degger - '+titles[ppage];
						favicon.change('/images/icons/fav_'+ppage+'.ico'); if(is_chrome) { window.location = window.location; }

						$$('.highlight').tween('color', phighlights[ppage]);

						currentP = ppage;
						
						// check special page
						//alert(currentP +" ("+pageTypes[currentP]+")");
						if(pageTypes[currentP] != 1){
							scroller.update(); $('scrollhandle').setStyle('top','0px'); $('scrollerMain').scrollTo(0,0);
							$$('.scrollarea').fade('in');
							//alert(true);
						}else{
							$$('.scrollarea').fade('out');
							makeMagicStory.delay(500);;
						}
						
						pageTracker._trackPageview("/"+ppage+"/"+urlName[ppage]+".html");
						this.start(0,1);
					}
				);
			}
		}else{
			setTimeout("openPageByID('"+id+"');",100);
		}	

	}
}

openPage = function(){
	page = (this.id.replace(/href-/gi,''));

	if(page != currentP){

		changeBackground(page);
		if(!spinning){

			window.location = "#/"+page+"/"+urlName[page]+".html";

			spinning = true;

			var fadeAwayText = new Fx.Tween('scrollerMain', {property: 'opacity'});
			fadeAwayText.start(1,0).chain(
				function(){
					$('scrollerMain').set('html',$('pageTemp_'+page).get('html'));

					// set right container dimensions
					if($$('.realcontent')){
						$$('.realcontent').setStyle('width','430px');
						$$('.realcontent').setStyle('height','255px');
						$$('.realcontent').setStyle('overflow-x','visible');
					}

					// make lightbox
					new Lightbox($$('.photo-image a'));

					// set title
					document.title = 'Jesse Degger - '+titles[page];
					favicon.change('/images/icons/fav_'+page+'.ico'); if(is_chrome) { window.location = window.location; }

					$$('.highlight').tween('color', phighlights[page]);

					currentP = page;
					
					// check special page
					//alert(currentP +" ("+pageTypes[currentP]+")");
					if(pageTypes[currentP] != 1){
						scroller.update(); $('scrollhandle').setStyle('top','0px'); $('scrollerMain').scrollTo(0,0);
						$$('.scrollarea').fade('in');
						//alert(true);
					}else{
						$$('.scrollarea').fade('out');
						makeMagicStory.delay(500);
					}
						
					pageTracker._trackPageview("/"+page+"/"+urlName[page]+".html");
					this.start(0,1);
				},
				function(){ spinning = false; }
			);
		}
	}
}


changeBackground = function(what){
	if(what == 0){
		what = 1;
	}

	// fade
	$('background_'+currentBg).fade('out');
	$('background_'+what).fade('in');

	// highlights
	$$('.highlight').tween('color', phighlights[what]);

	// save new background
	currentBg = what;

}

moveElementUp = function(selector,steps){
	var newHeight	= $$(selector).getStyle('top');
	newHeight 		= newHeight[1].toInt();
	
	var storyHeight = $$(selector).getStyle('height');
	storyHeight		= storyHeight[1].toInt();
	
	newHeight  -= steps;
	
	if(storyHeight <= (newHeight *-1)){
		$clear(magicStory);
		
		openPageByID(1);
	}
	
	$$(selector).setStyle('top', newHeight);
}

makeMagicStory = function(){
	magicStory = moveElementUp.periodical(50, null, new Array('.special_story_content', 1));
}

