/*
 * jQuery selectbox plugin
 *
 * Copyright (c) 2007 Sadri Sahraoui (brainfault.com)
 * Licensed under the GPL license and MIT:
 *   http://www.opensource.org/licenses/GPL-license.php
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * The code is inspired from Autocomplete plugin (http://www.dyve.net/jquery/?autocomplete)
 *
 * Revision: $jId$j
 * Version: 0.5
 * 
 * Changelog :
 *  Version 0.5 
 *  - separate css style for current selected element and hover element which solve the highlight issue 
 *  Version 0.4
 *  - Fix width when the select is in a hidden div   @Pawel Maziarz
 *  - Add a unique id for generated li to avoid conflict with other selects and empty values @Pawel Maziarz
 */
jQuery.fn.extend({
	selectbox: function(options) {
		return this.each(function() {
			new jQuery.SelectBox(this, options);
		});
	}
});


/* pawel maziarz: work around for ie logging */
if (!window.console) {
	var console = {
		log: function(msg) { 
	 }
	}
}
/* */

jQuery.SelectBox = function(selectobj, options) {
	
	var opt = options || {};
	opt.inputClass = opt.inputClass || "selectbox";
	opt.containerClass = opt.containerClass || "selectbox-wrapper";
	opt.hoverClass = opt.hoverClass || "current";
	opt.currentClass = opt.selectedClass || "selected"
	opt.debug = opt.debug || false;
	
	var elm_id = selectobj.id;
	var active = -1;
	var inFocus = false;
	var hasfocus = 0;
	//jquery object for select element
	var $jselect = $j(selectobj);
	// jquery container object
	var $jcontainer = setupContainer(opt);
	//jquery input object 
	var $jinput = setupInput(opt);
	// hide select and append newly created elements
	$jselect.hide().before($jinput).before($jcontainer);
	
	// detect changes in the underlying selectbox and repopulate
	$j(selectobj).bind('change', function() {
		$jcontainer.empty();
		init();
	});
	
	init();
	
	$jinput
	.click(function(){
		// rebuild from underlying control in case data changed
		$jcontainer.empty();
		init();
		$jcontainer.show();
		if (!inFocus) {
			$jcontainer.toggle();
		}
	})
	.focus(function(){
	   if ($jcontainer.not(':visible')) {
	       inFocus = true;
	       $jcontainer.show();
	   }
	})
	.keydown(function(event) {	   
		switch(event.keyCode) {
			case 38: // up
				event.preventDefault();
				moveSelect(-1);
				break;
			case 40: // down
				event.preventDefault();
				moveSelect(1);
				break;
			//case 9:  // tab 
			case 13: // return
				event.preventDefault(); // seems not working in mac !
				$j('li.'+opt.hoverClass).trigger('click');
				break;
			case 27: //escape
			  hideMe();
			  break;
		}
	})
	.blur(function() {
		if ($jcontainer.is(':visible') && hasfocus > 0 ) {
			if(opt.debug) console.log('container visible and has focus')
		} else {
			hideMe();	
		}
	});


	function hideMe() { 
		hasfocus = 0;
		$jcontainer.hide(); 
	}
	
	function init() {
		$jcontainer.append(getSelectOptions($jinput.attr('id'))).hide();
		var width = $jinput.css('width');
		$jcontainer.width(width);
    }
	
	function setupContainer(options) {
		var container = document.createElement("div");
		$jcontainer = $j(container);
		$jcontainer.attr('id', elm_id+'_container');
		$jcontainer.addClass(options.containerClass);
		
		return $jcontainer;
	}
	
	function setupInput(options) {
		var input = document.createElement("input");
		var $jinput = $j(input);
		$jinput.attr("id", elm_id+"_input");
		$jinput.attr("type", "text");
		$jinput.addClass(options.inputClass);
		$jinput.attr("autocomplete", "off");
		$jinput.attr("readonly", "readonly");
		$jinput.attr("tabIndex", $jselect.attr("tabindex")); // "I" capital is important for ie
		return $jinput;	
	}
	
	function moveSelect(step) {
		var lis = $j("li", $jcontainer);
		if (!lis) return;

		active += step;

		if (active < 0) {
			active = 0;
		} else if (active >= lis.size()) {
			active = lis.size() - 1;
		}

		lis.removeClass(opt.hoverClass);

		$j(lis[active]).addClass(opt.hoverClass);
	}
	
	function setCurrent() {	
		var li = $j("li."+opt.currentClass, $jcontainer).get(0);
		var ar = (''+li.id).split('_');
		var el = ar[ar.length-1];
		$jselect.val(el);
		//$jinput.val($j(li).html());

		//gets span text and sets that to display
		$jinput.val(jQuery.trim($j(li).children('span').text()));
		
		return true;
	}
	
	// select value
	function getCurrentSelected() {
		return $jselect.val();
	}
	
	// input value
	function getCurrentValue() {
		return $jinput.val();
	}
	
	function getSelectOptions(parentid) {
		var select_options = new Array();
		var ul = document.createElement('ul');
		$jselect.children().each(function() {
			
			if(this.tagName == 'OPTGROUP') {
				var id = parentid + '_' + $j(this).attr('label');
				
				// create optgroup header
				var li = document.createElement('li');
				li.setAttribute('id', id);
				li.innerHTML = '<span class="' + $j(this).attr('class') + '">' + $j(this).attr('label') + "</span>";
				ul.appendChild(li);
				
				// create children under optgroup
				$j(this).children().each(function() {
					var child_id = id + '_' + $j(this).val();
					//get image path from option title attribute
					var $jimgPath = $j(this).attr('title');
					
					var li = document.createElement('li');
					
					li.setAttribute('id', child_id);
					//li.innerHTML = $j(this).html();
					
					//adds image before the text
					//also adds a span around the text
					li.innerHTML = "<img src='" + $jimgPath + "' width='16' height='16' border='0' alt='' />"  + 
								   '<span class="' + $j(this).attr('class') + '">'  + $j(this).html() + "</span>";
								   
					if ($j(this).is(':selected')) {
						$jinput.val($j(this).html());
						$j(li).addClass(opt.currentClass);
					}
					ul.appendChild(li);
					createOption(li);
				});
			} else {
				var id = parentid + '_' + $j(this).val();
				
				//get image path from option title attribute
				var $jimgPath = $j(this).attr('title');
				var li = document.createElement('li');
				
				li.setAttribute('id', id);
				//li.innerHTML = $j(this).html();
				
				//adds image before the text
				//also adds a span around the text
				li.innerHTML = "<img src='" + $jimgPath + "' width='16' height='16' border='0' alt='' /> <span>" + $j(this).html() + "</span>";
				
				if ($j(this).is(':selected')) {
					$jinput.val($j(this).html());
					$j(li).addClass(opt.currentClass);
				}
				ul.appendChild(li);
				createOption(li);
			}
		});
		return ul;
	}
	
	function createOption(li) {
		$j(li)
			.mouseenter(function(event) {
				hasfocus = 1;
				if (opt.debug) console.log('over on : '+this.id);
				jQuery(event.target, $jcontainer).addClass(opt.hoverClass);
			})
			
			.mouseleave(function(event) {
				hasfocus = -1;
				if (opt.debug) console.log('out on : '+this.id);
				jQuery(event.target, $jcontainer).removeClass(opt.hoverClass);
			})

			.click(function(event) {
			  var fl = $j('li.'+opt.hoverClass, $jcontainer).get(0);
				if (opt.debug) console.log('click on :'+this.id);
				
				$j('li.'+opt.currentClass).removeClass(opt.currentClass); //remove selected class selected
				$j(this).addClass(opt.currentClass);//add selected class
				
				//get selected index from ul li
				var myIndex = $j(this).parent().children('li').index(this);
				//remove selected from select option
				////$j('select#birthstoneSelect option:selected').removeAttr("selected");
				//add selected to select option
				////$j('select#birthstoneSelect').val(myIndex);
				eval($jinput.attr('onchange'));
				setCurrent();
				hideMe();
				$jselect.trigger("change") // added to fire onchange on original element
			});
			
		return li;
	}
};
