extensions.push('toolbar');

function toolbarBegin() {
	eventListeners.attachToEvent('mapLoaded','initZoomSteps');
	eventListeners.attachToEvent('scaleChange','zoomStepsChange');
	eventListeners.attachToEvent('scaleChange','scaleViewChange');
	eventListeners.attachToEvent('enableEvent','buttonOff');
	eventListeners.attachToEvent('disableEvent','buttonOn');
}

function buttonOn(arg) {
	if(arg=='zoomBox') {
		document.getElementById('puls_zoomfin').onmouseover=new Function("MM_swapImage('puls_zoomfin','','images/new_pulsantiera_r1_c5_f2.gif',1);");
		document.getElementById('puls_zoomfin').onmouseout=new Function("MM_swapImgRestore()");
		document.getElementById('puls_zoomfin').src='images/new_pulsantiera_r1_c5.gif';
	}
	if(arg=='pan') {
		document.getElementById('puls_pan').onmouseover=new Function("MM_swapImage('puls_pan','','images/new_pulsantiera_r1_c7_f2.gif',1);");
		document.getElementById('puls_pan').onmouseout=new Function("MM_swapImgRestore()");
		document.getElementById('puls_pan').src='images/new_pulsantiera_r1_c7.gif';
	}
	if(arg=='measureDistance') {
		document.getElementById('puls_linea').onmouseover=new Function("MM_swapImage('puls_linea','','images/new_pulsantiera_r1_c13_f2.gif',1);");
		document.getElementById('puls_linea').onmouseout=new Function("MM_swapImgRestore()");
		document.getElementById('puls_linea').src='images/new_pulsantiera_r1_c13.gif';
	}
	if(arg=='measureArea') {
		document.getElementById('puls_area').onmouseover=new Function("MM_swapImage('puls_area','','images/new_pulsantiera_r1_c15_f2.gif',1);");
		document.getElementById('puls_area').onmouseout=new Function("MM_swapImgRestore()");
		document.getElementById('puls_area').src='images/new_pulsantiera_r1_c15.gif';
	}
}

function buttonOff(arg) {
	if(arg=='zoomBox') {
		document.getElementById('puls_zoomfin').onmouseover=null;
		document.getElementById('puls_zoomfin').onmouseout=null;
		document.getElementById('puls_zoomfin').src='images/new_pulsantiera_r1_c5_f2.gif';
	}
	if(arg=='pan') {
		document.getElementById('puls_pan').onmouseover=null;
		document.getElementById('puls_pan').onmouseout=null;
		document.getElementById('puls_pan').src='images/new_pulsantiera_r1_c7_f2.gif';
	}
	if(arg=='measureDistance') {
		document.getElementById('puls_linea').onmouseover=null;
		document.getElementById('puls_linea').onmouseout=null;
		document.getElementById('puls_linea').src='images/new_pulsantiera_r1_c13_f2.gif';
	}
	if(arg=='measureArea') {
		document.getElementById('puls_area').onmouseover=null;
		document.getElementById('puls_area').onmouseout=null;
		document.getElementById('puls_area').src='images/new_pulsantiera_r1_c15_f2.gif';
	}
}

function zoomStepsChange() {
	var currentZoom=mapInfo.getProperty('currentZoom');
	var zoomSteps=mapInfo.getProperty('zoomSteps');
	var step=0;
	var found=false;
	
	for(var i=0;i<zoomSteps.length && !found;i++) {
		if(zoomSteps[i]>=currentZoom) {
			found=true
			step=i;
		}
	}
	if(!found)
		step=zoomSteps.length-1;
	for(var i=0;i<zoomSteps.length;i++) {
		var img=document.getElementById('zoomStepImg'+(i+1));
		if (img!=null)
		{
			if(i==step) {
				img.src='images/zoom_'+(i+1)+'red.gif';
				img.onmouseout=null;
				img.onmouseover=null;
			} else {
				img.src='images/zoom_'+(i+1)+'black.gif';
				img.onmouseout=new Function("MM_swapImgRestore()");
				img.onmouseover=new Function('MM_swapImage(\'zoomStepImg'+(i+1)+'\',\'\',\'images/zoom_'+(i+1)+'red.gif\',1);');
			}
		}
	}
	
	
}

function initZoomSteps() {
	var zoomSteps=new Array();
	
	var zoomStep=Math.floor(map.maxScale/mapInfo.getProperty('zoomScaleSteps'));
	for(var i=0;i<mapInfo.getProperty('zoomScaleSteps');i++) {
		//alert(i*zoomStep);
		zoomSteps.push(i*zoomStep);
	}
	mapInfo.setProperty('zoomSteps',zoomSteps);
}

function toolbarCommand(command,arg) {
	eventListeners.fireEvent(command,arg);
}

function scaleViewChange() {
	
	var dpi=72;
	var mapMeters=mapWidth/dpi*2.54/100.0;
	
	var scale=map.realRange.getDeltaX()/mapMeters;
	scale=writeFormat(scale,0,'.,');
	document.getElementById('scaleViewText').innerHTML=scale;
}

function writeFormat(num,dp,sepString) { 
 
 num=Math.round(num*Math.pow(10,dp));
 num=num.toString() ;
 var str='';
 if(dp!=0)
	 str=sepString.charAt(1)+num.substr(num.length-dp);
 num=num.substr(0,num.length-dp);
 while (num.length>3) { 
  str=sepString.charAt(0)+num.substr(num.length-3)+str;
  num=num.substr(0,num.length-3);
 } 
 str=num+str;
 return str;


} 

