/*
Author: Robert Hashemian
http://www.hashemian.com/

You can use this code in any manner so long as the author's
name, Web address and this disclaimer is kept intact.
********************************************************
Usage Sample:

<script language="JavaScript">
TargetDate = "12/31/2020 5:00 AM";
BackColor = "palegreen";
ForeColor = "navy";
CountActive = true;
CountStepper = -1;
LeadingZero = true;
DisplayFormat = "%%D%% Days, %%H%% Hours, %%M%% Minutes, %%S%% Seconds.";
FinishMessage = "It is finally here!";
</script>
<script language="JavaScript" src="http://scripts.hashemian.com/js/countdown.js"></script>
*/

function calcage(secs, num1, num2)
{
	s1 = ((Math.floor(secs/num1))%num2).toString();

	if (LeadingZero && s1.length < 2)
		returnImgString = "<img src=\"/img/count0.gif\" alt=\"0\" />" + "<img src=\"/img/count" + s1 + ".gif\" alt=\" \" />";

	else if (s1.length >= 2)
	{
		var temp = "" + s1;
		returnImgString = ""

		for (var i = 0; i < temp.length; i++)
		{
			returnImgString = returnImgString + "<img src=\"/img/count" + temp.substring(i,i+1) + ".gif\" alt=\" \" />";
		}
	}

	return returnImgString;
}

function CountBack(secs, countdown)
{
	if (secs == 0 && document.getElementById("newarrivalscont") != null)
	{
		browserdetect=document.getElementById("newarrivalscont").filters? "ie" : typeof document.getElementById("newarrivalscont").style.opacity=="string"? "mozilla" : ""

		if (browserdetect=="mozilla")
		{
			location.reload(true)
			return;
		}
	}

	if (secs < 0)
	{
		if (twoMinuteWarning!=0)
			stopfade()

		document.getElementById(countdown).innerHTML = FinishMessage;
		document.getElementById("redtoparrivals").innerHTML = "<ul><li><a href=\"/newarrivals/\"><img src=\"/img/thetopmagicbox2.jpg\" alt=\"Arrivals Just In\" /></a></li></ul>"

		return;
	}

	if (secs <=120 && twoMinuteWarning==0)
		startfade(document.getElementById("redtoparrivals"))

	DisplayStr = DisplayFormat.replace(/%%D%%/g, calcage(secs,86400,100000));
	DisplayStr = DisplayStr.replace(/%%H%%/g, calcage(secs,3600,24));
	DisplayStr = DisplayStr.replace(/%%M%%/g, calcage(secs,60,60));
	DisplayStr = DisplayStr.replace(/%%S%%/g, calcage(secs,1,60));

	document.getElementById(countdown).innerHTML = "<a href=\"/newarrivals/\">" + DisplayStr + "</a>";

	if (CountActive)
		setTimeout("CountBack(" + (secs+CountStepper) + " , '" + countdown + "')", SetTimeOutPeriod);
}

function putspan(countdown,backcolor, forecolor, secs)
{
	if (secs < 0) {
		document.write("<span id='" +countdown +"' class=\"homearrivaltimer\" style='background-color:" + backcolor + 
			"; color: red'></span>");
	}

	else {
		document.write("<span id='" +countdown +"' class=\"homearrivaltimer\" style='background-color:" + backcolor + 
			"; color:" + forecolor + "'></span>");
	} 
}


/***********************************************
* Gradual Highlight image script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
*
* modified
***********************************************/

function startfade(which2)
{
	imgobj=which2
	browserdetect=which2.filters? "ie" : typeof which2.style.opacity=="string"? "mozilla" : ""
	instantset(baseopacity)
	twoMinuteWarning=setInterval("gradualfade(imgobj)",50)
}

function stopfade()
{
	clearInterval(twoMinuteWarning)
	twoMinuteWarning=0
	instantset(baseopacity)
}

function instantset(degree)
{
	if (browserdetect=="mozilla")
		imgobj.style.opacity=degree
	else if (browserdetect=="ie")
		imgobj.style.filter = "alpha(opacity=" + (degree*100) + ")"
}

function gradualfade(cur2)
{
	browserdetect=cur2.filters? "ie" : typeof cur2.style.opacity=="string"? "mozilla" : ""

	if (browserdetect=="mozilla")
	{
		if (cur2.style.opacity<1 && fadein==1)		
			cur2.style.opacity=Math.min(parseFloat(cur2.style.opacity)+0.05, 1)		
		else
			cur2.style.opacity=Math.min(parseFloat(cur2.style.opacity)-0.05, 1)		

		if (cur2.style.opacity==1)		
			fadein=0
		else if (cur2.style.opacity==0.1)
			fadein=1
	}

	else if (browserdetect=="ie")
	{
		//var IEopacityString = "" + cur2.style.filter;
		//
		//IEopacityString = IEopacityString.substring(IEopacityString.indexOf("=")+1, IEopacityString.indexOf(")"));
		//
		//document.write("string:" + IEopacityString);
		//var IEopacity = parseInt(IEopacityString);
		//
		//if (IEopacity<100 && fadein==1)
		//	cur2.style.filter = "alpha(opacity=" + (IEopacity+10) + ")";
		//else
		//	cur2.style.filter = "alpha(opacity=40)"
		//
		//if (cur2.style.filter == "alpha(opacity=100)")		
		//	fadein=0
		//else if (cur2.style.filter=="alpha(opacity=10)")
		//	fadein=1
	}
}


if (typeof(BackColor)=="undefined")
  BackColor = "white";
if (typeof(ForeColor)=="undefined")
  ForeColor= "black";
if (typeof(TargetDate)=="undefined")
  TargetDate = "12/31/2020 5:00 AM";
if (typeof(DisplayFormat)=="undefined")
  DisplayFormat = "%%D%% d, %%H%% h, %%M%% m, %%S%% s";
if (typeof(CountActive)=="undefined")
  CountActive = true;
if (typeof(FinishMessage)=="undefined")
  FinishMessage = "Time's up! Please Refresh";
if (typeof(CountStepper)!="number")
  CountStepper = -1;
if (typeof(LeadingZero)=="undefined")
  LeadingZero = true;


CountStepper = Math.ceil(CountStepper);
if (CountStepper == 0)
  CountActive = false;
var SetTimeOutPeriod = (Math.abs(CountStepper)-1)*1000 + 990;
var dthen = new Date(TargetDate);
var dnow = new Date(CurrentDate);
if(CountStepper>0)
  ddiff = new Date(dnow-dthen);
else
  ddiff = new Date(dthen-dnow);

fadein=1
baseopacity=1

gsecs = Math.floor(ddiff.valueOf()/1000);
putspan(spantag,BackColor, ForeColor, gsecs);
CountBack(gsecs,spantag);
