/*
function urldecode(str){
// return str
    return decodeURIComponent(str.replace(/\+/g, '%20'));
}
*/
function urlencode(str){
    str = (str+'').toString();
    return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/%20/g, '+');
}


var no_msg = '暂无信息';
var huoche_post_action = '/live/bus';    //ajax地址
var checkUrlTimer;
var oldHash = "";
var searchTab;

//===================车站精确选择@zen=====================
var trainSeletor=(function(){
    var that=$(this);
    var uniqueInstance;
    var sType=0,
        sStep=0,
        target=null,
        tempData={
            0:{},
            1:{}
        },
        activeId="";
    var pURL="/?ct=live&ac=bus_station_api&flag=stat&city="+encodeURI(cur_city),
        cURL="/?ct=live&ac=bus_station_api&flag=line&city="+encodeURI(cur_city);
    //初始化内容框
    var initBox=function(){
        if(!that.MsgBox){
            that.MsgBox=$("<div id='js_train_search_msg' class='box-alert'></div>");
            that.MsgHead=$("<div id='js_train_search_msg_hd'></div>").appendTo(that.MsgBox);
            that.MsgContent=$("<div id='js_train_search_msg_content' class='box-base'></div>").appendTo(that.MsgBox);
            that.MsgClose=$("<span id='js_train_search_msg_cls'></span>").appendTo(that.MsgHead);
            that.MsgTitle=$("<span id='js_train_search_msg_tit'></span>").appendTo(that.MsgHead);
            that.Selected=$("<span id='js_train_search_msg_selector'></span>").appendTo(that.MsgHead);
            that.ReSelect=$("<span id='js_train_search_msg_reselect'>重选</span>").appendTo(that.MsgHead);
            $("body").append(that.MsgBox);
            that.ReSelect.bind("click",function(){
                Didplay();
            });
            that.MsgClose.bind("click",function(){
                Public.ScreenManager.Hide();
            });
        }
        that.Selected.html("");
        that.ReSelect.hide();
        sStep=0;
    }
    var Didplay=function(){
        initBox();
        Public.ScreenManager.Show(that.MsgBox[0]);
        that.MsgBox.show();
        sStep==0?that.ReSelect.hide():that.ReSelect.show();
        that.MsgTitle.text(sType==0?"选择索引：":"选择线路类型：");
        GetData();
    }
    //获取数据并写入html
    var GetData=function(data){
        url=sType==0?pURL:cURL;
        if(sStep){
            url=url+"&id="+data;
        }else{
            data=sType==0?"s":"l";
        }
        if(typeof tempData[sType][data]!=="undefined"){
            that.MsgContent.html(tempData[sType][data]);
            LiveEvent();
        }else{
            $.ajax({
                url:url,
                success:function(text){
                    eval(text);
                    if(typeof stationJSON!="undefine"){
                        var html="";
                        for(var i=0,j=stationJSON.length;i<j;i++){
                            if(sType==0){
                                html+='<li><span rel="'+i+'">'+stationJSON[i]+'</span></li>';
                            }else{
                                html+='<li><span rel="'+i+'">'+stationJSON[i]+'</span></li>';
                            }
                        }
                        tempData[sType][data]=html;
                        that.MsgContent.html(html);
                        LiveEvent();
                    }
                }
            });
        }
    }
    var LiveEvent=function(){
        that.MsgContent.find("span").click(function(){
            var text=$(this).text();
            if(sStep==0){
                that.Selected.html(text+" ");
                that.ReSelect.show();
                sStep=1;
                GetData($(this).attr("rel"));
            }else{
                target.val(text);
                sStep=0;
                initBox();
                Public.ScreenManager.Hide();
            }
        });
    }
    return {
        Bind:function(acElement,tarElement,type){
            acElement.bind("click",function(){
                sStep=0;
                sType=type;
                target=tarElement;
                activeId=this.id;
                Didplay();
            });
        }
    };
})();
//=======================车站精确选择@zen============

//初始化事件
var initEvents = function(){
    // 清除当前获得焦点的文本框 ID
    $('#js_box_focus').val('');
    
    // 点击输入框下的“例如”链接时
    $('#tab_box span.js_input_eg').click(function() {
        var text = $(this).text();
        if ($('#search_ss').css('display') != 'none') {
            if ($('#js_box_focus').val() == 'js_box_end_station') {
               $('#js_box_end_station').val(text);
               document.getElementById("js_box_end_station").focus();
            }
            else {
                $('#js_box_start_station').val(text);
                document.getElementById("js_box_start_station").focus();
            }
        }
        if ($('#search_s').css('display') != 'none') {
            $('#js_box_station').val(text);
            document.getElementById("js_box_station").focus();
        }    
        if ($('#search_t').css('display') != 'none') {
            $('#js_box_bus').val(text);
            document.getElementById("js_box_bus").focus();
        }
    });

    // 起始站 focus
    $('#js_box_start_station').focus(function() {
        $('#js_box_focus').val('js_box_start_station');
    });

    // 终点站 focus
    $('#js_box_end_station').focus(function() {
        $('#js_box_focus').val('js_box_end_station');
    });
    
    //设置默认城市
    $("#default_city").bind("click",SetDefaultCity);
    
    
    
    //车站的线路列表
    $("#result_table td.line-detail").click(function(){
        var detail=$(this).parent().next();
        if(detail.css("display")=="none"){
            detail.show();
        }else{
            detail.hide();
        }
    });
}
//城市选择
var citySeletor=function(){
    var stat=0;
    var ulCity=$("#bus_city_list");
    var btnChange=$("#change_city");
    var display=function(){
        if(!stat){
            ulCity.show();
            btnChange.html("关闭");
            stat=1;
        }else{
            ulCity.hide();
            btnChange.html("更换");
            stat=0;
        }
    }
    btnChange.click(function(){
        display();
    });
    $(document).click(function(e){
        var ele=e.srcElement?e.srcElement:e.target;
        //alert(ele.id);
        if(ele.id!="change_city"){
            stat=1;
            display();
        }
    });
}


//初始化TAB
var initTab = function(){
    searchTab = new tab_content(
            [{TabId:"js_tap_search_ss",ContentId:"search_ss",Type:0},
            {TabId:"js_tap_search_t",ContentId:"search_t",Type:2},
            {TabId:"js_tap_search_s",ContentId:"search_s",Type:1}],"active"
        );
}
// 处理天气
var handle_weather = function(result, callback) {
    if (!result) {
        return false;
    }
    var obj_tr = $('#js_weather tr:first').clone();
    $('#js_weather tr').remove();
    for (i in result) {
        var obj_city = obj_tr.find('.js_tq_city');
        var obj_tq = obj_tr.find('.js_tq_tq');
        var obj_temp0 = obj_tr.find('.js_tq_temp span:first');
        var obj_temp1 = obj_tr.find('.js_tq_temp span:last');
        obj_city.attr('href', tq_url + result[i].city);
        obj_city.html(result[i].city);
        obj_tq.html(result[i].desc[0]);
        obj_temp0.html(result[i].temp[1]);
        obj_temp1.html(result[i].temp[0]);
        $('#js_weather').append(obj_tr.clone().removeClass('hide'));
    }
    //callback();
}
//设置默认城市
var SetDefaultCity = function(){
	var city = '';
	try{
		city = cur_url;
	}catch(err){
		var ss_url = document.forms["search_ss"].action.split("/");		
		city = ss_url[4];
	}
	
    Public.Cookie.Set("tool_bus_city",city);	
    PageResult.Show("设置成功",true);
    return true;
}

$(document).ready(function(){
    initTab();
    initEvents();
    citySeletor();
    searchTab.SetChangeHandler(function(){   //设置转换Tab事件
        $("#js_start_train_search_msg,#js_end_train_search_msg,#js_box_train_search_msg,#js_box_station_search_msg").hide();
    });
    trainSeletor.Bind($("#js_box_start_station_btn"),$("#js_box_start_station"),0);
    trainSeletor.Bind($("#js_box_end_station_btn"),$("#js_box_end_station"),0);
    trainSeletor.Bind($("#js_box_train_btn"),$("#js_box_bus"),1);
    trainSeletor.Bind($("#js_box_station_btn"),$("#js_box_station"),0);

});