// Cookie Functions from Netscape's JavaScript Guide
// http://developer.netscape.com/docs/manuals/js/client/jsguide/

function setCookie(Name, Value, Expire, Path) {
	document.cookie = Name + '=' + escape(Value) + ((Expire == null) ? '' : ('; expires=' + Expire.toUTCString())) + ((Path == null) ? '' : ('; path=' + Path));
}

function getCookie(Name) {
	var Search = Name + '=';
	if (document.cookie.length > 0) {
		Offset = document.cookie.indexOf(Search);
		if (Offset != -1) {
			Offset += Search.length;
			End = document.cookie.indexOf(';', Offset);
			if (End == -1) End = document.cookie.length;
			return unescape(document.cookie.substring(Offset, End));
		}
	}
	return '';
}

function Switch(e) {
	var id = document.getElementById(e);
	if (id.style.display == 'none') id.style.display = 'block'; else id.style.display = 'none';
}

function opacity(e, percent) {
	var id = document.getElementById(e);
	id.style.opacity = percent;
	id.style.MozOpacity = percent;
	id.style.filter = (percent == 1) ? '' : "alpha(opacity=" + percent * 100 + ")";
}

function wcenter(e1, e2) {
	var id1 = document.getElementById(e1);
	var id2 = document.getElementById(e2);
	var m = Math.round((id2.offsetWidth - id1.offsetWidth) / 2);
	if (m > 0) id1.style.left = m + 'px'; else id2.style.left = Math.abs(m) + 'px';
}

function hcenter(e1, e2) {
	var id1 = document.getElementById(e1);
	var id2 = document.getElementById(e2);
	var m = Math.round((id2.offsetHeight - id1.offsetHeight) / 2);
	if (m > 0) id1.style.margin = m + 'px 10px 0 0'; else id2.style.padding = Math.abs(m) + 'px 0 0 0';
}

function WindowSize() {
	var windowWidth, windowHeight;
	if (self.innerHeight) {
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) {
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}
	return new Array(windowWidth, windowHeight);
}

function PageSize() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight) {
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else {
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var wSize = WindowSize();
	var pageWidth = (xScroll < wSize[0]) ? wSize[0] : xScroll;
	var pageHeight = (yScroll < wSize[1]) ? wSize[1] : yScroll;
	return new Array(pageWidth, pageHeight);
}

function PageScroll() {
	var xScroll, yScroll;
	xScroll = 0;
	if (self.pageYOffset)
		yScroll = self.pageYOffset;
	else if (document.documentElement && document.documentElement.scrollTop)
		yScroll = document.documentElement.scrollTop;
	else if (document.body)
		yScroll = document.body.scrollTop;
	return new Array(xScroll, yScroll);
}

function relcenter(e) {
	var id = document.getElementById(e);
	var wSize = WindowSize();
	var pSize = PageSize();
	var pScroll = PageScroll();
	id.style.left = Math.round((pSize[0] - id.offsetWidth) / 2) + 'px';
	id.style.top = Math.round(pScroll[1] + (wSize[1] - id.offsetHeight) / 2) + 'px';
}

function resize(e, width, height) {
	var id = document.getElementById(e);
	id.style.width = width;
	id.style.height = height;
}

function imgSwitcher(e, src) {
	var id = document.getElementById(e);
	id.src = src;
}

function dispSwitcher(e) {
	var id = document.getElementById(e);
	if (id.style.display == 'none') opacity('wrapper', 0.5); else opacity('wrapper', 1.0);
	Switch(e);
	relcenter(e);
}

function boxSwitcher(e) {
	var box = document.getElementById('switch-box');
	for (i = 0; i < box.childNodes.length; i++) {
		node = box.childNodes[i];
		if (node.nodeName == "DIV") node.style.display = 'none';
	}
	Switch(e);
	hcenter(e, 'links');
}

function graphSwitcher(e, ticker, width, height) {
	var dateNow = new Date();
	dateNow.setFullYear(dateNow.getFullYear() + 1);
	if (!ticker) ticker = getCookie(e); else setCookie(e, ticker, dateNow, '/');
	var src = 'inc/graphic.php?width=' + width + '&height=' + height + '&ticker=' + ticker + '&ticks=200';
	imgSwitcher(e, src);
}

function panelSwitcher(collapse, panel, cp) {
	var e1 = document.getElementById(collapse);
	var e2 = document.getElementById(panel);
	e1.className = (cp == 1) ? 'close' : 'open';
	e2.style.display = (cp == 1) ? 'block' : 'none';
	var dateNow = new Date();
	dateNow.setFullYear(dateNow.getFullYear() + 1);
	setCookie(panel, 1 - cp, dateNow, '/');
}

