(function($) {
	
	
	var timepicker_id = "timepicker";
	var hours = 24;
	var minutes_interval = 5;
	var active = {
		"val" : 0,
		"get" : function(){
			return this.val;
		},
		"set" : function(x){
			return this.val = x;
		}
	};
	var isIE6 = !$.support.opacity && !window.XMLHttpRequest;
	
	if (isIE6){
		var bodyOff_h = $("#content").height();
		var bodyOff_w = $("#content").width();
	}
	
	var def_h = function(x){
		var el =function(x){
			if (x<10){
				return "<li>0"+x+"</li>";	
			} else {
				return "<li>"+x+"</li>";
			}
			
		} 
		var list = "";
		for (y=0;y<x;y++){
			list += el(y);
		}
		return list;
	}
	
	var def_m = function(z){
		var el =function(x,z){
			if (x%z == 0){
				if (x<10){
					return "<li>:0"+x+"</li>";
				} else {
					return "<li>:"+x+"</li>";	
				}
					
			} else{
				return "";
			}
		} 
		var list = "";		
		for (y=0;y<60;y++){
			list += el(y,z);
		}
		return list;		
	}
	
	var timepicker_init = function(that, options){
		var timepicker_div = '<div id="timepicker"><div class="timepicker_desc"><span>'+options.hour+'</span><span>'+options.minute+'</span></div><ul id="timepicker_h"></ul><ul id="timepicker_m"></ul><div id="timepicker_save">'+options.save+'</div></div>';
		var parent = that.parent();
		var that_offset = that.offset();
		var that_width = that.width();
		var that_height = that.height();
		var temp = "";
		$("body").append(timepicker_div);
		if (isIE6){
			$("#timepicker").append('<iframe style="background:transparent; border:0 none; position:absolute; top:-1px; left:-1px;"></iframe>');
		}
		
		
		$("#timepicker_h").append(def_h(hours));
		$("#timepicker_m").append(def_m(minutes_interval));
		
		$("#"+timepicker_id).css({
			"left" :that_offset.left+"px",
			"top" :that_offset.top+that_height+7+"px"
		})
		if (that.val()!= ""){
			var getVal = that.val();
			getVal = getVal.split(":");
			$("#timepicker_h li").each(function(){
				if(getVal[0] == $(this).html()){
					$(this).addClass("selected");
				}
			});
			$("#timepicker_m li").each(function(){
				temp = $(this).html();
				temp = temp.replace(":","");
				if(getVal[1] == temp){
					$(this).addClass("selected");
				}
			});			
		}
		$("#"+timepicker_id).fadeIn("slow");
		$("#"+timepicker_id+" li").click(function(){
			var type = $(this).parent().attr("id");
			$("#"+type+" li").removeClass("selected");
			$(this).addClass("selected");
			var val = that.val();
			if (val.indexOf(":") == -1){
				val ="00:00";
				if(type == "timepicker_h"){
					val =$(this).html()+":00";
				} else {
					val ="00"+$(this).html();
				};
			} else {
				val = that.val();
				val = val.split(":");
				if(type == "timepicker_h"){
					val[0] = $(this).html();
				} else {
					val[1] = $(this).html();
				};			
				if (val[0] == ""){
					val[0] = $("#timepicker_h .selected").html();
				}
				
				if (val[1] == ""){
					val[1] = $("#timepicker_m .selected").html();
				}
				
				val = val[0]+":"+val[1];	
			}
			if (val.lastIndexOf("::") != -1){
				val = val.replace("::",":");
			}
			that.val(val);				
			
		});
		
		$("#timepicker_save").click(function(){
			timepicker_hide();
			active.set(0);
		});
		$("*").click(function(){

		});
	}
	
	var timepicker_hide = function(){
		$("#"+timepicker_id).remove();
	}
	
	$.fn.timepicker = function(options){
		
		return this.each(function(){
			$(this).focus(function(){
				if (active.val == 0){
					timepicker_init($(this), options);
					active.set(1);	
				}
				
			});		
		});
	};
})(jQuery);
