var mainHeight = 0, fadeInStep = 15, fadeOutStep = 10, scroller = 0, lastOpac = 0, traceCount = 0;
var hasTopShots = false, eventsVisible = false, dragHot = false, ddEnabled = false, keyPressed = -1, IE = document.all;
var mon = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
var spinner = "<img src=/img/spinner.gif class=spinner />";

function setInital(myUrl, valIn, valOut) {
	rootUrl = myUrl;
	fadeInStep = valIn;
	fadeOutStep = valOut;
}

function pageRefresh(url) {
	location.href = url + "/refresh"
}

function setNewShots() {
	if (readCookie("lastTopShot") != "") start = readCookie("lastTopShot");
	else start = Math.round(Math.random() * maxShot);

	start++;

	for (i=0; i<tsc.length; i++) {
		el = "ts_" + tsc[i]
		var newNbr = i + start;
		if (newNbr > maxShot) newNbr = 0;

		cross(el).src = tsList[newNbr];
		cross(el).alt = tsName[newNbr];
		cross(el).style.display = "";
	}

	writeCookie("lastTopShot", newNbr);
}

function setNewVideo() {
	if (readCookie("lastTopVideo") != "") newNbr = readCookie("lastTopVideo");
	else newNbr = Math.round(Math.random() * maxVideo);

	newNbr++;
	if (newNbr > maxVideo) newNbr = 0;

	flv.addParam("wmode", "opaque");
	flv.addParam("allowfullscreen", "true");
	flv.addVariable("autostart", "false");
	//flv.addVariable("repeat", "true");
	flv.addVariable("file", tvList[newNbr]);
	flv.addVariable("image", tvStat[newNbr]);
	flv.addVariable("callback", "analytics");
	flv.write("topvideo");
	
	writeCookie("lastTopVideo", newNbr);
}

function toggleBox(el, textOn, textOff) {
	if (cross(el + "Off").style.display == "none") {
		scroller = document.body.scrollTop;
		cross(el + "On").style.display = "none";
		cross(el + "Off").style.display = "";
		//document.body.scrollTop = getRealTop(cross(el)) - 8;
		cross(el).innerHTML = textOff + "&nbsp;&nbsp;&nbsp;&nbsp;";
	}
	else {
		cross(el + "On").style.display = "";
		cross(el + "Off").style.display = "none";
		//document.body.scrollTop = scroller;
		cross(el).innerHTML = textOn + "&nbsp;&nbsp;&nbsp;&nbsp;";
	}
}

function showHide(el, textOn, textOff) {
	if (cross(el + "Off").style.display == "none") {
		cross(el + "Off").style.display = "";
		cross(el).innerHTML = "&nbsp;&nbsp;&nbsp;" + textOff;
	}
	else {
		cross(el + "Off").style.display = "none";
		cross(el).innerHTML = "&nbsp;&nbsp;&nbsp;" + textOn;
	}
}

function showMatch(el1, el2, typ) {
	if (IE) {
		var mt = IE.tags("div");
		var ta = IE.tags(typ);
	}
	else if (document.getElementById) {
		var mt = document.getElementsByTagName("div");
		var ta = document.getElementsByTagName(typ);
	}
	else {
		return;
	}
	
	for (i=0; i<mt.length; i++) if (mt[i].id.indexOf("m_") == 0) cross(mt[i].id).style.display = "none";
	for (i=0; i<ta.length; i++) if (ta[i].id.indexOf("t_") == 0) cross(ta[i].id).className = "";

	cross(el1).style.display = "";
	cross(el2).className = typ;
}

function monClick(clear, next) {
	cross("mn_" + clear).style.display="none";
	cross("mn_" + next).style.display="block";
}

function checkObject(ev) {
	newObj = getEventObject(ev);
}

function loadEvents(that, delayed) {
	if (window.delayTimer) clearTimeout(delayTimer);

	if (delayed && !eventsVisible) {
		lastObj = that;
		delayTimer = window.setTimeout(function () { loadEvents(that, false); }, 650);
		document.onmouseover = checkObject;

		return;
	}
	else {
		document.onmouseover = "";

		if (newObj == lastObj) { 
			cross("info_loader").innerHTML = spinner;

			var day = that.id.replace("dy_", "");
			getContent(evParm.replace("xyz", day), true);

			showInfo(that, toEnDate(day), 0, 0, -4, -4, false);

			document.onmouseup = function() { hideInfo(); }
			eventsVisible = true;
		}
		else {
			eventsVisible = false;
			hideInfo();
		}
	}
}

function openInfo(that, left, top, parm, title) {
	cross("info_loader").innerHTML = spinner;
	showInfo(that, title, 0, 0, left, top, true, false);
	getContent(parm, false);
}

function showInfo(that, title, absX, absY, offsetX, offsetY, letDrag) {
	if (window.outTimer) clearTimeout(outTimer);
	if (window.autoTimer) clearTimeout(autoTimer);

	if (letDrag) {
		cross("info").onmousedown = function() { ddPrepare("info", "drag_me"); }
		cross("info").onmouseup = function() { ddEnabled = false; dragHot = false; }
	}
	else {
		cross("info_title").title = "";
		cross("info_title").style.cursor = "default";
		cross("info_loader").style.cursor = "default";
		cross("drag_me").style.cursor = "default";
	}

	if (absX +  absY != 0 && absX +  absY != "") {
		cross("info").style.left = absX + "px";
		cross("info").style.top = absY + "px";
	}
	else if (offsetX +  offsetY != "") {
		cross("info").style.left = getRealLeft(that) + offsetX + "px";
		cross("info").style.top = getRealTop(that) + offsetY + "px";
	}

	cross("info_title").innerHTML = title;
	fadeIn("info");
}

function hideInfo() {
	if (eventsVisible) eventsVisible = false;
	if (window.inTimer) clearTimeout(inTimer);
	if (window.autoTimer) clearTimeout(autoTimer);

	fadeOut("info", "info_loader");

	cross("info").onmousedown = "";
	cross("info").onmouseup = "";
	cross("drag_me").onmouseover = "";
	cross("drag_me").onmouseout = "";
	cross("info_handle").innerHTML = "";
	cross("more_handle").innerHTML = "";
	cross("more_handle").className = "";
	cross("more_handle").onmouseover = "";
	cross("more_handle").onmouseout = "";
	cross("more_handle").onclick = "";
	cross("more_handle").title = "";
	
	document.onmouseup = "";
}

function autoHide(out) {
	if (window.autoTimer) clearTimeout(autoTimer);
	if (out) autoTimer = window.setTimeout("hideInfo();", 350);
}

function setMainHeight() {
	if (cross("col_left")) newHeight("col_left");
	if (cross("col_right")) newHeight("col_right");
	if (cross("col_single")) newHeight("col_single");
	if (cross("header")) mainHeight = mainHeight + cross("header").offsetHeight;
}

function RandNr(high) { return 1 + Math.floor(Math.random() * high); }
function newHeight(el) { var newVal = cross(el).offsetHeight; mainHeight = (newVal > mainHeight) ? newVal : mainHeight; }
function ParentTable(that) { var temp = that; if (that.tagName) { var max = 10; that = that.parentNode; while (that.tagName && max--) { if (that.tagName == "TABLE") return that; that = that.parentNode; } return temp; } }

function cross(objectID) { return document.getElementById ? document.getElementById(objectID) : IE[objectID]; }
function getEventObject(ev) { ev = ev || window.event; if (ev.srcElement) return ev.srcElement; else return ev.target; }
function getRealLeft(el) { xPos = el.offsetLeft; tempEl = el.offsetParent; while (tempEl != null) { xPos += tempEl.offsetLeft; tempEl = tempEl.offsetParent; } return xPos; } 
function getRealTop(el) { yPos = el.offsetTop; tempEl = el.offsetParent; while (tempEl != null) { yPos += tempEl.offsetTop; tempEl = tempEl.offsetParent; } return yPos; } 

function isEven(value) { return (1 - (value % 2)); }
function isValidEmail(src) { var emailReg = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; var regex = new RegExp(emailReg); return regex.test(src); }
function toEnDate(day) { var dsplt = day.split("."); var dyDat = new Date(dsplt[2], dsplt[1]-1, dsplt[0]); return mon[dyDat.getMonth()] + " " + dyDat.getDate() + ", " + dyDat.getFullYear(); }
function newImage(arg) { if (document.images) { rslt = new Image(); rslt.src = arg; return rslt; } }
function tr(data) { traceCount += 1; cross("trace").innerHTML = traceCount + "|" + data; }

function getXMLRequester() { var xmlHttp = false; try { if (window.ActiveXObject) { for (i=5; i; i--) { try { if (i==2) xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); else xmlHttp = new ActiveXObject("Msxml2.XMLHTTP." + i + ".0" ); break; } catch(e) { xmlHttp = false; } } } else if (window.XMLHttpRequest) xmlHttp = new XMLHttpRequest(); } catch(e) { xmlHttp = false; } return xmlHttp; }
function getContent(parm, auto) { xmlHttp = getXMLRequester(); if (xmlHttp) { xmlHttp.open("GET", "/?xml=async&" + parm, true); xmlHttp.onreadystatechange = function () { if (xmlHttp.readyState == 4) completeRequest(xmlHttp.responseText, auto); } } xmlHttp.send(null); }
function completeRequest(strg, auto) { cross("info_loader").innerHTML = strg; if (auto) { cross("drag_me").onmouseover = function() { autoHide(false); }; cross("drag_me").onmouseout = function() { autoHide(true); }; } }
function sendData(field) { parm = cross(field).name + "=" + cross(field).value; xmlHttp = getXMLRequester(); xmlHttp.open("POST", "/responder.asp", true); xmlHttp.setRequestHeader("xmlHttptent-type", "application/x-www-form-urlencoded"); xmlHttp.setRequestHeader("xmlHttptent-length", parm.length); xmlHttp.onreadystatechange = handleResponse; xmlHttp.send(parm); return false; }

function writeCookie(name, value) { document.cookie = name + "=" + value; }
function readCookie(name) { var search = name + "="; var rVal = ""; 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; rVal = unescape(document.cookie.substring(offset, end)) } } return rVal; }
function readCookie_(name) { var nameEQ = name + "="; var ca = document.cookie.split(";"); for (i=0; i<ca.length; i++) { var c = ca[i]; while (c.charAt(0) == " ") c = c.substring(1, c.length); if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length); } return ""; }
function eraseCookie(name) { createCookie(name, ""); }

function fadeIn(el) { if (window.outTimer) clearTimeout(outTimer); var obj = cross(el); lastOpac += fadeInStep; if (lastOpac < 100) { changeOpac(obj, lastOpac); if (lastOpac == fadeInStep) obj.style.display = ""; inTimer = window.setTimeout("fadeIn('" + el + "');", 20); } else { lastOpac = 100; if (window.inTimer) clearTimeout(inTimer); changeOpac(obj, 100); } }
function fadeOut(el, cl) { if (window.inTimer) clearTimeout(inTimer); var obj = cross(el); lastOpac -= fadeOutStep; if (lastOpac > 0) { changeOpac(obj, lastOpac); outTimer = window.setTimeout("fadeOut('" + el + "', '" + cl + "');", 15); } else { lastOpac = 0; if (window.outTimer) clearTimeout(outTimer); obj.style.display = "none"; if (cl != "") cross(cl).innerHTML = ""; clearOpac(obj); } }
function changeOpac(obj, opacity) { obj.style.filter = "alpha(opacity=" + opacity + ")"; obj.style.opacity = (opacity / 100); obj.style.MozOpacity = (opacity / 100); obj.style.KhtmlOpacity = (opacity / 100); }
function clearOpac(obj) { obj.style.filter = ""; obj.style.opacity = ""; obj.style.MozOpacity = ""; obj.style.KhtmlOpacity = ""; }

function clearAllTimeouts() { var i = window.setTimeout(function() {}, 100) + 1; while (i--) window.clearTimeout(i); }
function clearAllIntervals() { var i = window.setInterval(function() {}, 100) + 1; while (i--) window.clearInterval(i); }

function ddPrepare(el, handle) {
	if (ddEnabled) return;

	crossEl = cross(el);
	dragHandle = (handle != "") ? handle : crossEl.id;
	crossEl.style.zIndex = 1001;

	dragHot = true;
}

function ddInit(ev) {
	if (!dragHot) return;

	ev = ev || window.event;

	var topMost = IE ? "BODY" : "HTML";
	var selected = IE ? ev.srcElement : ev.target;

	while (selected.id != dragHandle && selected.tagName != topMost) selected = IE ? selected.parentElement : selected.parentNode;

	if (selected.id == dragHandle) {
		offsetX = ev.clientX;
		offsetY = ev.clientY;
		newX = parseInt(crossEl.style.left);
		newY = parseInt(crossEl.style.top);

		ddEnabled = true;
		document.onmousemove = dd;
	}
}

function dd(ev) {
	if (!ddEnabled) return;

	ev = ev || window.event;

	crossEl.style.left = newX + ev.clientX - offsetX + "px"; 
	crossEl.style.top = newY + ev.clientY - offsetY + "px";

	return false;  
}

function captureKey(ev) {
	ev = ev || window.event;
	
	if (ev.shiftKey && ev.ctrlKey && ev.altKey && ev.keyCode == 32) pageRefresh(rootUrl);
}


function mmHandler(ev) {
	ev = ev || window.event;

	if (ev.srcElement) var obj = ev.srcElement;
	else var obj = ev.target;

	tr(obj.id);
}

document.onkeydown = captureKey;
document.onkeyup = function() { keyPressed = -1; }
document.onmousedown = ddInit;
//document.onmousemove = mmHandler;