
var the_timer;
var chosen = [];
var buttonWidth=86;
var class1="", class2="", class3="";

/* Implementation of the button "More" with the code from Wolfgang Pichler -> http://www.wolfpil.de/more-button.html. Thanks Wolfgang :) */

/* Array of GLayers
 * The "name" property is not being used here
*/
var layers = [
 { name: "Pano", obj: new GLayer("com.panoramio.all") },
 { name: "Tube", obj: new GLayer("com.youtube.all") },
 { name: "Wiki", obj: new GLayer("org.wikipedia.en") },
 { name: "Cams", obj: new GLayer("com.google.webcams") }
];

function hideAll(id) {
	var boxes = document.getElementsByName("mark_"+id);
	for(var i = 0; i < boxes.length; i++) {
		if(boxes[i].checked) {
			boxes[i].checked = false;
			switchLayer(false, layers[i].obj, id);
			chosen.push(i);
		}
	}
	var button = document.getElementById("more_inner_"+id);
	button.className ="more_inner";
}

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

function switchLayer(checked, layer, id) {
	/* Function was originally borrowed from Esa: http://esa.ilmari.googlepages.com/dropdownmenu.htm */
	var layerbox = document.getElementById("box_"+id);
	var boxlink = document.getElementById("boxlink_"+id);
	var button = document.getElementById("more_inner_"+id);

	if(checked) {
		if(class1=="") {
			class1=boxlink.className;
			class2=layerbox.className;
			class3=button.className;
		}

		eval("map_"+id).addOverlay(layer);
		// Reset chosen array
		chosen.length = 0;
		/* Highlight the link and make the button font bold. */
		boxlink.className =class1+" highlight";
		layerbox.className =class2+" highlight";
		button.className =class3+" highlight";
	}
	else {
		eval("map_"+id).removeOverlay(layer);
		/* Reset the link and the button if all checkboxes were unchecked.	*/
		if(!checkChecked(id)) {
			boxlink.blur();
			boxlink.className = class1;
			layerbox.className = class2;
			button.className = class3;
		}
	}
}

function showLayerbox(id) {
	if(window.the_timer) clearTimeout(the_timer);
	document.getElementById("box_"+id).style.display = "block";
	var button = document.getElementById("more_inner_"+id);
	button.style.borderBottomWidth = "4px";
	button.style.borderBottomColor = "white";
}


function setClose(id) {
	var layerbox = document.getElementById("box_"+id);
	var button = document.getElementById("more_inner_"+id);
	var bottomColor = checkChecked(id) ? "#6495ed" : "#c0c0c0";

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


function toggleLayers(id) {
	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]);
			switchLayer(true, layers[index].obj, id);
			document.getElementsByName("mark_"+id)[index].checked = true;
		}
	}
	else {
		hideAll(id);
	}
}

var use_yet=false;

function MoreControl_0() {};
MoreControl_0.prototype = new GControl();
MoreControl_0.prototype.initialize = function(map_0) {

	if(use_yet==false) {
		if(document.createStyleSheet) {
			document.createStyleSheet("http://www.kidsartsventura.org/wp-content/plugins/google-maps/maps.css");
		}
		else {
			var newSS=document.createElement("link");
			newSS.rel="stylesheet";
			newSS.type="text/css";
			newSS.href="http://www.kidsartsventura.org/wp-content/plugins/google-maps/maps.css";
			document.documentElement.firstChild.appendChild(newSS)
		}
		use_yet=true;
	}

	var more_0 = document.getElementById("outer_more_0");
	var buttonDiv = document.createElement("div");
	buttonDiv.id = "morebutton_0";
	buttonDiv.title = "Show/Hide Layers";
	buttonDiv.className="more_button";
	buttonDiv.style.width = buttonWidth+"px";
	var textDiv = document.createElement("div");
	textDiv.id = "more_inner_0";
	textDiv.className="more_inner";
	textDiv.appendChild(document.createTextNode("More..."));
	buttonDiv.appendChild(textDiv);

	// Register Event handlers
	more_0.onmouseover = function () { showLayerbox(0); }
	more_0.onmouseout = function () { setClose(0); }
	buttonDiv.onclick = function () { toggleLayers(0); }

	// Insert the button just after outer_more div
	more_0.insertBefore(buttonDiv, document.getElementById("box_0").parentNode);
	map_0.getContainer().appendChild(more_0);

	return more_0;
}

MoreControl_0.prototype.getDefaultPosition = function() {
	//moitie=(document.getElementById("Googlemap_0").offsetWidth/2)-(buttonWidth/2);
	moitie=70;
	return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(moitie, 7));
}

var map_0, geocoder=null, geoXml=null, latlng;

latlng="", q="856 East Thompson Blvd.Ventura, CA, 93001";
if (GBrowserIsCompatible()) {
	map_0 = new GMap2(document.getElementById("Googlemap_0"));
	map_0.clearOverlays();
	map_0.enableDoubleClickZoom();
	map_0.enableContinuousZoom();
	map_0.setMapType(G_NORMAL_MAP); // Vue carte, (G_SATELLITE_MAP: Vue satellite) - (G_HYBRID_MAP: Vue mixte)
	map_0.setUIToDefault();
	map_0.addControl(new MoreControl_0());

	if(q!="") {
		adresse="856 East Thompson Blvd.Ventura, CA, 93001";
		geocoder = new GClientGeocoder();
		geocoder.getLocations(adresse, 
		function(reponse) {
			if (!reponse || reponse.Status.code != 200) {
				ErrorMsg= "Impossible To GeoCode:\n";
				ErrorMsg+="Error Code: "+reponse.Status.code+"\n";
				ErrorMsg+="Info: http://www.google.com/apis/maps/documentation/reference.html#GGeoStatusCode";
				//alert(ErrorMsg);

				if(latlng!="") {
					var coord=new GLatLng();
					map_0.setCenter(coord, 16);

					var marker = new GMarker(coord);
					GEvent.addListener(marker, "click",
					function() {
						marker.openInfoWindowHtml("<div style='text-transform:capitalize;'><b>856 East Thompson Blvd.Ventura</b></div><p style='text-transform:capitalize;'> CA, 93001<br/><small>Approximate placement</small></p>");
					});
					map_0.addOverlay(marker);
					marker.openInfoWindowHtml("<div style='text-transform:capitalize;'><b>856 East Thompson Blvd.Ventura</b></div><p style='text-transform:capitalize;'> CA, 93001<br/><small>Approximate placement</small></p>");
				}

			}
			else {
				place = reponse.Placemark[0];
				var Adresse = place.address;
				var Glatitude = place.Point.coordinates[1];
				var Glongitude = place.Point.coordinates[0];

				InfoArray=Adresse.split(", ");
				Adresse="<div style='text-transform:capitalize;'><b>"+InfoArray[0]+"</b></div>";
				for(var i=1; i<InfoArray.length; i++) {
					Adresse+="<div style='text-transform:capitalize;'>"+InfoArray[i]+"</div>";
				}

				var coord=new GLatLng(Glatitude,Glongitude);
				map_0.setCenter(coord, 16);
				var marker = new GMarker(coord);
				GEvent.addListener(marker, "click",
				function() {
					marker.openInfoWindowHtml(Adresse);
				});
				map_0.addOverlay(marker);
				marker.openInfoWindowHtml(Adresse);
			}
		});
	}
	else {
		var coord=new GLatLng();
		map_0.setCenter(coord, 16);

		geoXml = new GGeoXml("http://www.google.com/maps/ms?output=nl&oe=UTF-8&q=856 East Thompson Blvd.Ventura, CA, 93001&ie=UTF8&hq=&hnear=856 E Thompson Blvd, Ventura, California 93001&gl=us&ei=sNBjS-_6BpPgtgP_hKmdAw&ved=0CAgQ8gEwAA&z=16");
		map_0.addOverlay(geoXml);
		//geoXml.gotoDefaultViewport(map_0);
	}

	PosLib = document.getElementById("Lib_0");
	TagSmall=document.createElement("small")
	Lien = document.createElement('a');
	Lien.setAttribute("href","http://maps.google.com/maps?oe=UTF-8&q=856 East Thompson Blvd.Ventura, CA, 93001&ie=UTF8&hq=&hnear=856 E Thompson Blvd, Ventura, California 93001&gl=us&ei=sNBjS-_6BpPgtgP_hKmdAw&ved=0CAgQ8gEwAA&z=16");
	Lien.appendChild(document.createTextNode("Zoom in"));
	TagSmall.appendChild(Lien);
	PosLib.appendChild(TagSmall);
}
else {
	document.getElementById("Googlemap_0").innerHTML="Sorry, your browser can't display Google Maps...";
}
