/*
 * copyright 2011 Albino bvba - http://albino.be
 *
 */

$.frk = { 
   	location : {host : 'http://frieke.com', path : '', hashbang : '/#!', hash : '', hashparts : '', previous : '', current : '/', target : '' },
   	state : {previous: 'blanco', current: 'blanco', target: ''},
   	projects: false,
   	images: {url: false},
   	contact : false,
   	news : false,
   	extra : { 
   				shortcutproject : '<a href="/projects/" id="shortcut" class="show-projects">just show me all frieke\'s projects</a>', 
   				shortcutsearch : '<a href="/" id="shortcut" class="show-search">just show me the searchbar</a>', 
   				imgid : false,
   				direction : 'next',
   				wrapperWidth : '',
   				wrapperHeight: '',
   				videoWidth   : '',
   				videoHeight  : '',
   				projectimages : '',
   				stfActive: false
   			},
   	albino : ''		
};


/*
 * document ready... and go!
 *
 */

$(function(){
	$.frk.albino = $('#albino');
	hashParser();
	resizeWrapper();	
	handleSearchForm();
	handleDirection();
	handleMtf();
	initImagePanner();
	bindSlideshowKeys();
	$('a:not(.ui-corner-all, .nohash, [target=_blank])').live('click', function(e){
		e.preventDefault();
		hashSetter($(this).attr('href'));
	});
			
	$(window).bind('resize', function(){ resizeWrapper(); });
});

/* * * * * END document ready... and go! * * * * */



/*
 * set of helper functions
 *
 */
  
function hashSetter($hs){
	$hs = $hs.replace($.frk.location.host, "");
	_gaq.push(['_trackPageview', $hs]);
	window.location = $.frk.location.host+$.frk.location.hashbang+$hs;
}

function hashRedirect($hs){
	window.location.replace($.frk.location.host+$.frk.location.hashbang+$hs);
}

function stateSetter(){
	$('#aToolTip').remove();
	$('#imagepanTip').remove();
	$.frk.state.previous = $.frk.state.current;
	$.frk.state.current = $.frk.state.target;
	titleSetter();
}

function titleSetter(){
	var len = $.frk.location.hashparts.length;
    var newTitle = '';
    for (var i = 0; i < len; i++){
    	if($.isNumeric($.frk.location.hashparts[i]) == false){
    		if(i == 0){
    			newTitle += $.frk.location.hashparts[i];
    		}
    		else {
    			newTitle += ' - '+$.frk.location.hashparts[i].replace(/-/g," ");
    		}
    	}
    }
	if(newTitle.length > 0)	{
		document.title = newTitle+' | frieke';
	}
	else {
		document.title = 'frieke';
	}
}

function handleMtf(){
	$('a#mtf-btn').live('click', function(e){
		e.preventDefault();
		$tmptxt = 'check this out: \n'+$(this).attr('href');
		$('#your').val('');
		$('#friend').val('');
		$('#message').html($tmptxt);
		$('#senderror').hide();
		$('#sendsucces').hide();
		$('#friendbtn').show();
		$("#stfform").show();	
		$.frk.extra.stfActive = true;
		$('#stfbox').animate({'top' : '0px'}, 450 );
	});
	
	$('#friendbtn').click( function(e){ 
		e.preventDefault();
		$tmpy = $('#your').val();
		$tmpf = $('#friend').val();
		$tmpm = $('#message').val();
		if(checkForm($tmpy,$tmpf,$tmpm)){ $(this).hide(); $('#senderror').hide(); sendMail(); }
		else { $('#senderror').show(); }
	});
	
	function checkForm($tmpy,$tmpf,$tmpm) {
		$err = false;
		if($tmpy != '' && $tmpf != '' && $tmpm != '' && isValidEmailAddress($tmpy) && isValidEmailAddress($tmpf)){ return true; }
		else{ return false };
		
		function isValidEmailAddress(emailAddress) {
		var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
		return pattern.test(emailAddress);
		}
	}
		
	function sendMail() {
		$stform = $("#stfform");
		$.ajax({type: 'POST', url:$.frk.location.host+'/projects/sendtofriend/', data: $stform.serialize(), dataType: 'json', success:function(data) {							
				if(data.transaction){
				 	$stform.hide(); $('#your').val(''); $('#friend').val(''); $('#message').html(''); $('#sendsucces').show();
					$('#stfbox').delay(1500).animate({'top' : '-500px'}, 350 );	
				}
				else {
					$('#senderror').fadeTo(1,100);
					$('#friendbtn').show();					
				}
			}
		});
	}	
	
	$('a#closestfbox').live('click', function(e){
		e.preventDefault();
		$('#stfbox').animate({'top' : '-500px'}, 350 );
		$.frk.extra.stfActive = false;
	});

}

function handleSearchForm(){
	
	$( "#searchfield" ).autocomplete({
		source: $jsontaglist,
		minLength: 2					
	});
	
	$('#searchbtn').live('click', function(e){
		e.preventDefault();
		performSearch();
	});
	
	$('#smallsearchbtn').live('click', function(e){
		e.preventDefault();
		$('#asearchbox').hide();
		performSearch();	
	});
	
	function performSearch() {
		$.ajax({type: 'POST', url:$.frk.location.host+'/images/', data: $("#searchform").serialize(), dataType: 'json', success:function(data) {			
					$.frk.images = data;
					$('#searchfield').val('');
					$("#asearchbox input[type='checkbox']").attr('checked', false);
					hashSetter('/images/'+$.frk.images.url+'/');
					$('#asearchbox').removeAttr("style");
				}
			});
	}
}

function handleDirection(){
	$('#next a').live('click', function(e){	$.frk.extra.direction = 'next'; });
	$('#prev a').live('click', function(e){	$.frk.extra.direction = 'prev'; });
}

function urlToForm($tags,$cats){
	$tags = $tags.replace(/-/g,' ');
	$cats = ($cats != false) ? $cats.split('-') : false;
	$('#searchfield').val($tags);
	$checkboxes = $("#asearchbox input[type='checkbox']");
	$checkboxes.attr('checked', false);
	if($cats != false){
		$.each($checkboxes, function() {
			$label = $(this).next('label').text();	
			for (i=0;i<$cats.length;i++)
				if($label.indexOf($cats[i]) >= 0)
					$(this).attr('checked', true);
	    });
	}
}

function initImagePanner() {
	
	$('#imagePanTrigger').live('click', function() {  		
  		if( $('#loader').is(":visible") ) {
		}
		else {
	  		$imageid = $(this).attr('rel');
	  		$('#loader').show();
	  		$('body').prepend('<div id="imagePan"><div><img src="/assets/uploads/large/'+$imageid+'.jpg" alt=""/></div></div>');
			var $imagePan = $('#imagePan');
	   		$imagePan.imagesLoaded( function() {
	        	$imagePan.imagepan();
	        	$('#loader').hide();
			});
			$('#imagePan').aToolTip({
				tipContent: 'zoom out',
				toolTipId: 'imagepanTip'
			});
		}
	});
			
	$('.article img').live('click', function() {
  		if( $('#loadern').is(":visible") ) {
		}
		else {
	  		$img = $(this);
	  		$imageid = $img.attr('rel');
	  		$pos = $img.position();
	  		$nx = $pos.left+($img.width()/2);  		
	  		$ny = $pos.top+($img.height()/2); 
	  		$img.parent().prepend('<div id="loadern"><div>loading..</div></div>');
	  		$('#loadern').css({'left':$nx, 'top': $ny})
	  		$('body').prepend('<div id="imagePan"><div><img src="/assets/uploads/largenews/'+$imageid+'.jpg" alt=""/></div></div>');
			var $imagePan = $('#imagePan');		
	   		$imagePan.imagesLoaded( function() {
	        	$imagePan.imagepan();
	        	$('#loadern').remove();
			});
			$('#imagePan').aToolTip({
				tipContent: 'zoom out',
				toolTipId: 'imagepanTip'
			});
		}	
	});
	
}


function resizeWrapper() {
	var galleryImgWidth = 252; /* thumb with margin of 1px */
	var wrapperWidth = (Math.floor(($(window).width()-40)/galleryImgWidth)*galleryImgWidth);
	var wrapperHeight = Math.floor(($(window).height())-96); 
	if(wrapperWidth <756) wrapperWidth = 756;
	if(wrapperHeight <500) wrapperHeight = 500;
	$.frk.extra.wrapperWidth = wrapperWidth-40;
	$.frk.extra.wrapperHeight = wrapperHeight-40;
	$('#imagePanTrigger').css('max-width',wrapperWidth-40);	
	$('#imagePanTrigger').css('max-height',wrapperHeight-40);
	$('#wrapper').css('width',wrapperWidth);
	$.frk.extra.projectimages = 350;
	if(wrapperWidth == 756) $.frk.extra.projectimages = 330;
	$projectimages = $('#projectimages');
	if($projectimages.length != 0){
		$($projectimages).css('padding-left',$.frk.extra.projectimages);
	}
	$videoFrame = $('#videoframe');
	if($videoFrame.length != 0){
		$.frk.extra.videoWidth = 720;
		$.frk.extra.videoHeight = 405;
		if($.frk.extra.wrapperWidth > 960 && $.frk.extra.wrapperHeight >540){
			$.frk.extra.videoWidth = 960;
			$.frk.extra.videoHeight = 540;		
		}
		if($.frk.extra.wrapperWidth > 960 && $.frk.extra.wrapperHeight >540){
			$.frk.extra.videoWidth = 960;
			$.frk.extra.videoHeight = 540;		
		}
		$($videoFrame).css('width',$.frk.extra.videoWidth);	
		$($videoFrame).css('height',$.frk.extra.videoHeight);						
	}
}

function processCurrent(){
	switch($.frk.state.current){
		case 'blanco': removeBlanco(); break;
		case 'home': removeHome(); break;
		case 'imagesOverview': removeImagesOverview();	break;
		case 'imagesSlideshow':removeImagesSlideshow(); break;	
		case 'projectsOverview': removeProjectsOverview(); break;
		case 'projectsDetail': removeProjectsDetail(); break;
		case 'projectsSlideshow': removeProjectsSlideshow(); break;
		case 'contact':	removeContact(); break;
		case 'news': removeNews(); break;
		default: removeAll();
	}
}


function renderProjectsDetail(){
	$project = $.frk.location.hashparts[2];	
	$key = $.inArray($project, $.frk.projects.order);
	$project_nr = $key+1;
	$total = $.frk.projects.total;
	if ($total>1) {	
			$prev_id = ($key-1>=0) ? $key-1 : $total-1; 			
			$next_id = ($key+1<=$total-1) ? $key+1 : 0;
			$prev = $.frk.projects.order[$prev_id];
			$next = $.frk.projects.order[$next_id]; 
	}
	else {
		$next = false; $prev = false;
	}
				
	$data = $.frk.projects.data[$project];
	
	$data.next = $next;
	$data.prev = $prev;
	$data.share = $.frk.location.host;
	
	$data.next_link = $.frk.projects.data[$next].project_link;
	$data.prev_link = $.frk.projects.data[$prev].project_link;
	
	$render = $('#tmpl-projectdetail').tmpl($data);
	return $render;		
}

function bindSlideshowKeys(){
	$(document).unbind('keyup').bind('keyup', function(e){
	    switch(e.keyCode) {
			case 37: $('#prev a').trigger('click'); break;
			case 39: $('#next a').trigger('click'); break;
			case 13: if($.frk.state.current == 'home' || $.frk.state.current == 'imagesOverview'){$('#searchbtn').trigger('click');} break;
		}
	});
}

function renderImagesSlideshow(){
	$img_id = $.frk.extra.imgid;
	$search_link = $.frk.images.url;
	$imagedata = $.frk.images.data[$img_id];
	$image = $img_id;
	$key = $.inArray($image, $.frk.images.order);
	$image_nr = $key+1;
	$total = $.frk.images.total;
	if ($total>1) {	
			$prev_id = ($key-1>=0) ? $key-1 : $total-1; 			
			$next_id = ($key+1<=$total-1) ? $key+1 : 0;
			$prev = $.frk.images.order[$prev_id];
			$next = $.frk.images.order[$next_id]; 
	}
	else {
		$next = false;
		$prev = false;
	}
				
	$data = {
		project_title : $imagedata.project_title,
		project_link : $imagedata.project_link,
		project_id : $imagedata.project_id,
		search_link : $search_link,
		image_nr : $image_nr,
		total : $total,
		image : $image,
		next : $next,
		prev : $prev
	}
	
	$render = $('#tmpl-imageslideshow').tmpl($data);
	return $render;		
}

function renderProjectsSlideshow(){

	$project = $.frk.projects.data[$.frk.location.hashparts[2]];
	$image = $.frk.location.hashparts[4];
	$key = $.inArray($image, $project.images.data);
	$imagetype = $project.images.imagetype[$key];	
	$image_nr = $key+1;
	$total = $project.images.total;
	if ($total>1) {	
			$prev_id = ($key-1>=0) ? $key-1 : $total-1; 			
			$next_id = ($key+1<=$total-1) ? $key+1 : 0;
			$prev = $project.images.data[$prev_id];
			$next = $project.images.data[$next_id]; 
	}
	else {
		$next = false;
		$prev = false;
	}
	
	if($imagetype == 2) $image = $project.images.videoid[$image];
				
	$data = {
		share : $.frk.location.host,
		project_title : $project.project_title,
		project_link : $project.project_link,
		project_id : $project.project_id,
		image_nr : $image_nr,
		total : $total,
		image : $image,
		imagetype : $imagetype,
		next : $next,
		prev : $prev
	}
	
	$render = $('#tmpl-projectslideshow').tmpl($data);
	return $render;		
}

/* * * * * END set of helper functions * * * * */




/*
 * main routing
 *
 */

function hashParser() {
	
	$(window).hashchange( function(){
		$('#imagePan').remove();
		$('#aToolTip').remove();
		$('#loader').hide();
		$('#loadern').remove();
		$('#secondcontact').hide();
		if($.frk.extra.stfActive){
			$('#stfbox').animate({'top' : '-500px'}, 250 );
			$.frk.extra.stfActive = false;
		};	
		// check if hash could be valid redirect if not	
		$.frk.location.hash = location.hash.replace( /^#!/, '' );
		$.frk.location.target = $.frk.location.hash;
		$.frk.location.hashparts = $.frk.location.hash.split('/');
		if($.frk.location.hashparts.constructor != Array || $.frk.location.hashparts.length < 2){
			hashRedirect('/');
		}
		// check for pattern in hash redirect if not
		else{
			$.frk.location.previous = $.frk.location.current;
			$.frk.location.current = $.frk.location.target;
			$.frk.location.hashparts.shift();
			if($.frk.location.hashparts[$.frk.location.hashparts.length-1] == '' && $.frk.location.hashparts.length > 1) $.frk.location.hashparts.pop();
			switch($.frk.location.hashparts[0]){
				case '': 			routeHome(); 		break;
				case 'images':		routeImages(); 		break;
				case 'projects':	routeProjects(); 	break;
				case 'contact':		routeContact();		break;
				case 'news':		routeNews();		break;
				default:			hashRedirect('/');
			}
		}
	});
  	
  	// check for hash in initial url
	$(window).hashchange(); 
}

/* * * * * END main routing * * * * */


/*
 * second round of routing
 *
 */

function routeHome(){
	$.frk.state.target = 'home';
	processCurrent();
}


function routeImages(){
	var $tags=false; var $cats=false; var $img=false; var $resume=true;
	var validateImage = function(){
		if($.frk.images.url == $url && $img == false) processCurrent();
		else if($.frk.images.url == $url && $img != false && $.frk.images.data[$img] != undefined) processCurrent();
		else hashSetter('/images/'+$.frk.images.url+'/');  
	}
	switch($.frk.location.hashparts.length){
		case 1:
			$.frk.state.target = 'imagesOverview';			
			break;
		case 2:
			$.frk.state.target = 'imagesOverview';
			$tags = $.frk.location.hashparts[1];
			break;
		case 3:
			if($.frk.location.hashparts[2] != parseInt($.frk.location.hashparts[2])){
				$.frk.state.target = 'imagesOverview';
				$tags = $.frk.location.hashparts[1];
				$cats = $.frk.location.hashparts[2];
			}
			else{
				$.frk.state.target = 'imagesSlideshow';
				$tags = $.frk.location.hashparts[1];
				$img = $.frk.location.hashparts[2];
			}
		break;
		case 4:
			if($.frk.location.hashparts[3] != parseInt($.frk.location.hashparts[3])) $resume = false;
			else{
				$.frk.state.target = 'imagesSlideshow';
				$tags = $.frk.location.hashparts[1];
				$cats = $.frk.location.hashparts[2];
				$img = $.frk.location.hashparts[3];
			}
			break;	
		default: $resume = false;
	}
	$.frk.extra.imgid = $img;
	if($resume){
		if($tags ==''||$tags==false) $tags='all';
		$url = $tags;
		if($cats!=''||$cats!=false) $url=$url+'/'+$cats;
		if($.frk.images.url != $url){
			urlToForm($tags,$cats);
			$.ajax({type: 'POST', url:$.frk.location.host+'/images/', data: $("#searchform").serialize(), dataType: 'json', success:function(data) {			
					$.frk.images = data;
					$('#searchfield').val('');
					$("#asearchbox input[type='checkbox']").attr('checked', false);
					validateImage();				
				}
			});
		} else validateImage();
	} else hashRedirect('/images/');
}


function routeProjects(){
	
	var validateImage = function(){
		if($img != false)					
			if($.inArray($img, $.frk.projects.data[$project].images.data) == '-1') hashRedirect('/projects/');											
			else processCurrent();										
		else processCurrent();
	}
	
	var validateProject = function(){
		if($project != false)
			if($.frk.projects.data[$project] != undefined)
				if($.frk.projects.data[$project].images == false){
					$.ajax({url:$.frk.location.host+'/projects/'+$.frk.location.hashparts[1]+'/'+$project+'/', dataType: 'json', success:function(data){
						$.frk.projects.data[$project].images = data;				
						validateImage();
					}});
				}
				else validateImage();
			else hashRedirect('/projects/');		
		else processCurrent();
	}
	
	var $project=false; var $img=false; var $resume=true;	
	switch($.frk.location.hashparts.length){
		case 1:
		$.frk.state.target = 'projectsOverview';
		break;
		case 3:
			if($.frk.location.hashparts[2] != parseInt($.frk.location.hashparts[2]))$resume = false;
			else{
				$.frk.state.target = 'projectsDetail';
				$project = $.frk.location.hashparts[2];
			}
		break;
		case 5:
			if($.frk.location.hashparts[2] != parseInt($.frk.location.hashparts[2]) || $.frk.location.hashparts[4] != parseInt($.frk.location.hashparts[4]) ) $resume = false;
			else{
				$.frk.state.target = 'projectsSlideshow';
				$project = $.frk.location.hashparts[2];
				$img = $.frk.location.hashparts[4];
			}
		break;
		default: $resume = false;	
	}
	
	if($resume)
		if($.frk.projects == false){
			$.ajax({url:$.frk.location.host+'/projects/', dataType: 'json', success:function(data) {			
					$.frk.projects = data;
					validateProject();
				}
			});
		}
		else validateProject();
	else hashRedirect('/projects/');		       		 	
}


function routeContact(){
	
	if($.frk.location.hashparts.length > 1) hashRedirect('/');
	else{
		$.frk.state.target = 'contact';
		processCurrent();
	}
}


function routeNews(){
	
	if($.frk.location.hashparts.length > 1) hashRedirect('/');
	else{		
		$.frk.state.target = 'news';
		if($.frk.news == false) {
			$.ajax({url:$.frk.location.host+'/news/', dataType: 'json', success:function(data) {			
					$.frk.news = data;
					processCurrent();			
				}
			});
		}
		else processCurrent();	
	}
}

/* * * * * END second round of routing * * * * */




/*
 * set of animation functions
 *
 */

function removeHotline(){
	$('#hotline').animate({'margin-left': '150px'}, 350 );
}

function showHotline(){
	$('#hotline').delay(50).animate({'margin-left': '0'}, 600);
}

function showBoxes($_elements){
	var i = -1;
    var _processShowBoxes = function(){ 
		if(++i < $_elements.length){
			var speed = 150-7*i;
			if(speed <20) speed = 20;
			if(i > 24) speed = 0;
			$($_elements[i]).fadeTo(speed, 1, _processShowBoxes);	 
    	
    	}
    };
	_processShowBoxes();
}

function hideBoxes($_elements,callback){
	var i = -1;
    var _processhideBoxes = function(){ 
		if(++i < $_elements.length){
			var speed = 150-10*i;
			if(speed <10) speed = 10;
			if(i > 24) speed = 0;
			$($_elements[i]).fadeTo(speed, 0, _processhideBoxes);
		}
		else if(typeof callback == 'function') callback.call(this); 
    };
	_processhideBoxes();
}

function hideSlideshowScreen(){
	$('#slideshow').fadeTo(500, 0, function(){
		$('#slideshow').empty().hide();
		showHotline();			
		$('#contentwrap').show().animate({marginLeft: 0}, 400, 'easeOutQuad' , function(){
			$('#content').removeClass('disabled');
			stateSetter();					
		});
	});
}

/* * * * * set of animation functions * * * * */





/*
 * remove current page
 *
 */

function removeBlanco(){
	
	if($.frk.state.target=='home'){
		showHome();	
	}
	else if($.frk.state.target=='imagesOverview'){
		showHotline();
		$('#shortcut').css('top', '-200px');
		$('#hboxinner').addClass('noshadow');
		$('#hbox').fadeTo(0,0).fadeTo(200,1, function(){showImagesOverview()});
	}
	else if($.frk.state.target=='imagesSlideshow'){
		$('html').css('overflow-x', 'hidden');
	    $('#prev').animate({'margin-left': '-700px'}, 390, 'easeInQuad');
	    $('#next').animate({'margin-right': '-700px'}, 390, 'easeInQuad');
	    showImagesSlideshow();
	    $('#content').empty();
		$('#tmpl-resultimages').tmpl($.frk.images).appendTo('#content');
		$('#shortcut').css('top', '-16px');
		$('#hboxinner').addClass('noshadow');
		$('#hbox').fadeTo(0,1);	
	}
	else if($.frk.state.target=='projectsOverview'){
		showHotline();
		$('#shortcut').replaceWith($.frk.extra.shortcutsearch);
		$('#shortcut').css('top', '-200px');
		$('#hboxinner').addClass('noshadow');
		$('#hbox').fadeTo(0,1);
		$('#hboxinner').hide();
		showProjectsOverview();	
	}
	else if($.frk.state.target=='projectsDetail'){
		$('html').css('overflow-x', 'hidden');
	    $('#prev').animate({'margin-left': '-700px'}, 390, 'easeInQuad');
	    $('#next').animate({'margin-right': '-700px'}, 390, 'easeInQuad');
	    showProjectsDetail();
	    $('#content').empty();	    
		$('#tmpl-resultprojects').tmpl($.frk.projects).appendTo('#content');
	    $('#shortcut').replaceWith($.frk.extra.shortcutsearch);
		$('#shortcut').css('top', '-16px');
		$('#hboxinner').addClass('noshadow');
		$('#hbox').fadeTo(0,1);
		$('#hboxinner').hide();		
	}
	else if($.frk.state.target=='projectsSlideshow'){
		$('html').css('overflow-x', 'hidden');
	    $('#prev').animate({'margin-left': '-700px'}, 390, 'easeInQuad');
	    $('#next').animate({'margin-right': '-700px'}, 390, 'easeInQuad');
	    showProjectsSlideshow();
	    $('#content').empty();	    
		$('#tmpl-resultprojects').tmpl($.frk.projects).appendTo('#content');
	    $('#shortcut').replaceWith($.frk.extra.shortcutsearch);
		$('#shortcut').css('top', '-16px');
		$('#hboxinner').addClass('noshadow');
		$('#hbox').fadeTo(0,1);
		$('#hboxinner').hide();	
	}
	else if($.frk.state.target=='contact'){
		$('#hboxinner').slideUp(0, function(){$('#hboxinner').hide(); showContact()});	
	}
	else if($.frk.state.target=='news'){
		showHotline();
		$('#hboxinner').addClass('noshadow');
		$('#hbox').fadeTo(0,1);
		$('#hboxinner').hide();
		showNews();		
	}

}
 
 
function removeHome(){
	
	$('#shareboxhome').delay(100).animate({'margin-top': '-100'}, 400);
	
	if($.frk.state.target=='imagesOverview'){
		$('#background').fadeTo(400,0, function(){
			$(this).hide();
			$('#hboxinner').addClass('noshadow');
			$('#secondcontact').hide();
			$('#hotline').delay(50).animate({'margin-left': '0'}, 600, function(){showImagesOverview()});
		});		
	}
	else if($.frk.state.target=='projectsOverview'){
		$('#background').fadeTo(400,0, function(){
			$(this).hide();
			showHotline();
			$('#hboxinner').addClass('noshadow');
			$('#hboxinner').delay(50).slideUp(300, function(){showProjectsOverview()});
		});	
	}
	else if($.frk.state.target=='news'){
		$('#background').fadeTo(400,0, function(){
			$(this).hide();
			showHotline();
			$('#hboxinner').addClass('noshadow');
			$('#hboxinner').delay(50).slideUp(300, function(){showNews()});		
		});
	}
	else if($.frk.state.target=='contact'){
		$('#background').fadeTo(400,0, function(){
			$(this).hide();
			$('#hboxinner').addClass('noshadow');
			$('#hboxinner').delay(50).slideUp(300, function(){showContact()});		
		});
	}
	else {
	
		// TODO not a standard move
	
	}
}

function removeImagesOverview(){
	
	if($.frk.state.target=='imagesSlideshow'){
        	showImagesSlideshow();
	}
	else{
		$.frk.albino.hide();
		$_elements = $('#content .result');
		hideBoxes($_elements, function(){ 
	    	
	    	$('#content').empty();
	    	
	    	if($.frk.state.target=='imagesOverview'){
	    		showImagesOverview();	
	    	}
	    	else if($.frk.state.target=='home'){
	    		$('#hotline').animate({'margin-left': '150px'}, 350, function(){showHome();});		
	    	}
	    	else if($.frk.state.target=='projectsOverview'){	
	    		$('#hboxinner').delay(50).slideUp(300, function(){showProjectsOverview()});
	    	}
	    	else if($.frk.state.target=='contact'){
	    		removeHotline();
	    		$('#hboxinner').slideUp(300, function(){showContact();});
	    		$('#shortcut').fadeTo(200,0);
	    	}
	    	else if($.frk.state.target=='news'){
	    		$('#hboxinner').delay(50).slideUp(300, function(){showNews()});	
	    	}
	    		    					
		});
	}
}

function removeImagesSlideshow(){
	if($.frk.state.target=='imagesOverview'){
	    hideSlideshowScreen();
	}
	else if($.frk.state.target=='imagesSlideshow'){
	    $newPosition = -($(window).width()+200)+'px';
	    if($.frk.extra.direction == 'prev') $newPosition = ($(window).width()+200)+'px';
	    $('html').css('overflow-x', 'hidden');
	    $('#imagewrapper').animate({'margin-left': $newPosition}, 400, 'easeInQuad', function(){
	    	$(this).hide();
	    	$('html').css('overflow-x','auto');
	    	showImagesSlideshow();	    
	    }); 
	}
	else if($.frk.state.target=='projectsDetail'){

		$('html').css('overflow-x', 'hidden');
	    $('#prev').animate({'margin-left': '-700px'}, 390, 'easeInQuad');
	    $('#next').animate({'margin-right': '-700px'}, 390, 'easeInQuad');
	    $('#imagewrapper').fadeTo(400,0, function(){
	    	$(this).hide();
	    	$('#next, #prev').css('overflow-x','auto');
	    	showProjectsDetail();
	    	$('#content').empty();
	    	$('#shortcut').replaceWith($.frk.extra.shortcutsearch);
			$('#shortcut').css('top', '-16px');
			$('#hboxinner').css('display', 'none');
			$('#tmpl-resultprojects').tmpl($.frk.projects).appendTo('#content');
	    });
	}
	
}


function removeProjectsOverview(){
	if($.frk.state.target=='projectsDetail'){
        	showProjectsDetail();	
	}
	else{
		$.frk.albino.hide();
		$_elements = $('#content .result');
		hideBoxes($_elements, function(){ 
	    	
	    	$('#content').empty();
	    	
	    	if($.frk.state.target=='imagesOverview'){
	    		$('#hboxinner').delay(50).slideDown(300, function(){showImagesOverview()});
	    	}
	    	else if($.frk.state.target=='home'){
	    		$('#hotline').animate({'margin-left': '150px'}, 350, function(){showHome();});		
	    	}
	    	else if($.frk.state.target=='contact'){
	    		removeHotline();
	    		$('#hboxinner').slideUp(300, function(){showContact();});
	    		$('#shortcut').fadeTo(200,0);
	    	}
	    	else if($.frk.state.target=='news'){
	    		showNews();
	    	}
	    		    					
		});
	}
}

function removeProjectsDetail(){
	if($.frk.state.target=='projectsOverview'){
	    hideSlideshowScreen();
	}
	else if($.frk.state.target=='projectsDetail'){    
	    $('#project, #projectheader').fadeTo(300,0, function(){
	    	showProjectsDetail();
	    });	    
	}
	else if($.frk.state.target=='projectsSlideshow'){
	    
	    $newPosition = ($(window).width())+'px';
	    $('html').css('overflow-x', 'hidden');
	    $('#projecttext, #prev').animate({'margin-left': '-700px'}, 400, 'easeInQuad');
	    $('#next').animate({'margin-right': '-700px'}, 400, 'easeInQuad', function(){$(this).hide();});
	    $('#projectimages').animate({'padding-left': $newPosition}, 400, 'easeInQuad', function(){
	    	$(this).hide();
	    	$('html').css('overflow-x','auto');
	    	showProjectsSlideshow();	    
	    });
	}
	else if($.frk.state.target=='imagesSlideshow'){ //new
	    
	    $newPosition = ($(window).width())+'px';
	    $('html').css('overflow-x', 'hidden');
	    $('#projecttext, #prev').animate({'margin-left': '-700px'}, 400, 'easeInQuad');
	    $('#next').animate({'margin-right': '-700px'}, 400, 'easeInQuad', function(){$(this).hide();});
	    $('#projectimages').animate({'padding-left': $newPosition}, 400, 'easeInQuad', function(){
	    	$(this).hide();
	    	$('html').css('overflow-x','auto');
	    	showImagesSlideshow();	    
	    });
	}
	
}


function removeProjectsSlideshow(){
	if($.frk.state.target=='projectsOverview'){
	    hideSlideshowScreen();
	}
	else if($.frk.state.target=='projectsDetail'){
	    $('html').css('overflow-x', 'hidden');
	    $('#prev').animate({'margin-left': '-700px'}, 390, 'easeInQuad');
	    $('#next').animate({'margin-right': '-700px'}, 390, 'easeInQuad');
	    $('#imagewrapper').fadeTo(400,0, function(){
	    	$(this).hide();
	    	$('#next, #prev').css('overflow-x','auto');
	    	showProjectsDetail();
	    });
	}
	else if($.frk.state.target=='projectsSlideshow'){    
	    $newPosition = -($(window).width()+200)+'px';
	    if($.frk.extra.direction == 'prev') $newPosition = ($(window).width()+200)+'px';
	    $('html').css('overflow-x', 'hidden');
	    $('#imagewrapper').animate({'margin-left': $newPosition}, 400, 'easeInQuad', function(){
	    	$(this).hide();
	    	$('html').css('overflow-x','auto');
	    	showProjectsSlideshow();	    
	    });
	}
}


function removeContact(){
	
	$('#background').fadeTo(400,0, function(){
		$(this).hide();
		$('#hboxcontact').fadeTo(200,0, function(){
			$(this).remove();

			if($.frk.state.target=='imagesOverview'){
				$('#hotline').delay(50).animate({'margin-left': '0'}, 600, function(){showImagesOverview()});
				$('#hboxinner').addClass('noshadow');
				$('#hboxinner').slideDown(300);		
			}
			else if($.frk.state.target=='projectsOverview'){
				$('#hotline').delay(50).animate({'margin-left': '0'}, 600, function(){showProjectsOverview()});	
			}
			else if($.frk.state.target=='news'){
				$('#hotline').delay(50).animate({'margin-left': '0'}, 600, function(){showNews()});
			}
			else if($.frk.state.target=='home'){
				showHome();
			}			
			else {
			
				// TODO not a standard move
			}
		});
	});
}


function removeNews(){
	$.frk.albino.hide();
	$_elements = $('#content .article');
		
	hideBoxes($_elements, function(){
	
        	$('#content').empty();
        	
        	if($.frk.state.target=='imagesOverview'){
        		$('#shortcut').fadeTo(200,0, function(){
        			$('#shortcut').replaceWith($.frk.extra.shortcutproject);
        			$('#shortcut').fadeTo(0,0).fadeTo(200,1);        		
        		});
				$('#hboxinner').slideDown(300, function(){showImagesOverview()});	
			}
			else if($.frk.state.target=='contact'){
				removeHotline();				
				$('#shortcut').fadeTo(200,0, function(){showContact();});
	    	}
			else if($.frk.state.target=='home'){
				$('#hotline').animate({'margin-left': '150px'}, 350, function(){showHome();});
			}		
			else {
				// TODO not a standard move
			}
	});
	
}


/* * * * * END remove current page * * * * */



/*
 * show new page
 *
 */

function showHome(){
	$('#background').attr('class', 'bghome');
	
	$('#searchfield').val('');
	$("#asearchbox input[type='checkbox']").attr('checked', false);
	$('#secondcontact').show();
	$('#shareboxhome').delay(800).animate({'margin-top': '0'}, 300);
	if($.frk.state.current == 'blanco'){
		$('#shortcut').css('top', '-200px'); 
		$('#background').delay(200).fadeTo(800,1).show();
		$('#hbox').delay(800).fadeTo(200,1).show();
		$('#shortcut').delay(820).animate({top: '-16px'}, 400, 'easeOutQuad');
	}
	
	if($.frk.state.current == 'news' || $.frk.state.current == 'imagesOverview' || $.frk.state.current == 'projectsOverview'){
		$('#background').delay(200).fadeTo(800,1).show();
		$('#hboxinner').slideDown(300, function(){$('#hboxinner').removeClass('noshadow');});
	}
	
	if($.frk.state.current == 'projectsOverview' || $.frk.state.current == 'news'){
		
		$('#shortcut').fadeTo(200,0, function(){
        	$('#shortcut').replaceWith($.frk.extra.shortcutproject);
        	$('#shortcut').fadeTo(0,0).fadeTo(200,1);        		
        });
	}	
	if($.frk.state.current == 'contact'){ // && ($.frk.state.previous == 'projectsOverview' || $.frk.state.previous == 'news')){      
       	$('#shortcut').replaceWith($.frk.extra.shortcutproject);
        $('#shortcut').css('top', '-200px');
        $('#shortcut').fadeTo(0,1); 
        $('#hboxinner').slideDown(300);
        $('#background').delay(200).fadeTo(800,1).show();
        $('#shortcut').delay(820).animate({top: '-16px'}, 400, 'easeOutQuad');
	}
	
	stateSetter();
}
 
 
function showImagesOverview(){
	if($.frk.state.current == 'projectsOverview'){
		$('#shortcut').fadeTo(200,0, function(){
        	$('#shortcut').replaceWith($.frk.extra.shortcutproject);
        	$('#shortcut').fadeTo(0,0).fadeTo(200,1);        		
        });		
	}
	else if($.frk.state.current == 'contact'){
        	$('#shortcut').replaceWith($.frk.extra.shortcutproject);
        	$('#shortcut').fadeTo(0,0).fadeTo(200,1);        				
	}
	else if($.frk.state.current == 'blanco'){
		$('#shortcut').delay(600).animate({top: '-16px'}, 400, 'easeOutQuad');
	}
	
	$('#content').empty();
	$('#tmpl-resultimages').tmpl($.frk.images).appendTo('#content');
	$('#content').find('.result').fadeTo(0,0);
	
	$_elements = $('#content .result');
	showBoxes($_elements);
	
	$.frk.albino.show();	
	stateSetter();
}

function showImagesSlideshow(){
	
	$render = renderImagesSlideshow();
	
	if($.frk.state.current == 'imagesSlideshow'){
		$('#slideshow').empty();		
		$('#slideshow').replaceWith($render);
		$('#imagePanTrigger').css('max-width',$.frk.extra.wrapperWidth);
		$('#imagePanTrigger').css('max-height',$.frk.extra.wrapperHeight);
		$('#imagewrapper').fadeTo(0,0).delay(50).fadeTo(700, 1, function(){
			$('#imagePanTrigger').aToolTip({
				tipContent: 'zoom in'
			});
		});
	}
	else if($.frk.state.current == 'blanco'){
		$newPosition = -($(window).width()+200)+'px';
	
		$('#content').addClass('disabled');
		$('#hotline').animate({'margin-left': '150px'}, 350 );
		$('#contentwrap').css({marginLeft: $newPosition}).hide();
		
		$('#wrapper').prepend($render);
		$('#imagePanTrigger').css('max-width',$.frk.extra.wrapperWidth);
		$('#imagePanTrigger').css('max-height',$.frk.extra.wrapperHeight);
		$('#imagewrapper').fadeTo(0,0).fadeTo(700, 1, function(){
			$('#imagePanTrigger').aToolTip({
				tipContent: 'zoom in'
			});
		});
	}	
	else{
		//change projectsoverview to imagesoverview
		if($.frk.state.current == 'projectsDetail'){
			$('#hboxinner').slideDown(0);
			$('#shortcut').replaceWith($.frk.extra.shortcutproject);
			$('#content').empty();
			$('#tmpl-resultimages').tmpl($.frk.images).appendTo('#content');
		}
			
		$newPosition = -($(window).width()+200)+'px';
		
		$('#content').addClass('disabled');
		$('#hotline').animate({'margin-left': '150px'}, 350 );
		$('#contentwrap').animate({marginLeft: $newPosition}, 600, 'easeInQuad' , function(){
			
			$('#contentwrap').hide();
				
			if($('#slideshow').length > 0){ 
				$('#slideshow').replaceWith($render);
				$('#imagePanTrigger').css('max-width',$.frk.extra.wrapperWidth);
				$('#imagePanTrigger').css('max-height',$.frk.extra.wrapperHeight);				
			}
			else{
				$('#wrapper').prepend($render);
				$('#imagePanTrigger').css('max-width',$.frk.extra.wrapperWidth);
				$('#imagePanTrigger').css('max-height',$.frk.extra.wrapperHeight);
			}
			
			$('#slideshow').fadeTo(0,0).fadeTo(700, 1, function(){
				$('#imagePanTrigger').aToolTip({
					tipContent: 'zoom in'
				});
			});
		});
	}
	stateSetter();
}



function showProjectsOverview(){
	if($.frk.state.current == 'contact'){
        $('#shortcut').replaceWith($.frk.extra.shortcutsearch);
        $('#shortcut').fadeTo(0,0).fadeTo(200,1);        		
	}
	else if($.frk.state.current == 'blanco'){
		$('#shortcut').delay(600).animate({top: '-16px'}, 400, 'easeOutQuad');
	}
	else{	
		$('#shortcut').fadeTo(200,0, function(){
        	$('#shortcut').replaceWith($.frk.extra.shortcutsearch);
        	$('#shortcut').fadeTo(0,0).fadeTo(200,1);        		
        });
	}
	$('#content').empty();
	$('#tmpl-resultprojects').tmpl($.frk.projects).appendTo('#content');
	$('#content').find('.result').fadeTo(0,0);
	
	$_elements = $('#content .result');	
	showBoxes($_elements);
	$.frk.albino.show();
	stateSetter();
}

function showProjectsDetail(){
	
	$render = renderProjectsDetail();
	bindSlideshowKeys();
	if($.frk.state.current == 'projectsDetail'){
		$('#slideshow').empty();		
		$('#slideshow').replaceWith($render);
			
		$('#project, #projectheader').fadeTo(0,0).fadeTo(700, 1, function(){
		
		});
	}
	else if($.frk.state.current == 'projectsSlideshow' || $.frk.state.current == 'imagesSlideshow'){
		 
		$('#slideshow').empty();		
		$('#slideshow').replaceWith($render);
		$('#project').fadeTo(0,0).delay(50).fadeTo(700, 1);
	}
	else if($.frk.state.current == 'blanco'){
		$newPosition = -($(window).width()+200)+'px';
	
		$('#content').addClass('disabled');
		$('#hotline').animate({'margin-left': '150px'}, 350 );
		$('#contentwrap').css({marginLeft: $newPosition}).hide();
		
		if($('#slideshow').length > 0){ 
			$('#slideshow').replaceWith($render);	
		}
		else{
			$('#wrapper').prepend($render);
		}
		$('#slideshow').fadeTo(0,0).fadeTo(700, 1, function(){
		});
		
	}	
	else{
	
		$newPosition = -($(window).width()+200)+'px';
	
		$('#content').addClass('disabled');
		$('#hotline').animate({'margin-left': '150px'}, 350 );
		$('#contentwrap').animate({marginLeft: $newPosition}, 400, 'easeInQuad' , function(){
			
			$('#contentwrap').hide();
				
			if($('#slideshow').length > 0){ 
				$('#slideshow').replaceWith($render);
				
			}
			else{
				$('#wrapper').prepend($render);
			}
			
			$('#slideshow').fadeTo(0,0).fadeTo(700, 1, function(){
		
			});
		});
	}
	$('#projectimages').css('padding-left',$.frk.extra.projectimages);
	stateSetter();
	
}

function showProjectsSlideshow(){
	
	$render = renderProjectsSlideshow();
	bindSlideshowKeys();
	if($.frk.state.current == 'projectsSlideshow'){
		$('#slideshow').empty();		
		$('#slideshow').replaceWith($render);
		$('#imagePanTrigger').css('max-width',$.frk.extra.wrapperWidth);
		$('#imagePanTrigger').css('max-height',$.frk.extra.wrapperHeight);
		$('#imagewrapper').fadeTo(0,0).delay(50).fadeTo(700, 1, function(){
			$('#imagePanTrigger').aToolTip({
				tipContent: 'zoom in'
			});
		});
	}
	else if($.frk.state.current == 'projectsDetail'){
		$('#slideshow').empty();		
		$('#slideshow').replaceWith($render);
		$('#imagePanTrigger').css('max-width',$.frk.extra.wrapperWidth);
		$('#imagePanTrigger').css('max-height',$.frk.extra.wrapperHeight);
		$('#imagewrapper').fadeTo(0,0).fadeTo(700, 1, function(){
			$('#imagePanTrigger').aToolTip({
				tipContent: 'zoom in'
			});
		});
	}
	else if($.frk.state.current == 'blanco'){
		$newPosition = -($(window).width()+200)+'px';
	
		$('#content').addClass('disabled');
		$('#hotline').animate({'margin-left': '150px'}, 350 );
		$('#contentwrap').css({marginLeft: $newPosition}).hide();
		
		$('#wrapper').prepend($render);
		$('#imagePanTrigger').css('max-width',$.frk.extra.wrapperWidth);
		$('#imagePanTrigger').css('max-height',$.frk.extra.wrapperHeight);
		$('#imagewrapper').fadeTo(0,0).fadeTo(700, 1, function(){
			$('#imagePanTrigger').aToolTip({
				tipContent: 'zoom in'
			});
		});
	}	
	else{
		
		if(('#slideshow').length > 0){ 
			$('#slideshow').replaceWith($render);
			$('#imagePanTrigger').css('max-width',$.frk.extra.wrapperWidth);
			$('#imagePanTrigger').css('max-height',$.frk.extra.wrapperHeight);
		}
		else{
			$('#wrapper').append($render);
			$('#imagePanTrigger').css('max-width',$.frk.extra.wrapperWidth);
			$('#imagePanTrigger').css('max-height',$.frk.extra.wrapperHeight);
		}
	}
	if($('#videoframe').length != 0) resizeWrapper();
	stateSetter();	
}

function showContact(){
	$('#shortcut').hide();
	$('#hboxcontact').remove();
	$('#content').empty();				
	var $_plh = { url : $.frk.location.previous };
	$('#tmpl-contact').tmpl($_plh).appendTo('#hbox');
	$('#hbox').fadeTo(0,0).fadeTo(600,1).show();
	$('#background').attr('class', 'bgcontact').fadeTo(0,0).fadeTo(800,1).show();
	stateSetter();
} 

function showNews(){
	
	if($.frk.state.current == 'home' || $.frk.state.current == 'imagesOverview'){
		$('#shortcut').fadeTo(200,0, function(){
        	$('#shortcut').replaceWith($.frk.extra.shortcutsearch);
        	$('#shortcut').fadeTo(0,0).fadeTo(200,1);        		
        });
	}
	else if($.frk.state.current == 'contact'){
        $('#shortcut').replaceWith($.frk.extra.shortcutsearch);
        $('#shortcut').fadeTo(0,0).fadeTo(200,1);	
	}
	else if($.frk.state.current == 'blanco'){
		$('#shortcut').replaceWith($.frk.extra.shortcutsearch);
		$('#shortcut').css('top', '-200px'); 
		$('#shortcut').delay(600).animate({top: '-16px'}, 400, 'easeOutQuad');
	}
	
	$('#tmpl-news').tmpl($.frk.news).appendTo('#content');
	
	$('#content').find('.article').fadeTo(0,0);
		
	$_elements = $('#content .article');
		
	var i = -1;
    var animationCallback = function(){ 
		if(++i < $_elements.length)
			$($_elements[i]).fadeTo(400, 1, animationCallback);  
    };
	
	
	$('#content .article img').aToolTip({
				tipContent: 'zoom in'
			});
	
	
	$.frk.albino.show();
		
	animationCallback();
	
	stateSetter();
}


/* * * * * END show new page * * * * */
