var timeHash=undefined, currentHash=undefined;

$(document).ready(init);

function init() {
	var sb=CONSTANT.get("ROOT")+"/#", href=window.location.href;
	if(href.length>sb.length && href.substr(0,sb.length)!=sb) {
		var href_n=href.search(/\?/)+1, hash="";
		if(href_n>0) { hash=href.substring(href_n); }
		window.location.href = sb+hash;
		return false;
	}
	
	resized(); $(window).resize(resized);
	updateHash();
	updateLinks("a");
}

function resized() {
	var win=new Array($(window).width(),$(window).height());
	var container=new Array($("#container").width(),$("#container").height());
	var content=new Array($("#content").width(),$("#content").height());
	var head=new Array($("#head").width(),$("#head").height());
	var foot=new Array($("#foot").width(),$("#foot").height());
	
	$("#head, #foot").css({
		"width":(win[0]>container[0] ? "auto" : container[0]+"px")
	});
	$("#container").css({
		"height":(win[1]-(head[1]+foot[1])>content[1] ? win[1]-(head[1]+foot[1])+"px" : content[1]+"px")
	});
	return true;
}

function updateHash() {
	if(timeHash!=undefined) { clearTimeout(timeHash); }
	var href=window.location.href, href_n=href.search(/\#/)+1, hash="", res=false;
	if(href_n>0 && href.length>href_n) { hash=href.substring(href_n); }
	if(currentHash==undefined) { currentHash=hash; res=true; }
	else if(hash!=currentHash) { currentHash=hash; res=true; }
	if(res) { loadSection(currentHash); }
	timeHash = setTimeout("updateHash()",1000);
	return res;
}

function updateLinks(c) {
	if(c==undefined) { return false; }
	var a=$(c);
	var b;
	for(var n=0; n<a.size(); n++) {
		b=a.eq(n).attr("href").substring(0,4).toLowerCase();
		if(b!="mail" && b!="http") {
			a.eq(n).attr({
				"href":"javascript:void(0)",
				"sectionString":a.eq(n).attr("href").replace(/\?/i,"")
			}).click(function() { loadSection($(this).attr("sectionString")); });
		}
	}
	return true;
}

function setHash(str) {
	if(currentHash!=str) {
		window.location.href=CONSTANT.get("ROOT")+"/#"+(currentHash=str);
	}
}

function loadSection(str) {
	if(currentHash!=str) {
		window.location.href=CONSTANT.get("ROOT")+"/#"+(currentHash=str);
	}
	$("#section div.content").html("");
	var menuItems=$("#menu div.content a img");
	for(var n=0; n<menuItems.size(); n++) {
		var menuItem=menuItems.eq(n);
		var menuItemSrc=menuItem.attr("src").replace("_u.png",".png");
		if(menuItem.attr("src")!=menuItemSrc) {
			menuItem.attr({"src":menuItemSrc}); }
	}
	$.ajax({
		type:"GET", url:"_section.php", data:currentHash,
		success:function(content) {
			$("#section div.content").html(content);
			var menuActiveClass;
			if(!(menuActiveClass=currentHash.substring(currentHash.indexOf("=")+1))) {
				menuActiveClass=CONSTANT.get("HASH"); }
			var menuActive=$("#menu div.content a img."+menuActiveClass);
			menuActive.attr({"src":menuActive.attr("src").replace(".png","_u.png")});
			updateLinks("#section div.content a, #section div.content area");
			resized();
		}
	});
	return true;
}

