//"use strict";

(function ($) {
	var d = new Date();
	var hours = d.getHours() < 10 ? "0" + d.getHours() : d.getHours();
	var minutes = d.getMinutes() < 10 ? "0" + d.getMinutes() : d.getMinutes();

	$.fn.cmTime = function (options) {

		var opts = $.extend({}, $.fn.cmTime.defaults, options);

		if(opts.styleType === 'default') {
			opts.style = $.fn.cmTime.defaultStyle;
		} else if(opts.styleType === 'reversed') {
			opts.style = $.fn.cmTime.defaultStyleReversed;
		}

		return this.each(function () {
			_this = this;

			if($(_this).data('cmTime') === 'on'){
				return;
			}

			if(opts.dropDown)
			{
				$(_this).wrap('<div style="position: relative; display: inline-block;" />');

				var txt = "";
				for (var i = 0; i < 24; i++) {
					if (i < 10) {
						txt += "<div class='timeValues" + $(_this).attr('name') + "' data-value='" + i + "'>0" + i + ":00</div>";
						txt += "<div class='timeValues" + $(_this).attr('name') + "'>0" + i + ":30</div>";
					} else {
						txt += "<div class='timeValues" + $(_this).attr('name') +"' data-value='" + i + ":00'>" + i + ":00</div>";
						txt += "<div class='timeValues" + $(_this).attr('name') +"'>" + i + ":30</div>";
					}
				}
				var pos = $(this).position();

				$("<div class='timeSelection' id='customSelection" + $(_this).attr('name') + "'/>")
					.html(txt)
					.css(opts.style)
					.insertAfter($(this)).hide();

				$('.timeValues' + $(_this).attr('name') ).on('click', function (event) {
					$(_this).val($(this).text());
					$('#customSelection' + $(_this).attr('name')).hide();
					$(_this).trigger('change');
				});

				$('html').on('click', function (event) {
					if (_this != event.target) {
						$('.timeSelection').hide();
					}
				});
			}

			$(_this).attr('maxlength', opts.timesize)
			.val(opts.defaultTime)
			.data('cmTime', 'on')
			.keydown(

			function (event) {
				if ($.inArray(event.keyCode, $.fn.cmTime.keyNumbers) < 0) {
					event.preventDefault();
				}
				if ($(this).val().length == 2 && event.which != 8) {
					$(this).val(($(this).val() + ':'));
				}
			})
			.blur(function () {
				if($(this).val().length == 5)
				{
					var arr = $(this).val().split(':');
					var correct = false;
					if (arr.length != 2) {
						correct = false;
					} else {
						if (arr[0] == 24) {
							arr[0] = "00";
						}
						if (arr[1] == 60) {
							arr[1] = "00";
						}
						correct = (arr[0] >= 0 && arr[0] < 24) && (arr[1] >= 0 && arr[1] < 60) ? true : false;
					}
					if (!correct) {
						$(this).val('');
					} else {
						$(this).val(arr.join(':'));
					}
				}
				else
				{
					$(this).val('');
				}
			})
			
			.on('click', function (event) {
				if (_this != event.target) { $('.timeSelection').hide(); }
				_this = event.target;
				$('#customSelection' + $(_this).attr('name')).toggle();
				if($(_this).is(':visible'))
				{
                    var index = ($('.timeValues' + $(_this).attr('name') + ':eq(0)').outerHeight() * $(_this).val().split(':')[0] * 2);
                     $('#customSelection' + $(_this).attr('name')).scrollTop(index);
                }
                $(this).select();
			});
		});
	};

	$.fn.cmTime.defaults = {
		defaultTime: (hours + ":" + minutes),
		timesize: '5',
		dropDown: true,
		styleType: 'default',
		style: {}
	};

	$.fn.cmTime.defaultStyle = {
		'background-color': 'white',
		'z-index': 1000,
		height: '75px',
		width: '60px',
		position: 'absolute',
		overflow: 'auto',
		border: '1px solid black',
		'text-align': 'center',
		top: '23px'
	}

	$.fn.cmTime.defaultStyleReversed = {
		'background-color': 'white',
		'z-index': 1000,
		height: '75px',
		width: '60px',
		position: 'absolute',
		overflow: 'auto',
		border: '1px solid black',
		'text-align': 'center',
		bottom: '23px'
	}

	$.fn.cmTime.keyNumbers = [8, 9, 35, 36, 37, 39, 46, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105];

				

	function debug($obj) {
		if (window.console && window.console.log) {
			window.console.log('cmTime count: ' + $obj.size());
		}
	}
})(jQuery);
