
function progZoomer(oKaMap){
	this.kaMap = oKaMap;
	this.isDragging = false;
	this.kaMap.progZoomer = this;
	this.kaMap.registerForEvent( KAMAP_MAP_INITIALIZED, this, this.draw );
};


progZoomer.prototype.draw = function(){
	var t = this;

	this.domObj = document.createElement("div");
	this.domObj.id = "progZoomer";
	this.kaMap.domObj.appendChild(this.domObj);
	this.domObj.onmouseover = function(){t.setOpacityOfController(1)};
	this.domObj.onmouseout = function(){t.setOpacityOfController(0.8)};
	
	var table = document.createElement("table");
	table.id = "progZoomer_tableText";
	this.domObj.appendChild(table);
	table.cellSpacing = "0";
	table.style.borderSpacing = "0px";
	
	var tbody = document.createElement("tbody");
	table.appendChild(tbody);
	
	var tr = document.createElement("tr");
	tbody.appendChild(tr);
	
	var td = document.createElement("td");
	tr.appendChild(td);
	td.className = "td1";
	td.innerHTML = "Zoom";
	
	var table = document.createElement("table");
	this.domObj.appendChild(table);
	table.id = "progZoomer_table";
	table.cellSpacing = "0";
	table.style.borderSpacing = "0px";
	
	var tBody = document.createElement("tbody");
	table.appendChild(tBody);
	
	var tr = document.createElement("tr");
	tBody.appendChild(tr);
	
	var td = document.createElement("td");
	tr.appendChild(td);
	
	var imageMINUS = document.createElement("img");
	td.appendChild(imageMINUS);
	imageMINUS.className = "progZoomer_imgPLUSMINUS";
	imageMINUS.src = "/OPO/tools/progZoomer/images/MINUS.gif";
	imageMINUS.onclick = function(){t.kaMap.zoomOut()};
		
	var td = document.createElement("td");
	tr.appendChild(td);
	td.className = "progZoomer_td_support";
	
	// support
	this.support = document.createElement("div");
	td.appendChild(this.support);
	this.support.id = "progZoomer_support";
	this.support.style.position = "relative";
	this.support.style.overflow = "visible";
	this.support.onclick = function(e){t.handleSupportClick(e)};
	
	// slider
	this.slider = document.createElement("div");
	this.support.appendChild(this.slider);
	this.slider.id = "progZoomer_slider";
	this.slider.style.position = "absolute";
	
	var td = document.createElement("td");
	tr.appendChild(td);
	
	var imagePLUS = document.createElement("img");
	td.appendChild(imagePLUS);
	imagePLUS.src = "/OPO/tools/progZoomer/images/PLUS.gif";
	imagePLUS.className = "progZoomer_imgPLUSMINUS";
	imagePLUS.onclick = function(){t.kaMap.zoomIn()};

	// get initial position
	var oMap = this.kaMap.getCurrentMap();
    var nScales = oMap.getScales().length;
    var currentScale = oMap.currentScale;

	// get width and height values (with borders ) of different elements
	this.supportWidth = this.kaMap.getObjectWidth(this.support);
	this.supportHeight = this.kaMap.getObjectHeight(this.support);
	this.sliderWidth = this.kaMap.getObjectWidth(this.slider);
	this.sliderHeight = this.kaMap.getObjectHeight(this.slider);
    
	// vertical position of slider
	this.vertSliderPos = -(Math.round((this.sliderHeight - this.supportHeight)/2));
	this.slider.style.display = "inline";
	this.slider.style.top = this.vertSliderPos + "px";
	this.slider.style.left = "0px";
	
	
	// Drag and Drop init
	Drag.init(this.slider, null, 0, this.supportWidth - this.sliderWidth, this.vertSliderPos, this.vertSliderPos);
	
	this.slider.onDragStart = function(){t.isDragging = true};
	this.slider.onDragEnd = function(){t.findScale(); t.isDragging = false; t.setOpacityOfController(0.8)};

   	this.kaMap.registerForEvent( KAMAP_SCALE_CHANGED, this, this.update );
};

progZoomer.prototype.setOpacityOfController = function(amount){
	if(!this.isDragging){
	    this.domObj.style.opacity = amount;
	    this.domObj.style.mozOpacity = amount;
		this.domObj.style.filter = "Alpha(opacity="+(amount*100)+")";
	}
};

progZoomer.prototype.findScale = function(){
	//move thumb to closest scale marker
    var sliderPosition = this.slider.offsetLeft;
    var nbScales = this.kaMap.getCurrentMap().getScales().length;
    var nearestScale = Math.round((sliderPosition / (this.supportWidth - this.sliderWidth))*(nbScales-1));
	if(nearestScale > nbScales-1){
		nearestScale = nbScales-1;
	}
	this.adjustSliderOnScale(nearestScale);
	this.kaMap.zoomToScale(this.kaMap.getCurrentMap().aScales[nearestScale]);
};

progZoomer.prototype.adjustSliderOnScale = function(scale){
	var nbScales = this.kaMap.getCurrentMap().getScales().length;
	var moveX = scale * Math.round(((this.supportWidth-this.sliderWidth)/(nbScales-1)));
	
	// adjust if it's last zoom level
	if(scale == (nbScales-1)){
		moveX = this.supportWidth - this.sliderWidth;
	}
	
	this.slider.style.left = moveX + "px";

};

progZoomer.prototype.update = function(){
	var currentScale = this.kaMap.getCurrentScale();
	var allScales = this.kaMap.getCurrentMap().getScales();
	// retreive current scale number
	for(var i = 0; i < allScales.length; i++){
		if(allScales[i] == currentScale)
			var currentScaleNb = i;
	}
	this.adjustSliderOnScale(currentScaleNb);
};

progZoomer.prototype.handleSupportClick = function(e){
	
	e = (e)?e:((event)?event:null);
	var src = (e.target)?e.target:((e.srcElement)?e.srcElement:e);
	if(src.id == "progZoomer_support"){
		var relPos = (typeof(e.offsetX) != "undefined")?(e.offsetX):((typeof(e.layerX) != "undefined")?(e.layerX):null);
		
		if(relPos > this.supportWidth - this.sliderWidth)
			relPos = this.supportWidth - this.sliderWidth;
			
		this.slider.style.left = relPos + "px";
		
		this.findScale();
	}
};
