/**
 * [OElove] (C)2010-2099 oelove.com biz
 * Email: service@phpcoo.com ,phpcoo@qq.com
 * This is NOT a freeware, use is subject to license terms
 * $ Last update 2020/05/08 by CL $
*/
var _inst_oesearch = null;
$(function(){
    
    //搜索类
    function OESearch() {
        this._search_dialog = null; //弹出对象
        this._search_box = "search_wrap"; //搜索框名称
        this.slide_time = 200;
        this.params = ""; //搜索项组合 item=value&item=value
        this.back_data = {}; //瀑布流回传参数
        
        this.this_dom = null;
        this.slide_box = null;
        
        this.initSearchDialog();
        this.wake();
        this.clickReset();
    }
    
    OESearch.prototype = {
        //初始化
        initSearchDialog:function() {
            setTimeout(function(){
                var $window_url = window.location.href;
                if ($window_url != '') {
                    var $open = $window_url.indexOf('opendialog=1');
                    if (typeof($open) == 'undefined') {
                        $open = '-1';
                    }
                    if ($open != '-1' && $open != '') {
                        _this._search_dialog = OEUI.Dialog({
                            el: '#'+_this._search_box,
                            animated: 'slideRightIn',
                            time:_this.slide_time
                        });
                    }
                }
                
            }, 100);
        },
        
        //唤醒
        wake:function() {
            _this = this;
            
            //打开侧滑
            $(document).on("click", "[f='but_wake_search']", function(){
                _this._search_dialog = OEUI.Dialog({
                    el: '#'+_this._search_box,
                    animated: 'slideRightIn',
                    time:_this.slide_time
                });
            });
            //关闭侧滑
            $(document).on("click", "[f='close_wake_search']", function(){
                if (_this._search_dialog) {
                    _this._search_dialog.hide();
                }
            });
            //侧滑提交
            $(document).on("click", "[f='but_confirm_search']", function(){
                //关闭侧滑
                if (_this._search_dialog) {
                    _this._search_dialog.hide();
                }
                _this.submitSearch();
            });

            
            //显示隐藏下拉参数
            $(document).on("click", "[f='but_slide_item']", function(){
                _this.this_dom = $(this);
                var $slide = $(this).attr("data-slide");
                _this.slide_box  = $slide+"_item_box";
                if (!$(this).hasClass("selected")) {
                    $(this).addClass("selected");
                    $(".item_list").hide();
                    $("#"+_this.slide_box).show();
                }
                else {
                    $(this).removeClass("selected");
                    $(".item_list").hide();
                }
            });
            
            //选中下拉参数
            $(document).on("click", "[f='but_sel_seritem']", function(){
                var $val = $(this).attr("data-val");
                $(this).addClass("color_main").siblings().removeClass("color_main");
                $(this).siblings().find("i").hide();
                $(this).find("i").show();
                var $input = $(this).attr("data-input");
                $("#"+$input).val($val);
                var $text = $(this).find("em").text();
                
                $("#"+_this.slide_box).hide();
                $(_this.this_dom).removeClass("selected");
                $(_this.this_dom).find("em").html($text);
                $(_this.this_dom).addClass("current");
                
                //0.05.08 处理侧滑当选年龄
                if ($input == 's_age'){
                    if ($('.dialog_s_age').length > 0) {
                        $('.dialog_s_age span').removeClass('current');
                        $('.dialog_s_age span').each(function(iv, elm){
                            var $elm_val = $(this).attr('data-val');
                            if ($val == $elm_val) {
                                $(this).addClass('current');
                            }
                        });
                    }
                }
                //处理侧滑当选婚况
                else if ($input == 's_marry') {
                    if ($('.dialog_s_marry').length > 0) {
                        $('.dialog_s_marry span').removeClass('current');
                        $('.dialog_s_marry span').each(function(iv, elm){
                            var $elm_val = $(this).attr('data-val');
                            if ($val == $elm_val) {
                                $(this).addClass('current');
                            }
                        });
                    }
                }
                
                _this.submitSearch();
            });
            
            //展开参数值
            $(document).on("click", "[f='but_more_seritem']", function(){
                var obj_item = $(this).parent();
                if ($(obj_item).find('dd').css('display') == 'none') {
                    $(obj_item).find('dd').show();
                    $(obj_item).find('dt').find('i').removeClass('icon-jiantouxiaxi').addClass('icon-jiantoushangxi');
                }
                else {
                    $(obj_item).find('dd').hide();
                    $(obj_item).find('dt').find('i').removeClass('icon-jiantoushangxi').addClass('icon-jiantouxiaxi');
                }
            });
            
            //去登录
            $(document).on("click", "[f='but_login']", function(){
                _this.login();
                return;
            });
            
            //开通VIP
            $(document).on("click", "[f='but_vip']", function(){
                var $c = $("#c").val();
                if (typeof($c) == 'undefined') {
                    $c = 'userwall';
                }
                window.location.href = WAP_APPFILE+"&c=vip&from="+$c;
                return;
            });
            
            //0.05.08
            $(document).on("click", "[f='but_clear_skey']", function(){
                $("#s_key").val('');
            });
            
        },
        
        //点击重置
        clickReset:function() {
            var _this = this;
            $(document).on("click", "[f='but_reset']", function(){
                var $c = $("#c").val();
                if (typeof($c) == 'undefined') {
                    $c = 'userwall';
                }
                _this.sendReset($c);
            });
        },
        
        
        //执行搜索
        submitSearch:function() {
            _this = this;
            
            _this.params = _this.buildParams(); //获取参数
            $reqs_url = WAP_APPFILE+"&c=userwall&a=list&datatype=json";
            $reqs_url += "&"+_this.params;
            $("#reqs_url").val($reqs_url);
            $("#page").val("1");
            
            //重新搜索...
            setTimeout(function(){
                if (_inst_oepage) {
                    $("#"+_inst_oepage.pull_data_id).html("").css('height', 'auto');
                    var data = $.extend({}, _this.back_data, {page: '1'});
                    _inst_oepage.scrollList.setData(data);
                    _inst_oepage.heightArray = [0,0];

                    _inst_oepage.scroll.refresh();
                }
            }, 50);
        },
        
        //组合参数
        buildParams:function() {
            var $build_params = "";
            
            var arr_data = new Array();
            arr_data = [
                    's_key', 's_gender', 's_age', 's_dist1', 's_dist2', 's_dist3', 
                    's_height', 's_weight', 's_marry', 's_education', 's_avatar', 
                    's_orderby', 's_map', 
                    's_job', 's_salary', 's_lunar', 's_astro', 's_child', 
                    's_house', 's_car', 's_home1', 's_home2', 's_home3'
                ];
                
            $.each(arr_data, function(i, item) {
                $val = $("#"+item).val();
                if (typeof($val) == "undefined") {
                    $val = "";
                }
                if ($build_params.length == 0 || $build_params == "") {
                    $build_params += item+"="+encodeURIComponent($val);
                }
                else {
                    $build_params += "&"+item+"="+encodeURIComponent($val);
                }

                _this.back_data[item] = $val;
            });
            return $build_params;
        },
        
        //发送重置
        sendReset:function($c) {
            if (oeui_cache) {
                oeui_cache.clearCache();
            }
            $.ajax({
                type: "POST",
                url: WAP_APPFILE+"&c=user",
                cache: false,
                data: {
                    a:'delcond'
                },
                dataType: "json",
                success: function($data) {
                    var $json = eval($data);
                    var $response = $json.response;
                    var $result = $json.result;
                    if ($response == "1") {
                        var $url = WAP_APPFILE+"&c="+$c+"&opendialog=1";
                        window.location.href = $url;
                    }
                },
                error: function() {
                }
            });    
        }
        
    }
    _inst_oesearch = new OESearch();
});