(function($) {

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

			$this.offset = opts.offset;

			$.fn.servertime.startClock($this);
		});
  };
       
  $.fn.servertime.startClock = function(el) {
		$.fn.servertime.stopClock(el);
		$.fn.servertime.displayTime(el);
  }
  $.fn.servertime.stopClock = function(el) {
		if(el.running) {
			clearTimeout(el.timerID);
		}
		el.running = false;
  }
  $.fn.servertime.displayTime = function(el) {
		el.html($.fn.servertime.getTime(el));
		el.timerID = setTimeout(function(){$.fn.servertime.displayTime(el)}, 1000);
  }
  $.fn.servertime.getTime = function(el) {
		var month, day, year, hours, minutes, seconds, am_pm;
		
		var d = new Date();
		var now = new Date((d.getTime() + (d.getTimezoneOffset() * 60000)) + (3600000 * el.offset));
    
		month = now.getMonth();
		day = now.getDate();
		year = now.getFullYear();
    
		hours = now.getHours();
		minutes = now.getMinutes();
		seconds = now.getSeconds();

		am_pm = (hours >= 12) ? " PM" : " AM";
		hours = ((hours > 12) ? hours - 12 : hours);
		hours = ((hours <  10) ? "0" : "") + hours;

		minutes = ((minutes <  10) ? "0" : "") + minutes;
		seconds = ((seconds <  10) ? "0" : "") + seconds;

		return "<strong>Server Time: </strong>" + (month + 1) + "/" + day + "/" + year + " " + hours + ":" + minutes + ":" + seconds + am_pm + " (CST)";
	};

	$.fn.servertime.defaults = {
		offset: 0
	};
})(jQuery);
