/*
 * jQuery kClock - Clock plugin - v 1.2.0
 * http://plugins.jquery.com/project/kClock
 *
 * Copyright (c) 2007-2008 Doug Sparling <http://www.dougsparling.com>
 * Licensed under the MIT License:
 *   http://www.opensource.org/licenses/mit-license.php
 */

/*
<script type="text/javascript">
$(function($) {
   var options = {
        timeNotation: '24h',
        am_pm: false,
        fontFamily: 'Verdana',
        fontSize: '11px',
        foreground: '#FFFFFF',
        background: ''
      }; 
   $('.clocl').kClock(options);
});
</script>
*/

(function($) {

  $.fn.kClock = function(options) {

    // options
    var opts = $.extend({}, $.fn.kClock.defaults, options);
         
    return this.each(function() {
      jThis = $(this);
      jThis.timerID = null;
      jThis.running = false;

      var o = $.meta ? $.extend({}, opts, jThis.data()) : opts;

      jThis.timeNotation = o.timeNotation;
      jThis.am_pm = o.am_pm;
      jThis.utc = o.utc;
      jThis.utc_offset = o.utc_offset;

      jThis.css({
        fontFamily: o.fontFamily,
        fontSize: o.fontSize,
        backgroundColor: o.background,
        color: o.foreground
      });

      $.fn.kClock.startClock(jThis);

    });
  };
       
  $.fn.kClock.startClock = function(el) {
    $.fn.kClock.stopClock(el);
    $.fn.kClock.displayTime(el);
  }
  $.fn.kClock.stopClock = function(el) {
    if(el.running) {
      clearTimeout(el.timerID);
    }
    el.running = false;
  }
  $.fn.kClock.displayTime = function(el) {
    var time = $.fn.kClock.getTime(el);
    el.html(time);
    el.timerID = setTimeout(function(){$.fn.kClock.displayTime(el)},1000);
  }
  $.fn.kClock.getTime = function(el) {
    var now = new Date();
    var hours, minutes, seconds;

    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);
    }
    hours = now.getHours();
    minutes = now.getMinutes();
    seconds = now.getSeconds();
	day = 100+parseInt(now.getDate());
	day = day.toString().substr(1);
	
	weekDay = now.getDay();
	
	month = now.getMonth()+1;
	year = now.getFullYear();

    var am_pm_text = '';
    (hours >= 12) ? am_pm_text = " P.M." : am_pm_text = " A.M.";

    if (el.timeNotation == '12h') {
      hours = ((hours > 12) ? hours - 12 : hours);
    } else if (el.timeNotation == '12hh') {
      hours = ((hours > 12) ? hours - 12 : hours);
      hours   = ((hours <  10) ? "0" : "") + hours;
    } else {
      hours   = ((hours <  10) ? "0" : "") + hours;
    }

    minutes = ((minutes <  10) ? "0" : "") + minutes;
    seconds = ((seconds <  10) ? "0" : "") + seconds;

    var timeNow = hours + ":" + minutes + ":" + seconds;
    if ( (el.timeNotation == '12h' || el.timeNotation == '12hh') && (el.am_pm == true) ) {
     timeNow += am_pm_text;
    }

    return "CET "+day+"/"+month+"/"+year+" "+timeNow;
  };
       
  // plugin defaults
  $.fn.kClock.defaults = {
    timeNotation: '24h',
    am_pm: false,
    utc: false,
    fontFamily: '',
    fontSize: '',
    foreground: '',
    background: '',
    utc_offset: 0
  };

})(jQuery);

