/*
 * jListbox jQuery plugin
 *
 * Copyright (c) 2009 Giovanni Casassa (senamion.com - senamion.it)
 *
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * http://www.senamion.com
 *
 */

jQuery.fn.jListbox = function(o) {

	o = jQuery.extend({
		selectText: "No option",
		viewText: true
	}, o);

	return this.each(function() {
		var el = $(this);
		name = (el.attr('id')) + '_jlb';
		el.css({
			position:"absolute",
			top:"-9999"
		});

		stropt = "";
		var els = el.children("option");
		$.each(els, function(i,n) {
			text = ($(n).attr("rel") || '') + ' ' + (o.viewText ? $(n).text() : '');
			stropt += "<li rel='" + $(n).val() + "'>" + text + "</li>";
			if ($(n).attr("selected"))
				o.selectText = text;
		}); 

		el.after("<div id='" + name + "' class='jlb_class'><a id='a" + name + "' href='#'>" + o.selectText + "</a><ul>" + stropt + "</ul></div>");

		// CLICK ON TITLE
		$("div#" + name + " a").click(function(){
			$(this).next().slideToggle("fast");
			return false;
		});

		// CLICK ON ELEMENT
		$("div#" + name + " ul li").click(function(){
			listName = $(this).parent().parent().attr('id');

			
			listName = listName.substr(0, listName.length - 4);


			

			




	if ($(this).attr("rel") == $("#"+listName+" > option:selected").val())
	{
	}else{
		$('[id=' + listName + ']').val($(this).attr("rel")).trigger('change');
	}



			


			$(this).parent().parent().children().eq(0).html($(this).html());
			
		});
		$("div#" + name + " ul li").mouseover(function() {
					
					$(this).css({
						backgroundColor:"#b0a89e",
						color:"#fffefe"
					});

				}).mouseout(function() {
					$(this).css({
						backgroundColor:"#cac7c3",
						color:"#65554b"
					});

		});
		// CLICK OUTSIDE
		$("body").click(function(){
			$(".jlb_class ul").slideUp("fast");
		});
	});
};    

