// rts2007
// note: this is loading from document root
// get daypass_timeLeft in seconds as js var from PHP (when PHP writes page)
// get timeNow convert to seconds
// compute endingTime (in seconds): add daypass_timeLeft + timeNow
var timeExpiredStr = " no time ";
var sTimerTextMsg = '';
var nSecLeft;
var dutyCycle = 1;// in seconds
var daypass_timeLeft = 10200;// for dev, will be set by PHP/Smarty - day: 86400
var d = new Date();
var timeNow = Math.floor(d.getTime()/1000);
var endingTime = timeNow + daypass_timeLeft;
var timesUp;
var didAlert = false;
//alert(" ending time: " + endingTime + " dutyCycle: " + dutyCycle);

dayOfSeconds    = 86400;// 1 day = 24 * 3600 = 86400 seconds
hourOfSeconds   = 3600;// 1 hr =  60 * 60 = 3600 seconds
minuteOfSeconds = 60;

function getTimeLeft(){
	
// every iteration:
// get timeNow and subtract it from endingTime to calculate time left
	var sTimerTextMsg = '';
    var d = new Date();
    myTimeNow = Math.floor(d.getTime()/1000);// in seconds!    
    nSecLeft = endingTime - myTimeNow;// in seconds// 
   // nSecLeft = timeLeftInSeconds;

// calculate the remaining time in hrs, min and seconds
         
    secondsLeftSec     = nSecLeft%60;//modulo
    minutesLeft   = Math.floor(nSecLeft/60);// total min
    minutesLeftMin     = minutesLeft%60;// modulo
    hoursLeftHours     = Math.floor(minutesLeft/60);
     
    hoursLeftStr = hoursLeftHours;
    if(hoursLeftHours < 10) {
        hoursLeftStr = "0" + hoursLeftHours;
        if(hoursLeftHours < 1) { hoursLeftStr = "00"; }        
    } 
         
    minutesLeftStr = minutesLeftMin;
    if(minutesLeftMin < 10) {
        minutesLeftStr = "0" + minutesLeftMin;
        if(minutesLeftMin < 1){ minutesLeftStr = "00"; }
    }     

    secondsLeftStr = secondsLeftSec;
    if(secondsLeftSec < 10) {
       secondsLeftStr = "0" + secondsLeftSec;
       if(secondsLeftSec < 1) { secondsLeftStr = "00"; }
    }
    flashingColon = ":";
    //(nSecLeft%2) ? flashingColon = ":" : flashingColon = "<b>:</b>";
   
    if(nSecLeft<1) { timesUp = true; }
    
    // turned off - fires for every page once time is up 
    // - could modify using more elaborate rules to be more useful if need develops
    //if(timesUp) { doTimesUpThing(); }
         
    timeLeftStr = hoursLeftStr + ":" +  minutesLeftStr  + "" + flashingColon + "" + secondsLeftStr;// + " didAlert: " + didAlert
    
    (!timesUp) ? sTimerTextMsg = timeLeftStr : sTimerTextMsg = timeExpiredStr;
	//alert(" ending time: " + endingTime + " dutyCycle: " + dutyCycle + " " + sTimerTextMsg);
	
	// style the timer text if less than 2 hours (7200 seconds)	
	if(nSecLeft < 7200 && nSecLeft > 0) {
	// simple styling (only works in XHTML 1.0 Transitional or XHTML tables or plain HTML)
		sTimerTextMsg = "<font color='#ff0000'>" + sTimerTextMsg + "</font>";
	}
	
    timeLeftTimer();
    
    document.getElementById("timerText").innerHTML = sTimerTextMsg;
    document.getElementById("timerText2").innerHTML = sTimerTextMsg;

}// end getTimeLeft

function timeLeftTimer(){
    setTimeout("getTimeLeft()",dutyCycle*1000);
}

//<body onLoad="timeLeftTimerInit(41986)"> neg numbers ok
function timeLeftTimerInit(timeleftinseconds){
// BUT only do this if a number is supplied
// this timer's msg should only seen if a daypass cookie has been detected
// set by logic of Smarty so make appropriate msg for Reelhouse customer
	//var test = new String(timeleftinseconds);
	timeleftinseconds = Number(timeleftinseconds);
	//alert(Number(timeleftinseconds)); 
		
	if( isNaN(timeleftinseconds) ) {
		//alert(timeleftinseconds + " NaN!");
		// if not sent a number DO NOT calculate - indicate no time
		timeleftinseconds = 0;
		sTimerTextMsg = timeExpiredStr;
		document.getElementById("timerText").innerHTML = sTimerTextMsg;

	}else{
	// recieved a number so calsculate
		calculateEndingTime(Number(timeleftinseconds));
    	setTimeout("getTimeLeft()",dutyCycle*1000);
    }
}

function calculateEndingTime(timeLeftInSeconds){
// part of initialization: calculate ending time from supplied seconds left
	endingTime = timeNow + timeLeftInSeconds;
}

function doTimesUpThing(){
// could limit this if only within so many minutes of expiration if annoying
// this is very annoying
    if(!didAlert){
        alert("Your Day Pass has expired!");
        didAlert = true;
    }
}