if (!Object.keys) {
  Object.keys = function(obj) {
    var keys = new Array();
    for (k in obj) if (obj.hasOwnProperty(k)) keys.push(k);
      return keys;
  };
}

Timezone = $.Class({
  initialize: function() {
    this._calculateTimezone();
    this._initializeTimezoneData(); 
  },
  getTimezoneOffset: function() {
    var offset = this.baseTimezoneOffset;
    if(this.timezoneDST == '1') {
      offset++;
    }
    return offset;
  },
  getTimezoneName: function() {
    var keyName = this._generateTimezoneKey();
    return this.timezoneNames[keyName];
  },
  getTimezoneValue: function() {
    var keyName = this._generateTimezoneKey();
    return this.timezoneValues[keyName];
  },
  _calculateTimezone: function() {
    var rightNow = new Date();
    var jan1 = new Date(rightNow.getFullYear(), 0, 1, 0, 0, 0, 0);  // jan 1st
    var june1 = new Date(rightNow.getFullYear(), 6, 1, 0, 0, 0, 0); // june 1st
    var temp = jan1.toUTCString();
    var jan2 = new Date(temp.substring(0, temp.lastIndexOf(' ')-1));
    temp = june1.toUTCString();
    var june2 = new Date(temp.substring(0, temp.lastIndexOf(' ')-1));
    var std_time_offset = (jan1 - jan2) / (1000 * 60 * 60);
    var daylight_time_offset = (june1 - june2) / (1000 * 60 * 60);
    var dst;
    if (std_time_offset == daylight_time_offset) {
            dst = '0'; // daylight savings time is NOT observed
    } 
    else {
            // positive is southern, negative is northern hemisphere
            var hemisphere = std_time_offset - daylight_time_offset;
            if (hemisphere >= 0) {
                    std_time_offset = daylight_time_offset;
            }
            dst = '1'; // daylight savings time is observed
    }
    this.baseTimezoneOffset = std_time_offset;
    this.timezoneDST = dst;

  },
  _initializeTimezoneData: function() {
    this.timezoneNames = {
      '-10:00,0' : 'Hawaii',
      '-09:00,1' : 'Alaska',
      '-08:00,1' : 'Pacific',
      '-07:00,0' : 'Arizona',
      '-07:00,1' : 'Mountain',
      '-06:00,1' : 'Central',
      '-05:00,0' : 'Indiana',
      '-05:00,1' : 'Eastern',
      '-04:00,0' : 'Puerto Rico',
      '-04:00,1' : 'Atlantic',
      '-03:30,1' : 'Newfoundland'
    };
    this.timezoneValues = {
      '-10:00,0' : 'PST-2_nodst',
      '-09:00,1' : 'PST-1',
      '-08:00,1' : 'PST',
      '-07:00,0' : 'MST_nodst',
      '-07:00,1' : 'MST',
      '-06:00,1' : 'CST',
      '-05:00,0' : 'EST_nodst',
      '-05:00,1' : 'EST',
      '-04:00,0' : 'EST+1_nodst',
      '-04:00,1' : 'EST+1',
      '-03:30,1' : 'NST'
    };
  },
  _generateTimezoneKey: function() {
    var value = this.baseTimezoneOffset;
    var dst = this.timezoneDST;
    var hours = parseInt(value);
    value -= parseInt(value);
    value *= 60;
    var mins = parseInt(value);
    value -= parseInt(value);
    value *= 60;
    var secs = parseInt(value);
    var display_hours = hours;
    // handle GMT case (00:00)
    if (hours == 0) {
            display_hours = '00';
    } 
    else if (hours > 0) {
            // add a plus sign and perhaps an extra 0
            display_hours = (hours < 10) ? '+0'+hours : '+'+hours;
    } 
    else {
            // add an extra 0 if needed 
            display_hours = (hours > -10) ? '-0'+Math.abs(hours) : hours;
    }
    if(mins < 10) {
     mins = '0' + mins;
    }
    return display_hours+':'+mins+','+dst;
  }
});

