﻿if (!MKW) {
    MKW = {};
}

MKW.TimeZone = {
    arrList: [-11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, +0, +1, +2, +3, +4, +5, +6, +7, +8, +9, +10, +11, +12],
    elementId: null,
    localTZ: 0,
    selectedTZ: 0,
    init: function(elId) {
        this.elementId = elId;
        this.timeZoneChanged = new YAHOO.util.CustomEvent("timeZoneChanged", this);
        var tz_date = new Date();
        tz_date = tz_date.getTimezoneOffset() / -60;
        tz_date = new Number(tz_date);
        this.localTZ = tz_date.toFixed(0);

        var tz = getCookie("TimeZone");
        if (!tz) {
            this.selectedTZ = this.localTZ;
        } else {
            this.selectedTZ = tz;
        }
        var self = this;
        self.writeTZcombo();
        window.setInterval(function() { self.writeTZcombo(); }, 60000);
    },
    writeTZcombo: function() {
        var selectElement = document.getElementById(this.elementId);
        YAHOO.util.Event.addListener(selectElement, "change", this.changeTZ);
        if (selectElement) {
            selectElement.innerHTML = "";
        }
        for (var i = 0; i < this.arrList.length; i++) {
            var localTime = new Date();
            var localTZ = localTime.getTimezoneOffset() / -60;
            localTime.setHours(localTime.getHours() - localTZ + this.arrList[i]);
            var option = new Option(localTime.toString() + " - GMT " + (this.arrList[i] > 0 ? "+" : "") + this.arrList[i], this.arrList[i]);
            if (this.arrList[i] == this.selectedTZ) {
                option.selected = true;
            }
            selectElement.options.add(option);
        }
    },
    changeTZ: function(e) {
        var dExpiration = new Date();
        dExpiration.setTime(dExpiration.getTime() + (24 * 3600 * 1000)); //90 dakika
        setCookie("TimeZone", this.value, dExpiration);
        MKW.TimeZone.selectedTZ = this.value;
        MKW.TimeZone.timeZoneChanged.fire();
    }

};