﻿
var checkcity = {
    debug: false,
    FriendList: [],   //航班列表
    FriendChecked: [],   //航班已选择
    upAndDownIndex: -1,
    upAndDowncurr: 0,
    loadcitylist: [],
    PopTextDiv: "", //创建层的名字
    friends: "",  //数据
    textboxID: "",  //文本框ID
    hidstring: "val",
    _postUrl: "",

    //相当于onload事件

    windload: $(document).ready(function() {

        checkcity.init();
    }
		  ),

    //初始化数据
    init: function() {
        //加载初始数据
        checkcity.loadcity();


        //friends = "AAT,阿勒泰,alt,ALETAI#PVG,上海,sh,ShangHai#PEK,北京,bj,beijing#bbc,阿勒,zg,ale#gbc,天津,tj,tianjing#CAN,广州,gz,guangzhou#bbc,深圳,sz,shenzhen#gbc,成都,cd,chengdu#PVG,昆明,km,kunming#PVG,海口,hk,haikou#PVG,重庆,cq,chongqing#PVG,杭州,hz,hangzhou#PVG,武汉,wh,wuhan#PVG,大连,dl,dalian";
        var tempFs = friends.split("#");
        for (var i = 0; i < tempFs.length; i++) {
            if (tempFs[i] != "") {
                var friend = new Array();

                friend.push(tempFs[i].split(",")[0]);
                friend.push(tempFs[i].split(",")[1]);
                friend.push(tempFs[i].split(",")[2]);
                friend.push(tempFs[i].split(",")[3]);
                this.FriendList.push(friend);
            }
        }

    },
    loadcity: function() //加载12城市
    {

        loadstring = "bjs,北京,bj,beijing#shh,上海,sh,ShangHai#CAN,广州,gz,guangzhou#SZX,深圳,sz,shenzhen#CTU,成都,cd,chengdu#KMG,昆明,km,kunming#HAK,海口,hk,haikou#CKG,重庆,cq,chongqing#HGH,杭州,hz,hangzhou#WUH,武汉,wh,wuhan#TSN,天津,tj,tianjing#DLC,大连,dl,dalian";
        var tempFs = loadstring.split("#");
        for (var i = 0; i < tempFs.length; i++) {
            if (tempFs[i] != "") {
                var city = new Array();
                city.push(tempFs[i].split(",")[0]);
                city.push(tempFs[i].split(",")[1]);
                city.push(tempFs[i].split(",")[2]);
                city.push(tempFs[i].split(",")[3]);
                this.loadcitylist.push(city);
            }
        }

    },
    createDiv: function() {
        //层的名字
        this.PopTextDiv = this.textboxID + "PopTextDiv";
        if (document.getElementById(this.PopTextDiv) == null) {
            //创建弹出层
            popDiv = $('<div></div>');
            popDiv.attr("id", this.PopTextDiv);
            //<div style=\"height:300px;background-color:yellow\">
            popDiv.html("<iframe src=\"\" frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\" scrolling=\"no\" style=\"position:absolute;top:0px;left:0px;width:100%;height:100%;z-index:-1;\"></iframe>");
            var position = checkcity._getPopPosition(this.textboxID);
            popDiv.css("left", position.left + 'px');
            popDiv.css("top", position.top + 'px');
            //            popDiv.css("z-index","205");
            popDiv.css("display", "none");
            popDiv.attr("class", "divcss");


            $("#" + this.textboxID).parent().append(popDiv);

        }

    },
    //隐藏层
    hiddenFriendListDiv: function(e) {
        checkcity.checkSubmit(e);
        $("#" + this.PopTextDiv).css('display', 'none');
        if (document.getElementById("seltype") != null) {

            var sels = $("#seltype").find("select");
            for (var k = 0; k < sels.length; k++) {
                $(sels[k]).css("visibility", "visible");
            }
        }

    },

    checkUp: function(e) //键盘向上
    {

        var div = document.getElementById(this.PopTextDiv);
        if (div.style.display != "none") {
            var divchild = div.getElementsByTagName("p");
            var divlength = divchild.length;

            //捕捉颜色为红的
            for (var i = 0; i < divlength; i++) {
                if ($(divchild[i]).attr("class") == "aaa") {
                    this.upAndDownIndex = i;
                }
            }



            if (this.upAndDownIndex <= 0) {
                this.upAndDownIndex = divlength;
            }

            this.upAndDownIndex--;
            for (var i = 0; i < divlength; i++) {
                if (i == this.upAndDownIndex) {
                    divchild[i].className = "aaa";
                }
                else {
                    divchild[i].className = "";
                }
            }

            if (this.upAndDownIndex < 0) {
                this.upAndDownIndex = divlength;
            }

        }
    },
    checkDown: function(e) //键盘向下
    {

        var div = document.getElementById(this.PopTextDiv);
        if (div.style.display != "none") {
            var divchild = div.getElementsByTagName("p");
            var divlength = divchild.length;

            //捕捉颜色为红的
            for (var i = 0; i < divlength; i++) {
                if ($(divchild[i]).attr("class") == "aaa") {
                    this.upAndDownIndex = i;
                }
            }


            if (this.upAndDownIndex >= divlength - 1) {
                this.upAndDownIndex = -1;
            }

            for (var i = 0; i < divlength; i++) {
                if (i == this.upAndDownIndex + 1) {
                    divchild[i].className = "aaa";
                }
                else {
                    divchild[i].className = "";
                }
            }
            this.upAndDownIndex++;
            if (this.upAndDownIndex >= divlength) {
                this.upAndDownIndex = -1;
            }

        }
    },
    checkSubmit: function(e) //回车提交
    {
        var div = document.getElementById(this.PopTextDiv);
        var divchild = div.getElementsByTagName("p");
        var citycode;
        var cityname;
        if (this.upAndDownIndex > -1) //正常
        {

            citycode = $(divchild[this.upAndDownIndex]).attr("C_code");
            cityname = divchild[this.upAndDownIndex].getElementsByTagName("span")[0].innerHTML;
        }
        else  //鼠标滑过
        {
            for (var i = 0; i < divchild.length; i++) {

                if ($(divchild[i]).attr("class") == "aaa") {
                    citycode = $(divchild[i]).attr("C_code");
                    cityname = divchild[i].getElementsByTagName("span")[0].innerHTML;
                }
            }

        }
        $("#" + this.textboxID).val(""); //清值
        $("#" + this.textboxID + this.hidstring).val(""); //清隐藏值
        $("#" + this.textboxID).val(cityname); //值
        $("#" + this.textboxID + this.hidstring).val(citycode); //隐藏值

    },
    onkeydown: function(e)   //按下键事件
    {

        if (e.keyCode == 13)//确定
        {

            checkcity.checkSubmit(e);

            return false;
            this.upAndDownIndex = -1;
            this.upAndDowncurr = 0;

        }


    },
    filterDataWhere: function() //进行数据筛选
    {

        var text = $("#" + this.textboxID).val();
        var friendlistDiv = $("#" + this.PopTextDiv);
        friendlistDiv.html("");
        friendlistDiv.html("<iframe src=\"\" frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\" scrolling=\"no\" style=\"position:absolute;top:0px;left:0px;width:100%;height:100%;z-index:-1;\"></iframe>");

        var flist = new Array();
        if (text && text != '') {
            for (var i = 0; i < this.FriendList.length; i++) {
                if (this.FriendList[i][0].toLocaleLowerCase().indexOf(text.toLocaleLowerCase

()) == 0 || (this.FriendList[i][1].toLocaleLowerCase().indexOf(text.toLocaleLowerCase()) == 0) || (this.FriendList[i]

[2].toLocaleLowerCase().indexOf(text.toLocaleLowerCase()) == 0) || (this.FriendList[i][3].toLocaleLowerCase().indexOf(text.toLocaleLowerCase()) == 0)) {

                    //判断是否已经选中
                    var haveChecked = true;

                    for (var j = 0; j < this.FriendChecked.length; j++) {
                        if (this.FriendChecked[j][1] == this.FriendList[i][1]) {
                            haveChecked = false;
                            break;
                        }
                    }

                    if (haveChecked) {
                        //尚未选中的填入待选择列表
                        var friend = new Array();
                        friend.push(this.FriendList[i][0]);
                        friend.push(this.FriendList[i][1]);
                        friend.push(this.FriendList[i][2]);
                        friend.push(this.FriendList[i][3]);
                        flist.push(friend);
                    }
                }
            }
            //排序
            return flist.sort();

        }
        else {
            return this.loadcitylist;
        }

    },
    // 开始输入
    onkeyup: function(e, flag) {
        //给当前文本框赋值
        checkcity.getdivid();
        checkcity.createDiv();
        if (e.keyCode == 13) {

            checkcity.hiddenFriendListDiv(e);
            return;
        }
        else {
            if (e.keyCode == 38)//向上
            {
                checkcity.checkUp(e);
                return;
            }
            else if (e.keyCode == 40)//向下 
            {

                checkcity.checkDown(e);
                return
            }

            var pList = new Array();
            this.upAndDownIndex = -1;
            this.upAndDowncurr = 0;

            var text = $("#" + this.textboxID).val();


            var friendlistDiv = $("#" + this.PopTextDiv);

            friendlistDiv.html("");
            friendlistDiv.html("<iframe src=\"\" frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\" scrolling=\"no\" style=\"position:absolute;top:0px;left:0px;width:100%;height:100%;z-index:-1;\"></iframe>");


            var ShowFriendList = new Array();
            ShowFriendList = null;
            if (flag == 'init') //得到焦点
            {
                $("#" + this.textboxID).val("");
                ShowFriendList = this.loadcitylist;
            }
            else {
                ShowFriendList = checkcity.filterDataWhere();
            }

            pList.length = 0;
            if (ShowFriendList != null) {
                for (var i = 0; i < ShowFriendList.length; i++) {
                    var lable = $('<p></p>');
                    lable.html("<span>" + ShowFriendList[i]
[1] + "</span><span>" + ShowFriendList[i][3] + "</span><span style='display:none'>" + ShowFriendList[i][0] + "</span>");
                    lable.attr("C_code", ShowFriendList[i][0]);


                    //移上
                    lable.mouseover(
							function(e) {
							    for (var i = 0; i < pList.length; i++) {
							        pList[i].className = "";
							    }
							    var target = e.target;
							    while (target.tagName != "P") {
							        target = target.parentNode;
							    }
							    target.className = "aaa";
							    $(target).css("cursor", "pointer");
							    //target.setAttribute("style", "cursor:pointer");


							}
							 );

                    //移出
                    lable.mouseout(
							 function(e) {
							     var target = e.target;
							     while (target.tagName != "P") {
							         target = target.parentNode;
							     }
							     target.className = ""; //绉婚櫎鏍峰紡
							 }
							);

                    //选 中
                    lable.click(
								function(e) {
								    //添加逻辑
								    var target = e.target;
								    while (target.tagName != "P") {
								        target = target.parentNode;
								    }
								    var cityname = target.getElementsByTagName

("span")[0].innerHTML;
								    var citycode = target.getElementsByTagName

("span")[2].innerHTML;

								    $("#" + this.textboxID).val(""); //值
								    $("#" + this.textboxID + this.hidstring).val(""); //隐藏值
								    $("#" + this.textboxID).val(cityname); //值

								    $("#" + this.textboxID + this.hidstring).val(citycode); //隐藏值
								    checkcity.hiddenFriendListDiv(e); //隐藏

								}
							);

                    pList.push(lable);
                    friendlistDiv.append(lable);
                    //默认第一个为红色
                    var div = document.getElementById(this.PopTextDiv);

                    div.getElementsByTagName("p")[0].className = "aaa";

                }

            }
            else {
                return;


            }
            friendlistDiv.scrollTop = 0;
            if (friendlistDiv.children().length <= 0)
            //friendlistDiv.css('display','none'); 
                friendlistDiv.html("<p style='background-color:red'>没有相应数据</p>");

            else
                friendlistDiv.css('display', 'block');
        }
        checkcity.hidSelect(this.PopTextDiv);
    },

    // 获取弹出层坐标
    _getPopPosition: function(divval) {
        var position;
        position = {
        left: $("#" + divval).position().left,
        top: $("#" + divval).position().top + $("#" + divval).height()
        };
        return position;
    },
    getdivid: function() {
        if ($("#" + document.activeElement.id).attr("city") == "yes") {

            this.textboxID = document.activeElement.id;
        }
    },
    hidSelect: function(divdd) {
        if (document.getElementById("seltype") != null) {
            //下拉框的高度
            var divheight = $("#" + this.PopTextDiv).height();
            var sels = $("#seltype").find("select");
            for (var k = 0; k < sels.length; k++) {
                var dropid = $(sels[k]).attr("id");
                var selheight = checkcity._getPopPosition(dropid).top - checkcity._getPopPosition(this.textboxID).top;
                if (divheight > selheight) {
                    $(sels[k]).css("visibility", "hidden");
                }
                else {
                    $(sels[k]).css("visibility", "visible");
                }
            }
        }
    }
}
