/*
 * jQuery selectbox plugin
 *
 * Copyright (c) 2007 Sadri Sahraoui (brainfault.com)
 * Licensed under the GPL license:
 *   http://www.gnu.org/licenses/gpl.html
 *
 * The code is inspired from Autocomplete plugin (http://www.dyve.net/jquery/?autocomplete)
 *
 * Revision: jQueryIdjQuery
 * Version: 0.3
 */
jQuery.fn.extend({
	selectbox: function(options) {
		return this.each(function() {
			new jQuery.SelectBox(this, options);
		});
	}
});

jQuery.SelectBox = function(selectobj, options) {

	var opt = options || {};
	opt.inputClass = opt.inputClass || "selectbox";
	opt.containerClass = opt.containerClass || "selectbox-wrapper";
	opt.hoverClass = opt.hoverClass || "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 jQueryselect = jQuery(selectobj);
	// jquery container object
	var jQuerycontainer = setupContainer(opt);
	//jquery input object
	var jQueryinput = setupInput(opt);

	//onchange-Befehle hinzufügen
	var attachedRadio = "";
	var loadVFormatEvent = "";

	jQueryselect.each(function(){

		loadVFormatEvent = jQueryselect.attr("loadVFormat");

		attachedRadio = jQueryselect.attr("attachedradio");

	});


	// hide select and append newly created elements
	jQueryselect.hide().before(jQueryinput).before(jQuerycontainer);

	init(loadVFormatEvent);

	jQueryinput
	.click(function(){
		if (!inFocus) {
		  jQuerycontainer.toggle();
		}
	})
	.focus(function(){
	   if(attachedRadio){
			if((attachedRadio!="undefined")&&(attachedRadio!="")){
				document.getElementsByName(attachedRadio)[1].checked = true;
			}
		}
	   if (jQuerycontainer.not(':visible')) {
	       inFocus = true;
	       jQuerycontainer.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 !
				setCurrent();
				hideMe();
				break;
		}
	})
	.blur(function() {
		if (jQuerycontainer.is(':visible') && hasfocus > 0 ) {
			if(opt.debug) console.log('container visible and has focus')
		} else {
			hideMe();
		}

	});
	//.attr("disabled", true);


	function hideMe() {
		hasfocus = 0;
		jQuerycontainer.hide();
	}

	function init(loadVFormatEvent) {
		jQuerycontainer.append(getSelectOptions(loadVFormatEvent)).hide();
		var width = jQueryinput.width()
		jQuerycontainer.width(width);
    }

	function setupContainer(options) {
		var container = document.createElement("div");
		jQuerycontainer = jQuery(container);
		jQuerycontainer.attr('id', elm_id+'_container');
		jQuerycontainer.addClass(options.containerClass);

		return jQuerycontainer;
	}

	function setupInput(options) {
		var input = document.createElement("input");
		var jQueryinput = jQuery(input);
		jQueryinput.attr("id", elm_id+"_input");
		jQueryinput.attr("type", "text");
		jQueryinput.addClass(options.inputClass);
		jQueryinput.attr("autocomplete", "off");
		jQueryinput.attr("readonly", "readonly");
		jQueryinput.attr("tabIndex", jQueryselect.attr("tabindex")); // "I" capital is important for ie
		return jQueryinput;
	}

	function moveSelect(step) {
		var lis = jQuery("li", jQuerycontainer);
		if (!lis) return;

		active += step;

		if (active < 0) {
			active = 0;
		} else if (active >= lis.size()) {
			active = lis.size() - 1;
		}

		lis.removeClass(opt.hoverClass);

		jQuery(lis[active]).addClass(opt.hoverClass);

	}

	function setCurrent() {
		var li = jQuery("li."+opt.hoverClass, jQuerycontainer).get(0);
		var el = li.id
		jQueryselect.val(el);
		jQueryinput.val(jQuery(li).html());
		return true;
	}

	// select value
	function getCurrentSelected() {
		return jQueryselect.val();
	}

	// input value
	function getCurrentValue() {
		return jQueryinput.val();
	}

	function getSelectOptions(loadVFormatEvent) {
		var select_options = new Array();
		var ul = document.createElement('ul');
		jQueryselect.children('option').each(function() {
			var li = document.createElement('li');
			li.setAttribute('id', jQuery(this).val());
			li.innerHTML = jQuery(this).html();
			if (jQuery(this).is(':selected')) {
				jQueryinput.val(jQuery(this).html());
				jQuery(li).addClass(opt.hoverClass);
			}
			ul.appendChild(li);
			jQuery(li)
			.mouseover(function(event) {
				hasfocus = 1;
				if (opt.debug) console.log('out on : '+this.id);
				jQuery(event.target, jQuerycontainer).addClass(opt.hoverClass);
			})
			.mouseout(function(event) {
				hasfocus = -1;
				if (opt.debug) console.log('out on : '+this.id);
				jQuery(event.target, jQuerycontainer).removeClass(opt.hoverClass);
			})
			.click(function(event) {
				if (opt.debug) console.log('click on :'+this.id);
				jQuery(this).addClass(opt.hoverClass);
				setCurrent();
				//alert('Click-Wechsel: ' + getCurrentValue() + ' id: ' + this.id);
				//alert(onchangeEvent);
				//eval(onchangeEvent);
				if(loadVFormatEvent){
					if(loadVFormatEvent!="undefined"){
						changeDivAreaAjax('#cdFormat', 'index.php?sid=420&cc=' + this.id);
					}
				}
				hideMe();
			});
		});
		return ul;
	}

};
