var debug=true;

// Map Class
function Map(){
	// Properties
	this.MapsPath = ""; // Path of image and html files
	this.ImageMapContainer = null; // Div containing the MAP
	this.ImageMap = null; // MAP element
	this.Map = null; // The IMG of the map
	this.MapContainer = null; // The Div containing the BackGround of the map
	this.AreasContainer = null; // The Div containing the selected areas
	this.CurrentArea = ""; // Current Area (es: Lombardia)
	this.Path = null; // Array of geografic hierarchy
	this.SelectedAreas = null; // Array of selected areas
	this.BreadCrumbsContainer = null; // Element containing Breadcrumbs
	this.SkipAreaFunction = null; // Function(area) for testing if an area is to skip 
	// (present in path but don't have map to be shown)
	// Private members
	var _maxAreaCount = 0; // Keep the max number of areas in imagemap to fix IE6/7 Image Map Crash
	// Init
	this.Init = function(){
		this.Path = new Array();
		this.SelectedAreas = new Array();
		this.MapsPath = "im/newMaps/";
		this.ImageMapContainer = $("imageMapContainer");
		this.Map = $("map");
		this.MapContainer = $("mapContainer");
		this.AreasContainer = $("areasContainer");
		//this.BreadCrumbsContainer = $("mapBc");
		this.Map.src = this.MapsPath + "spacer.gif";
		this.Map.style.position = "absolute";
		this.Map.border = 0;
		this.MapContainer.style.backgroundRepeat = 'no-repeat';
		this.MapContainer.style.position = "relative";
		this.MapContainer.style.width = "280px";
		this.MapContainer.style.height = "290px";
		this.Disabled=false;
	}
	// Construct path to file
	this.path = function(file){
		return (this.MapsPath + this.Path.join("/") + "/" + file);
	}
	// Update map areas
	this.UpdateMapAreas = function(){
		// Events handlers
		for (var i = 0; i < this.ImageMap.areas.length; i++) {
			var area = this.ImageMap.areas[i];
			
			area.onclick = maparea_click;
			
			area.onmouseover = maparea_mouseover;
			area.onmouseout = maparea_mouseout;
			area.href = "Javascript:void(0)";
		}
		// Workaround to fix IE6/7 Map Crash Bug
		// Appends blank areas to avoid crash
		_maxAreaCount = this.ImageMap.areas.length > _maxAreaCount ? this.ImageMap.areas.length : _maxAreaCount;
		for (var i = this.ImageMap.areas.length; i < _maxAreaCount; i++) {
			var area = document.createElement("area");
			area.shape = "poly";
			area.coords = "0,0,0,0";
			this.ImageMap.appendChild(area);
		}
	}
	// Show the map of an area
	this.ShowMap = function(area, depth){
		if (depth==0 && !area)
			this.CurrentArea="totale";
		else
			this.CurrentArea = area;
		// Update Path
		if (depth != null) {
			if (depth == 0) 
				this.Path = new Array();
			else 
				this.Path = this.Path.slice(0, depth);
		}
		if (area)
			this.Path.push(area);		
		// Check if the current area is to be skipped
		if (this.SkipAreaFunction) {
			if (this.SkipAreaFunction(area)) {
				if (this.Path.length >= 2) 
					this.ShowMap(this.Path[this.Path.length - 2], depth - 1);
				return;
			}
		}
		if (!area && depth==1){
			this.ShowMap(this.Path[0], 0);
			return;
		}
		// Remove previous Map
		if (this.ImageMapContainer.getElementsByTagName("map").length > 0) 
			this.ImageMapContainer.removeChild(this.ImageMapContainer.getElementsByTagName("map")[0]);
		// Create new Map
		this.ImageMap = document.createElement("map");
		this.ImageMap.id = "map" + area;
		this.ImageMap.name = this.ImageMap.id;
		// Load areas
		var areas = ajaxSyncGet(this.path(this.CurrentArea + ".html"));
		
		//______________________DEBUG______________________________
		//if(debug)
			//alert("PERCORSO DEL FILE HTML CONTENTE LA MAPPA"+this.path(this.CurrentArea + ".html"));
		//__________________________________________________________
			
		if (areas.search("<area") < 0) {
			areas = "";
		}
		this.ImageMap.innerHTML = areas;
		this.ImageMapContainer.appendChild(this.ImageMap);
		this.UpdateMapAreas();
		// Set images  
		this.MapContainer.style.backgroundImage = 'url(' + this.path(this.CurrentArea + ".gif") + ')';
		//______________________DEBUG______________________________
		//if(debug)
			//alert("IMMAGINE MAPPA"+this.path(this.CurrentArea + ".gif"));
		//__________________________________________________________
		
		this.Map.useMap = "#" + this.ImageMap.id;
		this.Map.src = this.MapsPath + "spacer.gif";
		// Update Breadcrumbs
		//this.BreadCrumbsContainer.innerHTML = this.BreadCrumbs();
		// Remove previously selected areas img
		this.RemoveArea();
	}
	this.EmptyMap = function(){
		this.ImageMap.innerHTML = "";
		this.MapContainer.style.backgroundImage = 'url(' + this.MapsPath + "spacer.gif" + ')';
		this.Map.src = this.MapsPath + "spacer.gif";
		this.BreadCrumbsContainer.innerHTML = "";
	}
	// Select an area (creating the img)
	this.SelectArea = function(area){
		if (area) {
			if (!this.SelectedAreas.find(area)) {
				var img = document.createElement("img");
				img.src = this.path("_over/" + area + ".gif");
				img.style.position = "absolute";
				img.style.top = "0";
				img.style.left = "0";
				img.border = 0;
				img.id = "img" + area;
				this.AreasContainer.appendChild(img);
				this.CurrentArea = area;
				if (this.Path.length == 4) 
					this.SelectedAreas.push(area);
			}
			// Update Breadcrumbs
			this.BreadCrumbsContainer.innerHTML = this.BreadCrumbs();
		}
		else {
			this.SelectedAreas = new Array();
		}
		
	}
	// Remove area(s) img
	this.RemoveArea = function(area){
		if (!area) {
			var imgs = this.AreasContainer.getElementsByTagName("img");
			for (var i = imgs.length - 1; i >= 0; i--) {
				this.AreasContainer.removeChild(imgs[i]);
			}
			this.SelectedAreas = new Array();
		}
		else {
			var img = document.getElementById("img" + area);
			if (img) 
				this.AreasContainer.removeChild(img);
			this.SelectedAreas.removeAll(area);
		}
	}
	// Construct Breadcrumbs
	this.BreadCrumbs = function(){
		var bc = "";
		for (var i = 0; i < this.Path.length; i++) {
			var area=new String(this.Path[i]);
			if (i==2)
				area= area.toUpperCase();
			else
				area= area.capitalize();
			bc += "<a href=\"Javascript:void(0)\" onclick=\"ShowMap('" + this.Path[i] + "'," + i + ")\">" + area + "</a>";
			if (i < this.Path.length - 1) 
				bc += " - ";
		}
		// CurrentArea
		/* Temporary removed
		if (this.Path[this.Path.length - 1] != this.CurrentArea) {
			var area=this.CurrentArea;
			if (this.Path.length - 1==1)
				area= area.toUpperCase();
			else
				area= area.capitalize();
			bc += " > " + area;
		}
		*/
		return bc;
	}
	// Rollover
	this.RollOver = function(id){
		this.Map.src = this.path("_over/" + id + ".gif");
	}
	// Rollout
	this.RollOut = function(id){
		this.Map.src = this.MapsPath + "spacer.gif";
	}
	
}


//______________________________________________________________________
//           CAMBIA LA CLASSE DI UN DIV	
//______________________________________________________________________
function changeClass(id, classElement)
{
	var element=document.getElementById(id);
	element.className=classElement;	
	

	return false;
		
}
//______________________________________________________________________

