/*
 * jQuery jclock - Clock plugin - v 0.2.1
 * http://plugins.jquery.com/project/jclock
 *
 * Copyright (c) 2007-2008 Doug Sparling <http://www.dougsparling.com>
 * Licensed under the MIT License:
 *   http://www.opensource.org/licenses/mit-license.php
 */


(function($) {

  $.fn.jclock = function(options) {
    var version = '0.2.1';

    // options
    var opts = $.extend({}, $.fn.jclock.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.h_open = o.h_open;
      $this.m_open = o.m_open;
      $this.h_close = o.h_close;
      $this.m_close = o.m_close;*/

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

      $.fn.jclock.startClock($this,o.h_open,o.m_open,o.h_close,o.m_close);

    });
  };
       
  $.fn.jclock.startClock = function(el,h_open,m_open,h_close,m_close) {
    $.fn.jclock.stopClock(el);
    $.fn.jclock.displayTime(el,h_open,m_open,h_close,m_close);
  }
  $.fn.jclock.stopClock = function(el) {
    if(el.running) {
      clearTimeout(el.timerID);
    }
    el.running = false;
  }
  $.fn.jclock.displayTime = function(el,h_open,m_open,h_close,m_close) {
    var time = $.fn.jclock.getTime(el,h_open,m_open,h_close,m_close);
    el.html(time);
    el.timerID = setTimeout(function(){$.fn.jclock.displayTime(el,h_open,m_open,h_close,m_close)},1000);
  }
  $.fn.jclock.getTime = function(el,h_open,m_open,h_close,m_close) {
    var now = new Date();
    var hours, minutes, seconds;

    if(el.utc == true) {
      if(el.utc_offset != 0) {
        now.setUTCHours(now.getUTCHours()+el.utc_offset);
      }
      hours = now.getUTCHours();
      minutes = now.getUTCMinutes();
      seconds = now.getUTCSeconds();
    } else {
      hours = now.getHours();
      minutes = now.getMinutes();
      seconds = now.getSeconds();
    }

	var exchange="closed";
	if ((hours > h_open) && (hours < h_close)) {
		exchange="open";
	} else if ((hours == h_open) && (minutes>=m_open) ) {
		exchange="open";		
	} else if ((hours == h_close) && (minutes<m_close)) {
		exchange="open";		
	} else {
		exchange="closed";
	}
	
	/*(((seconds-1)%2))? exchange="open" : exchange="closed";*/
    
	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 {
      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.am_pm == true) ) {
     timeNow += am_pm_text;
    }
	timeNow='<span class="' + exchange + '">' + timeNow +  '</span>';
    return timeNow;
  };
       
  // plugin defaults
  $.fn.jclock.defaults = {
    timeNotation: '24h',
    am_pm: false,
    utc: false,
    fontFamily: '',
    fontSize: '',
    foreground: '',
    background: '',
    utc_offset: 0,
	h_open: 9,
	m_open: 0,
	h_close: 16,
	m_close: 30
  };

})(jQuery);
