	var hash = location.hash;
	var date = new Date();
	var startTime = date.getTime();
	var lastTime = startTime;

	setInterval(function()
	{
	    if (location.hash != hash)
	    {
	    	parseHash(location.hash, hash);
	        hash = location.hash;
	    }
	}, 100);

$(function()
{
	// done in startupdone()
//	hash = location.hash;
//	parseHash(hash);
	
	
});

function parseHash(hash,oldhash) {
	//console.log('parsing '+hash + ' ('+oldhash+')');

	trackUser(hash,oldhash);
	
	if(hash == '' || hash.indexOf('/') === -1) {
		hash = '#p/introduction';
		//return;
	}
	
	if(hash == '#p/footprint')
		hash = '#p/footprint/0';

	
	
	arr = (hash.substring(1,hash.length)).split('/');
	for(i = 0; i < arr.length; i++)
		hashNo(i,arr);
	
}

function hashNo(i,arr) {
	/*
	 * pages/ 
	 * 
	 * **/
	
	if(i == 0) { // home
		
	} else if (i == 1) {
		//console.log(arr[1]);

		if(arr[1] == 'design') {
			design_gal(0);
		}
		
		if(arr[0] == 'p') {
			showPage(i,arr);
		}
		
		
	} else if( i == 2) {
		//console.log('hash i2 '+arr[1]);
		if(arr[1] == 'news') {
			//console.log('nn'+arr[1]+' '+arr[2]);
			showSubpage(i,arr,'news');
		} else if( arr[1] == 'contact') {
			showSubpage(i,arr,'contact');
		} else if( arr[1] == 'footprint') {
			showSubpage(i,arr,'footprint');
		}
		
	}
}

function showPage(i,arr) {
	//console.log('showpage '+i+'.'+ arr[i]);
	current = arr[i];

	menuSelect(arr[i],'menu_'+arr[1]);
	
	showLoop('pages', current);
}

function showSubpage(i,arr,page) {
	//console.log('showSubpage '+i+' '+ arr[i] + ' ' + page);
	current = arr[i];
	
	$("."+page+"_item").each(function(){
		//console.log('showSubpage '+$(this).attr("id"));
		if( $(this).attr("id") == page+'_'+current ) {
			//console.log('show');
			if( ! $(this).is(':visible') ) {
				var arg = $(this);
				fadeInC(arg);
				
				if(page == 'news') {
					//console.log('news gal '+current);
					$(".news_gallery1").cycle( parseInt(current) );
					// todo change image for news
				}
			}
		} else {
			//console.log('hide');
			if( $(this).is(':visible') ) {
				$(this).hide();
			}
		}
	});
	
	if(page == 'news') {
		// update index for news
		text = (_news_current+1)+'/'+_news_count;
		$("#news_index").text(text);
	}
	
}

function menuSelect(current,id) {
	//console.log('menuSelect '+current+' '+id);
	
	$("."+id).each(function(){
		if( $(this).attr("id") == id+'_'+current ) {
			////console.log('menu on '+$(this).attr("id"));
			$(this).addClass('menuactive');
			img_on($(this).attr("id"));
		} else {
			////console.log('menu off'+$(this).attr("id"));
			$(this).removeClass('menuactive');
			img_off($(this).attr("id"));
		}
	});

	menuSlide(current,id);
}

function menuSlide(current,id) {
	slide = -1;
	//console.log( "menuSlide "+current+" " + id);

	if(current == 'introduction')
		slide = 5;
	if(current == 'design')
		slide = 167;
	if(current == 'footprint')
		slide = 310;
	if(current == 'production')
		slide = 475;
	if(current == 'contact')
		slide = 635;
	if(current == 'news')
		slide = 765;
		
	if(slide != -1) {
		$("#menu_marker").animate({left: slide +"px"});
		//console.log( "#"+id+"_slide " + slide );
	}
	
}

function showLoop(id, current) {
	$("#"+id).children().each(function(){
		if( $(this).attr("id") == current ) {
			if( ! $(this).is(':visible') ) {
				//console.log('show '+$(this).attr("id"));
				var arg = $(this);
				fadeInC(arg);
			}
		} else {
			//console.log('hide '+$(this).attr("id"));
			if( $(this).is(':visible') ) {
				var arg = $(this);
				fadeOutC(arg);
			}
		}
	});
}

// change image for design/gallery
function design_gal(id) {
	$(".design_gallery1").cycle(id);
	$(".design_gallery2").cycle(id);
}

function fadeInC(arg) {
	if(_browser_ie)
		arg.show();
	else
		arg.fadeIn();
}
function fadeOutC(arg) {
	if(_browser_ie)
		arg.hide();
	else
		arg.fadeOut();
}

function trackUser(hash,oldhash) {

	if(hash.length > 0 && hash.substring(0, 1) == '#')
		hash = hash.substring(1);	
	if(oldhash.length > 0 && oldhash.substring(0, 1) == '#')
		oldhash = oldhash.substring(1);
	
	var date = new Date();	
	currentTime = date.getTime();
//	page logging 
	$.ajax(
	 { url: "log.php?hash="+ hash+"&oldhash="+ oldhash+"&starttime="+startTime+"&lasttime="+lastTime+"&currenttime="+currentTime, context: document.body, success: function(){
		 //console.log('logged '+hash);
	 	}
	 }		
	);
	lastTime = date.getTime();
//	
//	// Add google analytics here
//	_gaq.push(['_trackEvent', 'Pages', 'hash', hash]);
	
}

function hash_keymove(key) {
	gal = '#p/col/gal';	
	if( location.hash.substring(0, gal.length) == gal) {
//		console.log('in gallery');
		switch(key) {
		case KEY_UP :
			gal_change_no(-1);
			break;
		case KEY_DOWN :
			gal_change_no(1);
			break;
		case KEY_LEFT :
			gal_change_i(-1);
			break;
		case KEY_RIGHT :
			gal_change_i(1);
			break;
		case KEY_ESC :
			hideFullImage();
			break;
		}
	}
	

	craft = '#p/pov/craft';
	phi = '#p/pov/phi';
	if( location.hash.substring(0, craft.length) == craft) {
		
		switch(key) {
		case KEY_LEFT :
			photos('craft','prev');
			break;
		case KEY_RIGHT :
			photos('craft','next');
			break;
		}
	}
	if( location.hash.substring(0, phi.length) == phi) {
		
		switch(key) {
		case KEY_LEFT :
			photos('phi','prev');
			break;
		case KEY_RIGHT :
			photos('phi','next');
			break;
		}
	}
	
}

