//*******************************************************************************
// 2007 Copyright Francesco Maria Magnini Perugia (Italy)
// Triweb (r) Trademark 2007 Francesco Maria Magnini
// File:     toolmap.js
// Author:   Francesco Maria Magnini mailto:francesco.magnini@triweb.it
// Date:     10-05-2007
//*******************************************************************************
// This is free software; you can redistribute it and/or modify it under the terms
// of the GNU General Public License as published by the Free Software Foundation.
// It is illegal to remove or tamper with the notices at the top of each file,
// or this file, or the license file.
//*******************************************************************************

// Funzioni di aiuto per la creazione di GMap2

//*******************************************************************************
// Controlli Zoom

var map;
var opacity = 80;

function TriwebZoomControl() {}
TriwebZoomControl.prototype = new GControl();

var zoomOutImg = document.createElement("img");
var zoom_1 = document.createElement("img");
var zoom_2 = document.createElement("img");
var zoom_3 = document.createElement("img");
var zoom_4 = document.createElement("img");
var zoom_5 = document.createElement("img");
var zoom_6 = document.createElement("img");
var zoom_7 = document.createElement("img");
var zoom_8 = document.createElement("img");
var zoomInImg = document.createElement("img");

TriwebZoomControl.prototype.initialize = function(map) {

	var container_zoom = document.createElement("div");
	this.setControlStyle_(container_zoom);
	
	this.setImageStyle_(zoomOutImg);
	this.setZoomImageStyle_(zoom_1);
	this.setZoomImageStyle_(zoom_2);
	this.setZoomImageStyle_(zoom_3);
	this.setZoomImageStyle_(zoom_4);
	this.setZoomImageStyle_(zoom_5);
	this.setZoomImageStyle_(zoom_6);
	this.setZoomImageStyle_(zoom_7);
	this.setZoomImageStyle_(zoom_8);
	this.setImageStyle_(zoomInImg);
	
	container_zoom.appendChild(zoomOutImg);
	container_zoom.appendChild(zoom_1);
	container_zoom.appendChild(zoom_2);
	container_zoom.appendChild(zoom_3);
	container_zoom.appendChild(zoom_4);
	container_zoom.appendChild(zoom_5);
	container_zoom.appendChild(zoom_6);
	container_zoom.appendChild(zoom_7);
	container_zoom.appendChild(zoom_8);
	container_zoom.appendChild(zoomInImg);

	GEvent.addDomListener(zoomOutImg, "click", function() {
		map.zoomOut();
		TriwebZoomControl.prototype.setActiveZoom_();
	});
	GEvent.addDomListener(zoom_1, "click", function() {
		map.setZoom(2);
		TriwebZoomControl.prototype.setActiveZoom_();
	});
	GEvent.addDomListener(zoom_2, "click", function() {
		map.setZoom(4);
		TriwebZoomControl.prototype.setActiveZoom_();
	});
	GEvent.addDomListener(zoom_3, "click", function() {
		map.setZoom(6);
		TriwebZoomControl.prototype.setActiveZoom_();
	});
	GEvent.addDomListener(zoom_4, "click", function() {
		map.setZoom(8);
		TriwebZoomControl.prototype.setActiveZoom_();
	});
	GEvent.addDomListener(zoom_5, "click", function() {
		map.setZoom(10);
		TriwebZoomControl.prototype.setActiveZoom_();
	});
	GEvent.addDomListener(zoom_6, "click", function() {
		map.setZoom(12);
		TriwebZoomControl.prototype.setActiveZoom_();
	});
	GEvent.addDomListener(zoom_7, "click", function() {
		map.setZoom(14);
		TriwebZoomControl.prototype.setActiveZoom_();
	});
	GEvent.addDomListener(zoom_8, "click", function() {
		map.setZoom(16);
		TriwebZoomControl.prototype.setActiveZoom_();
	});
	GEvent.addDomListener(zoomInImg, "click", function() {
		map.zoomIn();
		TriwebZoomControl.prototype.setActiveZoom_();
	});
	
	zoomInImg.setAttribute('src','/images/map/zoom_in_control.jpg');
	zoomOutImg.setAttribute('src','/images/map/zoom_out_control.jpg');
	TriwebZoomControl.prototype.setActiveZoom_();
	map.getContainer().appendChild(container_zoom);
	return container_zoom;
	
}

TriwebZoomControl.prototype.getDefaultPosition = function() {
	return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(10, 10));
}

TriwebZoomControl.prototype.setControlStyle_ = function(control) {	
	setOpacity(control, opacity);
	control.style.backgroundColor = "#fff";
	if (navigator.userAgent.indexOf("MSIE") != -1) {
		control.style.padding = "3px 0px 3px 0px";
	} else {
		control.style.padding = "4px 0px 2px 0px";
	}
}

TriwebZoomControl.prototype.setImageStyle_ = function(image) {
	image.style.cursor = "pointer";
	image.style.padding = "0px 4px 0px 4px";
}

TriwebZoomControl.prototype.setZoomImageStyle_ = function(image) {
	image.style.cursor = "pointer";
	image.style.padding = "0px 1px 0px 1px";
}

TriwebZoomControl.prototype.setActiveZoom_ = function() {
	var zoom = map.getZoom();
	if ((zoom > 0)&&(zoom < 3)) {
		zoom_1.setAttribute('src','/images/map/zoom_1on.jpg');
		zoom_2.setAttribute('src','/images/map/zoom_2.jpg');
		zoom_3.setAttribute('src','/images/map/zoom_3.jpg');
		zoom_4.setAttribute('src','/images/map/zoom_4.jpg');
		zoom_5.setAttribute('src','/images/map/zoom_5.jpg');
		zoom_6.setAttribute('src','/images/map/zoom_6.jpg');
		zoom_7.setAttribute('src','/images/map/zoom_7.jpg');
		zoom_8.setAttribute('src','/images/map/zoom_8.jpg');
	} else if ((zoom > 2)&&(zoom < 5)) {
		zoom_1.setAttribute('src','/images/map/zoom_1on.jpg');
		zoom_2.setAttribute('src','/images/map/zoom_2on.jpg');
		zoom_3.setAttribute('src','/images/map/zoom_3.jpg');
		zoom_4.setAttribute('src','/images/map/zoom_4.jpg');
		zoom_5.setAttribute('src','/images/map/zoom_5.jpg');
		zoom_6.setAttribute('src','/images/map/zoom_6.jpg');
		zoom_7.setAttribute('src','/images/map/zoom_7.jpg');
		zoom_8.setAttribute('src','/images/map/zoom_8.jpg');		
	} else if ((zoom > 4)&&(zoom < 7)) {
		zoom_1.setAttribute('src','/images/map/zoom_1on.jpg');
		zoom_2.setAttribute('src','/images/map/zoom_2on.jpg');
		zoom_3.setAttribute('src','/images/map/zoom_3on.jpg');
		zoom_4.setAttribute('src','/images/map/zoom_4.jpg');
		zoom_5.setAttribute('src','/images/map/zoom_5.jpg');
		zoom_6.setAttribute('src','/images/map/zoom_6.jpg');
		zoom_7.setAttribute('src','/images/map/zoom_7.jpg');
		zoom_8.setAttribute('src','/images/map/zoom_8.jpg');		
	} else if ((zoom > 6)&&(zoom < 9)) {
		zoom_1.setAttribute('src','/images/map/zoom_1on.jpg');
		zoom_2.setAttribute('src','/images/map/zoom_2on.jpg');
		zoom_3.setAttribute('src','/images/map/zoom_3on.jpg');
		zoom_4.setAttribute('src','/images/map/zoom_4on.jpg');
		zoom_5.setAttribute('src','/images/map/zoom_5.jpg');
		zoom_6.setAttribute('src','/images/map/zoom_6.jpg');
		zoom_7.setAttribute('src','/images/map/zoom_7.jpg');
		zoom_8.setAttribute('src','/images/map/zoom_8.jpg');		
	} else if ((zoom > 8)&&(zoom < 11)) {
		zoom_1.setAttribute('src','/images/map/zoom_1on.jpg');
		zoom_2.setAttribute('src','/images/map/zoom_2on.jpg');
		zoom_3.setAttribute('src','/images/map/zoom_3on.jpg');
		zoom_4.setAttribute('src','/images/map/zoom_4on.jpg');
		zoom_5.setAttribute('src','/images/map/zoom_5on.jpg');
		zoom_6.setAttribute('src','/images/map/zoom_6.jpg');
		zoom_7.setAttribute('src','/images/map/zoom_7.jpg');
		zoom_8.setAttribute('src','/images/map/zoom_8.jpg');		
	} else if ((zoom > 10)&&(zoom < 13)) {
		zoom_1.setAttribute('src','/images/map/zoom_1on.jpg');
		zoom_2.setAttribute('src','/images/map/zoom_2on.jpg');
		zoom_3.setAttribute('src','/images/map/zoom_3on.jpg');
		zoom_4.setAttribute('src','/images/map/zoom_4on.jpg');
		zoom_5.setAttribute('src','/images/map/zoom_5on.jpg');
		zoom_6.setAttribute('src','/images/map/zoom_6on.jpg');
		zoom_7.setAttribute('src','/images/map/zoom_7.jpg');
		zoom_8.setAttribute('src','/images/map/zoom_8.jpg');		
	} else if ((zoom > 12)&&(zoom < 15)) {
		zoom_1.setAttribute('src','/images/map/zoom_1on.jpg');
		zoom_2.setAttribute('src','/images/map/zoom_2on.jpg');
		zoom_3.setAttribute('src','/images/map/zoom_3on.jpg');
		zoom_4.setAttribute('src','/images/map/zoom_4on.jpg');
		zoom_5.setAttribute('src','/images/map/zoom_5on.jpg');
		zoom_6.setAttribute('src','/images/map/zoom_6on.jpg');
		zoom_7.setAttribute('src','/images/map/zoom_7on.jpg');
		zoom_8.setAttribute('src','/images/map/zoom_8.jpg');		
	} else if ((zoom > 14)&&(zoom <= 17)) {
		zoom_1.setAttribute('src','/images/map/zoom_1on.jpg');
		zoom_2.setAttribute('src','/images/map/zoom_2on.jpg');
		zoom_3.setAttribute('src','/images/map/zoom_3on.jpg');
		zoom_4.setAttribute('src','/images/map/zoom_4on.jpg');
		zoom_5.setAttribute('src','/images/map/zoom_5on.jpg');
		zoom_6.setAttribute('src','/images/map/zoom_6on.jpg');
		zoom_7.setAttribute('src','/images/map/zoom_7on.jpg');
		zoom_8.setAttribute('src','/images/map/zoom_8on.jpg');
	}
}

function setOpacity(element, opacity) {
	if (navigator.userAgent.indexOf("MSIE") != -1) {
		element.style.filter = "alpha(opacity=" + opacity + ")";
	} else {
		element.style.opacity = "0." + opacity;
	}
}

function loadMap(lat, lng) {
	if (GBrowserIsCompatible()) {
		
		map = new GMap2(document.getElementById("map"));
		var point;
		if (navigator.userAgent.indexOf("MSIE") != -1) {
			point = new GLatLng(lat, lng);
		} else {
			point = new GLatLng(lat, lng);
		}	
		map.enableContinuousZoom();
		map.enableDoubleClickZoom();	
		map.addControl(new TriwebZoomControl());
		map.setCenter(point, 14, G_HYBRID_MAP);
		TriwebZoomControl.prototype.setActiveZoom_();
		
		var casaIcon = new GIcon();
		casaIcon.iconSize = new GSize(32,32);
		casaIcon.shadowSize = new GSize(59,32);
		casaIcon.iconAnchor = new GPoint(16,32);
		casaIcon.infoWindowAnchor = new GPoint(16,0);
		casaIcon.image = "http://www.mappare.com/images/map/red_cloud_marker.png";
		casaIcon.shadow = "http://www.mappare.com/images/map/shadow_cloud_marker.png";
		
		var marker = new GMarker(point, casaIcon, {clickable:false});
		map.addOverlay(marker);
		GEvent.addListener(marker, "click", function() {
			map.zoomIn();
		});	
		
		GEvent.addListener(map, "moveend", function() {
			TriwebZoomControl.prototype.setActiveZoom_();
		});
		
	} else {
		alert("Le Google Maps API non sono compatibili con questo browser");
	}
}

window.onunload = function() {
	GUnload();
}