if (window.gadgets && gadgets.Prefs) {
  window.prefs = new gadgets.Prefs();
}
function get_pref() {
  if (window.prefs) return prefs.getString("cleartz");
  return window.location.hash;
}
function set_pref(newvalue) {
  if (window.prefs) return prefs.set("cleartz", newvalue);
  return window.location.hash = newvalue;
}
function hh2string(hh) {
  if (hh == 12) {
    return '12 noon';
  } else if (hh == 0) {
    return '12 am';
  } else if (hh < 12) {
    return hh + " am";
  } else {
    return (hh - 12) + " pm";
  }
}
hh2string_hash = [];
for (var x = 0; x < 24; x++) {
  hh2string_hash.push(hh2string(x));
}

function shrink_labels(jele) {
  var stdwidth = 120;
  if (jele.width() > stdwidth) {
    var new_fontsize = (stdwidth / jele.width()) * parseInt(jele.css('font-size'));
    jele.css('font-size', new_fontsize + "px");
  } else {
    jele.css('width', stdwidth + 'px');
  }
}

function print(dateobj, offset, label) {
  var div = jQuery('<ol style="float: left;"><h2>' + label + '</h2></ol>');
  if (jQuery.browser.msie) div.css('width', '130px');

  div.appendTo('#output');
  shrink_labels(jQuery('h2', div).css('height', (jQuery.browser.msie ? '60px' : '40px')));
  div.remove();
  setTimeout(function() {
    var last_dow = null;
    for (var offset = 0; offset < 24; offset++) {
      var dow = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"][dateobj.getDay()];
      var dow_css = "dow_" + (last_dow == dow ? 'same' : 'new');
      var hh = dateobj.getHours();
      var hh_css = "hh_" + hh;
      var new_li = jQuery('<li class="' + dow_css + ' ' + hh_css + '">' + 
        "<span class='dow " + dow_css + "'>" + dow + "</span>" + 
        " " + 
        "<span class='hh " + hh_css + "'>" + hh2string_hash[hh] + "</span>" + 
        "</li>").appendTo(div);
      var color = 255;
      if (hh > 18) {
        color = color - (hh - 18) * 20;
      } else if (hh < 4) {
        color = color - (8 - 0) * 20;
      } else if (hh < 8) {
        color = color - (8 - hh) * 20;
      }
      var rgb_color = 'rgb(' + color + ',' + color + ',' + color + ')';
      new_li.css('background', rgb_color);
      jQuery('span.dow_same', new_li).css('color', rgb_color); // IE opacity style not working
      last_dow = dow;
      dateobj.setHours(dateobj.getHours() + 1);
    }
    jQuery('<sup><a href="" title="Removes this column" class="hint">[-]</a></sup>').appendTo(jQuery('h2', div)).click(function(evt) {
      var delete_hash = ',' + jQuery('h2', div).html().replace(/\<.+$/, '');
      if (! get_pref().indexOf(delete_hash)) {
        delete_hash = ',' + jQuery('h2', div).html().replace(/GMT\+|GMT|\<.+$/, '');
      }
      set_pref( get_pref().replace(delete_hash, '') );
      div.hide(true);
      evt.stopPropagation();
      return false;
    });
    div.appendTo('#output');
  }, 100);
}

function parseNewOffset(newoff) {
  var newdt = new Date();
  var offset_amt = timezone_names_map[newoff];
  newdt.setHours(localhour + localoffset + offset_amt);
  print(newdt, offset_amt, newoff);
}

jQuery('#timezone_names').change(function(a, b, c) {
  var newoff = jQuery(this).val();
  set_pref( get_pref() + "," + newoff );
  parseNewOffset(newoff);
});

window.localtime = new Date();
window.localoffset = (localtime).getTimezoneOffset() / 60;
window.localhour = localtime.getHours();

var holder = ""; 
jQuery.each(timezone_names_array, function(index, array) {
  if (array) {
    holder = holder + ('<option value="' + array[1] + '">' + array[0] + '</option>');
  }
}); 
for (var x = -23; x < 24; x++) {
  if (timezone_names_map[x]) {
    holder = holder + ('<option value="' + timezone_names_map[x] + '">' + timezone_names_map[x] + '</option>');
  }
};
jQuery('#timezone_names').html(holder);

setTimeout(function() {
  var thistz = timezone_names_map["" + (-1 * (localtime).getTimezoneOffset() / 60)];
  jQuery('#timezone_names').val(thistz);
  if (get_pref() == "") set_pref( '#,' + thistz );
  jQuery.each(get_pref().split(/[\#\,]|,NaN/), function(index, newoff) {
    if (newoff != "") {
      if (newoff == parseInt(newoff)) {
        parseNewOffset(timezone_names_map[newoff]);
      } else {
        parseNewOffset(newoff);
      }
    }
  });
}, 500);

