/* jQuery timepicker
 * replaces a single text input with a set of pulldowns to select hour, minute, and am/pm
 *
 * Copyright (c) 2007 Jason Huck/Core Five Creative (http://www.corefive.com/)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) 
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * Version 1.0
 */

(function($){
	jQuery.fn.timepicker = function(){
		this.each(function(){
			// get the ID and value of the current element
			var i = this.id;
			var curr = $(this);
			var v = curr.val();
			var selectNum=0;
			var origSelectNum=0;
	
			// the options we need to generate
			var hrs = new Array('12','1','2','3','4','5','6','7','8','9','10','11');
			var mins = new Array('00','30');
			var ap = new Array('am','pm');
			
			// default to the current time
			var d = new Date;
			var h = d.getHours();
			var m = d.getMinutes();
			var p = (h >= 12 ? 'pm' : 'am');
			
			// adjust hour to 12-hour format
			if(h > 12) h = h - 12;
				
			// round minutes to nearest quarter hour
			for(mn in mins){
				if(m <= parseInt(mins[mn])){
					m = parseInt(mins[mn]);
					break;
				}
			}
			
			// increment hour if we push minutes to next 00
			if(m > 30){
				m = 0;
				
				switch(h){
					case(11):
						h += 1;
						p = (p == 'am' ? 'pm' : 'am');
						break;
						
					case(12):
						h = 1;
						break;
						
					default:
						h += 1;
						break;
				}
			}

			// override with current values if applicable
			if(v.length == 7){
				h = parseInt(v.substr(0,2));
				m = parseInt(v.substr(3,2));
				p = v.substr(5);
			}
			
			// build the new DOM objects
			var output = '';
			var tmp=0;
			output += '<ul id="p_' + i + '" class="p timepicker" size="10" style="display:none;">';				
			for(pp in ap){
				for(hr in hrs){
					for(mn in mins){
						output += '<li';
						if((parseInt(mins[mn]) == m) && (ap[pp] == p) && (parseInt(hrs[hr]) == h)){
							output += ' class="selected"';
							selectNum = tmp;
							origSelectNum = tmp;
							//curr.val(hrs[hr] + ":" + mins[mn] + ap[pp]);
						} 
						output += '><a href="#">' + hrs[hr] + ":" + mins[mn] + ap[pp] + '</a></li>';
						tmp++;
					}
				}
			}
			output += '</ul>';				
	
			// hide original input and append new replacement inputs
			curr.after(output).click(function(e){
				curr.next().css("top",curr[0].offsetTop+curr[0].offsetHeight + "px").show()[0].scrollTop = 24*(selectNum-2);
				e.stopPropagation();
				$("body").click(function(){
					curr.next().hide();
				});
			}).bind("keydown",function(){ curr.next().hide(); }).blur(function(){
				curr.val(curr.val().replace(/ /ig, ""));
				var validTime = (curr.val().search(/^(\b[01]?[0-9]:[0-5][0-9](am|pm)\b)$/i) >= 0) ? true : false;
				if(!validTime) validTime = ((curr.val().search(/^(\bStart\b)/i) >= 0)) ? true : false;
				if(!validTime) validTime = ((curr.val().search(/^(\bEnd\b)/i) >= 0)) ? true : false;
				
				if(!validTime){
					curr.val("Now");
					showMessage("Invalid Time","Alert");
					selectNum = origSelectNum;
                                        if($("#p_time").length > 0){
					$("#p_time li").removeClass("selected")[origSelectNum].className="selected";
				}
				}
			})
                        if($("#p_time").length > 0){
			$("#p_time")[0].scrollTop = 24*(selectNum-2);
                        }
			$("#p_"+i).css("left",curr[0].offsetLeft + "px").click(function(e){ e.stopPropagation(); }).find("a").click(function(){
				curr.val(this.innerHTML);
				
				$(this).parent().addClass("selected").siblings().removeClass("selected").end().parent().hide();
				
				selectNum = $("#p_time li").index($(this).parent());
				return false;
			});
			
		});
		
		return this;
	};
})(jQuery);



/* SVN: $Id: jquery.timepicker.js 456 2007-07-16 19:09:57Z Jason Huck $ */
