/**
 * [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/04/08 by CL $
*/
var _inst_oesearch = null;
$(function(){
    
    //搜索类
    function OESearch() {
        this.pull_box_id = "pull_box";
        this._search_dialog = null; //弹出对象
        this._search_box = "search_wrap"; //搜索框名称
        this.slide_time = 200;
        this.params = ""; //搜索项组合 item=value&item=value
        
        this.this_dom = null;
        this.slide_box = null;
        
        this.initSearchDialog();
        this.wake();
        this.clickReset();
        this.backTop();
    }
    
    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(function(){
                        _this.submitSearch();
                    });
                }
                else {
                    _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 = 'user';
                }
                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 = 'user';
                }
                _this.sendReset($c);
            });
        },
        
        //登录跳转
        login:function() {
            var _this = this;
            var $c = $("#c").val();
            if (typeof($c) == 'undefined') {
                $c = 'user';
            }
            
            var $back_url = WAP_APPFILE+"&c="+$c;
            window.location.href = WAP_APPFILE+"&c=passport&a=login&forward="+encodeURIComponent($back_url);
        },
        
        //执行搜索
        submitSearch:function() {
            _this = this;
            _this.params = _this.buildParams(); //获取参数
            
            var $c = $("#c").val();
            if (typeof($c) == 'undefined') {
                $c = 'user';
            }
            $reqs_url = WAP_APPFILE+"&c="+$c+"&a=list&datatype=json&searchfrom=post";
            $reqs_url += "&"+_this.params;
            $("#reqs_url").val($reqs_url);
            $("#page").val("1");
            
            //重新搜索...
            setTimeout(function(){
                if (_inst_oepage) {
                    $("#"+_inst_oepage.pull_data_id).html("");
                    _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);
                }
            });
            return $build_params;
        }, 
        
        //返回顶部
        backTop: function () {
            var _this = this;
            var scroll = $('#'+ _this.pull_box_id+' .scroll')[0];
            $('[f="back_to_top"]').on('click', function () {
                _this.animateToTop(scroll.scrollTop, 0, 4, function (value) {
                    scroll.scrollTop = value;
                });
            });
        },
        //返回顶部动画
        animateToTop: function (m, n, rate, cb) {
            if(m == n || typeof m != 'number') {
                return;
            }
            if(!window.requestAnimationFrame) {
                requestAnimationFrame = function (fn) {
                    setTimeout(fn, 1000/60);
                }
            }
            n = n || 0;
            rate = rate || 2;
            var step = function () {
                m = m + (n - m)/rate;
                if(m<1) {
                    cb(n, true);
                    return;
                }
                cb(m, false);
                requestAnimationFrame(step);
            }
            step();
        },
        
        //发送重置
        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();
});