/*    Script: Tooltip.js
        
        Dependencies:
        
        Class: gsMapTooltip
        Classe de gestion des étiquettes.
    */
OpenLayers.Control.Tooltip = OpenLayers.Class({
	/*
    
        Paramètres:
        options - objet avec des clés/valeurs
        
        Options:
        offsetX - décalage en X (pixel)
        offsetY - décalage en Y (pixel)
        align - alignement ('tl', 'tr', 'bl', 'br', 'in', 'cc')
        fixed - si true ne suit pas la souris et ne sera pas deplacé avec le panzoom
    */
    offsetX: 5,
		offsetY: 5,
		align: 'tl',
		fixed: false,
		text: '',
		tooltip : null,
		id : null,		
		lonlat : null,
/*  Function: initialize
        Initialise l'objet Tooltip (appel automatique lors de la crétion de l'objet).
    
        Paramètres:
        map - Instance de l'objet carte.
        options - objet avec des clés/valeurs voir 
    */
    initialize: function(map,options){    	
        OpenLayers.Util.extend(this,options);
        this.id = OpenLayers.Util.createUniqueID(this.CLASS_NAME + "_");
        this.map = map ;
        this.build();
    },
    build: function() {
        this.tooltip = OpenLayers.Util.createDiv(this.id);  
        $(CMap.visu).appendChild(this.tooltip);       
        this.tooltip.style.position = 'absolute';
        this.tooltip.style.fontFamily = 'Arial, Helvetica, sans-serif';
        this.tooltip.style.fontSize = '11px';
        this.tooltip.style.color = '#000';
        this.tooltip.style.margin = '0';
        this.tooltip.style.padding = '0';
        this.tooltip.style.border = '1px solid #b0acc5';
        this.tooltip.style.backgroundColor = '#dc569f';
        this.tooltip.style.zIndex = 9999999999;
        
        
        OpenLayers.Util.modifyDOMElement(this.tooltip, OpenLayers.Util.createUniqueID('tooltip'+ "_"), null, null, null, 
                                            null, null, 0.75);
        if(!this.fixed){
	        this.map.events.register("movestart", this, function() {
	        	if(this.map){
		        	if(this.map.panTween){        		
		        		if(this.map.panTween.begin && this.map.panTween.finish){
		        			begPx = this.map.getLayerPxFromLonLat(this.map.panTween.begin);
		        			finPx = this.map.getLayerPxFromLonLat(this.map.panTween.finish);        			
			        		panWidth 	= begPx.x - finPx.x ;
			        		panHeight = begPx.y - finPx.y ;
			        		this.moveBy(panWidth,panHeight);
			        	}
		        	}
		        }
	        });
	      }
        //this.tooltip.setOpacity(0.75);
        this.hide();
    },
/*  Function: destroy
        Supprime le Tooltip du DOM.
    */
    destroy: function(){
        this.map.div.removeChild(this.tooltip);
        this.map = this.id = null ;
        this.tooltip = null;
    },
/*  Function: update
        Actualise la position et le texte du tooltip.
    
        Paramètres:
        Pt - Point {<Point>}.
        text - texte à afficher
    */
    update: function(Pt, text){
        this.setPosition(Pt);
        this.setText(text);
    },
/*  Function: setPosition
        Détermine la position en fonction du point et de l'alignement <alignPosition> et <setAlign>.
        
        Si le texte est vide le tooltip est masqué.
        
        Paramètres:
        Pt - Point {<Point>}.
    */
    setPosition: function(Pt){
        if(this.text==""){
            this.hide();
            return;
        }
        this.show();        
        var nPt = new OpenLayers.Pixel(Pt.x, Pt.y);
        nPt = this.alignPosition(nPt, this.setAlign(nPt));
        this.lonlat = this.map.getLonLatFromLayerPx(nPt);
        this.tooltip.style.top = nPt.y+"px";
        this.tooltip.style.left = nPt.x+"px";
        delete nPt;
    },
    /** 
     * Method: updatePosition
     * if the popup has a lonlat and its map members set, 
     * then have it move itself to its proper position
     */
    updatePosition: function() {
        if ((this.lonlat) && (this.map)) {
            var px = this.map.getLayerPxFromLonLat(this.lonlat);
            if (px) {
                this.moveTo(px);           
            }    
        }
    },

    /**
     * Method: moveTo
     * 
     * Parameters:
     * px - {<OpenLayers.Pixel>} the top and left position of the popup div. 
     */
    moveTo: function(px) {
        if ((px != null) && (this.tooltip != null)) {
            //this.tooltip.innerHTML = parseInt(this.tooltip.style.left)+" "+parseInt(this.tooltip.style.top) ;
            this.tooltip.style.left = px.x + "px";
            this.tooltip.style.top = px.y + "px";
            //this.tooltip.innerHTML += "<br>"+px.x+" "+px.y ;
        }
    },
    moveBy: function(dx, dy){
        var ox = parseInt(this.tooltip.style.left);
        var oy = parseInt(this.tooltip.style.top);
        this.tooltip.style.top = (oy+dy)+"px";
        this.tooltip.style.left = (ox+dx)+"px";
    },
    
/*  Function: setText
        Affiche le texte.
        
        Ajoute des espaces insecable dans la chaîne.
        
        Paramètres:
        text - texte à afficher
    */
    setText: function(text){
        this.text = text.replace(/\s/g,"&nbsp;");
        this.tooltip.innerHTML = this.text;
    },
/*  Function: setAlign
        Fixe l'alignement à la volée si aucun n'est précisé.
                
        Paramètres:
        Pt - Point {<Point>}.
    */
    setAlign: function(Pt){
        if((this.align!='tl')||(this.fixed)) return this.align;
        var al = 't';
        if(Pt.y < (this.map.getSize().h*0.25)) al = 't';
        if(Pt.y > (this.map.getSize().h*0.75)) al = 'b';
        if(Pt.x < (this.map.getSize().w *0.25)) al += 'l';
        if(Pt.x > (this.map.getSize().w *0.75)) al += 'r';
        if(al.length==1) al += 'l';
        return al;
    },
/*  Function: alignPosition
        Adapate la position en fonction de l'alignement.
                
        Paramètres:
        Pt - Point {<Point>}.
        al - alignement ('tl', 'tr', 'bl', 'br', 'in', 'cc')
    */
    alignPosition: function(Pt, al){
        //var nPt = new Point(Pt.x, Pt.y);
        switch(al){
            case 'tl':
                if(this.fixed) {
                	Pt.x = 0;
                	Pt.y = 0;
                }
                Pt.add(this.offsetX,this.offsetY);
                break;
            case 'tr':
                if(this.fixed) {
                	Pt.x = this.map.getSize().w;
                	Pt.y = 0;
                }
                Pt.x -= this.tooltip.offsetWidth + this.offsetX;
                Pt.y += this.offsetY;
                break;
            case 'bl':
                if(this.fixed){
                	Pt.x = 0;
                	Pt.y = this.map.getSize().h;
                }
                Pt.x += this.offsetX;
                Pt.y -= this.tooltip.offsetHeight + this.offsetY;
                break;
            case 'br':
                if(this.fixed){
                	Pt.x = this.map.getSize().w;
                	Pt.y = this.map.getSize().h;
                }
                Pt.x -= this.tooltip.offsetWidth + this.offsetX;
                Pt.y -= this.tooltip.offsetHeight + this.offsetY;
                break;
            case 'cc':
                if(this.fixed) {
                	Pt.x = this.offsetX;
                	Pt.y = this.offsetY;
                }else{
                    Pt.x -= parseInt(this.tooltip.offsetWidth/2);
                    Pt.y -= parseInt(this.tooltip.offsetHeight/2);
                }
                break;
            case 'in':
                if(this.fixed) {
                	Pt.x = this.offsetX;
                	Pt.y = this.offsetY;
                }else{
                    Pt.x += this.offsetX;
                    Pt.y += this.offsetY;
                    if((Pt.x+this.tooltip.offsetWidth+this.offsetX) > this.map.getSize().w)
                        Pt.x = this.map.getSize().w - this.tooltip.offsetWidth - this.offsetX;
                    if((Pt.y+this.tooltip.offsetHeight+this.offsetY) > this.map.getSize().h)
                        Pt.y = this.map.getSize().h - this.tooltip.offsetHeight - this.offsetY;
                    if(Pt.x < 0) Pt.x = this.offsetX;
                    if(Pt.y < 0) Pt.y = this.offsetY;
                }
                break;
        }
        return Pt;
    },
/*  Function: show
        Affiche le tooltip.
    */
    show: function(){
        if(this.tooltip.style.display!='block') this.tooltip.style.display = 'block';
    }, 
/*  Function: hide
        Masque le tooltip.
    */
    hide: function(){this.tooltip.style.display = 'none';},
/*  Function: clear
        Efface le tooltip.
    */
    clear: function(){
        this.hide();
        this.setText("");
        this.tooltip.style.top = "-100px";
        this.tooltip.style.left = "-100px";
        /*
        this.map._divMap.removeChild(this.tooltip);
        this.tooltip = null;*/
    },

    CLASS_NAME: "OpenLayers.Tooltip"
});

/*        Class: gsMapTooltips
        Classe de collection de tooltip.
    */
OpenLayers.Control.Tooltips = OpenLayers.Class({
		tooltips :[],
    initialize: function(map){
        this.map = map;
        this.tooltips = [];
    },
/*  Function: add
        Ajoute un tooltip à la collection.
        
        Paramètres:
        tt - un tooltip {<gsMapTooltip>}.
    */
    add: function(tt){
        this.tooltips.push(tt);
    },
    moveBy: function(dx, dy){
        for(var t=0;t<this.tooltips.length;t++){
            this.tooltips[t].moveBy(dx,dy);
        }
    },
    updatePosition : function(){
        for(var t=0;t<this.tooltips.length;t++){
            this.tooltips[t].updatePosition();
        }    	
    },
/*  Function: destroy
        Efface la collection.
    */
    destroy: function(){
        for(var t=0;t<this.tooltips.length;t++){
            this.tooltips[t].destroy();            
        }
        this.tooltips = [];
        //this.map = null;
    },
    CLASS_NAME: "OpenLayers.Tooltips"
});
