var map;
var chosen = [];
var Teolo128 = new TPhoto();Teolo128.id = 'Teolo128';Teolo128.src = 'http://www.arpa.veneto.it/upload_teolo/radar_teolo/BASE_128_Z_01.jpg';Teolo128.percentOpacity = 55;Teolo128.anchorTopLeft = new GLatLng(46.51,10.02);Teolo128.anchorBottomRight = new GLatLng(43.73,14.14);
var Concordia128 = new TPhoto();Concordia128.id = 'Concordia128';Concordia128.src = 'http://www.arpa.veneto.it/upload_teolo/radar_concordia/BASE_128_Z_01.jpg';Concordia128.percentOpacity = 55;Concordia128.anchorTopLeft = new GLatLng(46.85,11.14);Concordia128.anchorBottomRight = new GLatLng(44.07,15.26);
var Liguria = new TPhoto();Liguria.id = 'Liguria';Liguria.src = 'http://www.meteoliguria.it/dati_osservati/areali/radar_rpa.gif';Liguria.percentOpacity = 55;Liguria.anchorTopLeft = new GLatLng(46.60,6.15);Liguria.anchorBottomRight = new GLatLng(43.00,11.20);var Emilia = new TPhoto();Emilia.id = 'Emilia';Emilia.src = 'http://www.arpa.emr.it/smr/datiiningresso/Immagini/Radar/comp_b1n.png';Emilia.percentOpacity = 55;Emilia.anchorTopLeft = new GLatLng(47.65,6.90);Emilia.anchorBottomRight = new GLatLng(41.55,16.08);
var Trentino = new TPhoto();Trentino.id = 'Trentino';Trentino.src = 'http://www.meteotrentino.it/AspWeb/Monitoraggi/radar/movie/anim.gif';Trentino.percentOpacity = 55;Trentino.anchorTopLeft = new GLatLng(47.56,9.63);Trentino.anchorBottomRight = new GLatLng(45.386877,12.76);var Sardegna = new TPhoto();Sardegna.id = 'Sardegna';Sardegna.src = 'http://www.meteo4.com/stazioni/rete/radarsardo.php';Sardegna.percentOpacity = 55;Sardegna.anchorTopLeft = new GLatLng(43.00,5.50);Sardegna.anchorBottomRight = new GLatLng(37.70,12.55);
var Lazio = new TPhoto();Lazio.id = 'Lazio';Lazio.src = 'http://cetemps.aquila.infn.it/radarweb/home/realtime/WEB/spotref240CZ.gif';Lazio.percentOpacity = 55;Lazio.anchorTopLeft = new GLatLng(44.10,10.20);Lazio.anchorBottomRight = new GLatLng(39.91,16.12);var radars = [Teolo128,Concordia128,Liguria,Emilia,Trentino,Sardegna,Lazio];
///////////////////////////////////////////////////////////////
function hideAll() {

	var boxes = document.getElementsByName("mark");
	for(var i = 0; i < boxes.length; i++) {
		if(boxes[i].checked) {
			boxes[i].checked = false;
			switchRadar(false, map, radars[i]);
			chosen.push(i);
		}
	}
}

function checkChecked() {

	/* Returns true if a checkbox is still checked
	   *  otherwise false
	*/
	var boxes = document.getElementsByName("mark");
	for(var i = 0; i < boxes.length; i++) {
		if(boxes[i].checked) return true;
	}
	return false;
}

function switchRadar(checked, mappa, radar) {

	/* Function was originally borrowed from Esa:
	   *  http://esa.ilmari.googlepages.com/dropdownmenu.htm
	*/
	var layerbox = document.getElementById("box");
	var boxlink = document.getElementById("boxlink");
	var button = document.getElementById("more_inner");

	if(checked) {
		mappa.addTPhoto(radar);
		// Reset chosen array
		chosen.length = 0;
		/* Highlight the link and
		   *  make the button font bold.
		*/
		boxlink.className ="highlight";
		layerbox.className ="highlight";
		button.className ="highlight";
	}
	else {
		mappa.removeTPhoto(radar);
		/*  Reset the link and the button
		   * if all checkboxes were unchecked.
		*/
		if(!checkChecked()) {
			boxlink.blur();
			boxlink.className ="";
			layerbox.className ="";
			button.className ="";
		}
	}
}
function showLayerbox() {

	if(window.timer) clearTimeout(timer);
	document.getElementById("box").style.display = "block";
	var button = document.getElementById("more_inner");
	button.style.borderBottomWidth = "4px";
	button.style.borderBottomColor = "white";
}


function setClose() {

	var layerbox = document.getElementById("box");
	var button = document.getElementById("more_inner");
	var bottomColor = checkChecked() ? "#6495ed" : "#c0c0c0";

	timer = window.setTimeout(function() {
		layerbox.style.display = "none";
		button.style.borderBottomWidth = "1px";
		button.style.borderBottomColor = bottomColor;
	}, 400);
}

function toggleRadars() {

	if(chosen.length > 0 ) {
		/* Make an independent copy of chosen array since switchLayer()
		   *  resets the chosen array, which may not be useful here.
		*/
		var copy = chosen.slice();
		for(var i = 0; i < copy.length; i++) {
			var index = parseInt(copy[i]);
			switchRadar(true, map,radars[index].obj);
			document.getElementsByName("mark")[index].checked = true;
		}
	}
	else {
		hideAll();
	}
}
function MoreControl() {};
MoreControl.prototype = new GControl();
MoreControl.prototype.initialize = function(map) {

	var more = document.getElementById("outer_more");
	var buttonDiv = document.createElement("div");
	buttonDiv.id = "morebutton";
	buttonDiv.title = "Mostra/Nascondi Radars";
	buttonDiv.style.border = "1px solid black";
	buttonDiv.style.width = "62px";
	var textDiv = document.createElement("div");
	textDiv.id = "more_inner";
	textDiv.appendChild(document.createTextNode("Radar"));
	buttonDiv.appendChild(textDiv);

	// Register Event handlers
	more.onmouseover = showLayerbox;
	more.onmouseout = setClose;
	buttonDiv.onclick = toggleRadars;

	// Insert the button just after outer_more div
	more.insertBefore(buttonDiv, document.getElementById("box").parentNode);
	// Remove the whole div from its location and reinsert it to the map
	map.getContainer().appendChild(more);
	return more;
};

MoreControl.prototype.getDefaultPosition = function() {
	return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(260, 7));
};
