/*
* 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.
*
*/

jQuery.fn.styledSelect = function (settings) {
    settings = jQuery.extend({
        selectClass: 'richFormSelectOne',
        openSelectClass: 'open',
        optionClass: 'option',
        selectedOptionClass: 'selected',
        closedOptionClass: 'closed',
        firstOptionClass: 'first',
        lastOptionClass: 'last',
        zIndexApply: false,
        zIndexStart: 250,
        deactiveOnBackgroundClick: true
    }, settings);

    var currentZIndex = settings.zIndexStart;

    this.each(function () {
        var s = jQuery(this);

        var container = jQuery('<div class="select-box"></div>');
        var cs = jQuery('<div><div></div></div>').attr('class', settings.selectClass);
        if (settings.zIndexApply) { cs.css('z-index', currentZIndex - 2); };
        var csl = jQuery('<ul></li>');
        if (settings.zIndexApply) { csl.css('z-index', currentZIndex - 1); };
        cs.children().append(csl);
        container.append(cs)

        if (s.css('display') == 'none') {
            container.hide();
        }

        s.hide().after(container);
        //cs = s.next();

        s.bind('updateDisplay', function (event, visible) {
            container[visible ? 'show' : 'hide']();
        });

        jQuery('option', s).each(function () {
            if (jQuery(this).attr('value') == undefined) {
                jQuery(this).attr('value', jQuery(this).text());
            }
        });

        var closedSelect = function () {
            if (cs.css('position') == 'absolute') {
                cs.css({ 'position': 'relative', 'z-index': 0, 'top': 0, 'left': 0 });
                container.append(cs);
            }

            jQuery('ul', cs).html('');
            addOption(s.val(), jQuery(':selected', s).text(), clickSelect);
            cs.removeClass(settings.openSelectClass);
            jQuery('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 () {

            cs.css({ 'position': 'absolute', 'z-index': 10000, 'top': cs.offset().top, 'left': cs.offset().left });
            jQuery('body').append(cs);

            jQuery('ul', cs).empty();
            jQuery('option', s).each(function (i) { addOption(jQuery(this).val(), jQuery(this).text(), clickOption); });
            cs.addClass(settings.openSelectClass);
            jQuery('ul li:first-child', cs).addClass(settings.firstOptionClass);
            jQuery('ul li:last-child', cs).addClass(settings.lastOptionClass);
            if (settings.deactiveOnBackgroundClick) {
                $(document).bind('mousedown', closedSelect);
                cs.bind('mousedown', function () { return false; });
            }
        };

        var clickOption = function () {

            cs.css({ 'position': 'relative', 'z-index': 0, 'top': 0, 'left': 0 });
            container.append(cs);
            var val = jQuery(this).attr('rel');
            s.val(val);
            s.trigger('change'); //.trigger('click');
            closedSelect();
        };

        var addOption = function (optVal, optName, callBack) {
            var cso = jQuery('<li><span>'+ optName + '</span></li>').attr('rel', optVal).click(callBack).addClass(settings.optionClass);
            if (settings.zIndexApply) { cso.css('z-index', currentZIndex); };
            if (s.val() == optVal) {
                cso.addClass(settings.selectedOptionClass);
            };
            jQuery('ul', cs).append(cso);
        };

        closedSelect();
        s.change(closedSelect);
        currentZIndex -= 3;
    });

    return this;
};

