/**
* 弹出导航菜单 
* 
* @author 庞超
***/
Ext.ns("ssh.util");
ssh.util.MEvent = function () {
    this.mouseCoords = function (e) {
        return { 
             x:e.clientX + document.documentElement.scrollLeft - document.body.clientLeft, 
             y:e.clientY + document.documentElement.scrollTop - document.body.clientTop 
        }; 
    };
    
    this.GetEvent = function () {
        if(document.all) { //IE
            return window.event;
        }
        var func = this.GetEvent.caller; // 返回调用本函数的函数 走天涯
        while(func !== null) {
            // Firefox 中一个隐含的对象 arguments，第一个参数为 event 对象 学校介绍
            var arg0 = func.arguments[0];
            // alert('参数长度：' + func.arguments.length);
            if(arg0) {
                if((arg0.constructor == Event || arg0.constructor == MouseEvent)
                ||(typeof(arg0) == "object" && arg0.preventDefault && arg0.stopPropagation)) {
                     return arg0;
                }
            }
            func = func.caller;
        }
        return null;
    };
}

ssh.util.CCMenu = function(mid,pid){
	this.nodeObj = Ext.get(mid);
	this.panel = Ext.get(pid);
	this.isShow =true;
	this.lock = true;
	this.menuItem = {rect:{}};
    this.itemPanel = {rect:{}};
    this.menuItems = new Array();

    this.addItems = function(mid,pid){
    	var item = new ssh.util.CCMenu(mid, pid);
    	this.menuItems.push(item);
    	//item.isShow = false;
    };   
	this.panel.setStyle("display", "block");
	this.init =function(){
        this.menuItem.rect.x = this.nodeObj.getX() ;
        this.menuItem.rect.y = this.nodeObj.getY() ;
        this.menuItem.rect.r = this.nodeObj.getX() + this.nodeObj.getWidth();
        this.menuItem.rect.b = this.nodeObj.getY() + this.nodeObj.getHeight() ;
        
        this.itemPanel.rect.x = this.panel.getX();
        this.itemPanel.rect.y = this.panel.getY();
        this.itemPanel.rect.r = this.panel.getX() + this.panel.getWidth() ;
        this.itemPanel.rect.b = this.panel.getY() + this.panel.getHeight() ;
    }
    this.init();
    this.panel.setStyle("display", "none");
    
	this.show = function (){
		this.panel.setStyle("display", "block");
	};
    
    this.hide = function () {
       if(this.isShow){
           var m =this;
           setTimeout(function(){
              if(m.isShow === false) {
                   m.panel.setStyle("display", "none");
              }
          },500);
          this.isShow=false;
       }
    };
    
    this.closePanel = function () {
        if(this.isShow === false) {
           this.panel.setStyle("display", "none");
           isShow = true;
        }
    };
	
	this.doVisible = function(m){
		  if(m.x>this.menuItem.rect.x && m.y>this.menuItem.rect.y && 
                   m.x<this.menuItem.rect.r && m.y<this.menuItem.rect.b){
                   this.show();
          }else if(m.x>this.itemPanel.rect.x && m.y>this.itemPanel.rect.y 
                       && m.x<this.itemPanel.rect.r && m.y<this.itemPanel.rect.b){
	          this.show();
	          for(var i=0; i<this.menuItems.length; i++) {
	             this.menuItems[i].doVisible(m);
	          }
          }else if(this.isShow){
          	this.show();
          }else{
            this.hide();
          }
	};
	
	this.isVisible = function (m) {
		 if(m.x>this.menuItem.rect.x && m.y>this.menuItem.rect.y && 
                   m.x<this.menuItem.rect.r && m.y<this.menuItem.rect.b){
               return true;
          }
          if(m.x>this.itemPanel.rect.x && m.y>this.itemPanel.rect.y 
                       && m.x<this.itemPanel.rect.r && m.y<this.itemPanel.rect.b){
             return true;
          }
          for(var i = 0; i < this.menuItems.length; i++) {
          	if(this.menuItems[i].isVisible(m)) {
                return true;
          	}
          }
          return false;
	};
}  

ssh.util.CCMenu.Tip = function (array){
     document.body.onmousemove = function () {
        var ev = new ssh.util.MEvent();
        var m = ev.mouseCoords(ev.GetEvent());
        ssh.util.CCMenu.Tip.show(m, array);
     };
        
    ssh.util.CCMenu.Tip.show = function (m, array) {
    	for(var i = 0; i< array.length; i++){
    	    array[i].init();
            if(array[i].lock || array.length==1){
                if(array[i].isVisible(m)){
                    array[i].isShow =true;
                    array[i].panel.show();
                    
                    for(var j = 0; j< array.length; j++){
                        if(i!=j){
                            array[j].lock = false;
                            //array[j].panel.setStyle("display", "none");
                            array[j].closePanel();
                        }
                     }
                } else {
                    array[i].hide();
                    array[i].lock = false;
                    for(var j = 0; j< array.length; j++){
                        if(i!=j){
                            array[j].lock = true;
                        }
                    }
                }
            }
            ssh.util.CCMenu.Tip.show(m, array[i].menuItems);
        }
    };
};

ssh.util.CCMap = function(){
    this._container = {}
    this.get = function(key){
        if( typeof key !="String" || rad === null){
            throw new Error('key must be String and not empty ' );
        }else{
            try{
               return this._container[key];
            }catch(r){
                return null;
            }
        }
    }
    
    this.put = function(key,value){
        if( typeof key !="String" || rad === null){
            throw new Error('key must be String and not empty ' );
        }else{
            var oldValue = this.get(key);
            this._container[key] = value;
            return oldValue;
        }
    }
}





