$(function()
{
	// done in footer
});

// capture window resize
window.onresize = function(){
  setSizes();
};



// track position of clicks
window.onmousedown = function(e) {

	top = $("#page").css('top');
	left = $("#page").css('left');
	
	myhash = location.hash;
	if(myhash != '')
		myhash = myhash.substring(1);
		
	
	//console.log('down'+e.pageX+e.pageY+'  '+top+','+left);
	mouse = e.pageX+','+e.pageY+','+left+','+top+','+hash;
	
	
	// mouse logging disabled
//	$.ajax(
//			 { url: "log.php?mouse="+mouse, context: document.body, success: function(){
//				 //console.log('logged '+mouse);
//			 	}
//			 }		
//			);
	
};



// maximize and center divs
function setSizes() {
	maximize('#loader');
	maximize('#popup');
	
	center('#page',990,700);
	center('#loaderdiv',330,450);
}

function startup() {
  setSizes();
	//center('#copyright',1200,0);
	
	
	
	// preload page here!
	
	// preload home
	
	
	//setTimeout('startupDone()',17);
}

function startupDone() {
	//console.log('startupDone');
	parseHash(location.hash,'');
	

	//$('#loader').fadeOut();
	
	preload2();
}


function center(layer, width, height) {
	
	
	
	if(height != 0) {
		var wheight = $(window).height();
		var top = wheight/2 - height/2;
		if(top<0) top = 0;
		$(layer).css('top', top)
		
	}
	if(width != 0) {
		var wwidth = $(window).width();
		var left = wwidth/2 - width/2;
		if(left<0) left = 0;
		$(layer).css('left', left);
	}

	//console.log(layer+' is centered '+left+','+top);
}

function maximize(layer) {
	var wheight = $(window).height();
	var wwidth = $(window).width();
	
	$(layer)
	 .css('width', wwidth)
	 .css('height', wheight);

	//console.log(layer+' is maximized '+wwidth+'x'+wheight);
}


function home_on(layer) {
	//console.log('on'+layer);
	$("#home_room"+layer).stop().animate({left: 0});
	if(layer<3)
		$("#home_room"+(layer+1)).stop().animate({left: 40});
}

function home_off(layer) {
	//console.log('off'+layer);
	$("#home_room"+layer).stop().animate({left: 20});
	if(layer<3)
		$("#home_room"+(layer+1)).stop().animate({left: 20});
}



function img_on(id) {
	src = $("#"+id).attr('src');
	if( src.indexOf('-mo') === -1 ) {
		src = src.substr(0,src.length-4)+'-mo.png';
		$("#"+id).attr('src',src);
	}
}
function img_off(id) {
	src = $("#"+id).attr('src');
	if( src.indexOf('-mo') !== -1 ) {
		src = src.substr(0,src.length-7)+'.png';
		$("#"+id).attr('src',src);
	}
}


function debugrgb() {
	$("a").each(
		
		function( intIndex ) {
			console.log(this.href);
		}
		
	);	
}

function news_move(dir) {
	_news_current+=dir;
	if(_news_current >= _news_count)
		_news_current = _news_count-1;
	if(_news_current < 1)
		_news_current = 0;
	
	parseHash('#p/news/'+_news_current, location.hash);
}
