﻿var _month = 9; // 1 through 12 or '*' within the next month, '0' for the current month
var _day = '29';   // day of month or + day offset
var _hour = 8;    // 0 through 23 for the hour of the day
var _min = 8;


var dow = 0;     // day of week sun=1 sat=7 or 0 for whatever day it falls on

var _month2 = 10;
var _day2 = '7';
var _hour2 = 16;    // 0 through 23 for the hour of the day
var _min2 = 16;


var tz = 10;     // offset in hours from UTC to your timezone
var lab = 'cd';  // id of the entry on the page where the counter is to be inserted
//var day2added = false;



var section = "homepage";

function start(section) {
    
    displayCountdown(setCountdown(_month,_day,_hour,_min,tz),lab, section);
}

loaded(lab,start);


var pageLoaded = 0; 
window.onload = function() {
    pageLoaded = 1;
}

function loaded(i,f) {
    if (document.getElementById && document.getElementById(i) != null) f(); 
    else if (!pageLoaded) setTimeout('loaded(\''+i+'\','+f+')',100);
}

function setCountdown(month,day,hour,minute,tz) {

    var m = month; 
    if (month=='*') m = 0;  
    var c = setC(m,day,hour,minute,tz); 
    return c;
} 

function setC(month,day,hour,minute,tz) {
    var toDate = new Date();
    if (day.substr(0,1) == '+') {
        var day1 = parseInt(day.substr(1));
        toDate.setDate(toDate.getDate()+day1);
    } 
    else
    {
        toDate.setDate(day);
    }
    if (month == '*')toDate.setMonth(toDate.getMonth() + 1);
    else if (month > 0) { 
        toDate.setMonth(month-1);
    }
    if (dow >0) toDate.setDate(toDate.getDate()+(dow-1-toDate.getDay())%7);
    toDate.setHours(hour);
    toDate.setMinutes(0-(tz*60)+(minute));    
    toDate.setSeconds(0);
    var fromDate = new Date();
    fromDate.setMinutes(fromDate.getMinutes() + fromDate.getTimezoneOffset());
    var diffDate = new Date(0);
    diffDate.setMilliseconds(toDate - fromDate);
    return Math.floor(diffDate.valueOf()/1000);
}

function displayCountdown(countdn,cd, section) {

    // check if date2 has passed
    if(setCountdown(_month2,_day2,_hour2,_min2,tz) < 0)
    {
        // after date2
        
        if(section == "homepage")
        {
            document.getElementById("homepageCounterContainer").style.backgroundImage="url('images/timerDuringBackground.jpg')";
        }
        else if(section == "sub")
        {
            document.getElementById("counterContainer").style.backgroundImage="url('images/subTimerDuringBackground.jpg')";
        }
        
        document.getElementById("timerBefore").style.display = "none";
        document.getElementById("timerDuring").style.display = "block";
        
        document.getElementById("td_seconds2").style.backgroundPosition = "0px 0px";
        document.getElementById("td_seconds1").style.backgroundPosition = "0px 0px";
        
        document.getElementById("td_minutes2").style.backgroundPosition = "0px 0px";
        document.getElementById("td_minutes1").style.backgroundPosition = "0px 0px";
        
        document.getElementById("td_hours3").style.backgroundPosition = "0px 0px";
        document.getElementById("td_hours2").style.backgroundPosition = "0px 0px";
        document.getElementById("td_hours1").style.backgroundPosition = "0px 0px";
    }
    else if(setCountdown(_month,_day,_hour,_min,tz) < 0)
    {
        // during
        countdn = setCountdown(_month2,_day2,_hour2,_min2,tz);
        
        document.getElementById("timerBefore").style.display = "none";
        document.getElementById("timerDuring").style.display = "block";
        document.getElementById("timerAfter").style.display = "none";
        
        if(section == "homepage")
        {
            document.getElementById("homepageCounterContainer").style.backgroundImage="url('images/timerDuringBackground.jpg')";
        }
        else if(section == "sub")
        {
            document.getElementById("counterContainer").style.backgroundImage="url('images/subTimerDuringBackground.jpg')";
        }
        
        var secs = countdn % 60; 
        if (secs < 10)
        {
            secs1 = '0';
            secs2 = secs;
        }
        else
        {
            secs1 = secs.toString().substring(0,1);
            secs2 = secs.toString().substring(1,2);
        }
        
        var countdn1 = (countdn - secs) / 60;
        var mins = countdn1 % 60;
        if (mins < 10)
        {
            mins1 = '0';
            mins2 = mins;
        }
        else
        {
            mins1 = mins.toString().substring(0,1);
            mins2 = mins.toString().substring(1,2);
        }
        
        
        countdn1 = (countdn1 - mins) / 60;
        var hours = countdn1 % 24;
        
        
        
        var days = (countdn1 - hours) / 24;
        if (days < 10)
        {
            days1 = '0';
            days2 = days;
        }
        else
        {
            days1 = days.toString().substring(0,1);
            days2 = days.toString().substring(1,2);
        }
        
        // 3 digit hours, no more days.
        hours = hours + (days * 24);
        
        if(hours > 99)
        {
            hours1 = hours.toString().substring(0,1);
            hours2 = hours.toString().substring(1,2);
            hours3 = hours.toString().substring(2,3);
        }
        else if(hours < 100 && hours > 9)
        {
            hours1 = '0';
            hours2 = hours.toString().substring(0,1);
            hours3 = hours.toString().substring(1,2);
        }
        else if(hours < 10)
        {
            hours1 = '0';
            hours2 = '0';
            hours3 = hours;
        }
        
        document.getElementById("td_seconds2").style.backgroundPosition = "0px " + (secs2 * 23 *-1) + "px";
        document.getElementById("td_seconds1").style.backgroundPosition = "0px " + (secs1 * 23 *-1) + "px";
        
        document.getElementById("td_minutes2").style.backgroundPosition = "0px " + (mins2 * 23 *-1) + "px";
        document.getElementById("td_minutes1").style.backgroundPosition = "0px " + (mins1 * 23 *-1) + "px";
        
        document.getElementById("td_hours3").style.backgroundPosition = "0px " + (hours3 * 23 *-1) + "px";
        document.getElementById("td_hours2").style.backgroundPosition = "0px " + (hours2 * 23 *-1) + "px";
        document.getElementById("td_hours1").style.backgroundPosition = "0px " + (hours1 * 23 *-1) + "px";
        
        setTimeout('displayCountdown('+(countdn-1)+',\''+cd+'\');',999);
        
    }
    else
    {
        // before
        countdn = setCountdown(_month,_day,_hour,_min,tz);        
        
        document.getElementById("timerBefore").style.display = "block";
        document.getElementById("timerDuring").style.display = "none";
        document.getElementById("timerAfter").style.display = "none";
        
        if(section == "homepage")
        {
            document.getElementById("homepageCounterContainer").style.backgroundImage="url('images/timerBeforeBackground.jpg')";
        }
        else if(section == "sub")
        {
            document.getElementById("counterContainer").style.backgroundImage="url('images/subTimerBeforeBackground.jpg')";
        }
        
        var secs = countdn % 60; 
        if (secs < 10)
        {
            secs1 = '0';
            secs2 = secs;
        }
        else
        {
            secs1 = secs.toString().substring(0,1);
            secs2 = secs.toString().substring(1,2);
        }
        
        var countdn1 = (countdn - secs) / 60;
        var mins = countdn1 % 60;
        if (mins < 10)
        {
            mins1 = '0';
            mins2 = mins;
        }
        else
        {
            mins1 = mins.toString().substring(0,1);
            mins2 = mins.toString().substring(1,2);
        }
        
        
        countdn1 = (countdn1 - mins) / 60;
        var hours = countdn1 % 24;
        if (hours < 10)
        {
            hours1 = '0';
            hours2 = hours;
        }
        else
        {
            hours1 = hours.toString().substring(0,1);
            hours2 = hours.toString().substring(1,2);
        }
        
        var days = (countdn1 - hours) / 24;
        if (days < 10)
        {
            days1 = '0';
            days2 = days;
        }
        else
        {
            days1 = days.toString().substring(0,1);
            days2 = days.toString().substring(1,2);
        }
        
        document.getElementById("tb_seconds2").style.backgroundPosition = "0px " + (secs2 * 23 *-1) + "px";
        document.getElementById("tb_seconds1").style.backgroundPosition = "0px " + (secs1 * 23 *-1) + "px";
        
        document.getElementById("tb_minutes2").style.backgroundPosition = "0px " + (mins2 * 23 *-1) + "px";
        document.getElementById("tb_minutes1").style.backgroundPosition = "0px " + (mins1 * 23 *-1) + "px";
        
        document.getElementById("tb_hours2").style.backgroundPosition = "0px " + (hours2 * 23 *-1) + "px";
        document.getElementById("tb_hours1").style.backgroundPosition = "0px " + (hours1 * 23 *-1) + "px";
        
        document.getElementById("tb_days2").style.backgroundPosition = "0px " + (days2 * 23 *-1) + "px";
        document.getElementById("tb_days1").style.backgroundPosition = "0px " + (days1 * 23 *-1) + "px";
        
        setTimeout('displayCountdown('+(countdn-1)+',\''+cd+'\');',999);
    }
    
}



