// ==============================================
//    Dynupdown: A Countup and Countdown Timer
// ==============================================
//          Version "Delta" (2007-06-06)
//
//  http://www.6times9.com/javascript/dynupdown/
//
//        Copyright 2006  Richard Winskill
// ==============================================

// For details and instructions, see http://www.6times9.com/javascript/dynupdown/

var months=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")
var msecondssecond=(1000) //Number of milliseconds in a second
var msecondsminute=(msecondssecond*60) //Number of milliseconds in a minute
var msecondshour=(msecondsminute*60) //Number of milliseconds in a hour
var msecondsday=(msecondshour*24) //Number of milliseconds in a day
var msecondsweek=(msecondsday*7) //Number of milliseconds in a week
var averagemonthdays=(365.25/12)  //Average number of days in a month since it varies from 28 to 31
var msecondsmonth=(msecondsday*averagemonthdays) //Average number of milliseconds in a month
var msecondsyear=(msecondsmonth*12) //Number of milliseconds in a year

function start_counter(hr, mn, sc, mo, da, year, dir, beforetext, aftertext, eventname, compact, countername, notyears){
	document.write('<span id="'+countername+'" class="dynupdown"><\/span>')
	counter(hr, mn, sc, mo, da, year, dir, beforetext, aftertext, eventname, compact, countername, notyears)
}

function zerotime(comp, prevperiod, value, name){
	//Function to determine whether or not a specific period (days, months, etc.) should be displayed if it is zero.
	if((comp=="yes" && prevperiod=="" && value==0) || (comp=="strict" && value==0)){ outperiod=""
	} else if(value==1){ outperiod=" "+value+ " "+name+", "
	} else { outperiod=" "+value+ " "+name+"s, "}
	return outperiod
}

function counter(hr, mn, sc, mo, da, year, dir, beforetext, aftertext, eventname, compact, countername, notyears){
	var today=new Date()
	var todayy=today.getYear()
	if (todayy < 1000){todayy+=1900}
	var todaym=today.getMonth()
	var todayd=today.getDate()
	var todayh=today.getHours()
	var todaymin=today.getMinutes()
	var todaysec=today.getSeconds()
	
	//Handle annual events
	if (year=="all"){yr=todayy} else {yr=year}
	
	//Create and parse strings of the dates
	todayparse=Date.parse(months[todaym]+" "+todayd+", "+todayy+" "+todayh+":"+todaymin+":"+todaysec)
	dateparse=Date.parse(months[mo-1]+" "+da+", "+yr+" "+hr+":"+mn+":"+sc)
	
	//Handle switched directions
	if(dir=="switch" && (dateparse>todayparse)){
		countdir="down"
		if(aftertext==""){switchword=""}else{switchword="to "}
	} else if(dir=="switch" && (dateparse<todayparse)){
		countdir="up"
		if(aftertext==""){switchword=""}else{switchword="since "}
	} else if(dir=="switch" && (dateparse==todayparse)){
		countdir="down"
		if(aftertext==""){switchword=""}else{switchword="to "}
	} else {
		countdir=dir
		switchword=""
	}
	
	//Handle annual events again
	if(countdir=="down"){predd=dateparse-todayparse} else {predd=todayparse-dateparse}
	if (predd<0 && year=="all"){yr=todayy+1; dateparse=Date.parse(datestring=months[mo-1]+" "+da+", "+yr+" "+hr+":"+mn+":"+sc)}
	
	//Calculate difference based in set direction
	if(countdir=="down"){
		dd=dateparse-todayparse
		timeupmsg=eventname+" has passed"
	} else {
		dd=todayparse-dateparse
		timeupmsg=eventname+" is in the future"
	}
	
	//Handle elapsed time
	if(dd<0){notime=1}else{notime=0}
	
	//Calculate difference in Years, Months, etc.
	if(notyears>0){
		//Remove months and years from calculations if notyears>0
		dyear=0
		dmonth=0
		if(notyears==2){
			dweek=Math.floor(dd/msecondsweek)
		}else{
			dweek=0
		}
	} else {
		//Calculate
		dyear=Math.floor(dd/msecondsyear)
		dmonth=Math.floor((dd-(dyear*msecondsyear))/msecondsmonth)
		dweek=0
	}
	dday=Math.floor((dd-(dyear*msecondsyear)-(dmonth*msecondsmonth)-(dweek*msecondsweek))/msecondsday)
	dhour=Math.floor((dd-(dyear*msecondsyear)-(dmonth*msecondsmonth)-(dweek*msecondsweek)-(dday*msecondsday))/msecondshour)
	dminute=Math.floor((dd-(dyear*msecondsyear)-(dmonth*msecondsmonth)-(dweek*msecondsweek)-(dday*msecondsday)-(dhour*msecondshour))/msecondsminute)
	dsecond=Math.floor((dd-(dyear*msecondsyear)-(dmonth*msecondsmonth)-(dweek*msecondsweek)-(dday*msecondsday)-(dhour*msecondshour)-(dminute*msecondsminute))/msecondssecond)
	
	if(notime==1){
		//Incorporate elapsed time
		document.getElementById(countername).innerHTML=timeupmsg
		if(!(document.getElementById(countername).className=="dynupdown elapsed")){document.getElementById(countername).className="dynupdown elapsed";}
	} else {
		//Handle 0 times
		//YEAR
		if(dyear==0 && (compact=="yes" || compact=="strict")){outyear=""} else if(dyear==1){outyear=" "+dyear+ " year, "} else {outyear=" "+dyear+ " years, "}
		//MONTH
		outmonth=zerotime(compact, outyear, dmonth, "month")
		//WEEK
		outweek=zerotime(compact, outmonth, dweek, "week")
		//DAY
		if(notyears==2){
			outday=zerotime(compact, outweek, dday, "day")
		}else{
			outday=zerotime(compact, outmonth, dday, "day")
		}
		//HOUR
		outhour=zerotime(compact, outday, dhour, "hour")
		//MINUTE
		outminute=zerotime(compact, outhour, dminute, "minute")
		//SECOND
		if(dsecond==1){outsecond=" "+dsecond+ " second "} else {outsecond=" "+dsecond+ " seconds "}
		//"AND" BEFORE SECONDS
		if(outyear=="" && outmonth=="" && outday=="" && outhour=="" && outminute==""){secondand=""} else {secondand="and"}
		//Show appropriate times before days based on value of "notyears"
		if(notyears==2){
			weekmonthyear=outweek
		}else if(notyears==1){
			weekmonthyear=""
		}else{
			weekmonthyear=outyear+outmonth
		}
		
		document.getElementById(countername).innerHTML=beforetext+weekmonthyear+outday+outhour+outminute+secondand+outsecond+switchword+aftertext
	}
	//Stop incorporating elapsed time
	
	setTimeout('counter("'+hr+'", "'+mn+'", "'+sc+'", "'+mo+'", "'+da+'", "'+year+'", "'+dir+'", "'+beforetext+'", "'+aftertext+'", "'+eventname+'", "'+compact+'", "'+countername+'", "'+notyears+'")',1000)
}