var TimeInput_windowloaded = false;
var TimeInput = Class.create(
{
	initialize: function(elem_id)
	{
		// Create observer for window clicks so that we can hide an open timeinput, ITIS
		Event.observe(window, 'load', function()
		{
			TimeInput_windowloaded = true;
		});
		this.initWindowClickHandler();

		// Create observers for each timeinput box and each HTML input that instantiates a timeinput box to override propagation of the window click observer, thereby not closing the timeinput when we click within the timeinput
		for (i = 0; i < $$('.timeinput').length; i++)
		{
			Event.stopObserving($$('.timeinput')[i], 'click', this.clickHandler);
			
			Event.observe($$('.timeinput')[i], 'click', this.clickHandler);
		}
	},
	
	initWindowClickHandler: function()
	{
		if (TimeInput_windowloaded)
		{
			Event.stopObserving($('body'), 'click', this.windowClickHandler);
			Event.observe($('body'), 'click', this.windowClickHandler);
			
		}else{
			window.setTimeout('TimeInputObj.initWindowClickHandler();', 250);
		}
	},
	
	open: function(elem_id)
	{
		if ($(elem_id+'_select'))
		{
			$(elem_id+'_select').show();
		}
	},
	
	close: function(elem_id)
	{
		elem_id = elem_id.replace('_select', '')+'_select';
		
		if ($(elem_id))
		{
			elem = $(elem_id);

			elem.hide();
		}
	},
	
	closeAll: function(exception_id)
	{
		exception_id = exception_id.replace('_select', '')+'_select';

		for (i = 0; i < $$('.timeinput').length; i++)
		{
			if (typeof($$('.timeinput')[i].id) != 'undefined' && $$('.timeinput')[i].id != exception_id)
			{
				this.close($$('.timeinput')[i].id);
			}
		}
	},
	
	windowClickHandler: function(e)
	{
		exception_id = Event.findElement(e).id;
		
		TimeInputObj.closeAll(exception_id);
	},
	
	clickHandler: function(e)
	{
		// This is a click into our time input, so stop propogation to anything below
		e.stopPropagation();
	},
	
	keyPressHandler: function(e, elem)
	{
		if (e.keyCode == 9)
		{
			// Tab key
			elem.focus();
		}
	},
	
	parseTime: function(elem_id)
	{
		entry = $F(elem_id).toLowerCase().replace(/ /g, '');

		if (entry == '') return '12:00am';

		parts = entry.split(':');

		if (entry.indexOf(':') == -1 || parts[0].indexOf('a') > -1 || parts[0].indexOf('p') > -1)
		{
			// Get suffix
			if (parseInt(parts[0]) > 12)
			{
				suffix = 'pm';
			}else{
				suffix = (parts[0].indexOf('p') > parts[0].indexOf('a') ? 'pm' : 'am');
			}

			// Parse for valid time
			hourstext = (parts[0].replace(/ /g, '') == '' ? 12 : (parseInt(parts[0]) < 1 || parseInt(parts[0]) > 24 ? 12 : (parseInt(parts[0]) > 12 ? parseInt(parts[0]) - 12 : parseInt(parts[0])))).toString();

			timetext = hourstext+':00'+suffix;
		}else{
			// Get suffix
			if (parseInt(parts[0]) > 12)
			{
				suffix = 'pm';
			}else{
				suffix = (parts[1].indexOf('p') > parts[1].indexOf('a') ? 'pm' : 'am');
			}

			// Parse for valid time
			hourstext = (parts[0].replace(/ /g, '') == '' ? 12 : (parseInt(parts[0]) < 1 || parseInt(parts[0]) > 24 ? 12 : (parseInt(parts[0]) > 12 ? parseInt(parts[0]) - 12 : parseInt(parts[0])))).toString();
			minutestext = (parseInt(parts[1]) < 0 ? 0 : (parseInt(parts[1]) > 59 ? 59 : parseInt(parts[1])));

			timetext = hourstext+':'+(minutestext < 10 ? '0' : '')+minutestext.toString().replace('NaN', '00')+suffix;
		}
		
		return timetext;
	}
});

TimeInputObj = new TimeInput;
