(function($){
	$.fn.combobox = function(options){
		var defaults = {
			defaultText: '---',
			maxHeight: 200
		};
		var opts = $.fn.extend(defaults, options);

		function _generateDropdownList(selectElement, searchString){
			var list = '';
			var searchPattern = searchString.toUpperCase();
			$(selectElement).children('option').each(function(index, optionElement){
				if(searchString != ""){
					if(optionElement.innerHTML.toUpperCase().indexOf(searchPattern) != 0){
						return;
					}
				}
				list += '<li selectindex="' + index + '">' + optionElement.innerHTML + '</li>';
			});

			return list;
		};

		function _showDropDown(uniqueID, list){
			var comboPos = $('#combobox-' + uniqueID).offset();
			var comboHeight = $('#combobox-' + uniqueID).height();
			var jsp = $('#combobox-' + uniqueID + '-dropdown .combobox-jsp');
			var ddList = $('#combobox-' + uniqueID + '-dropdown ul');

			ddList.html(list);
			$('#combobox-' + uniqueID + '-dropdown')
				.css('left', comboPos.left + 'px')
				.css('top', (comboPos.top + comboHeight) + 'px')
				.show();

			if(ddList.height() >= opts.maxHeight){
				jsp.height(opts.maxHeight);
			}else{
				jsp.height(ddList.height());
			}
			jsp.data('jsp').reinitialise();
		};

		return this.each(function(index, element){
			var uniqueID = index + '-' + new Date().valueOf();
			var searchInput = $('<input type="text" class="combobox-search" autocomplete="false" />');
			var dropdown = $('<div id="combobox-' + uniqueID + '-dropdown" class="combobox-dropdown" style="position: absolute; display: none;"><div class="combobox-dropdown-inner"><div class="combobox-jsp" style="max-height: ' + opts.maxHeight + 'px;"><ul class="combobox-dropdown-list"><li></li></ul></div></div></div>');
			var button = $('<a tabindex="' + (index+1) + '" class="combobox-button"></a>');
			var selectedOption = $(element).children('option[selected=selected]');
			var keyUpDelayTimer;
			var mouseOver = false;

			//console.log($(element).children('option:selected'));

			if(selectedOption.size() > 0){
				searchInput.val(selectedOption.html());
			}else{
				searchInput.val(opts.defaultText);
			}

			$(element).hide();

			searchInput.keyup(function(ev){
				clearInterval(keyUpDelayTimer);
				keyUpDelayTimer = setInterval(function(){
					_showDropDown(uniqueID, _generateDropdownList(element, ev.currentTarget.value));
					clearInterval(keyUpDelayTimer);
				}, 400);
			});

			function onInputBlur(){
				if(!mouseOver){
					$(dropdown).hide();
				}
			}

			searchInput.focus(function(ev){
				if($(this).val() == opts.defaultText){
					$(this).val('');
				}
			});

			searchInput.blur(function(ev){
				onInputBlur();
				if($(this).val() == ''){
					$(this).val(opts.defaultText);
				}
			});

			button.blur(function(ev){
				onInputBlur();
			});

			searchInput.focus(function(ev){
				if(ev.currentTarget.value != ''){
					_showDropDown(uniqueID, _generateDropdownList(element, ev.currentTarget.value));
				}
			});

			dropdown.mouseenter(function(ev){
				mouseOver = true;
			});

			dropdown.mouseleave(function(ev){
				mouseOver = false;
			});

			dropdown.click(function(ev){
				if(ev.target.nodeName.toUpperCase() == 'LI'){
					$('#combobox-' + uniqueID + ' select')[0].selectedIndex = $(ev.target).attr('selectindex');
					$('#combobox-' + uniqueID + ' input.combobox-search').val(ev.target.innerHTML);
				}
				if(!($(ev.target).hasClass('jspDrag') || $(ev.target).hasClass('jspTrack'))){
					$(dropdown).hide();
				}
				//console.log(ev.target);

			});

			button.click(function(ev){
				if(dropdown.is(':visible')){
					dropdown.hide();
				}else{
					_showDropDown(uniqueID, _generateDropdownList(element, ''));
				}
			});

			$(element).wrap('<div id="combobox-' + uniqueID + '" class="combobox"></div>');
			$(element).parent()
				.append(searchInput)
				.append(button);
			$('body').append(dropdown);

			$('#combobox-' + uniqueID + '-dropdown .combobox-jsp').jScrollPane();
			//console.log($('#combobox-' + uniqueID + '-dropdown ul'));
			//dropdown.css("left", $('#combobox-' + uniqueID).);
			//$(element).parent().append(_dropdownHTML(element));
		});
	}
})(jQuery);

