(function($) {

    // follows are copied from datePicker/date.js
    // utility method
  var _zeroPad = function(num) {
      var s = '0'+num;
      return s.substring(s.length-2)
      //return ('0'+num).substring(-2); // doesn't work on IE :(
  };

  var Y = 0; // Years
  var O = 1; // Months
  var W = 2; // Weeks
  var D = 3; // Days
  var H = 4; // Hours
  var M = 5; // Minutes
  var S = 6; // Seconds

  function _calculatePeriods(until)
  {
		// Find endpoints
		var now = new Date();
    now.setMilliseconds(0);
		var until = new Date(until.getTime());

		// Calculate differences by period
		var periods = [0, 0, 0, 0, 0, 0, 0];

    var diff = Math.floor((until.getTime() - now.getTime()) / 1000);
    if (until.getTime() < now.getTime())
    {
      diff = Math.floor((now.getTime() - until.getTime()) / 1000);
    }
		
    var extractPeriod = function(period, numSecs)
    {
			periods[period] = Math.floor(diff / numSecs);
			diff -= periods[period] * numSecs;
		};
    extractPeriod(W, 604800);
		extractPeriod(D, 86400);
		extractPeriod(H, 3600);
		extractPeriod(M, 60);
		extractPeriod(S, 1);
		return periods;
	}
  
  function displayPeriod(value)
  {
    
  }

  $.timezones = function()
  {
    $(".date .gmt").each(function (i) {
            var s = $(this).text();
            var f = this.id; //format
            if (! f) {
                f = 'yyyy-mm-dd hh:ii:ss';
            }
            
            var d = new Date(1997, 1, 1, 1, 1, 1);
            var iY = f.indexOf('yyyy');
            if (iY > -1) {
                d.setFullYear(Number(s.substr(iY, 4)));
            }
            var iM = f.indexOf('mm');
            if (iM > -1) {
                d.setMonth(Number(s.substr(f.indexOf('mm'), 2)) - 1);
            }
            d.setDate(Number(s.substr(f.indexOf('dd'), 2)));    
            d.setHours(Number(s.substr(f.indexOf('hh'), 2)));
            d.setMinutes(Number(s.substr(f.indexOf('ii'), 2)));
            d.setSeconds(Number(s.substr(f.indexOf('ss'), 2)));
            
            var timezoneOffset = -(new Date().getTimezoneOffset());
            d.setMinutes(d.getMinutes() + timezoneOffset);

            //Display stuff
            var months = new Array(12);
            months[0] = "Jan";
            months[1] = "Feb";
            months[2] = "Mar";
            months[3] = "Apr";
            months[4] = "May";
            months[5] = "Jun";
            months[6] = "Jul";
            months[7] = "Aug";
            months[8] = "Sep";
            months[9] = "Oct";
            months[10] = "Nov";
            months[11] = "Dec";

            localTimeFormat = 'hh:ii';
            var localTime = localTimeFormat
                .split('hh').join(_zeroPad(d.getHours()))
                .split('ii').join(_zeroPad(d.getMinutes()));
            $(this).parent().children('.localTime').text(localTime);

            var hours = d.getHours();
            var ampm = 'am';
            if (hours >= 12)
            {
              ampm = 'pm';
            }
            if (hours == 0)
              hours = 12;
            if (hours > 12)
            {
              hours = hours - 12;
            }
            var localTimeAMPM = localTimeFormat
                .split('hh').join(hours)
                .split('ii').join(_zeroPad(d.getMinutes()))
                 + ampm;
            $(this).parent().children('.timeAMPM').text(localTimeAMPM);

            var day = months[d.getMonth()] + ' ' + d.getDate();
            if (d.getMonth() == (new Date()).getMonth() && d.getDate() == new Date().getDate())
            {
              day = 'Today';
            }
            else if (d.getMonth() == ((new Date()).addDays(-1)).getMonth() && d.getDate() == ((new Date()).addDays(-1)).getDate())
            {
              day = 'Yesterday';
            }
            else if (d.getFullYear() != (new Date()).getFullYear())
            {
              day = months[d.getMonth()] + ' ' + d.getDate() + ' ' + d.getFullYear();
            }
            var localPostTime = day + ' at ' + localTimeAMPM;
            $(this).parent().children('.localPostTime').text(localPostTime);

            var periods = _calculatePeriods(d);
            var beforeText = '';
            if (d < new Date())
            {
              beforeText = '-';
              $(this).parent().parent('.negative').removeClass('noShow');
              $(this).parent().parent('.positive').addClass('noShow');
            }
            else
            {
              $(this).parent().parent('.negative').addClass('noShow');
              $(this).parent().parent('.positive').removeClass('noShow');
            }
            //Second countdown
            $(this).parent().children('.minuteCountdown').text(beforeText+
              (periods[W] > 0 ? periods[W]+'w ' : '')+    
              (periods[D] > 0 ? periods[D]+'d ' : '')+  
              (periods[H] > 0 ? periods[H]+'h ' : '')+
              periods[M]+'m ' +
              (periods[D] >= 1 ? '('+localPostTime+')' : '')
            );

            $(this).parent().children('.secondCountdown').text(beforeText+
              (periods[W] > 0 ? periods[W]+'w ' : '')+    
              (periods[D] > 0 ? periods[D]+'d ' : '')+  
              (periods[H] > 0 ? periods[H]+'h ' : '')+
              periods[M]+'m ' +
              periods[S]+'s '
            );
       } );
    return this;
  }

  $(document).ready(function() { $.timezones(); });
})(jQuery);