var isCSS, isW3C, isIE4, isNN4, isIE6, is_gecko, is_safari, is_khtml, is_opera;
var on_load = new Array();                // An array of functions to be executed in init()

function initBaseStuff() 
{
    isCSS     = (document.body && document.body.style);
    isIE4     = (isCSS && document.all);
    isIE6     = (document.compatMode && document.compatMode.indexOf("CSS1") >= 0);
	
    for (var key in on_load) {
        on_load[key]();
    }
}

function getElement(id)
{
	return document.getElementById(id);
}

function showHideElement(elementID, show)
{
	var x = getElement(elementID);
	
	if (x)
	{
		if (show)
		{
			showEl(x);
		}
		else
		{
			hideEl(x);
		}
	}
}

function hideEl(el)
{
	if (el)
	{
		el.style.display='none';
	}
}

function hideElement(elementID)
{
	var x = getElement(elementID);
	if (x)
	{
		x.style.display='none';
	}
}

function showEl(el)
{
	if (el)
	{
		//if (isIE4 || isIE6 || navigator.appName == 'Microsoft Internet Explorer')
		//{
			el.style.display='block';
		//}
		//else
		//{
		//	el.style.display=null;
		//}
	}
}

function showElement(elementID)
{
	var x = getElement(elementID);
	if (x)
	{
		//if (isIE4 || isIE6)
		//{
			x.style.display='block';
		//}
		//else
		//{
		//	x.style.display=null;
		//}
	}
}

function removeFromArray(arrArray, intIndex) 
{
	if (intIndex >= arrArray.length) return;
	if (intIndex == arrArray.length - 1) {
		arrArray.length -= 1;
		return;
	}
	for (var i = intIndex + 1; i < arrArray.length; i ++) arrArray[i - 1] = arrArray[i];
	arrArray.length -= 1;
}

function formatTime(days, hours, minutes, seconds)
{
	return padZero(days) + ":" + padZero(hours) + ":" + padZero(minutes) + ":" + padZero(seconds);
}

function padZero(field)
{
	if (field < 10)
	{
		field = '0' + field;
	}
	return field;
}

function sWindow(url, name)
{
	openMinimalWindow(url, name, 450, 125);
}

function mWindow(url, name)
{
	openMinimalWindow(url, name, 600, 450);
}

function mWideWindow(url, name)
{
	openMinimalWindow(url, name, 800, 450);
}

function lWindow(url, name)
{
	openMinimalWindow(url, name, 800, 600);
}

function openMinimalWindow(url, name, width, height)
{
	var left = (getScreenWidth() - width) / 2;
	var top = (getScreenHeight() - height) / 2;
	var win = window.open(url,name,'width=' + width + ',height=' + height + ',left=' + left + ',top=' + top + ',toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes'); 
	win.focus();
}

function cSB(text)
{
	window.status = text;
}

function clearSB()
{
	window.status = '';
}

function closeWindow()
{
	if (window.opener)
	{
		window.opener.focus();
	}
	window.close();
}

function toggleElement(elementID)
{
	var x = getElement(elementID);
	if (x)
	{
		if (x.style.display=='none')
		{
			showEl(x);
		}
		else
		{
			hideEl(x);
		}
	}
}

function openPlayer(url)
{
    var width = 400;
    var height = 120;
    
	
	var left = (getScreenWidth() - width) / 2;
	var top = (getScreenHeight() - height) / 2;
	var win = window.open(url,'play','width=' + width + ',height=' + height + ',left=' + left + ',top=' + top + ',toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no'); 
	win.focus();
}

function resizeWindow(url, width, height)
{
	document.location.href = url;
	var left = (getScreenWidth() - width) / 2;
	var top = (getScreenHeight() - height) / 2;
	window.resizeTo(width, height);
	window.moveTo(left, top);
}

function getScreenWidth()
{
	return screen.width;
}

function getScreenHeight()
{
	return screen.height;
}

var theDate = new Date();
var now;
msDay = 60*60*1000*24;
msHour = 60*60*1000;
msMin = 60*1000;
function setcountdown(time)
{
	theDate.setTime(time);
	countdown();
}

function countdown()
{
	var timer = $('countDown');
	if (timer)
	{
		now = new Date();
		dd = theDate - now;
		day=Math.floor(dd/msDay);
		hr=Math.floor((dd%msDay)/msHour);
		min=Math.floor(((dd%msDay)%msHour)/msMin);
		sec=Math.floor((((dd%msDay)%msHour)%msMin)/1000);
		if(day<=0&&hr<=0&&min<=0&&sec<1)
		{
			timer.innerHTML = "Race Due";
		}
		else
		{
			timer.innerHTML = "Starting in " + formatTime(day, hr, min, sec);
			setTimeout("countdown()",1000);
		}
	}
}

var ns6 = document.getElementById&&!document.all;
var ie = document.all;
var dialogWidth = 0;
var dialogHeight = 0;
function drag_drop(e)
{
	if (ie&&dragapproved)
	{
		if ((tempx + event.clientX-offsetx > 0) &&
			(tempy + event.clientY-offsety > 0) &&
			((tempx+event.clientX-offsetx + dialogWidth) <= document.body.clientWidth) &&
			(tempy+event.clientY-offsety <= document.body.clientHeight))
		{
			crossobj.style.left=tempx+event.clientX-offsetx;
			crossobj.style.top=tempy+event.clientY-offsety;
			return false;
		}
	}
	else if (ns6&&dragapproved)
	{
		if ((tempx + e.clientX-offsetx > 0) &&
			(tempy + e.clientY-offsety > 0) &&
			((tempx + e.clientX-offsetx + dialogWidth) <= document.body.clientWidth) &&
			(tempy+e.clientY-offsety <= document.body.clientHeight))
		{
			crossobj.style.left=tempx+e.clientX-offsetx+"px";
			crossobj.style.top=tempy+e.clientY-offsety+"px";
			return false;
		}
	}
	return false;
}

function initializedrag(e, id, width, height)
{
	dialogWidth = width;
	dialogHeight = height;
	crossobj=getElement(id)
	var firedobj=ns6? e.target : event.srcElement
	var topelement=ns6? "html" : document.compatMode && document.compatMode!="BackCompat"? "documentElement" : "body"
		
	while (firedobj.tagName!=topelement.toUpperCase() && firedobj.id!="dragbar")
	{
		firedobj=ns6? firedobj.parentNode : firedobj.parentElement
	}

	if (firedobj.id=="dragbar")
	{		
		offsetx=ie? event.clientX : e.clientX
		offsety=ie? event.clientY : e.clientY

		tempx=parseInt(crossobj.style.left)
		tempy=parseInt(crossobj.style.top)
		dragapproved = true;
		document.onmousemove=drag_drop;
	}
}
document.onmouseup=new Function("dragapproved=false")

//this function is used for RSS Stylesheet to ensure that html is render and not just 
// printed as text
function go_decoding () 
{
  var to_decode = document.getElementsByName('decodeable');

  var s;
  for(var i = to_decode.length - 1; i >= 0; i--) { 
    data = to_decode[i].textContent;

      to_decode[i].innerHTML = data;  // that's the magic

  }

  return;
}

function externalLinks() 
{
	if (!document.getElementsByTagName) 
		return;
	var anchors = document.getElementsByTagName("a");
	
	for (var i=0; i<anchors .length; i++) 
	{
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") 
		{
			anchor.target = "_blank";
			//anchor.title = (anchor.title != "") ? anchor.title+" (opens in a new window)" : "opens in a new window";
			//anchor.className = (anchor.className != '') ? anchor.className+' external' : 'external';
		}
	}
}