duration = function()
{
    this.txtSecond = 'second';
    this.txtSeconds = 'seconds';
    this.txtMinute = 'minute';
    this.txtMinutes = 'minutes';
    this.txtHour = 'hour';
    this.txtHours = 'hours';
    this.txtAnd = 'and';

    this.duration_update = function(div_id, div_bar, seconds, totalSeconds, mode, countUp, showDiv, width)
    {
        if (seconds < 1  && countUp != 'true')
        {
            if(document.getElementById(div_id) != null)
            {
                document.getElementById(div_id).innerHTML = "0 seconds";
            }
        }
        else
        {
            result = this.hum_read_duration(seconds, totalSeconds, mode, showDiv, width);
            if(document.getElementById(div_id) != null)
            {
                document.getElementById(div_id).innerHTML = result[0];
            }
            if(document.getElementById(div_bar) != null)
            {
                document.getElementById(div_bar).innerHTML = result[1];
            }

            if (countUp == 'true')
            {
                seconds += 1;
            }
            else
            {
                seconds -= 1;
            }

            thisObj = this;
            setTimeout(function() { thisObj.duration_update(div_id, div_bar, seconds, totalSeconds, mode, countUp, showDiv, width); }, 1000);
        }
    }

    this.addTranslations = function(second, seconds, minute, minutes, hour, hours, and)
    {
        this.txtSecond = second;
        this.txtSeconds = seconds;
        this.txtMinute = minute;
        this.txtMinutes = minutes;
        this.txtHour = hour;
        this.txtHours = hours;
        this.txtAnd = and;
    }

    this.refresh = function()
    {
        window.location.reload(false);
    }

    // mode = 0	[normal]					 x hours x minutes x seconds
    // mode = 1	[short]						 x hrs x mins x secs
    // mode = 2	[shortest]					 x h x m x s
    // mode = 3	[digits]					 HH:mm:ss
    this.hum_read_duration = function(seconds, totalSeconds, mode, showDiv, width)
    {
        // Prepare div.
        usePixels = width;
        height = 16;
        percentageFinished = ((totalSeconds-seconds) / totalSeconds)*usePixels;

        // Prepare calculator
        mode = (mode == null) ? 0 : mode;
        hours = Math.floor(seconds/3600);
        seconds -= hours*3600;
        minutes = Math.floor(seconds/60);
        seconds -= minutes*60;
        ret_val = "";

        if (hours == 1)
        {
            if (mode == 0)
            {
                ret_val = "1 " + this.txtHour + ' ';
                if ((minutes == 0 && seconds > 0) || (seconds == 0 && minutes > 0))
                {
                    ret_val += this.txtAnd + ' ';
                }
            }
            else if (mode == 1)
            {
                ret_val = "1 hr ";
            }
            else if (mode == 2)
            {
                ret_val = "1 h ";
            }
            else
            {
                ret_val = "01:";
            }
        }
        else if (hours > 1)
        {
            if (mode == 0)
            {
                ret_val = hours + ' ' + this.txtHours + ' ';
                if ((minutes == 0 && seconds > 0) || (seconds == 0 && minutes > 0))
                {
                    ret_val += this.txtAnd + ' ';
                }
            }
            else if (mode == 1)
            {
                ret_val = hours + " hrs ";
            }
            else if (mode == 2)
            {
                ret_val = hours + " h ";
            }
            else if (hours > 9)
            {
                ret_val = hours + ":";
            }
            else
            {
                ret_val = "0" + hours + ":";
            }
        }
        else if (mode == 3)
        {
            ret_val = "00:";
        }
        if (minutes == 1)
        {
            if (mode == 0)
            {
                ret_val += "1 " + this.txtMinute + ' ';
                if (seconds > 0)
                {
                    ret_val += this.txtAnd + ' ';
                }
            }
            else if (mode == 1)
            {
                ret_val += "1 min ";
            }
            else if (mode == 2)
            {
                ret_val += "1 m ";
            }
            else
            {
                ret_val += "01:";
            }
        }
        else if (minutes > 1)
        {
            if (mode == 0)
            {
                ret_val += minutes + " " + this.txtMinutes + ' ';
                if (seconds > 0)
                {
                    ret_val += this.txtAnd + ' ';
                }
            }
            else if (mode == 1)
            {
                ret_val += minutes + " mins ";
            }
            else if (mode == 2)
            {
                ret_val += minutes + " m ";
            }
            else if (minutes > 9)
            {
                ret_val += minutes + ":";
            }
            else
            {
                ret_val += "0" + minutes + ":";
            }
        }
        else if (mode == 3)
        {
            ret_val += "00:";
        }
        if (seconds == 1)
        {
            if (mode == 0)
            {
                ret_val += "1 " + this.txtSecond;
            }
            else if (mode == 1)
            {
                ret_val += "1 sec";
            }
            else if (mode == 2)
            {
                ret_val += "1 s";
            }
            else
            {
                ret_val += "01";
            }
        }
        else if (seconds > 1)
        {
            if (mode == 0)
            {
                ret_val += seconds + " " + this.txtSeconds + ' ';
            }
            else if (mode == 1)
            {
                ret_val += seconds + " secs";
            }
            else if (mode == 2)
            {
                ret_val += seconds + " s";
            }
            else if (seconds > 9)
            {
                ret_val += seconds;
            }
            else
            {
                ret_val += "0" + seconds;
            }
        }
        else if (mode == 3)
        {
            ret_val += "00";
        }
        if (showDiv == 'true')
        {
            ret_val_div = '<div style="float:left;background:url(\'images/layout_v2/background_timer_red.gif\') repeat-x;height:'+height+'px;width:'+percentageFinished+'px;';
            if (percentageFinished == usePixels)
            {
                ret_val_div+='';
            }
            ret_val_div+='"></div>';
            if (percentageFinished < usePixels)
            {
                ret_val_div+='<div style="float:left;height:'+height+'px;background:#333333;width:'+(usePixels-percentageFinished)+'px;"></div>';
            }
        }

        var result = new Array(ret_val, ret_val_div);

        return result;
    }

}
