/**
 * Gestion du zoom par Palier
 */

var olZoomScale = new Class({
	getOptions: function(){
		return {
            levels: [6,5,4,3,2,1,0]
		};
	},
	initialize: function(zoomDivName, pMap, options){
		this.map = pMap;
		this.setOptions(this.getOptions(), options);
		this.maxScaleLimit = 100000000 ;
		this.Intervals = Array();
        
        this.aScaleBar = $(zoomDivName).getElements('a');
        this.aScaleBar.each(function(el, it) {
					el.addEvent('click', function() {
	                    this.liClick(el); 
						this.zoomCur('zoom',it+1,this.aScaleBar.length);
	                }.bind(this));	
					el.addEvent('mouseover',function(e) {
	                    this.liMouseOver(el,e);
	                }.bind(this));
					el.addEvent('mouseout',function() {
	                    this.liMouseOut(el);
	                }.bind(this));
          var levelClick = 1/this.getScaleNumber(this.options.levels[it]) ;
          el.setProperty('level', levelClick);
		}, this);
        
        
    //Appel de requestResult après la réponse du serveur
    this.buildIntervals() ;
    this.map.events.register("zoomend", this, function(){
    		var scaleCur = parseInt(this.map.getScale()); 
        this.subCurrent(scaleCur);
    });
	},
	subCurrent: function(icur) {
		if(this.Intervals.length == 0)return false ;
		for(var i=0; i < this.Intervals.length; i++){
			var curentActiveScale = i+1 ;
			if(this.Intervals[i].min < icur && icur <= this.Intervals[i].max){
				$('zoom'+curentActiveScale).className = 'current' ;
			}
			else {
				$('zoom'+curentActiveScale).className = '' ;
			}
		}
	},
	getScaleNumber : function(param){
		return parseInt(param.substr(2,param.length-1));
	},
	buildIntervals : function(){
		if(typeof(this.options.levels) != 'object')return false ;
		var msg = '' ;
		for(var i=0; i < this.options.levels.length; i++){
			var inter = {
				min : 0 ,
				max : 0	
			};
			if(i==0){
				inter.max =  this.getScaleNumber(this.options.levels[i])+parseInt((this.getScaleNumber(this.options.levels[i+1])-this.getScaleNumber(this.options.levels[i]))/2);
			}
			else if(i == this.options.levels.length-1){
				inter.min =  this.getScaleNumber(this.options.levels[i])-parseInt((this.getScaleNumber(this.options.levels[i])-this.getScaleNumber(this.options.levels[i-1]))/2);
				inter.max =  this.maxScaleLimit;
			}
			else{
				inter.min =  this.getScaleNumber(this.options.levels[i])-parseInt((this.getScaleNumber(this.options.levels[i])-this.getScaleNumber(this.options.levels[i-1]))/2);
				inter.max =  this.getScaleNumber(this.options.levels[i])+parseInt((this.getScaleNumber(this.options.levels[i+1])-this.getScaleNumber(this.options.levels[i]))/2);
			}
			this.Intervals.push(inter) ;
		}
	},
	zoomCur: function(nomNav,iCur,nbreNav) {
		for(nav=1;nav<=nbreNav;nav++){
			var cur = $(nomNav+nav);		
			if(cur) {
				if (iCur == nav) {
					cur.className="current";
				}else {
					cur.className="";
				}
			}
		}
	},


	/**
	    Lancer les actions apres le click sur l'élément
	    appelle la méthode zoomScale de gsMap
	*/
	liClick: function(el) {
        this.map.zoomToScale(el.getProperty('level'));        
				this.removeBox();
        //this.map._divZoomBox.hide();        
	},
	
	/**
	   Pour afficher les cadres de zoom lorsque la souris survole une échelle prédéfinie
	*/
	liMouseOver: function(el,e) {
        var res = OpenLayers.Util.getResolutionFromScale(el.getProperty('level'), 
                                                         this.map.baseLayer.units);
				if(res < this.map.getResolution() && res != this.map.getResolution())this.showZoomBox(res);
        var idscale = el.id;
        var ttid = 'tt_nav'+idscale; 
        //ShowTip(e,ttid,idscale);
	},
	
	/**
	  Description de l'action lorsque le pointeur de la souris sort d'une barre de zoom
	*/
	liMouseOut: function(el) {
		this.removeBox();
        //this.map._divZoomBox.hide();	
        //HideTip('tt_nav'+el.id);    
	},
	
	/**
	    Fonction permettant d'afficher le zoombox
	*/
	showZoomBox : function(resolution) {
			this.createBox();
			carte = this.map ; 
      var centerPx = carte.getViewPortPxFromLonLat(carte.getCenter());
			var boxExtent = carte.calculateBounds(carte.getCenter(),resolution);
      leftTopGeo = new OpenLayers.LonLat(boxExtent.left, boxExtent.top); 
      var leftTopPx = carte.getViewPortPxFromLonLat(leftTopGeo);
      rightBottomGeo = new OpenLayers.LonLat(boxExtent.right, boxExtent.bottom); 
      var rightBottomPx = carte.getViewPortPxFromLonLat(rightBottomGeo);      
			var boxWidth = Math.abs(rightBottomPx.x - leftTopPx.x);
			var boxHeight = Math.abs(rightBottomPx.y - leftTopPx.y);
    	this.zoomBox.style.left   = Math.abs(centerPx.x - (boxWidth/2))+"px";
    	this.zoomBox.style.top   = Math.abs(centerPx.y - (boxHeight/2))+"px";
    	this.zoomBox.style.width  = boxWidth+"px";
    	this.zoomBox.style.height = boxHeight+"px";
  },
  createBox : function(){
  	this.zoomBox = OpenLayers.Util.createDiv('zoomBox');
    this.zoomBox.className = 'olHandlerBoxZoomBox';                                         
    this.zoomBox.style.zIndex = this.map.Z_INDEX_BASE["Popup"] - 1;
    this.map.viewPortDiv.appendChild(this.zoomBox);
  },
  removeBox: function() {
  		if(this.zoomBox)
      	this.map.viewPortDiv.removeChild(this.zoomBox);
      this.zoomBox = null;
  }

});
olZoomScale.implement(new Options);

