(function ($) {
    $.fn.sexySelect = function (options) {
        var defaults = {}
        var options = $.extend(defaults, options);
        this.each(function (index) {
            var $old_select = $(this);

            if ($.browser.version == "6.0" && parseInt($(this).css('margin-left')) == -1000) {
                return;
            }

            if ($old_select.css('display') != 'none') {
                //隐藏原select
                if ($.browser.version == "6.0") {
                    $(this).css('margin-left', '-1000px');
                } else {
                    $(this).hide();
                }

                var $new_select = $('<div class="sexySelect" id="sexySelect">' +
					'<div id="select_now" class="select_now clearfix"><span class="select_input" id="select_input"></span><span class="select_open" id="select_open"><span></span></span></div>' +
					'<ul></ul>' +
				+'</div>');
                $new_select.insertBefore($old_select);
                var $new_select_ul = $new_select.find('ul');
                var $new_select_ipunt = $new_select.find("#select_input");
                var $new_select_now = $new_select.find("#select_now");
                var $new_select_open = $new_select.find("#select_open");

                var auto_width = true;
                if (parseInt($old_select.css('width')) > 0) {
                    auto_width = false;
                }

                $old_select.find("option").each(function (i) {
                    $(this).attr('index', i);
                    if ($(this).attr('selected')) {
                        $new_select_ipunt.html($(this).html());
                        $new_select_ul.append('<li value="' + $(this).val() + '" title="' + $(this).html() + '" index="' + i + '" selected="true" class="over">' + $(this).html() + '</li>');
                    } else {
                        $new_select_ul.append('<li value="' + $(this).val() + '" title="' + $(this).html() + '" index="' + i + '">' + $(this).html() + '</li>');
                    }
                });

                var $new_select_li = $new_select.find("li");
                if ($new_select_ul.height() > 250) {
                    $new_select_ul.height(250);
                }

                //初始化宽度
                if (auto_width) {//自动宽度
                    $new_select_now.css({ width: $new_select_ul.outerWidth() + $new_select_open.outerWidth() });
                    $new_select_ul.css({ width: $new_select_now.outerWidth() - 2 });
                } else {//手动宽度
                    $new_select_now.css({ width: $old_select.outerWidth() });
                    $new_select_ul.css({ width: $new_select_now.outerWidth() - 2 });
                    $new_select_ipunt.css({ width: $new_select_now.outerWidth() - 2 - $new_select_open.outerWidth() - parseInt($new_select_ipunt.css("padding-left")) - parseInt($new_select_ipunt.css("padding-right")) });
                }
                $new_select_ul.hide(); 	//初始隐藏 ul

                //点击 展开或隐藏 ul , 外部点击隐藏 ul
                var event_click = function () {
                    if ($new_select_ul.css('display') == 'none') {
                        $new_select_ul.show();
                        $new_select.unbind("outerClick");
                        $new_select.bind("outerClick", function () {
                            $new_select_ul.hide();
                        });
                    } else {
                        $new_select_ul.hide();
                    }
                }

                //鼠标移到 li 上时的效果
                $new_select_li.hover(
					function () {
					    $(this).addClass('over');
					},
					function () {
					    if ($(this).attr('selected') != 'true') {
					        $(this).removeClass('over');
					    }
					}
				);

                //点击 li 时更新各值
                function li_click() {
                    //alert($(this).attr('index'));
                    var ischange = true;
                    if ($(this).attr('index') == $new_select_ul.find("li[selected='true']").attr('index')) {
                        ischange = false;
                    }

                    $new_select_ipunt.text($(this).html());
                    $new_select_li.removeAttr('selected');
                    $new_select_li.removeClass('over');
                    $(this).attr('selected', 'true');
                    $(this).addClass('over');
                    var index = $(this).attr('index');
                    $old_select.get(0).selectedIndex = index;


                    if (ischange) {
                        //$old_select.trigger("change");//执行原select的onchange事件
                        $old_select.unbind('change', select_change);
                        $old_select.trigger('change', select_change); //执行原select的onchange事件
                    }
                    $new_select_ul.hide();
                }
                $new_select_li.click(li_click);



                //鼠标移到 新 select 上的效果
                var select_over = function () {
                    $new_select.find('#select_open').find('span').stop();
                    $new_select.find('#select_now').stop();

                    $new_select.find('#select_open').find('span').animate({ 'opacity': '0' }, 250);
                    /*
                    $new_select.find('#select_now').animate({
                    'borderTopColor':'#f0bcbc',
                    'borderRightColor':'#f0bcbc',
                    'borderBottomColor':'#f0bcbc',
                    'borderLeftColor':'#f0bcbc'
                    },250);
                    //*/
                }
                //鼠标移出 新 select 上的效果
                var select_out = function () {
                    $new_select.find('#select_open').find('span').stop();
                    $new_select.find('#select_now').stop();

                    $new_select.find('#select_open').find('span').animate({ 'opacity': '1' }, 250);
                    /*
                    $new_select.find('#select_now').animate({
                    'borderTopColor':'#e1e1e1',
                    'borderRightColor':'#e1e1e1',
                    'borderBottomColor':'#e1e1e1',
                    'borderLeftColor':'#e1e1e1'
                    },250);
                    //*/
                }
                var select_disabled_over = function () { }
                var select_disabled_out = function () { }


                //给原 select 加一个改变值侦听
                var select_change = function () {

                    $new_select_ul.html('');
                    $old_select.find("option").each(function (i) {
                        $(this).attr('index', i);
                        if ($(this).attr('selected')) {
                            $new_select_ipunt.html($(this).html());
                            $new_select_ul.append('<li value="' + $(this).val() + '" title="' + $(this).html() + '" index="' + i + '" selected="true" class="over">' + $(this).html() + '</li>');
                        } else {
                            $new_select_ul.append('<li value="' + $(this).val() + '" title="' + $(this).html() + '" index="' + i + '">' + $(this).html() + '</li>');
                        }
                    });
                    if ($new_select_ul.height() > 250) {
                        $new_select_ul.height(250);
                    }
                    $new_select_li = $new_select.find("li");
                    $new_select_li.click(li_click);
                    $new_select_li.hover(function () {
                        $(this).addClass('over');
                    }, function () {
                        if ($(this).attr('selected') != 'true') {
                            $(this).removeClass('over');
                        }
                    });

                    var auto_width = true;
                    if (parseInt($old_select.css('width')) > 0) {
                        auto_width = false;
                    }
                    //初始化宽度
                    if (auto_width) {//自动宽度
                        $new_select_now.attr("style", "");
                        $new_select_ul.attr("style", "display:none;");
                        $new_select_now.css({ width: $new_select_ul.outerWidth() + $new_select_open.outerWidth() });
                        $new_select_ul.css({ width: $new_select_now.outerWidth() - 2 });
                    } else {//手动宽度
                        $new_select_now.css({ width: $old_select.outerWidth() });
                        $new_select_ul.css({ width: $new_select_now.outerWidth() - 2 });
                        $new_select_ipunt.css({ width: $new_select_now.outerWidth() - 2 - $new_select_open.outerWidth() - parseInt($new_select_ipunt.css("padding-left")) - parseInt($new_select_ipunt.css("padding-right")) });
                    }

                    var $now_selected = $(this).children('option:selected');
                    $new_select_ipunt.html($now_selected.html());
                    $new_select_ul.css({ width: $new_select_now.outerWidth() - 2 });

                    $new_select_li.removeClass('over');
                    $new_select_li.removeAttr('selected');

                    $new_select_li.css("width", "100%");

                    var $now_selected_li = $new_select_li.eq($now_selected.attr('index'));

                    $now_selected_li.addClass('over');
                    $now_selected_li.attr('selected', 'true');

                    $old_select.unbind('change', select_change);
                    $old_select.trigger("change"); //执行原select的onchange事件
                    $old_select.bind('change', select_change);
                }
                //$old_select.bind('change',select_change);
                $old_select.bind('setChange', select_change);

                //禁用原 select
                $old_select.bind("setDisabled", function () {
                    $new_select_now.unbind('click', event_click);
                    $new_select.addClass('sexySelect_disabled');
                    $new_select.unbind();

                    $new_select_ul.hide();
                });
                //启用原 select
                $old_select.bind("setUnDisabled", function () {
                    $new_select_now.bind('click', event_click);
                    $new_select.removeClass('sexySelect_disabled');
                    $new_select.bind({ 'mouseover': select_over, 'mouseout': select_out });
                });


                if ($old_select.attr('disabled')) {
                    $old_select.trigger("setDisabled");
                } else {
                    $old_select.trigger("setUnDisabled");
                }
            }
        });
    }
})(jQuery);
