(function($) {

  $.fn.jdate = function(options) {
    var version = '1.2.0';

    // options
    var opts = $.extend({}, $.fn.jdate.defaults, options);
         
    return this.each(function() {
      $this = $(this);
      $this.timerID = null;
      $this.running = false;

      var o = $.meta ? $.extend({}, opts, $this.data()) : opts;

      $this.timeNotation = o.timeNotation;
      $this.am_pm = o.am_pm;
      $this.utc = o.utc;
      $this.utc_offset = o.utc_offset;

      $this.css({
        fontFamily: o.fontFamily,
        fontSize: o.fontSize,
        backgroundColor: o.background,
        color: o.foreground
      });

      $.fn.jdate.startClock($this);

    });
  };
       
  $.fn.jdate.startClock = function(el) {
    $.fn.jdate.stopClock(el);
    $.fn.jdate.displayTime(el);
  }
  $.fn.jdate.stopClock = function(el) {
    if(el.running) {
      clearTimeout(el.timerID);
    }
    el.running = false;
  }
  $.fn.jdate.displayTime = function(el) {
    var time = $.fn.jdate.getTime(el);
    el.html(time);
    el.timerID = setTimeout(function(){$.fn.jdate.displayTime(el)},1000);
  }
  $.fn.jdate.getTime = function(el) {
    var now = new Date();
    var day, month, year;

    if(el.utc == true) {
      var localTime = now.getTime();
      var localOffset = now.getTimezoneOffset() * 60000;
      var utc = localTime + localOffset;
      var utcTime = utc + (3600000 * el.utc_offset);
      now = new Date(utcTime);
    }
    day = now.getDate();
    if(day<10){
    	day= '0' + day;
    }
    month = now.getMonth()+1;
    if(month<10){
    	month= '0' + month;
    }
    year = now.getFullYear();
    
    var timeNow = day + "/" + month + "/" + year;
    //var timeNow = now.getDate();
    return timeNow;
  };
       
  // plugin defaults
  $.fn.jdate.defaults = {
    timeNotation: '24h',
    am_pm: false,
    utc: false,
    fontFamily: '',
    fontSize: '',
    foreground: '',
    background: '',
    utc_offset: 0
  };

})(jQuery);

