function opacityController(oKaMap){
	this.kaMap = oKaMap;
	this.currentOrthoOpacity = 0;
	this.chrono = null;
	this.isRunning = false;
	this.isDragging = false;

	this.kaMap.registerForEvent(KAMAP_INITIALIZED, this, this.draw);
}

opacityController.prototype.draw = function(){
	var t = this;
	
	this.domObj = document.createElement("div");
	this.kaMap.domObj.appendChild(this.domObj);
	this.domObj.id = "opacityController";
	this.domObj.style.visibiliy = "hidden";
	this.setOpacityOfController(0.7);
	this.domObj.onmouseover = function(){t.setOpacityOfController(1)};
	this.domObj.onmouseout = function(){t.setOpacityOfController(0.8)};
	
	// FOR TEXT
	var table = document.createElement("table");
	table.id = "opacityController_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 = tra['opController_map'];
	
	var td = document.createElement("td");
	tr.appendChild(td);
	td.className = "td2";
	td.innerHTML = tra['opController_satellite'];
	
	// MAIN 
	var table = document.createElement("table");
	this.domObj.appendChild(table);
	table.cellSpacing = "0";
	table.style.borderSpacing = "0px";
	table.id = "opacityController_table";
	
	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 = "opacityController_imgPLUSMINUS";
	imageMINUS.src = "/OPO/tools/opacityController/images/MINUS.gif";
	imageMINUS.onmousedown = function(){t.isRunning = true; t.opacityLoop(-4);};
	imageMINUS.onmouseup = function(){clearTimeout(this.chrono); t.isRunning = false;};
	
	var td = document.createElement("td");
	tr.appendChild(td);
	
	this.support = document.createElement("div");
	td.appendChild(this.support);
	this.support.id = "opControl_support";
	this.support.style.position = "relative";
	this.support.onclick = function(e){t.handleSupportClick(e)};
	
	this.dragger = document.createElement("div");
	this.support.appendChild(this.dragger);
	this.dragger.id = "opControl_dragger";
	this.dragger.style.position = "absolute";
	this.dragger.style.left = "-1px";
	this.dragger.style.top = -(this.kaMap.getObjectHeight(this.dragger) - this.kaMap.getObjectHeight(this.support))/2 -1 + "px";
	
	var td = document.createElement("td");
	tr.appendChild(td);
	
	var imagePLUS = document.createElement("img");
	td.appendChild(imagePLUS);
	imagePLUS.src = "/OPO/tools/opacityController/images/PLUS.gif";
	imagePLUS.className = "opacityController_imgPLUSMINUS";
	imagePLUS.onmousedown = function(){t.isRunning = true; t.opacityLoop(4);};
	imagePLUS.onmouseup = function(){t.isRunning = false;};
	
	this.checkScale();
	this.kaMap.registerForEvent(KAMAP_SCALE_CHANGED, this, this.checkScale);
	
	this.supportWidth = this.kaMap.getObjectWidth(this.support);
	this.supportHeight = this.kaMap.getObjectHeight(this.support);
	this.draggerWidth = this.kaMap.getObjectWidth(this.dragger);
	this.draggerHeight = this.kaMap.getObjectHeight(this.dragger);
	
	Drag.init(this.dragger, null, -1, this.supportWidth - this.draggerWidth - 1, this.dragger.offsetTop, this.dragger.offsetTop);
	
	this.dragger.onDragStart = function(){t.isDragging = true};
	this.dragger.onDragEnd = function(){t.isDragging = false; t.setOpacityOfController(0.8)};
	this.dragger.onDrag = function(){t.updateOpacity()};
	
	this.updateDraggerPosition();
	this.kaMap.registerForEvent( KAMAP_SCALE_CHANGED, this, this.updateDraggerPosition );
};

opacityController.prototype.checkScale = function(){
	if(this.kaMap.getCurrentMap().getLayer("orthophotos").isVisible()){
		this.domObj.style.visibility = "visible";
	}
	else{
		this.domObj.style.visibility = "hidden";	
	}
};

opacityController.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)+")";
	}
};

// function called while dragger is being moved
opacityController.prototype.updateOpacity = function(){
	var range = this.supportWidth - this.draggerWidth;
	this.currentOrthoOpacity = Math.round((this.dragger.offsetLeft * 100)/range);
	// disable invisble layers
	if (this.currentOrthoOpacity<=5)  {
		this.mySetLayerVisibility('orthophotos',false); 
	}
	else {
		this.mySetLayerVisibility('orthophotos',true); 
		this.kaMap.setLayerOpacity('orthophotos', this.currentOrthoOpacity);
	}
	if (this.currentOrthoOpacity>=95) {
		this.mySetLayerVisibility('basemap',false); 
	}
	else {
		this.mySetLayerVisibility('basemap',true); 
	}
};

opacityController.prototype.mySetLayerVisibility = function (name,bVisible){
	var layer = this.kaMap.getCurrentMap().getLayer(name);
	// if layer's visibility is already set to bVisible, we don't do anything
	if(layer.visible != bVisible){
                if(!this.kaMap.loadUnchecked && bVisible) {
                	layer.visible = true;
                	this.kaMap.setMapLayers();
                        if(layer.domObj){
                                layer.domObj.style.visibility = bVisible?'visible':'hidden';
                                layer.domObj.style.display = bVisible?'block':'none';
                                for( var i = 0; i < layer.domObj.childNodes.length; i++) {
                                        layer.setTile(layer.domObj.childNodes[i]);
                                }
                        }
                	this.kaMap.paintLayer(layer);
            	}
                else {
                	if(layer.domObj){
                                layer.visible = false;
                                layer.domObj.style.visibility = bVisible?'visible':'hidden';
                                layer.domObj.style.display = bVisible?'block':'none';
                                for( var i = 0; i < layer.domObj.childNodes.length; i++) {
                                        layer.setTile(layer.domObj.childNodes[i]);
                                }
                        }
            }
        }

};
opacityController.prototype.handleSupportClick = function(e){
	e = (e)?e:((event)?event:null);
	var src = (e.target)?e.target:((e.srcElement)?e.srcElement:null);
	if(src.id == "opControl_support"){
		var relPos = (typeof(e.offsetX) != "undefined")?(e.offsetX):((typeof(e.layerX) != "undefined")?(e.layerX):null);
		relPos = Math.round(relPos - this.draggerWidth/2);
		
		if(relPos > this.supportWidth - this.draggerWidth){
			relPos = this.supportWidth - this.draggerWidth;
		}
		
		if(relPos <= 0)
			relPos = -1;
		
		this.dragger.style.left = relPos + "px";
		this.updateOpacity();
	}
};

// function called after the opacity of a layer has been modified
opacityController.prototype.updateDraggerPosition = function(){
	var range = this.supportWidth - this.draggerWidth;
	var amount = Math.round((range / 100) * this.currentOrthoOpacity);
	if (amount<=0) amount=-1;
	this.dragger.style.left = amount + "px";
	
	this.updateOpacity();
};

opacityController.prototype.cancelEvent = function(e){
	e = (e)?e:((event)?event:null);
    e.cancelBubble = true;
    e.returnValue = false;
    if (e.preventDefault) {
        e.preventDefault();
    }
    if (e.stopPropogation) {
        e.stopPropogation();
    }
}


// loop for buttons
opacityController.prototype.opacityLoop = function(amount){
	this.setOpacityOfLayers(amount);
	var t = this;

	if(this.isRunning){
		this.chrono = setTimeout(function(){t.opacityLoop(amount)}, 50);
	}
	else{
		clearTimeout(this.chrono);
	}
};

// if amount positive, increase opacity of orthophotos
opacityController.prototype.setOpacityOfLayers = function(amount){
	if(amount > 0 && this.currentOrthoOpacity < 100){
		this.currentOrthoOpacity = (this.currentOrthoOpacity + amount > 100)?100:(this.currentOrthoOpacity + amount);
		this.kaMap.setLayerOpacity("orthophotos", this.currentOrthoOpacity);
	}
	
	if(amount < 0 && this.currentOrthoOpacity > 0){
		this.currentOrthoOpacity = (this.currentOrthoOpacity + amount < 0)?0:(this.currentOrthoOpacity + amount);
		this.kaMap.setLayerOpacity("orthophotos", this.currentOrthoOpacity);
	}
		
	this.updateDraggerPosition();
};


