﻿(function ($) {
    // définition du plugin jQuery
    $.fn.jCustomSelect = function (params) {
        // init faux select
        this.each(function () {
            var $t = $(this);
            //var name = $t.attr("name");	
            var firstSelected = $t.find("option:selected");
            var html = '';
            $t.find("option").each(function (i) {
                if (i == 0) {
                    html += '<dl class="customList"><dt><a><span>' + $(this).text() + '</span></a></dt><dd><ul class="itemList">';
                    html += '<li><a href="#"><span value="' + $(this).attr("value") + '">' + $(this).text() + '</span></a></li>';
                } else {
                    html += '<li><a href="#"><span value="' + $(this).attr("value") + '">' + $(this).text() + '</span></a></li>';
                }
            });
            html += '</ul></dd></dl>';
            $t.parent().append(html);
            defaultSelected(firstSelected);
        });

        // size du faux select
        $(".customList dd").each(function () {
            var widthSelect = $(this).parent().parent().children("select").width();
            $(this).css("width", widthSelect);
        });

        // animation du faux select 
        $(".customList dt a").click(function () {
            var widthArea = $(this).parents(".customList").width();
            var itemList = $(this).parents(".customList").find(".itemList");
            itemList.css("min-width", widthArea);
            if (itemList.is(":visible")) {
                itemList.hide();
            } else {
                itemList.show();
            }
        });

        $(".customList").mouseenter(function () { }).mouseleave(function () {
            $(this).find(".itemList").hide();
        });

        // action sur les items	
        $('.itemList li a').click(function () {
            itemClick($(this));
            return false;
        });

    };

    function itemClick(obj) {
        var itemSelected = obj.parents(".cntList").find('select option').eq(obj.parent().index());
        obj.parents(".customList").find("dt a").html('<span>' + obj.text() + '</span>');
        obj.parents(".itemList").hide();
        // selection de l'item dans le select
        obj.parents(".cntList").find("option").each(function () {
            $(this).removeAttr("selected");
        });
        itemSelected.attr("selected", "selected");
        return false;
    }

    function defaultSelected(obj) {
        var itemSelected = obj.parents(".cntList").find('dl dd a').eq(obj.index());
        itemSelected.parents(".customList").find("dt a").html('<span>' + itemSelected.text() + '</span>');
        obj.parents(".itemList").hide();
        // selection de l'item dans le select
        itemSelected.parents(".cntList").find("option").each(function (i) {
            $(this).removeAttr("selected");
        });
        itemSelected.attr("selected", "selected");
        obj.attr("selected", "selected");
        return false;

    }
})(jQuery);
