/*
*/

OpenLayers.Control.olInfo = OpenLayers.Class(OpenLayers.Control, {
	timerName : null ,
	interval : 750 ,
	loadingBox : null ,
	map : null ,
	winInfo : null,
	popup : null ,
	idBox : 'loadingInfosbull' ,
	initialize: function(map, options){
    OpenLayers.Control.prototype.initialize.apply(this, [options]);
    this.timerName = new Ext.util.DelayedTask();
    this.map = map ;
		this.loadingBox = document.createElement('div');
		this.loadingBox.id = this.idBox ;
		var oStyle = {
								  'display':'none' ,
									'zIndex':'500000' ,
									'position':'absolute' ,
									'backgroundImage': 'url(fonctionnalites/info/images/loading.gif)',
									'backgroundPosition': '0 0',
									'backgroundRepeat': 'no-repeat',
									'backgroundColor':'#FFF',	
									'width':'17px',
									'top':'0' ,
									'left':'0' ,
									'height':'17px' 	
									} ; 
		Ext.DomHelper.applyStyles(this.loadingBox, oStyle);
		this.map.div.appendChild(this.loadingBox) ;
  },
    
	activate: function(){
    if (OpenLayers.Control.prototype.activate.apply(this, arguments)) {
        this.map.events.register("mousemove", this, this.getInfo);
        this.map.events.register("mouseout", this, this.cancelInfo);
        //this.draw() ;
        return true;
    } else {
        return false;
    }
	},
	deactivate: function(){
    if (OpenLayers.Control.prototype.deactivate.apply(this, arguments)) {
        this.map.events.unregister("mousemove", this, this.getInfo);
        this.map.events.unregister("mouseout", this, this.cancelInfo);
        return true;
    } else {
        return false;
    }
	},
  getInfo: function(evt){
		if(evt == null) {
			lonLat = new OpenLayers.LonLat(0, 0);
		} else {
			lonLat = this.map.getLonLatFromPixel(evt.xy);
		}
    this.timerName.delay(this.interval, this.request, this, [lonLat.lon, lonLat.lat, evt.xy]) ;
  },
  
  cancelInfo: function(evt){
    this.timerName.cancel() ;
  },
  
  buildLoading : function (evtPoint){
  	if(this.loadingBox != null){
  		var pointLoadingX = parseInt(evtPoint.x) ;
  		var pointLoadingY = parseInt(evtPoint.y - 16) ;
  		this.loadingBox.style.top = pointLoadingY+"px" ;
  		this.loadingBox.style.left = pointLoadingX+"px" ;
  		this.loadingBox.style.display = 'block' ;
  	}
  },
  maskLoading : function (){
  	if(this.loadingBox != null){
  		this.loadingBox.style.display = 'none' ;
  	}
  },
  
  request: function(ptX, ptY, evtPoint){
  	this.buildLoading(evtPoint) ;
  	var urlRqt =  'fonctionnalites/info/ajax.php' ;
    var success = OpenLayers.Function.bind(this.commitSuccess, this);
    var failure = OpenLayers.Function.bind(this.commitFailure, this);
        
  	new OpenLayers.Ajax.Request(urlRqt, 
                  { parameters: {
                  		info:'true',
                  		pt : ptX+","+ptY
                  	},
                    method: 'get',
                    onComplete: success, 
                    onFailure: failure,
                    asynchronous: true
                  });
  },
  
  commitSuccess: function(request){
  	this.maskLoading() ;
  	this.showInfoBull(request) ;
  	
  },
  
  showInfoBull: function (request){
  	try {
  		this.winInfo.destroy();
  	}catch(e){};
  		
  	var visu = Ext.get(this.map.div) ;
  	var leftWin = visu.getLeft()+visu.getWidth()-310 ;
	  this.winInfo = new Ext.Window({
	  	id: "infoDivCont",
	    title: "Information",
	    collapsible: true,
	    width: 300,
	    x : leftWin,
	    y : visu.getTop()+10,
	    height: 300,
	    autoScroll: true,
	    html: request.responseText
	  });
		this.winInfo.show(this);
  },
  
  commitFailure: function(request) {
  	this.maskLoading() ;
  },
  CLASS_NAME: "OpenLayers.Control.olInfo"
});


