﻿// ==UserScript==
// @name                tabs切换
// @description         统一的一个tabs切换功能
// @created date        2009/8/21   15:19
// @created author      Tiger(tigershi0507@gmail.com)
// @version             1.0.1
// @version owner       Copyright (C) 2009-2010 keerr.com All rights reserved.
// ==/UserScript==

/*************************************************
verified time 	:	2009/9/26   15:08
verified author	:	Tiger
verified purpose:	在toggleSearchTabs方法中增加了“全部”分类
**************************************************/
/*************************************************
verified time 	:	2009/10/16   10:40
verified author	:	Tiger
verified purpose:	改进toggleTabs的逻辑扩展
**************************************************/
/*************************************************
verified time 	:	2009/10/20   15:19
verified author	:	Tiger
verified purpose:	改进了searchbar的鼠标事件
**************************************************/
/*************************************************
verified time 	:	2009/10/29   19:07
verified author	:	Tiger
verified purpose:	修改了toggleTabs的callback的一个传入参数问题
**************************************************/
/********************************************************************
verified time 	:	2010/4/6   16:31
verified author	:	Tiger
verified purpose:	为新策划的bylou修改了头部search bar 
*********************************************************************/

var toggleSearchTabs = function(options) {
    var IndexOfHoveredLI,
        $searchInput = $("#search_input"),
        isKeyEnter = false,
        isInit = true,
    // 当input有值，但鼠标移动了分类，那这是存放分类值的变量
        tempStr;

    /// <summary>
    /// 搜索框的动作程序
    /// </summary>
    //    $searchInput
    //    .keyup(function(e) {
    //        //
    //        e = e || window.event;
    //        ;
    //        if (e.keyCode === 13) {
    //            // zai keerr.header.search.js中的doSearch()
    //            doSearch(encodeURIComponent($.trim(this.value)));
    //        }

    //        if ($(this).val() !== null || $(this).val() !== "") {
    //            isKeyEnter = true;
    //            //$searchInput.unbind();
    //        }
    //        else {
    //            isKeyEnter = false;
    //            $searchInput.blur(function() {
    //                $(this).val(tempStr);
    //            });
    //        }
    //        //isInit = false;           //????????????????????????????????????
    //    })
    //    .blur(function() {
    //        if (isKeyEnter && $(this).val() !== "") {
    //            return false;
    //        }
    //        else {
    //            $(this).val(tempStr);
    //            isKeyEnter = false;
    //            return true;
    //        }
    //        $(this).val(tempStr);

    //    });

    /// <summary>
    /// tabs切换的主动作程序，包括：mouseover
    /// </summary>
    //$(".search-bar > ul > li[class^='search']")
    $(".search-bar ul li")
    .click(function() {
        $("." + options.onCls).removeClass(options.onCls).addClass(options.offCls);
        $(this).removeClass(options.offCls).addClass(options.onCls);
        if (isInit && (!isKeyEnter || $searchInput.val() === "")) {
            headSearchType = IndexOfHoveredLI = $(".search-bar ul li").index(this);
            switch (IndexOfHoveredLI) {
                case 0:
                    $searchInput.val("输入关键字进行查询");
                    break;
                case 1:
                    $searchInput.val("输入资讯标题或内容的关键字进行查询");
                    break;
                case 2:
                    $searchInput.val("输入楼盘项目名称、地址等关键字进行查询");
                    break;
                case 3:
                    $searchInput.val("输入二手房地址等关键字进行查询");
                    break;
                case 4:
                    $searchInput.val("输入租房地址等关键字进行查询");
                    break;
                case 5:
                    $searchInput.val("输入地理名称进行查询");
                    break;
                case 6:
                    $searchInput.val("输入活动名称进行查询");
                    break;
                default:
            };
            tempStr = $searchInput.val();
            $searchInput
            //.blur()
            .focus(function() {
                $(this)
                .val("")
                .blur(function() {
                    if (isKeyEnter && $(this).val() === "") {
                        $searchInput.val(tempStr);
                        isKeyEnter = false;
                    }
                    $(this).css("color", "#CCCCCC");
                });
                $(this).css("color", "#000000");

            });
        }
        else {
            headSearchType = IndexOfHoveredLI = $(".search-bar ul li").index(this);
            switch (IndexOfHoveredLI) {
                case 0:
                    tempStr = "输入关键字进行全站查询查询";
                    break;
                case 1:
                    tempStr = "输入资讯标题或内容的关键字进行查询";
                    break;
                case 2:
                    tempStr = "输入楼盘项目名称、地址等关键字进行查询";
                    break;
                case 3:
                    tempStr = "输入二手房地址等关键字进行查询";
                    break;
                case 4:
                    tempStr = "输入租房地址等关键字进行查询";
                    break;
                case 5:
                    tempStr = "输入楼盘名称进行查询";
                    break;
                case 6:
                    tempStr = "输入关于问题的关键字进行查询";
                    break;
                case 7:
                    tempStr = "输入查找地址等关键字进行查询";
                    break;
                case 8:
                    tempStr = "输入活动名称、地址等关键字进行查询";
                    break;
                default:
            };
        }
        //$(".kws").hide();
        //$($(".kws").get(IndexOfHoveredLI)).show();
    });
};

$(function() {
    // 搜索框js初始入口
    toggleSearchTabs({ onCls: "searchtabon", offCls: "" });
    var urlPath = document.location.pathname,
        typeStr = urlPath.split("/")[1];
    switch (typeStr.toLowerCase()) {
        case "index.html":
        case "index.aspx":
        case "default.aspx":
            $(".search-bar ul > li:eq(1)").click();
            break;
        case "news":
            $(".search-bar ul > li:eq(1)").click();
            break;
        case "lp":
            $(".search-bar ul > li:eq(2)").click();
            break;
        case "map":
            $(".search-bar ul > li:eq(5)").click();
            break;
        case "fy":
            if (urlPath.split("/")[2].split(".")[0] === "zflist1" || urlPath.split("/")[2].split(".")[0] === "zf_search") {
                $(".search-bar ul > li:eq(4)").click();
            } else if (urlPath.split("/")[2].split(".")[0] === "seclist1" || urlPath.split("/")[2].split(".")[0] === "secsearch") {
                $(".search-bar ul > li:eq(3)").click();
            }
            break;
        default:
            $(".search-bar ul > li:eq(1)").click();
            break;
    }
    $("body").keydown(function(event) {
        return enterSubmit(event);
    });
});
function enterSubmit(e) {
    if (window.event)
        keyPressed = window.event.keyCode; // IE
    else
        keyPressed = e.which; // Firefox
    if (keyPressed === 13) {
        //login();
        $("#searchBtn").click();
        return false;
    }

}


function toggleKW(typeSel) {
    $(".kws").hide();
    $(typeSel).show();
}

/// <summary>
/// tabs切换功能
/// </summary>
/// <param name=""></param>
/// <param name=""></param>
(function($) {
    var d = {
    //        callbacks: function() {
    //            
    //        }
};
var flagFunc;   //全局变量，鼠标延时事件保护
$.fn.toggleTabs = function(options) {
    var s = options ? $.extend({}, d, options) : d;
    return this.each(function() {
        var $_thisObj = $(this),
            $_this_parent = $_thisObj.parents("." + s.ParentsCls);  // 以父元素来做保护限定
        $_thisObj
        .click(function() {
            //var $this = $(this);
            flagFunc = setTimeout(function() {
                if (!$_thisObj.hasClass(s.ShownTabsCls)) {
                    // 先将options.ShownTabsCls对应的内容部分隐藏掉
                    $("." + $("." + s.ShownTabsCls, $_this_parent).attr("id")).hide();
                    // 将options.ShownTabsCls对应的tab做隐藏切换处理
                    $("." + s.ShownTabsCls, $_this_parent).removeClass(s.ShownTabsCls).addClass(s.HiddenTabsCls);
                    // 将mouseover处的tab做显示切换
                    $_thisObj.removeClass(options.HiddenTabsCls).addClass(options.ShownTabsCls);
                    // 将mouseover的tab对应的内容部分显示出来
                    $("." + $_thisObj.attr("id")).css("display", "block");
                    if ($.isFunction(s.callbacks)) {
                        s.callbacks($_thisObj);
                    }
                }
                if ($.isFunction(s.callbacks)) {
                    s.callbacks($(this));
                }
            }, 150);
        });
        //.mouseout(function() {
        //    clearTimeout(flagFunc);
        //});
    });
};
})(jQuery);

///// <summary>
///// 获取参数
///// </summary>
///// <param name=""></param>
///// <param name=""></param>
//$.GetRequest = function() {
//    var url = document.location.search; //获取url中"?"符后的字串
//    var theRequest = new Object();
//    if (url.indexOf("?") != -1) {
//        var str = url.substr(1);
//        strs = str.split("&");
//        for (var i = 0; i < strs.length; i++) {
//            theRequest[strs[i].split("=")[0]] = decodeURI(strs[i].split("=")[1]);
//        }
//    }
//    return theRequest;
//}