/***** Variabili globali per supportare la visualizzazione della mappa e dei suoi oggetti ****/
	// contiene tutti i punti dei tronchetti
	var boundsMap = new Array();
	var allowedBounds;
	var latNE=0, lngNE=0, latSW=1000, lngSW=1000;

	// limiti di zoomOut e zoomIn
	var minZoomLevel     = 8;
	var initialZoomLevel;
	var maxZoomInLevel   = 15;

	// punto iniziale in base a cui centrare la mappa
	var lat;
	var lng;
		
	// devo inizializzare gli array con i valori relativi alle diverse tipologie di marker
	var ordinanze_lavori_markers = new Array();
	var ordinanze_limiti_markers = new Array();
	var comunicati_neve_markers  = new Array();
	var mevdar_markers           = new Array();
	var smitcam_markers          = new Array();

	// mappa gli id dei div per etichetta e le loro posizioni
	var divElementReg     = new Map();
	var divElementAnas    = new Map();
	var divElementAnasFix = new Map();
	var mouse_pos         = new Array(2);
	
	// mappa dei codici ASCII
	var asciiCodeMap;

	// variabili necessarie alla visualizzazione della polyline delle etichette ANAS
	//var polylinesVertex = new Array(); // array di vertici
	//var polylines       = null;        // array di oggetti che verranno inizializzati a polyline
	var anasPolylines   = new Map();
	var lineColor       = "#ffffff";     // colore della polyline (colore di VS:#124652)
	var lineWeight      = 6;             // dimensione della polyline
	var lineOpacity     = 0;             // livello di opacit� della polyline

	
/***** Oggetti necessari per supportare la visualizzazione dei marker sulla mappa ****/
// oggetto che contiene lo stato dei tronchi per le ordinanze, comunicati e centraline
function Info(icon) {
	this.id;
	this.icon = icon;
	this.description;
	this.state;
	this.lat;
	this.lng;
	// metodi
	this.populate       = Info_populate;
	this.format         = Info_format;
	this.setDescription = Info_setDescription;
	this.getDescriptionWithParX = function() {
		var i = this.description.indexOf("XXX");
		var a = this.description.substring(0, i);
		var b = this.description.substring(i+3);
		return a + Math.random() + b;
	}
}


/**
 * Metodo che sostituisce i caratteri particolari con i caratteri speciali per la visualizzazione
 * della stringa nel file jsp
 *  - @ --> \"
 *  - # --> \n
 *  - $ --> \t
 * **
function Info_format(val) {
	val = parseValue(val,"@","\"");
	val = parseValue(val,"#","\n");
	val = parseValue(val,"$","\t");
	return val;
}**/

function Info_format(val) {
	if (val!=null && val!="") {
		var keys = asciiCodeMap.keySet();
		for (var x=0; x<keys.length; x++) {
			var k=keys[x];
			while(val.indexOf(k)!=-1) {
				val = val.replace(k,asciiCodeMap.get(k));
			}
		}
	}
	return val;
}

function Info_populate(id, description, state, lat, lng) {
	this.id          = id;
	this.description = this.format(description);
	this.state       = state;
	this.lat         = parseFloat(lat);
	this.lng         = parseFloat(lng);
}

function Info_setDescription(newDescription, separator, deleteOld) {
	if (deleteOld) {
		this.description = newDescription;
	} else {
		this.description = this.description + separator + newDescription;
	}
}

function parseValue( string, _old, _new) {
	if (string!=null && string!="") {
		while(string.indexOf(_old)!=-1) {
			string = string.replace(_old,_new);
		}
	}
	return string;
}

function Container(){
	this.description;
	this.latStart;
	this.lngStart;
	this.latEnd;
	this.lngEnd;
	this.street;
	this.info;
	this.showPopUp;
	
	// metodi
	this.populate      = Container_populate;
	this.latMedium     = Container_latMedium;
	this.lngMedium     = Container_lngMedium;
	this.setLatLngInfo = Container_setLatLngInfo;
}

function Container_populate(description, latStart, lngStart, latEnd, lngEnd, street, infoObject, showPopUp) {
	this.description = description;
	this.latStart    = parseFloat(latStart);
	this.lngStart    = parseFloat(lngStart);
	this.latEnd      = parseFloat(latEnd);
	this.lngEnd      = parseFloat(lngEnd);
	this.street      = street;
	this.info        = infoObject;
	this.showPopUp   = showPopUp;
}

function Container_setLatLngInfo() {
	this.info.lat = this.latMedium();
	this.info.lng = this.lngMedium();
}

function Container_latMedium() {
	return (this.latStart+this.latEnd)/2;
}

function Container_lngMedium() {
	return (this.lngStart+this.lngEnd)/2;
}

/**** Oggetto che rappresenta un punto con le info delle label di gesitone ****/
function labelP(id, point, rotation, order, street, way, offsetX, offsetY, zoom) {
	this.id       = id;
	this.point    = point;
	this.rotation = rotation;
	this.order    = order;
	this.street   = street;
	this.way      = way;
	this.offsetX  = offsetX;
	this.offsetY  = offsetY;
	this.zoom     = zoom;
}

/**** Oggetto che simula un HashMap Java ****/

function removeAt( index ) {
	var part1 = this.slice( 0, index);
	var part2 = this.slice( index+1 );
	return( part1.concat( part2 ) );
}

Array.prototype.removeAt = removeAt;

function Map() {
	// members
	this.keyArray = new Array();	// Keys
	this.valArray = new Array();	// Values
	// methods    
	this.put    = put;
	this.get    = get;
	this.size   = size;
	this.clear  = clear;
	this.keySet = keySet;
	this.valSet = valSet;
	this.showMe = showMe;	// returns a string with all keys and values in map.
	
	this.findIt = findIt;
	this.remove = remove;
}

function put( key, val ) {
	var elementIndex = this.findIt( key );
	if( elementIndex == (-1) ) {
		this.keyArray.push( key );
		this.valArray.push( val );
	}
	else {
		this.valArray[ elementIndex ] = val;
	}
}

function get( key ) {
	var result = null;
	var elementIndex = this.findIt( key );
	if( elementIndex != (-1) ) {
		result = this.valArray[ elementIndex ];
	}
	return result;
}

function remove( key ) {
	var elementIndex = this.findIt( key );
	if( elementIndex != (-1) ) {
		this.keyArray = this.keyArray.removeAt(elementIndex);
		this.valArray = this.valArray.removeAt(elementIndex);
	}
	return ;
}

function size() {
	return (this.keyArray.length);  
}

function clear() {
	for( var i = 0; i < this.keyArray.length; i++ ) {
		this.keyArray.pop();
		this.valArray.pop();       
	}
}

function keySet() {
	return (this.keyArray);
}

function valSet() {
	return (this.valArray);   
}

function showMe() {
	var result = "";
	for( var i = 0; i < this.keyArray.length; i++ ) {
		result += "Key: " + this.keyArray[ i ] + "\tValues: " + this.valArray[ i ] + "\n";    
	}
	return result;
}

function findIt( key ) {
	var result = (-1);
	for( var i = 0; i < this.keyArray.length; i++ ) {
		if( this.keyArray[ i ] == key ) {
			result = i;
			break;
		}
	}
	return result;
}
