/* Copyright (c) 2009 José Joaquín Núñez (josejnv@gmail.com) http://joaquinnunez.cl/blog/
 * Licensed under GPL (http://www.opensource.org/licenses/gpl-2.0.php)
 * Use only for non-commercial usage.
 *
 * Version : 0.1
 *
 * Requires: jQuery 1.2+
 */

(function(jQuery)
{
  jQuery.fn.clock = function(network)
  {
	$(this).each(function(k, v) {
		var _this = v;
		var opts = null
		
		/*
		{
		  "tz": "America\/Chicago", 
		  "hour": 2, 
		  "datetime": "Wed, 31 Aug 2011 02:36:32 -0500", 
		  "second": 32, 
		  "error": false, 
		  "minute": 36
		}
		*/
		var offset = $(_this).attr('offset')
		var type = $(_this).attr('type')
		var timezone = $(_this).attr('timezone')
		if (network && timezone) {
			$.getJSON("http://json-time.appspot.com/time.json?tz="+timezone+"&callback=?", function(data) {
				if(!data.error) {
					offset = parseInt(data.datetime.split(" ")[5], 10) * 0.01
					//alert(timezone+" "+offset)
				}
			});
		}
		
		jQuery.runClock(_this, {
			offset: offset, 
			type: type
		})
	});
  }
})(jQuery);

jQuery.runClock = function(_this, options) {
	var defaults = {
	  offset: '+0',
	  type: 'string' //analog, digital, string
	};
	
	var opts = jQuery.extend(defaults, options);
	
	var timeArr = [0,0,0];

    setInterval( function() {
      var seconds = jQuery.calcTime(opts.offset).getSeconds();
      if(opts.type=='analog')
      {
        var sdegree = seconds * 6;
        var srotate = "rotate(" + sdegree + "deg)";
        jQuery(_this).find(".sec").css({"-moz-transform" : srotate, "-webkit-transform" : srotate});
      }
      else if(opts.type=='digital')
      {
        jQuery(_this).find(".sec").html(seconds);
      }
	  else 
	  {
		timeArr[2] = (100+seconds).toString().substr(1)
		jQuery(_this).text(timeArr.join(':'))
	  }
    }, 1000 );

    setInterval( function() {
      var hours = jQuery.calcTime(opts.offset).getHours();
      var mins = jQuery.calcTime(opts.offset).getMinutes();
      if(opts.type=='analog')
      {
        var hdegree = hours * 30 + (mins / 2);
        var hrotate = "rotate(" + hdegree + "deg)";
        jQuery(_this).find(".hour").css({"-moz-transform" : hrotate, "-webkit-transform" : hrotate});
      }
      else if(opts.type=='digital')
      {
        jQuery(_this).find(".hour").html(hours+':');
      }
	  else 
	  {
		timeArr[0] = (100+hours).toString().substr(1)
		jQuery(_this).text(timeArr.join(':'))
	  }
      var meridiem = hours<12?'AM':'PM';
      jQuery(_this).find('.meridiem').html(meridiem);
    }, 1000 );

    setInterval( function() {
      var mins = jQuery.calcTime(opts.offset).getMinutes();
      if(opts.type=='analog')
      {
        var mdegree = mins * 6;
        var mrotate = "rotate(" + mdegree + "deg)";        
        jQuery(_this).find(".min").css({"-moz-transform" : mrotate, "-webkit-transform" : mrotate});                
      }
      else if(opts.type=='digital')
      {
        jQuery(_this).find(".min").html(mins+':');
      }
	  else 
	  {
		timeArr[1] = (100+mins).toString().substr(1)
		jQuery(_this).text(timeArr.join(':'))
	  }
    }, 1000 );
}

jQuery.calcTime = function(offset) {

  // create Date object for current location
  d = new Date();

  // convert to msec
  // add local time zone offset
  // get UTC time in msec
  utc = d.getTime() + (d.getTimezoneOffset() * 60000);

  // create new Date object for different city
  // using supplied offset
  nd = new Date(utc + (3600000*offset));

  // return time as a string
  return nd;
};



