/**
 * countdown.js
 *
 * This file is copyrighted; you cannot redistribute it and/or
 * modify it under any circumstance.
 *
 * @version 1.0.0
 * @copyright Copyright: 2007 Harun & Josh
 * @author Josh Beistle <josh@usprofitsearch.com>
 */

// customize these variables
var tag_id = 'countdown'; // ID of the html tag
var startDate = new Date();
var endDate = new Date("February 26, 2009 16:00:00 GMT"); // expiration date
var countdownText = "%%DAYS%% %%HOURS%% %%MINUTES%% %%SECONDS%%";
var endText = "We Are LIVE!";
var offset = (3600 * 7) * 1000;
var http = createRequestObject();

function createRequestObject()
{
	var xmlhttp;
	try
	{
		xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); 
	}
	catch(e)
	{
		try 
		{ 
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(f) 
		{
			xmlhttp=null;
		}
	}
	if(!xmlhttp&&typeof XMLHttpRequest!="undefined")
	{
		xmlhttp=new XMLHttpRequest();
	}
	return  xmlhttp;
}

function GetServerDate()
{
	var randUnrounded=Math.random()*999999999;
	var randNumber=Math.floor(randUnrounded);
	
	var url = 'tiny.txt';
	var args = '?rand=' + randNumber;
		
	try
	{
		http.open('GET', url+args , true);
		http.setRequestHeader('Content-Type', 'text/html');
		http.onreadystatechange = function()
		{
			if (http.readyState == 4)
			{
				var date = http.getResponseHeader('Date');
				if (date != '')
				{
					startDate = new Date(date);
				} 
				else
				{
					startDate = new Date();
				}
			}
		};
		http.send(null);
	}
	catch(e){}
	finally{}
}

function startCountdown()
{
	if (document.getElementById(tag_id))
	{
		GetServerDate();
		updateCountdown();
	}
}

function updateCountdown()
{
	var tag_object = document.getElementById(tag_id);
	GetServerDate();
	//now = new Date();
	now = startDate;
	count = Math.floor(endDate.getTime()-now.getTime())+offset;
	
	days = Math.floor((count) / 1000 / 60 / 60 / 24);
	hours = Math.floor((count) / 1000 / 60 / 60 - (24 * days));
	minutes = Math.floor((count) / 1000 / 60 - (24 * 60 * days) - (60 * hours));
	seconds = Math.round((count) / 1000 - (24 * 60 * 60 * days) - (60 * 60 * hours) - (60 * minutes));
	
	if (days < 0)
	{
		tag_object.innerHTML = endText;
		clearTimeout(timeoutID);
	}
	else
	{
		ct = countdownText.replace(/%%DAYS%%/g, days + " day" + (days == 1 ? '' : 's'));
		ct = ct.replace(/%%HOURS%%/g, hours + " hour" + (hours == 1 ? '' : 's'));
		ct = ct.replace(/%%MINUTES%%/g, minutes + " minute" + (minutes == 1 ? '' : 's'));
		ct = ct.replace(/%%SECONDS%%/g, seconds + " second" + (seconds == 1 ? '' : 's'));
		tag_object.innerHTML = ct;
		timeoutID = setTimeout("updateCountdown();",1000);
	}
}

function addEvent(obj, evType, fn)
{ 
	if (obj.addEventListener)
	{ 
		obj.addEventListener(evType, fn, false); 
		return true; 
	} 
	else if (obj.attachEvent)
	{ 
		var r = obj.attachEvent("on"+evType, fn); 
		return r; 
	} 
	else 
	{ 
		return false;
	}
}

addEvent(window, 'load', startCountdown);