var classname = 'countdown';
var re_short = /(0?(\d*) days, )?(0?(\d{1,2}):)?(0?(\d{1,2}):)?0?(\d{1,2}) (Hrs|Min|Sec)/;
var re_long = /(0?(\d*) days, )?(0?(\d{1,2}) hrs, )?(0?(\d{1,2}) mins, .* )?0?(\d{1,2}) secs/;
// 0? is used in front of all digits so we wouldn't have to deal with leading zeros
// otherway we will start getting funny results latter, because sometimes numbers with leading
// zeros are counted as zero

function getElementsByClassName(classname, node)  {
    if(!node) node = document.getElementsByTagName("body")[0];
    var a = [];
    var re = new RegExp('\\b' + classname + '\\b');
    var els = node.getElementsByTagName("*");
    for(var i=0,j=els.length; i<j; i++)
        if(re.test(els[i].className))a.push(els[i]);
    return a;
}

var addLeadingZero = function(i) {
    i = i.toString();
    if (i.length < 2) {
        i = '0'+i;
    }
    return i;
}

var stringToSeconds = function(s) {
    var matchArray = re_short.exec(s);
    if (!matchArray) {
        var matchArray = re_long.exec(s);
    }

    var days = matchArray[2];
    var hours = matchArray[4];
    var minutes = matchArray[6];
    var seconds = matchArray[7];

	// if there is no leading hour then regex can mix up hour and minutes in short format
	// because it takes first available number with leading colon, assumes it is hours
	// and skips minutes since it doesn't find match
	if (minutes == undefined && hours != undefined) {
		minutes = hours;
		hours = undefined;
	}

    var secondsLeft = 0;
    if (days != undefined) {
        secondsLeft += days*24*60*60;
    }
    if (hours != undefined) {
        secondsLeft += hours*60*60;
    }
    if (minutes != undefined) {
        secondsLeft += minutes*60;
    }
    secondsLeft += parseInt(seconds);
    return secondsLeft;
}

var count = function(secondsLeft, s) {
    if (secondsLeft > 0) {
        secondsLeft--;
    }

    var days = Math.floor( secondsLeft / (24*60*60) );
    var hours = addLeadingZero(Math.floor( (secondsLeft-days*24*60*60) / (60*60) ));
    var minutes = addLeadingZero(Math.floor( (secondsLeft-days*24*60*60-hours*60*60) / 60 ));
    var seconds = addLeadingZero(secondsLeft - days*24*60*60 - hours*60*60 - minutes*60);

    var r = '';
    if (days) {
        r += days+' days, ';
    }

    if (re_short.exec(s)) {
    	if (hours != '00') {
    		r += hours+':';
    	}
    	if (minutes != '00' || hours != '00') {
    		r += minutes+':';
    	}

        r += seconds;

    	if (hours != '00') {
    		r += ' Hrs';
    	} else if (minutes != '00') {
    		r += ' Min';
    	} else {
    		r += ' Sec';
    	}
    } else {
    	if (hours != '00') {
    		r += hours+' hrs, ';
    	}
    	if (minutes != '00' || hours != '00') {
    		r += minutes+' mins, & ';
    	}
        r += seconds+' secs.';
    }
    return r;
}

counters = getElementsByClassName(classname);
var len = counters.length;
function foo() {
    for (var i=0; i < len; i++) {
        var seconds = stringToSeconds(counters[i].innerHTML);
        counters[i].innerHTML = count(seconds, counters[i].innerHTML);
    }
}
function pageRefresh()
{
	//alert("6 Minutes");
	//alert(window.location);
	window.location = window.location;
}
this.interval = setInterval(foo, 1000);
this.interval2 = setInterval(pageRefresh, 360000);
//this.interval2 = setInterval(pageRefresh, 10000);
