/* Custom Script for Rawkitchen 
Copyright 2010 Kamph.com•mu•ni•ca•tion
*/

var x;
function preload()
{
	p	= new Array();
	x	= 0;

 	for(i=0;i<preloadimages.length;i++)
	{
		p[i]		= new Image();
		p[i].onabort 	= update;
		p[i].onerror 	= update;
		p[i].onload  	= update;
		p[i].src	= preloadimages[i];
	}
}

function update()
{
	x++;
	pro=Math.floor((x/preloadimages.length)*100);
	$("#loading").html("loading "+pro+" %");
	//$("#loading").progressbar({ value: pro });
	
	if(pro==100) {
		var pageloaded = true;
		$("#loading").hide();
		$('#buehnebg').fadeIn(2000);
		window.setTimeout("$('#leftnavi').fadeIn(1200);",2000);
		window.setTimeout("checkAnchor();",2000);
	}
}









var lastmain = '';
var lastsub = '';


function startanimation(page) {

	var anchor = self.document.location.hash.substring(1);
	if(anchor=="") var startanimation = true;


	if(loadperajax && startanimation) {

//	$('#buehnebg').queue(function() {
		preload();
//	  $(this).dequeue();
//	});
		
	} else {
		$('#buehnebg').css({ display: "block" });
		$('#leftnavi').css({ display: "block" });
				$("#loading").hide();
				checkAnchor(page);
		//preload();
		$('#headline').html('<img src="'+base+'img/headl_'+page+'.png" alt="login" />');
	}

}



function Capital(value) {
	if (value != "") {
		var firstLetter = value.substring(0, 1).toUpperCase();
	    var restOfWord = value.substring(1, value.length).toLowerCase();
	    value = firstLetter + restOfWord;
	}
		return value;
}


function checkAnchor(page) {
	
	// double check if page-request is /page or #page and deliver!
	if(page!='' && page!='undefined' && typeof(page)!='undefined') {
	var anchor = page;
	} else {
	var anchor = self.document.location.hash.substring(1);
	}	
	
	if(anchor=='') anchor = 'news';
	
	var anchorsplit = anchor.split("-");
	var main = anchorsplit[0];
	var sub = anchorsplit[1];

	self.document.location.hash = anchor;
	
	// document title
	var titletag1 = '.:: Raw Kitchen - '+Capital(main);
	var titletag2 = '';
		if(typeof(sub)!='undefined') titletag2 = ' - '+Capital(sub);
	var titletag3 = ' ::.';
	document.title = titletag1+titletag2+titletag3;


		if(anchor=='news')     	 		{ setContent('news',''); }
		if(anchor=='support')  		 	{ setContent('support',''); }
		if(anchor=='equipment')  		{ setContent('equipment',''); }
		if(anchor=='equipment-set01')  	{ setContent('equipment','set01'); }
		if(anchor=='equipment-set02')  	{ setContent('equipment','set02'); }
		if(anchor=='equipment-set03')  	{ setContent('equipment','set03'); }
		if(anchor=='equipment-kamera')  { setContent('equipment','kamera'); }

		if(anchor=='links')     		{ setContent('links',''); }
		if(anchor=='referenzen') 		{ setContent('referenzen',''); }
		if(anchor=='referenzen-showreel') { setContent('referenzen','showreel'); }

		if(anchor=='kontakt')    		{ setContent('kontakt',''); }
		if(anchor=='kontakt-email')		{ setContent('kontakt','email'); }
		if(anchor=='kontakt-agb')    	{ setContent('kontakt','agb'); }
		if(anchor=='kontakt-impressum') { setContent('kontakt','impressum'); }

		if(anchor=='login')      		{ setContent('login',''); }

}



function headline(main) {

	$('#headline').fadeOut(300);
	$('#headline').queue(function() {
	  $('#headline').html('<img src="'+base+'img/headl_'+main+'.png" alt="headl_NEWS" />');
	  $('#headline').fadeIn(300);
	  $(this).dequeue();
	});

}
	
	
function SetIllustation(sub) {

	if(typeof(sub)=='undefined' || sub=='') {
	$('#equipment_icons').html('');
	} else {
		 $('#equipment_icons').html('<img src="'+base+'img/illus/'+sub+'.png" alt="" />');
	}
}
	
		
		



function setContent(main,sub) {

	$.ajaxSetup({ cache: false	});

	$(".active").removeClass("active");
	$('#navi_'+main).addClass('active');
	$('#subnavi_'+sub).addClass('active');
	SetIllustation();
	

	if(loadperajax) {
		//jan: news neu mit slider
		if(main=='news') {
			// build headline if directly linked to showreel from URL
			headline(main);
			// hide showreel
			$("#imageFlow").fadeOut(200);

			$("#scroller_wrap").fadeOut( 200 );

			$("#newsSlideshow").fadeOut(0, function() {
		
			$("#newsSlideshow").css({ top: "300px" } );
			$("#newsSlideshow").fadeIn( 400 );			
			});			

		
		}else if(sub=='showreel') {
			// build headline if directly linked to showreel from URL
			if(lastmain=='') headline(main);

			$("#scroller_wrap").fadeOut( 200 );

			$("#imageFlow").fadeOut(0, function() {
		
			$("#imageFlow").css({ top: "300px" } );
			$("#imageFlow").fadeIn( 400 );			
			});			

		
		} else if((lastmain==main && lastmain!='referenzen' && sub!='')) {

		// load subcontent
		$.get(base+'ajax/'+main+'-'+sub+'.html', function(data) {
		  $('#ajaxload').html(data);
		  // recalculate scrollPance
		  $('#scrolltext').jScrollPane();
			// sets illustrations
			if(main=='equipment' && sub!='') SetIllustation(sub); 
		});

		
		} else  {

		// replace headline on all but not from showreel to references
		if(lastsub!='showreel' || lastmain!=main) headline(main,200);


		// hide scroller_wrap
		$('#scroller_wrap').fadeOut(300);
		$('#scroller_wrap').queue(function() {
		
			// hide showreel
			$("#imageFlow").fadeOut(200);
			//jan
			$("#newsSlideshow").fadeOut(200);
	
			// hide subnavi
			$('#subnavi_kontakt').hide();
			$('#subnavi_equipment').hide();
			$('#subnavi_referenzen').hide();

		
			// load subfile when directly linked from URL (NO DUPLICATE!)		
			var ajaxfile = base+'ajax/'+main+'.html';
			if(sub!='') ajaxfile = base+'ajax/'+main+'-'+sub+'.html';
			//if(main=='news') ajaxfile = base+'ajax/news.php';

			// replace hidden content
			$.get(ajaxfile, function(data) {
			  $('#ajaxload').html(data);
			  // recalculate scrollPance
			  window.setTimeout("$('#scrolltext').jScrollPane();",50);

			  // animate scroller_wrap to show up back on page
			  $("#scroller_wrap").fadeIn(300);


			});
			
			

		
		
	
			// unhide applicable subnavi while animation scroller_wrap is in progress
			if(main=='kontakt') { $('#subnavi_kontakt').show(); }
			if(main=='referenzen') { $('#subnavi_referenzen').show(); }	
			if(main=='equipment') { $('#subnavi_equipment').show(); SetIllustation(sub); }	
		
			$(this).dequeue();
		});
			
			
			
			
		}	
	
		// set last vars to check animation conditions above
		// will be set on first animation an is empty by default
		lastmain = main;
		lastsub = sub;
	}
}






