﻿// 变量定义
var selectType = "0";//功能类别
var busType = "0";//公交功能类别
var selectcity = "010";//当前城市
var g_Type = ["mapsearch", "roundsearch", "bussearch", "navsearch"];//类别分类
var url = ""; //url前缀
var currBusQuery = "Transfer"; //公交查询类别
var Prompt = {
    //提示
    SEARCH_KEYWORD_IS_NULL: "请输入要搜索的关键字",
    NAV_BEGIN: "请输入起点",
    NAV_END: "请输入终点",
    SEARCH_MAP_HINT: "输入您想搜索的地名",
    SEARCH_SITE_NAME: "请输入要查询的站点名称",
     SEARCH_LINE_NAME: "请输入要查询的线路名称",
    //默认值
    DEFAULT_ROUND_BEGIN:"例：火车站",
    DEFAULT_ROUND_END:"例：酒店",
    DEFAULT_BUS_BEGIN: "例：火车站",
    DEFAULT_BUS_END: "例：飞机场",
    DEFAULT_NAV_BEGIN: "例：火车站",
    DEFAULT_NAV_END: "例：飞机场"
};

var arr = new Array(Prompt.SEARCH_MAP_HINT, Prompt.SEARCH_SITE_NAME, Prompt.SEARCH_LINE_NAME, Prompt.DEFAULT_ROUND_BEGIN, Prompt.DEFAULT_ROUND_END, Prompt.DEFAULT_BUS_BEGIN, Prompt.DEFAULT_BUS_END);

//首页加载事件
$(document).ready(function() {
    //初始化默认值
    $("#txtShop").val(Prompt.SEARCH_MAP_HINT);
    $("#txtBound1").val(Prompt.DEFAULT_ROUND_BEGIN);
    $("#txtBound2").val(Prompt.DEFAULT_ROUND_END);
    $("#txtBusFrom").val(Prompt.DEFAULT_BUS_BEGIN);
    $("#txtBusTo").val(Prompt.DEFAULT_BUS_END);
    $("#txtBusLine").val(Prompt.SEARCH_LINE_NAME);
    $("#txtBusStation").val(Prompt.SEARCH_SITE_NAME);
    $("#txtFrom").val(Prompt.DEFAULT_NAV_BEGIN);
    $("#txtTo").val(Prompt.DEFAULT_NAV_END);
    //绑定回车事件
    $(":text").each(function(i) {
        var isEnter = this.name;
        $(this).bind('keydown', function(e) {
            bindenter(e, isEnter, i);
        });
    });

    // 初始化功能导航事件
    $(".menu").find("a").each(function(i) {
        $(this).click(function() {
            $(".menu").find("a").removeClass("active");
            $(this).addClass("active");
            funchange(i);
        });
    });

    // 初始化公交功能导航事件
    $(".bus_m").find("label").each(function(i) {
        $(this).click(function() {
            $(".bus_m").find("label").removeClass("radioOn").addClass("radioOff");
            $(this).removeClass("radioOff").addClass("radioOn");
            currBusQuery = $(this).find("input").attr("id");
            busfunchange(i);
        });
    });

    // 城市选择列表显示
    $(".city").hover(function() {
        $(".top_city_list").css("display", "block");
    }, function() {
        $(".top_city_list").css("display", "none");
    });
    // 选择城市
    $(".top_city_list").hover(function() {
        $(".top_city_list").css("display", "block");
    }, function() {
        $(".top_city_list").css("display", "none");
    }).find("a").click(function() {
        selectcity = $(this).attr("id"); //获取当前城市id
        $(".city_word").text($(this).text()); //设置选择城市文本
        //隐藏城市列表
        $(".top_city_list").hide();
        $(".top_city_list").css("display", "none");
    });
IpToCity_Page_OnLoad();
    //选择输入框
    $("input[type='text']").click(function() {
        if ($.inArray($(this).val(), arr) >= 0) {
            $(this).select();
        }
    });
});
// 绑定回车事件
function bindenter(e,isenter,i) {
    var key = e.which;
    if (key == 13) {
        //if (isenter=="true") {
            search();
        //}
        //else {
        //    var n = i + 1;
        //    $("input[type='text']:eq(n)").focus();
        //    $("input[type='text']:eq(n)").select();
        //}
    }
}

// 功能显示切换
function funchange(type) {
    selectType = type;
    $(".box_abcd > div").each(function(i) {
        if (i == type) $(this).show(); else $(this).hide();
    });
}

// 公交功能显示切换
function busfunchange(bustype) {
    busType = bustype;
    $(".box_c > div").each(function(i) {
        if (i < 3) {
            if (i == bustype) $(this).show(); else $(this).hide();
        }
    });
}

//取城市url前缀
function getUrl() {
    switch (selectcity) {
        case "027":
            url = "http://wh.ishowchina.com/shopping/";
            break
        case "010":
            url = "http://bj.ishowchina.com/shopping/";
            break
        case "021":
            url = "http://sh.ishowchina.com/shopping/";
            break
        case "020":
            url = "http://gz.ishowchina.com/shopping/";
            break
        case "00852":
            url = "http://hk.ishowchina.com/shopping/";
            break
        case "00853":
            url = "http://mo.ishowchina.com/shopping/";
            break
 	case "0413":
            url = "http://fs.ishowchina.com/shopping/";
            break
 	case "028":
            url = "http://cd.ishowchina.com/shopping/";
            break
	case "0837":
            url = "http://jzg.ishowchina.com/shopping/";
            break
	case "0454":
            url = "http://jms.ishowchina.com/shopping/";
            break
    }
}

//搜索
function search() {
    //取url
    getUrl();
    //地图搜索
    if ($("#mapsearch").attr("class") == "active") {
        vKeyWord = $("#txtShop").val();
        if (vKeyWord == "" || vKeyWord == Prompt.SEARCH_MAP_HINT) {
            alert(Prompt.SEARCH_KEYWORD_IS_NULL);
            $("#txtShop").focus();
            return;
        }
        location.href = url + "index/index.aspx?url=" + encodeURIComponent("../search/search.aspx?flag=poi&keyWord=" + encodeURIComponent(vKeyWord)) + "&searchType=shop";
        return;
    }
    //周边查询
    if ($("#roundsearch").attr("class") == "active") {
        vFrom = $("#txtBound1").val();
        vTo = $("#txtBound2").val();
        if (vFrom == "" || vFrom == Prompt.DEFAULT_ROUND_BEGIN) {
            alert(Prompt.SEARCH_KEYWORD_IS_NULL);
            $("#txtBound1").focus();
            return;
        }
        if (vTo == "" || vTo == Prompt.DEFAULT_ROUND_END) {
            alert(Prompt.SEARCH_KEYWORD_IS_NULL);
            $("#txtBound2").focus();
            return;
        }
          location.href = url+"index/index.aspx?url=" + encodeURIComponent("../search/search_bound.aspx?flag=Bound&keyWord=" + encodeURIComponent(vTo) + "&location=" + encodeURIComponent(vFrom)) + "&searchType=bound";
        return;
    }
    //公交查询
    if ($("#bussearch").attr("class") == "active") {
	if(selectcity=="0837"){
	alert("该城市暂时不提供公交信息!");
	return;
	}
        //公交换乘
        if (currBusQuery == "Transfer") {
            vFrom = $("#txtBusFrom").val();
            vTo = $("#txtBusTo").val();
            if (vFrom == "" || vFrom == Prompt.DEFAULT_BUS_BEGIN) {
                alert(Prompt.NAV_BEGIN);
                $("#txtBusFrom").focus();
                return;
            }
            if (vTo == "" || vTo == Prompt.DEFAULT_BUS_END) {
                alert(Prompt.NAV_END);
                $("#txtBusTo").focus();
                return;
            }

            location.href = url + "index/index.aspx?url=" + encodeURIComponent(url + "busnav/busnav_transfer_list.aspx?endPOI=" + encodeURI(vTo) + "&startPOI=" + encodeURI(vFrom) + "&type=Poi2Poi&step=1") + "&searchType=bus_transfer";
        }
        //线路查询
        else if (currBusQuery == "Line") {
            vFrom = $("#txtBusLine").val();
            if (vFrom == "" || vFrom == Prompt.SEARCH_LINE_NAME) {
                alert(Prompt.SEARCH_MAP_HINT);
                $("#txtBusLine").focus();
                return;
            }

            location.href = url + "index/index.aspx?url=" + encodeURIComponent(url + "busnav/busnav_line_list.html?busLineKey=" + vFrom) + "&searchType=bus_line";
        }
        //站点查询
        else if (currBusQuery == "Station") {
            vFrom = $("#txtBusStation").val();
            if (vFrom == "" || vFrom == Prompt.SEARCH_SITE_NAME) {
                alert(Prompt.SEARCH_SITE_NAME);
                $("#txtBusStation").focus();
                return;
            }

            location.href = url + "index/index.aspx?url=" + encodeURIComponent(url + "busnav/busnav_station_list.html?stationKey=" + vFrom) + "&searchType=bus_station";
        }
    }
    //驾驶查询
    if ($("#navsearch").attr("class") == "active") {
        vFrom = $("#txtFrom").val();
        vTo = $("#txtTo").val();
        if (vFrom == "" || vFrom == Prompt.DEFAULT_NAV_BEGIN) {
            alert(Prompt.NAV_BEGIN);
            $("#txtFrom").focus();
            return;
        }
        if (vTo == "" || vTo == Prompt.DEFAULT_NAV_END) {
            alert(Prompt.NAV_END);
            $("#txtTo").focus();
            return;
        }

        location.href = url + "index/index.aspx?url=" + encodeURIComponent(url + "carnav/index.aspx?CityCode=" + top.g_currentCity + "&Nav_ToKey=" + encodeURI(vTo) + "&Nav_FromKey=" + encodeURI(vFrom) + "&NavType=Poi2Poi&step=1") + "&searchType=car";
    }
}



//添加到收藏
function AddFavorite(sURL, sTitle) {
    var url = sURL.href;
    if (url.substring(url.length - 1) == "/")
        url = url.substring(0, url.length - 1);
    try {
        window.external.addFavorite(url, sTitle);
    }
    catch (e) {
        try {
            window.sidebar.addPanel(sTitle, url, "");
        }
        catch (e) {
            alert("加入收藏失败，请使用Ctrl+D进行添加!");
        }
    }
}
//设为首页
function SetHome(obj, vrl) {
    try {
        obj.style.behavior = 'url(#default#homepage)'; obj.setHomePage(vrl);
    }
    catch (e) {
        if (window.netscape) {
            try {
                netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
            }
            catch (e) {
                //alert(Prompt.SHOPPING_SET_HOME_ERROR);
                alert("此操作被浏览器拒绝！\n请在浏览器地址栏输入“about:config”并回车\n然后将[signed.applets.codebase_principal_support]设置为'true'");
            }
            var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
            prefs.setCharPref('browser.startup.homepage', vrl);
        }
    }
}

//获取城市网址
function IpToCity_Page_OnLoad() {
    var paramObject = new Object();
    paramObject["requestMethod"] = "getCityAddress";
    paramObject["ipAddress"] = "";
    $.ajax({
        url: "ajax/index.aspx",                     //后台处理程序
        type: 'post',                                   //数据发送方式
        dataType: 'json',                            //接受数据格式
        data: paramObject,                        //要传递的数据
        success: GetCityAddressAjaxReturn  //回调方法
    });
}

//获取城市回调方法
function GetCityAddressAjaxReturn(json) {
    var strMethod = json["requestMethod"];
    switch (strMethod) {
        case "getCityAddress":
            {
		var currCity = json["province"];
		$(".top_city_list").find("a").each(function(){
			var cityname = $(this).text();
			
			if(currCity.indexOf(cityname) > -1){
			  $(this).click();
			}
		});
            }
            break;
    }
}
