/*
 * jQuery Styled Select Boxes
 * version: 1.1 (2009/03/24)
 * @requires jQuery v1.2.6 or later
 *
 * Examples and documentation at: http://code.google.com/p/lnet/wiki/jQueryStyledSelectOverview
 *
 * Copyright (c) 2008 Lasar Liepins, liepins.org, liepins@gmail.com
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 */
(function($){
	$.fn.styledSelect = function(settings) {
		settings = $.extend({
			selectClass: 'styledSelect',
			openSelectClass: 'open',
			optionClass: 'option',
			selectedOptionClass: 'selected',
			closedOptionClass: 'closed',
			firstOptionClass: 'first',
			lastOptionClass: 'last',
			zIndexApply: false,
			firstSelectedClass : 'first-selected',
			readonlyClass : 'readonly',
			disabledClass : 'disabled',
			zIndexStart: 250,
			deactiveOnBackgroundClick: true
		}, settings);
	
		var currentZIndex = settings.zIndexStart;
	
		this.each(function() {
			var s = $(this);
			
			var cs = $('<div></div>').attr('class', settings.selectClass);
	        var tabindex = $(s).attr('tabindex')==undefined ? 0 : $(s).attr('tabindex');
			if(settings.zIndexApply) { cs.css('z-index', currentZIndex-2); };
			cs.addClass(settings.closedOptionClass);
			if(s.hasClass(settings.firstSelectedClass)) cs.addClass(settings.firstSelectedClass);

			var csl = $('<ul tabindex="'+tabindex+'"></li>');
			if(settings.zIndexApply) { csl.css('z-index', currentZIndex-1); };
			cs.append(csl);
			s.hide().after(cs);
			cs = s.next();
	
			$('option', s).each(function() {
				if($(this).attr('value')==undefined) {
					$(this).attr('value', $(this).text());
				}
			});
	
			var closedSelect = function() {
				$('ul', cs).html('');
				cs.addClass(settings.closedOptionClass);				
				cs.removeClass(settings.openSelectClass).addClass(settings.closedOptionClass);
				addOption(s.val(), $(':selected', s).text(), clickSelect);
				$('ul li', cs).removeClass(settings.selectedOptionClass).removeClass(settings.optionClass).addClass(settings.closedOptionClass);
				if(settings.deactiveOnBackgroundClick) {
					$(document).unbind('mousedown', closedSelect);
					cs.unbind('mousedown');
				}
			};
	
			var clickSelect = function() {
				if(s.attr('readonly') !== undefined) return false;

				$('ul', cs).empty();
				cs.addClass(settings.openSelectClass).removeClass(settings.closedOptionClass);
				var optgroup = $('optgroup', s);
				if(optgroup.size()){
					cs.addClass(settings.selectClass + '-with-groups');
					optgroup.each(function(j) {
						addGroup($(this).attr('label'));
	
						$('option', this).each(function(i){
							var el = $(this);
							addOption(el.val(), el.text(), clickOption);
						});
					});
				}else{
					$('option', s).each(function(i){
						var el = $(this);
						addOption(el.val(), el.text(), clickOption);
					});
				}
				$('ul li:first-child', cs).addClass(settings.firstOptionClass);
				$('ul li:last-child', cs).addClass(settings.lastOptionClass);
				if(settings.deactiveOnBackgroundClick) {
					$(document).bind('mousedown', closedSelect);
					cs.bind('mousedown', function(){return false;});
				}
			};
	
			var clickOption = function() {
				var val = $(this).attr('rel');
				s.val(val);
	
				// Fire basic select onchange event
				// Added by splurov@gmail.com, 2010-06-23
				s.change();
				closedSelect();
			};

			var addGroup = function(optVal) {
				var cso = $('<li></li>').addClass(settings.selectClass + '-group').text(optVal);
				//if(settings.zIndexApply) { cso.css('z-index', currentZIndex); };
				$('ul', cs).append(cso);
			};
			
			var addOption = function(optVal, optName, callBack) {
				var cso = $('<li></li>').attr('rel', optVal).text(optName).click(callBack).addClass(settings.optionClass);
				if(settings.zIndexApply) { cso.css('z-index', currentZIndex); };
				if(s.val()==optVal) {
					if(cs.hasClass(settings.firstSelectedClass) && cs.hasClass(settings.openSelectClass)){
						var cso2 = $('<li></li>').attr('rel', optVal).text(optName);
						$('ul', cs).prepend(cso2);
					}
					cso.addClass(settings.selectedOptionClass);
				};
				$('ul', cs).append(cso);
			};
	
			closedSelect();
			s.change(closedSelect);
			currentZIndex -= 3;
		});
	
		return this;
	};
})(jQuery);

