if (typeof(hideText) == "undefined")
    hideText = false;

function getElementsByClassName(oElm, strTagName, strClassName){
    var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++){
        oElement = arrElements[i];
        if(oRegExp.test(oElement.className)){
            arrReturnElements.push(oElement);
        }
    }
    return (arrReturnElements)
}

function initCounterElement(e, increment) {
    e.increment = increment;
    var timeString = e.innerHTML;
    var timeStringParts = timeString.split(" ");
    var hms = timeStringParts[0].split(":");
    var multiplier = 1;
    var seconds = 0;
    while (hms.length > 0) {
        seconds += hms.pop() * multiplier;
        multiplier *= 60;
    }
    e.seconds = seconds;
    e.hideText = timeStringParts.length > 1 ? false : true;
}

function updateCounterElement(e) {
    // if the result of this decrement will cross from positive to negative, refresh page
    if ( e.seconds > 0 && e.increment < 0 && (e.seconds+e.increment) <= 0) {
        // window.location.reload(false);
    } else {
        e.seconds += e.increment;
        e.innerHTML = formatSeconds(e.seconds, e.hideText);
    }
}

function updateTimeout() { 
    var countUps = getElementsByClassName(document, "span", "countup");  
    var countDowns = getElementsByClassName(document, "span", "countdown");  
    var oldCountDown = document.getElementById("updateTime");
    if (oldCountDown) {
        countDowns.push( oldCountDown );
    }

    for (i in countDowns) {
        if (! countDowns[i].seconds) {
            initCounterElement(countDowns[i], -1);
        }
        updateCounterElement(countDowns[i]);
    }

    for (i in countUps) {
        if (! countUps[i].seconds) {
            initCounterElement(countUps[i], 1);
        }
        updateCounterElement(countUps[i]);
    }

    setTimeout(updateTimeout, 1000); 
} 

function formatSeconds(secs, hideText) {
    minutes = parseInt((secs / 60) % 60);
    hours = parseInt(secs / 3600);
    if (minutes == 0 && hours == 0) {
        result = "" + secs;
        if (!hideText) result += " seconds";
        return result;
    } else {
        seconds = secs % 60;
        if (seconds < 0 || hours < 0 || minutes < 0) sign = "-"; else sign = "";
        hours = Math.abs(hours);
        minutes = Math.abs(minutes);
        seconds = Math.abs(seconds);
        hoursStr = "" + hours;
        minutesStr = "" + minutes;
        secondsStr = "" + seconds;
        if (seconds < 10)
            secondsStr = "0" + secondsStr;
        if (minutes < 10 && hours > 0)
            minutesStr = "0" + minutesStr;
        result = sign + minutesStr + ":" + secondsStr;
        if (!hideText) result += " minutes";
        if (hours > 0) {
            result = sign + hoursStr + ":" + minutesStr + ":" + secondsStr;
            if (!hideText) result += " hours";
        }
        return result;
    }
}

window.onload = updateTimeout;

