function geturl(href) {
	return (href.indexOf("http://") == 0) ? href.substr(href.indexOf("/", 8)) : href;
//	return href;
	var h = document.location.href.split('#')[0];
	var i;
	var min = h.length;
	if (href.length < min) min = href.length; 
	for(i = 1; i < min; i++) {
		if (h.charAt(i) != href.charAt(i)) {
			return h.substring(i - 1);
		}
	}
	return href.substring(i - 1, href.length - 1);
}

function getid(el) {
	var e = el;
	while(!e.id) {
		e = e.parentNode;
		if (!e) return null;
	}
	return e.id;
}
function ld(lnk) {
	go(lnk.href+'?ifr');
}

function go(s) {
	var f = geturl(s);
//	s = "data/" + f;
	s = f;
	sel(f);
//	$('#content').fadeOut(500);
//	$('#text').animate({
//		opacity: 0,
//		left: "-200px",
//		top: "300px",
//		height: "0px"
	//}, 500, "easeInQuad");
	var start = new Date();
	$.post(s, {ajax: 12}, function(data, textStatus) {
//		var f = function() {
			$("#page").html(data);
			//$('#content').fadeIn(1000);
//			$('#text').animate({
//				opacity: 1,
//				left: "0px",
//				top: "0px",
//				height: "100%"
//			}, 1000);
//		};
//		var x= new Date() - start;
//		if (x > 1000) {f();} else {setTimeout(f, 1000 - x);}
	});
	if (document.location.href.split("#")[0] == f) {
		document.location.replace(document.location.href.split("#")[0] + '#');
	} else {
		document.location.replace(document.location.href.split("#")[0] + "#" + f);
	}
}

$('document').ready(function() {

$('#topinfo').hover(
function(){$('#topinfo').addClass("hover");},
function(){$('#topinfo').removeClass("hover");}
);
/*$('#date').hover(
function(){$('#date').addClass("hover");},
function(){$('#date').removeClass("hover");}
);
*/

$('.sub').hover(
function(e){ssub(getid(e.target));},
function(e){hsub(getid(e.target));}
);


var s=document.location.href.split("#")[1];
if (s && (s != document.location.href.split("#")[0])) go(s);
/*document.ad1 = document.getElementById("ad1");
document.ad2 = document.getElementById("ad2");
document.ad3 = document.getElementById("ad3");
setInterval(function() {
	var l = parseInt(document.ad1.style.left, 10);
	if (isNaN(l)) l = -1000;
	l += 3;
	if (l > document.body.offsetWidth) l = -1000;
	document.ad1.style.left=l + "px";
	l = parseInt(document.ad2.style.left, 10);
	if (isNaN(l)) l = document.body.offsetWidth;
	l -= 4;
	if (l < -800) l = document.body.offsetWidth;
	document.ad2.style.left=l + "px";
	
}, 100);

var ff=function() {
$("#ad3").animate({opacity: "show"}, 5000, function(){setTimeout(function(){$("#ad3").animate({opacity: "hide"}, 3000);}, 4000)});
};
*/
//setInterval(ff, 30000);
//ff();

});

function hall() {
	$('.sel').hide(500, function(){$('#' + getid(this)).removeClass('sel')});
//	$('.sel').hide().removeClass('sel');
}

function sel(url) {
	var links = $("#menu li");
	var i;
	$("#menu li.selected").removeClass("selected");
	for(i = 0; i < links.length; i++) {
		var a = links[i].getElementsByTagName("A");
		if (a.length == 0) continue;
		a = a[0];
		if (geturl(a.href) == url) {
			var id=getid(a);
			if (id) {
				var o = $('#' + id);
				if (id == 'menu') o = $(a).next('.sub');
				if (o.hasClass('sub')) {
					if (!o.hasClass('sel')) {
						hall();
						o.addClass('sel').hide().show(500);
					}
				} else {
					hall();
				}
			}
		
			$(links[i]).addClass("selected");
		}
	}
}
var prevshow = false;
function ssub(id) {
	var o = $('#' + id);
	if (prevshow != false && o != prevshow && !prevshow.hasClass('sel')) prevshow.hide();
	prevshow = o;
	if (o.attr("tmr")) clearTimeout(o.attr("tmr"));
	if (isNaN(o.attr("vis"))) o.attr("vis", 0);
	//o.show(300);
	o.show();
	o.attr("vis", parseInt(o.attr("vis"), 10) + 1);
}

function hsub(id) {
	if ($('#' + id).hasClass('sel')) return;
	
	$('#' + id).attr("tmr",setTimeout(function() {
		var o=$('#' + id);
/*		o.attr("vis", parseInt(o.attr("vis"), 10) - 1);
		if (o.attr("vis") == 0) */
//		o.hide(500);
		o.hide();		
	}, 500));
}