(function () {
    var initObject;
    var screenning = function (pannel, options) {
        this.pannel = pannel;
        this.data = options.data;
        this.callback = options.callback;
        this.contentWidth = "600px";
        this.expend = options.expend == undefined ? true : options.expend;
        //this.renderCSS();
        this.init();
        this.setItem();
        this.toggleExpand();
    };

    screenning.prototype = {
        init: function () {
            //var html = '<div class="choose">';
            var html = '';
            var _this = this;
            $(this.data).each(function (i, n) {
                if (n.data && n.data.length > 0) {
                    html += '<div class="control-group chooselist" data="' + n.paramKey + '">';

                 
                    html += ' <label class="control-label">';
                    if ($.trim(n.title) != '') {
                        html += n.title + '：';
                    }
                    html += '</label>';
                    html += '<div class="controls"><ul data="' + n.paramKey + '" index="' + i + '"';
                    if (n.searchByText != undefined && n.searchByText != null && n.searchByText == true) {
                        html += 'searchByText="true"';
                    }
                    html += '>';
                    var needHide = false;
                    $(n.data).each(function (j, m) {
                        html += '<li';
                        if (m.by) {
                            html += ' by="' + m.by + '" '
                        }
                        html += ' data="' + m.value + '"';

                        html += ' class="kq-left ';
                      
                        if (_this.setDefault(n.paramKey, m)) {
                            html += ' kq-sel';
                        }
                        html += '"';

                        html += '> <a href="javascript:;">' + m.name + '</a></li>';
                    });
                  
                    html += '</ul></div></div>';

                }
            });
            //html += '</div>';
           
            this.pannel.append(html);
        },
        showMore: function (o) {
            $(o).parent('dl').find('dd.itemHide').slideDown("slow");
        },
        setDefault: function (key, oData) {
            var v = this.urlParam(key);
            if (v == null) {
                return oData.isDefault;
            }
            if (oData.by == 'txt') {
                return oData.name.toLowerCase() == v.toLowerCase();
            }
            return oData.value == v;
        },
        getResult: function (index) {
            var result = '';
            var jsonData = [];
            this.pannel.find('.chooselist').each(function (i, n) {
                var liChild = $(this).find('.kq-sel');
                var byValue = '';
                var byType = liChild.parent().attr('searchbytext');
                if (byType == 'true') {
                    byValue = liChild.text();
                }
                else {
                    byValue = liChild.attr('data');
                }
                result += liChild.parent().attr('data');
                result += '=' + byValue;
                result += '&'
                jsonData.push({ name: $(this).find("ul").attr('data'), value: byValue });
            });

            this.callback(result, index, jsonData);
        },
        loopShow: function (hideitem) {
            var _this = this;
            hideitem.show(function () {
                var next = $(this).next('li');
                if (next.length > 0) {
                    _this.loopShow(next);
                }
            });
        },
        toggleExpand: function () {
            var _this = this;
            this.pannel.find(".close").click(function () {
                $(".screenning dl:gt(2)").slideToggle();
            });
        },
        onShow: function (o) {

        },
        setItem: function () {
            var _this = this;

            function onShow(o) {
                o.text('<<收起').parent().find('li.itemHide').show();
            };
            function onHide(o) {
                o.text('更多>>').parent().find('li.itemHide').hide();
            }
            //更多
            this.pannel.find('li.liMore').toggle(function () {
              
                onShow($(this));
            },
            function () {
                onHide($(this));
            });
            //展开隐藏点选的数据
            this.pannel.find('li.chooselist_xs').each(function (i, n) {
                if ($(this).hasClass('itemHide')) {
                    var p = $(this).parent('dl');
                    p.children('li.liMore').text('<<收起').toggle(function () {
                        onHide($(this));
                    }, function () {
                        onShow($(this));
                    });
                    p.children('li.itemHide').show();
                 
                }
            });
            //鼠标滑过和点击
            this.pannel.find('li').click(function () {
                var p = $(this).parent();
                p.find('.kq-sel').removeClass('kq-sel');
                $(this).addClass('kq-sel');
                var index = p.attr('index');
                _this.getResult(index);
                _this.showloading();
            });

        },
        urlParam: function (name) {

            var url = decodeURI(window.location.href);
            var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(url);
            if (results == null) {
                return null;
            }
            return results[1] || 0;
        },
        renderCSS: function () {
         

            var style = '<style type="text/css">';
             style += ' .choose{ height:auto;border-left:1px solid #e4e4e4;border-right:1px solid #e4e4e4;border-bottom:1px solid #e4e4e4; background:#fbfbfb url(http://file.1u.cn/Static/ETS/main/list_topbg.png)left top no-repeat; padding-top:2px;}';
             style += ' .choose .row{ width:100%; height:auto; padding:5px 0px; border-bottom:1px solid  #e4e4e4; background:url(http://file.1u.cn/Static/ETS/main/sk_bg.png) left repeat-y; clear:both;}';
             style += ' .choose .row .choosetitle{ display:block;  width:75px; text-align:center; line-height:24px; height:24px;color:#3f3f3f; float:left;clear:both;overflow:hidden;}';
             style += ' .choose .row .chooselist{ width:690px; height:auto; line-height:24px;   float:left; }';
             style += ' .choose .row .chooselist ul .chooselist_xs  a{ background:#fe8300; color:#fff;}';
             style += ' .choose .row .chooselist ul .chooselist_xs  a:hover{ background:#fe8300; color:#fff;}';
             style += ' .choose .row .chooselist ul { list-style-type:none;}';
             style += ' .choose .row .chooselist ul li{ float:left;line-height:24px; margin-left:15px;}';
             style += ' .choose .row .chooselist ul li a{color:#6179a2; text-decoration:none; padding:2px 3px;}';
             style += ' .choose .row .chooselist ul li a:hover{ background:#fe8300; color:#fff;}';
             style += ' .choose .prop-value{ width:auto; height:35px;}';
             style += ' .choose .prop-value .valuetitle{ display:block;  width:75px; text-align:center; line-height:35px; height:35px;color:#3f3f3f; float:left;}';
             style += ' .choose .prop-value .valuelist{ float:left; width:550px; height:27px; padding-top:8px;}';
             style += ' .choose .prop-value .valuelist a{ display:block;   color:#a6a6a6; text-decoration:none; float:left; padding:1px 18px 1px 4px; background:url(http://file.1u.cn/Static/ETS/main/gb2.png)center right no-repeat; border:1px solid #ffc89f; margin-left:10px;}';
             style += ' .choose .prop-value .valuelist a:Hover{ background:url(http://file.1u.cn/Static/ETS/main/gb1.png) center right no-repeat;  border:1px solid #fd9c53;color:#fd9c53;}';
             style += ' .choose .prop-value .dis{ display:block; width:130px; height:35px; float:left; border-left:1px solid #e4e4e4;line-height:35px; text-align:center;color:#575757;}';
             
            style += '</style>';
            this.pannel.before(style);
        },
        showloading: function () {

            var p = window.parent;
            if (p) {
                p.$('#loading').show();
            }
        }
    };

    $.fn.screenning = function (options) {
        return $(this).each(function () {
            initObject = new screenning($(this), options);
        });
    };


})(jQuery);

//动画效果
function SliderUpOrDown() {
    $("#searchPannel .screenning").slideToggle("slow", function () {
        var imgStr = $(".close").css("background-image");
        var img = imgStr.substr(imgStr.length - 11, 10);

        if (img == "lineqq.png") {
            $(".close").css("background-image", "url(/images/NewImage/linejj.png)");
        } else {
            $(".close").css("background-image", "url(/images/NewImage/lineqq.png)");
        }   
    });
}

$.extend({
    SearchPrefix: function () {
        var result = "";
        var jsonData = [];
        $(".chooselist ul").each(function (i, n) {
            var para = $(this).attr("data");
            var value = $(this).find(".kq-sel").attr("data");
            result = result + para + "=" + value + "&";
            //var para = $(this).attr("data");
            //var value = $(this).find(".kq-sel").attr("data");

         
            //result = result + para + "=" + value + "&";
            //if (para.indexOf(',') > 0) {
            //    if (value.indexOf(',') > 0) {
            //        for (var i = 0; i < value.split(',').length; i++) {
            //            jsonData.push({ name: $(this).attr('data').split(',')[i], value: value.split(',')[i] });
            //        }
            //    }
            //    else {
            //        jsonData.push({ name: $(this).attr('data').split(',')[0], value: value });
            //    }
            //}
            //else {
            //    if ($(n).attr('searchByText') == 'true') {
            //        jsonData.push({ name: $(this).attr('data'), value: ($.trim($(this).find(".kq-sel a").html()) == '全部' ? '' : $(this).find(".kq-sel a").html()) });
            //    }
            //    else {
            //        jsonData.push({ name: $(this).attr('data'), value: value });
            //    }
            //}
        });
        //return jsonData;
        return result;
    }
});