/*	
 *	jQuery Combobox with autocomplete plugin 1.0.3
 *	www.frebsite.nl
 *	Copyright (c) 2009 Fred Heusschen
 *	Licensed under the MIT license.
 *	http://www.opensource.org/licenses/mit-license.php
 */
jQuery(function($) {

	$.fn.combobox = function(settings) {
		var opts = $.extend({}, $.fn.combobox.defaults, settings);

		if (!$('#combo_overlay').length) {
			$('body').append('<div id="combobox_overlay"></div>');
			$('#combobox_overlay').unbind('click').click($.fn.combobox.sluitSelect);
		}

		return this.each(function(n) {
			var el = $(this);
			var id = el.attr('id');
			var nm = el.attr('name');

			var id_str = (typeof id != 'undefined') ? ' id="combobox_container_'+id+'"' : '';
			var is = (typeof el.attr('class') != 'undefined') ? el.attr('class') : '';
			id = (typeof id != 'undefined') ? ' id="'+id+'"' : '';
			nm = (typeof nm != 'undefined') ? ' name="'+nm+'"' : '';
			var nm_str = (typeof el.attr('name') != 'undefined') ? ' name="'+el.attr('name')+'"' : '';
			var tab_str = (typeof el.attr('tabindex') != 'undefined')	? ' tabindex="'+el.attr('tabindex')+'"'	: '';

			var cb='';

			if (opts.buttonText.length > 0) {	
				cb += '<a href="#" class="combobox_button"><span>'+opts.buttonText+'</span></a>';
			}

			cb += '<div class="combobox_for_images">';

			cb += '<div class="combobox_images_left"></div>';
			cb += '<div class="combobox_images_right"></div>';

			cb += '<div class="combobox_for_input">'
			cb += '<input class="combobox_input" type="text" value="'+$(':selected', el).text()+'"'+tab_str+' autocomplete="off" />';
			cb += '<input class="combobox_hidden" type="hidden"'+nm_str+''+id+' value="'+el.val()+'" />';
			cb += '</div>';

			cb += '</div>';

			cb += '<ul class="combobox_summary">';	
			$('option', el).each(function() {
				var t = $(this).html();
				var c = (typeof $(this).attr('class') != 'undefined') ? ' class="'+$(this).attr('class')+'"' : '';
				var v = $(this).attr('value');
				if (!v.length) v = t;
				cb += '<li'+c+'><a href="#'+v+'">'+t+'</a></li>';
			});
			cb += '</ul>';

			el.wrap('<div class="combobox_container '+is+'" '+id_str+'></div>');
			var di = el.parent();
			di.append(cb);
			el.remove();

			//selectie openen via button
			if (opts.showOnMouseOver) {
				$(di).mouseover(function() {
					$.fn.combobox.openSelectFromButton($(this), opts);
				}).click(function() {
					return false;
				});
			} else if (opts.buttonText.length > 0) {
				$('a.combobox_button', di).click(function() {
					if (opts.buttonToggle &&
						$('#combobox_overlay:visible').length
					) {
							$.fn.combobox.sluitSelect();
					} else	$.fn.combobox.openSelectFromButton($(this), opts);
					return false;
				});
			}

			//selectie sluiten na mouse-out
			if (opts.hideOnMouseOut) {
				$(di).mouseout(function() {
					$.fn.combobox.sluitSelect();
				});
			}
			
			//selectie openen via autocomplete
			$('input.combobox_input', di).keyup(function() {
				$('input.combobox_hidden', di).val($(this).val());
			});

			if (opts.autoComplete) {
				$('input.combobox_input', di).keyup(function() {
					var ul = $(this).parents('ul');
					var va = $(this).val().toLowerCase();
					var ar = new Array();
					if (va.length > 0) {
						$('li', ul).each(function() {
							var a = $(this).find('a')
							if (a.html().toLowerCase().indexOf(va) != -1 ||
								a[0].hash.substr(1).toLowerCase().indexOf(va) != -1) {
									$(this).show();
									ar.push(a[0].hash.substr(1));
							} else 	$(this).hide();
						});
					}
					$.fn.combobox.sluitSelect();
					if (ar.length > 0) $.fn.combobox.openSelect(ul, ar.length, opts);
					
					opts.callbackShowOptions(ar);
					return false;
				});
			}

			$('ul.combobox_summary li a', di).bind('click', {fan: opts.callbackSelectOption}, function(event) {
				var v = this.hash.substr(1);
				$('input.combobox_input', di).val($(this).html());
				$('input.combobox_hidden', di).attr('value', v);
				$.fn.combobox.sluitSelect();
				event.data.fan(v);
				return false;
			});
			
			$.fn.combobox.sluitSelect();
		});
	};

	$.fn.combobox.openSelectFromButton = function(btn, o) {
		var ul = btn.parent().find('ul');		
		$('li', ul).show();
		$.fn.combobox.sluitSelect();
		$.fn.combobox.openSelect(ul, ul.children('li').size(), o);
		var ar = new Array();
		$('li a', ul).each(function() {
			ar.push(this.hash.substr(1));
		});
		o.callbackShowOptions(ar);
	}

	$.fn.combobox.openSelect = function(el, aa, o) {
		el.show();
		var nh = (aa > o.maxVisibleOptions) ? (o.maxVisibleOptions * $('li:visible', el).height())+'px' : '';
		el.css('height', nh);
		el.scrollTop(0);
		$(el).parent().css('z-index', '200');
		$('#combobox_overlay').show();
	}

	$.fn.combobox.sluitSelect = function() {
		$('ul.combobox_summary').hide();
		$('#combobox_overlay').hide();
		$('ul.combobox_summary').parent().css('z-index', '0');
	}
	
	$.fn.combobox.defaults = {
		showOnMouseOver:	false,
		hideOnMouseOut:		false,
		autoComplete:		true,
		maxVisibleOptions:	1000000,
		buttonText:		'&nbsp;',
		buttonToggle:		true,
		callbackShowOptions:	function(arr) {},
		callbackSelectOption:	function(arr) {}
	}
	
	$.fn.makeCombobox = function(obj) {

		this.each(function(n) {
			var el = $(this);
			var id = $(this).attr('id');
			var list = obj.list;
			
			if ((typeof id != 'undefined') && (typeof list != 'undefined')) {
				var sz = list.length;
				if (sz) {
					var combobox = el.parents('.combobox_container');
					
					var select = $('<select></select>').attr('id', id).attr('name', id);
					if (typeof obj.className != 'undefined') select.addClass(obj.className);
					for (var i = 0; i < sz; ++i) {
						var child = $('<option value="'+i+'">'+list[i]+'</option>');
						select.append(child);
					}

					var parent = combobox.parent();
					combobox.remove();
					parent.prepend(select);
					
					if (typeof obj.selectOption != 'undefined') select.combobox({callbackSelectOption: obj.selectOption});
					else select.combobox();
				}
			}
		});
	}
});
