﻿/*
 新版的下拉框
 *--------------------------------------------------------------------------*/

Class("Qcyx.dropDownList", {
    dropDownObj: null ,  //地图下拉框对象
    dropStyle: null,  //存放下拉框的样式
    inputELE: null,  //输入的文本框
    cityCode: '110100',
    onChenge: null,
    onOver: null,
    onkeyDown: null,
    onkeyup: null,
    join: true,
    bindPoint: null,
    self: null,
    dropDownList: method( 
        function (nEle,input){
            this.control();
            this.inputELE = input;
            
            this.onChenge = new sfs.delegate();
            this.onOver = new sfs.delegate();
	        this.onkeyDown = new sfs.delegate();
	        this.onkeyup = new sfs.delegate();
	        
            this.onload.add(this.load.bind(this));
            
        },
        function (nEle, input,onSelectItem) {
            this.control();
            this.inputELE = input;
            
            this.onChenge = new sfs.delegate();
            this.onOver = new sfs.delegate();
	        this.onkeyDown = new sfs.delegate();
	        this.onkeyup = new sfs.delegate();
	        
            if( onSelectItem && typeof(onSelectItem) == "function") this.onChenge.add( onSelectItem.bind(this) );
            this.onload.add(this.load.bind(this));
        }
    ),
    load: function(){
        this.self = this;
        sfs.page.onloaded.add( (function(){
            this.initMapDropDown();
        }).bind(this));
    },
    /*初始化地图下拉框组件*/
    initMapDropDown: function(){
        function hasWinObject(name){
            names = name.split('.');
            hasFind = false;
            if( window[names[0]] != undefined){
                pObj = window[names[0]];
                hasFind = true;
                for( i=1, len = names.length; i<len; i++){
                    hasFind = hasFind && ( pObj[names[i]] != undefined );
                    if( !hasFind) break;
                    pObj = pObj[names[i]];
                }
            }
            return hasFind;
        }
        
        if( hasWinObject('QcyxModule.infoList')){
            mapObj = null;
            
            for( a in window){
                try{
                    if( window[a] != undefined ){
                        if( window[a].BaseMap != undefined && typeof window[a].BaseMap == 'function' 
                            && window[a].GUID != undefined )
                            mapObj = window[a];
                            break;
                    }
                }catch(e){}
            }
            
            if( mapObj == null){
                mapNames = ['map','_Map','__Map','MAPOBJ'];
                for( i=0; i<mapNames.length; i++){
                    try{
                        name = mapNames[i];
                        if( window[name] != undefined ){
                            if( window[name].BaseMap != undefined && typeof window[name].BaseMap == 'function' 
                                && window[name].GUID != undefined )
                                mapObj = window[name];
                        }
                    }catch(e){}
                }
            }
            
            style = this.dropStyle || {};
            
            ddO = new QcyxModule.infoList(mapObj, this.inputELE.id, style);
            ddO.parentSelf = this;
            if( mapObj == null) {
                if( hasWinObject('ICT.Tools')){
                    ddO.toolsInstance = new ICT.Tools();
                    ddO.city( this.cityCode);
                }
            }
            
            if( this.join != null && ddO.join != null){
                ddO.join( this.join);
            }
            
            function onOptionCreate(obj,i){
                node = document.createElement('span');
                node.innerHTML = obj.info.adminname;
                return node;
            }
            function onOptionOver (e){
                pa = this.parentSelf;
                rs = this.options[ this.selectIndex];
                rs.site = rs.x+','+rs.y;
                rs.poiid = rs.pid;
                rs.poiFrom = rs.from;
                pa.onOver.execute(rs);
            }
            function onOptionChange (e){
                pa = this.parentSelf;
                rs = this.options[ this.selectedIndex];
                rs.site = rs.x+','+rs.y;
                rs.poiid = rs.pid;
                rs.poiFrom = rs.from;
                pa.onChenge.execute(rs);
            }
            
//            //下拉框事件
            ddO.regEvent('onOptionOver', onOptionOver);
            ddO.regEvent('onChange', onOptionChange );
            ddO.regEvent('onOptionCreate',  onOptionCreate );
            ddO.regEvent('onKeyUp', 
                (function(){
                    pa = this.parentSelf;
                    pa.onkeyup.execute.apply(pa.onkeyup, Array.toArray(arguments));
                }) );
            ddO.regEvent('onKeyDown', 
                (function(){
                    pa = this.parentSelf;
                    pa.onkeyDown.execute.apply(pa.onkeyDown,Array.toArray(arguments));
                }) );
            
            //自动绑定坐标
            if( this.bindPoint){
                type = this.bindPoint.type;
                if( type =='var') type = 'variable';
                if( type =='vars') type = 'variables';
                ddO.setAutoBind( type, this.bindPoint.id );
            }
            ddO.load();
            this.dropDownObj = ddO;
        }
    }
}, sfs.page.control);
