/*
Time Input 0.1
Requires jQuery version: 1.3.2

Copyright (c) 2009 Kevin Field, Brant Aero

This work is licensed under the Creative Commons Attribution
2.5 Canada License. To view a copy of this license, visit
http://creativecommons.org/licenses/by/2.5/ca/ or send a
letter to Creative Commons, 171 Second Street, Suite 300, San
Francisco, California, 94105, USA.

*/

TimeInput = (function($) { // Localise the $ function

function TimeInput(el, opts) {
	if (typeof(opts) != "object") opts = {};
	$.extend(this, TimeInput.DEFAULT_OPTS, opts);
	
	this.input = $(el);
	this.bindMethodsToObj("show", "hide", "hideIfClickOutside", "hideOnEscOrTab", "selectTime", "adjustTime");
	
	this.build();
	this.hide();
};
TimeInput.DEFAULT_OPTS = {
	am_pm: ['am','pm'],
	twelve_hour: true,
	minute_chunk: 15,
	start_hour: 8,
	end_hour: 17
};
TimeInput.prototype = {
	pad: function (sd) { return ((sd < 10)?('0' + sd.toString()):(sd.toString())); },
	build: function()
	{
		var table = '<table class="time_selector">';
		for (var hour = this.start_hour; hour <= this.end_hour; hour++)
		{
			table += '<tr>';
			for (var minute = 0; minute < 60; minute += this.minute_chunk)
			{
				table += '<td>';
				if (this.twelve_hour)
					table += (hour % 12 == 0 ? 12 : hour % 12) + ':' + this.pad(minute) + ' ' + (hour < 12 ? this.am_pm[0] : this.am_pm[1]);
				else
					table += this.pad(hour) + ':' + this.pad(minute);
				table += '</td>';
			}
			table += '</tr>';
		}
		table += '</table>';
		
		this.rootLayers = $(this.input).wrap(document.createElement('span'));
		this.timeSelector = $(table).insertAfter(this.input).click(this.bindToObj(function(event) {
			var time = event.target.textContent;
			if (this.input.val() != time)
				this.input.val(time).change();
			this.hide();
			return false;
		}));;

// Do we need this?  I'm not sure what it's a workaround for exactly.
//		 if ($.browser.msie && $.browser.version < 7) {
//			 this.ieframe = $('<iframe class="time_selector_ieframe" frameborder="0" src="#"></iframe>').insertBefore(this.timeSelector);
//			 this.rootLayers = this.rootLayers.add(this.ieframe);
//		 };
		
	},
	
	show: function() {
		this.timeSelector.css("display", "block");
		var offset = this.input.offset();
		this.timeSelector.css({
			top: offset.top + this.input.outerHeight(),
			left: offset.left
		});
		
// 		if (this.ieframe) {
// 			this.ieframe.css({
// 				width: this.timeSelector.outerWidth(),
// 				height: this.timeSelector.outerHeight()
// 			});
// 		};

		this.input.unbind("focus", this.show);
		$([window, document.body]).click(this.hideIfClickOutside).keypress(this.hideOnEscOrTab);
	},
	
	hide: function() {
		this.timeSelector.css("display", "none");
		$([window, document.body]).unbind("click", this.hideIfClickOutside).unbind("keypress", this.hideOnEscOrTab);
		this.input.focus(this.show);
	},
	
	hideIfClickOutside: function(event) {
		if (event.target != this.input[0] && !this.insideSelector(event)) {
			this.hide();
		};
	},
	
	hideOnEscOrTab: function(event) {
		if (event.keyCode == 27 || event.keyCode == 9) {
			this.hide();
		};
	},

	insideSelector: function(event) {
		var offset = this.timeSelector.offset();
		offset.right = offset.left + this.timeSelector.outerWidth();
		offset.bottom = offset.top + this.timeSelector.outerHeight();
		
		return event.pageY < offset.bottom &&
					 event.pageY > offset.top &&
					 event.pageX < offset.right &&
					 event.pageX > offset.left;
	},
	
	bindToObj: function(fn) {
		var self = this;
		return function() { return fn.apply(self, arguments) };
	},
	
	bindMethodsToObj: function() {
		for (var i = 0; i < arguments.length; i++) {
			this[arguments[i]] = this.bindToObj(this[arguments[i]]);
		};
	},
};

$.fn.time_input = function(opts) {
	return this.each(function() { new TimeInput(this, opts); });
};
$.time_input = { initialize: function(opts) {
	$("input.time_input").time_input(opts);
} };

return TimeInput;
})(jQuery); // End localisation of the $ function

