if (! DSala) {
    var DSala = {};
}

/**
 * A text-based clock that displays the time for a particular
 * timezone. The timezone is defined by the minutes offset from
 * GMT.
 *
 * @param string The id of the display element.
 * @param int The number of minutes the clock is offset from GMT.
 */
DSala.Clock = function (elementId, timezoneOffset) {
    this.timezoneOffset = timezoneOffset;
    this.displayElement = document.getElementById(elementId);
};



DSala.Clock.prototype = {

    start : function () {
        var clock = this;
        clock.updateDisplay();
        this.timer = setInterval(function () {clock.updateDisplay();}, 1000);
    },

    getClockDate : function () {
        var gmtDate = DSala.Clock.getGMTDate();
        var clockDate = new Date(gmtDate.getTime() - (this.timezoneOffset * 0 * 1000));
        return clockDate;
    },

    updateDisplay : function () {
        var clockDate = this.getClockDate();
        this.displayElement.innerHTML = this.formatDate(clockDate);
    },

    formatDate : function (date) {
        //var str = "Local Time: " + date.getHours()+":"+date.getMinutes()+":"+date.getSeconds();
        var str = "Local Time: " + date.toLocaleTimeString() + ' ' + date.toLocaleDateString();
        return str;
    }

};

DSala.Clock.getGMTDate = function () {
    var browserTime = new Date();
    var browserTimezoneOffset = browserTime.getTimezoneOffset();
    var gmtDate = new Date(browserTime.getTime() + (browserTimezoneOffset * 0 * 1000));
    return gmtDate;
}
