// JavaScript Document
var fullscreen = false;

GMap2.prototype.controlInit = function(_map_div_id, _map_parent_div_id, _control_type) {

	/*
	_map.addControl(new GLargeMapControl());
	_map.addControl(new GMapTypeControl());
	return;
	*/
	
	function AddZoomControl() {}
	AddZoomControl.prototype = new GControl();
	
	AddZoomControl.prototype.initialize = function(_map) {		
		var container_zoom = document.createElement("div");
		container_zoom.setAttribute("style", "width:25px;");
		
		// +
		var imageDiv = document.createElement("img");
		this.setButtonStyle_(imageDiv);
		container_zoom.appendChild(imageDiv);
		imageDiv.setAttribute("src", "../fr/images/control_zoom_plus.png");
		imageDiv.setAttribute("width", "25");
		imageDiv.setAttribute("height", "25");
		GEvent.addDomListener(imageDiv, "click", function() {
			_map.zoomIn();
		});
		container_zoom.appendChild(document.createElement("br"));

		var available_height = _map.getSize().height - 40 -60;
		if ( available_height >= 120 )
		{
			available_height -= 120;
			
			zoom = document.createElement("img"); this.setButtonStyle_(zoom);
			zoom.setAttribute("src", "../fr/images/control_zoom_8.png");
			GEvent.addDomListener(zoom, "click", function() { _map.setZoom(16); }); container_zoom.appendChild(zoom);
			container_zoom.appendChild(document.createElement("br"));
	
			zoom = document.createElement("img"); this.setButtonStyle_(zoom);
			zoom.setAttribute("src", "../fr/images/control_zoom_7.png");
			GEvent.addDomListener(zoom, "click", function() { _map.setZoom(14); }); container_zoom.appendChild(zoom);
			container_zoom.appendChild(document.createElement("br"));
			
			zoom = document.createElement("img"); this.setButtonStyle_(zoom);
			zoom.setAttribute("src", "../fr/images/control_zoom_6.png");
			GEvent.addDomListener(zoom, "click", function() { _map.setZoom(12); }); container_zoom.appendChild(zoom);
			container_zoom.appendChild(document.createElement("br"));
	
			zoom = document.createElement("img"); this.setButtonStyle_(zoom);
			zoom.setAttribute("src", "../fr/images/control_zoom_5.png");
			GEvent.addDomListener(zoom, "click", function() { _map.setZoom(10); }); container_zoom.appendChild(zoom);
			container_zoom.appendChild(document.createElement("br"));
			
			zoom = document.createElement("img"); this.setButtonStyle_(zoom);
			zoom.setAttribute("src", "../fr/images/control_zoom_4.png");
			GEvent.addDomListener(zoom, "click", function() { _map.setZoom(8); }); container_zoom.appendChild(zoom);
			container_zoom.appendChild(document.createElement("br"));
			
			zoom = document.createElement("img"); this.setButtonStyle_(zoom);
			zoom.setAttribute("src", "../fr/images/control_zoom_3.png");
			GEvent.addDomListener(zoom, "click", function() { _map.setZoom(6); }); container_zoom.appendChild(zoom);
			container_zoom.appendChild(document.createElement("br"));
			
			zoom = document.createElement("img"); this.setButtonStyle_(zoom);
			zoom.setAttribute("src", "../fr/images/control_zoom_2.png");
			GEvent.addDomListener(zoom, "click", function() { _map.setZoom(4); }); container_zoom.appendChild(zoom);
			container_zoom.appendChild(document.createElement("br"));
			
			zoom = document.createElement("img"); this.setButtonStyle_(zoom);
			zoom.setAttribute("src", "../fr/images/control_zoom_1.png");
			GEvent.addDomListener(zoom, "click", function() { _map.setZoom(2); }); container_zoom.appendChild(zoom);
			container_zoom.appendChild(document.createElement("br"));
		}
		
		// -
		var imageDiv = document.createElement("img");
		this.setButtonStyle_(imageDiv);
		container_zoom.appendChild(imageDiv);
		imageDiv.setAttribute("src", "../fr/images/control_zoom_moins.png");
		imageDiv.setAttribute("width", "25");
		imageDiv.setAttribute("height", "25");
		GEvent.addDomListener(imageDiv, "click", function() {
			_map.zoomOut();
		});
		
		_map.getContainer().appendChild(container_zoom);
		return container_zoom;
	}
	
	AddZoomControl.prototype.getDefaultPosition = function() {
		return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(5, 5));
	}
	
	// Sets the proper CSS for the given button element.
	AddZoomControl.prototype.setButtonStyle_ = function(button) {
		button.style.cursor = "pointer";
	}

	function AddButtonControl() {}
	AddButtonControl.prototype = new GControl();
	
	AddButtonControl.prototype.initialize = function(_map) {		
		var container_button = document.createElement("div");
		var available_width = _map.getSize().width - 40 - 155; // 155 is for type buttons that are always here
		var thisthis = this;
		
		function createLeft() {
			var button_left = document.createElement("img");
			button_left.setAttribute("src", "../fr/images/control_left.png");
			button_left.setAttribute("width", "6");
			button_left.setAttribute("height", "25");
			thisthis.setButtonStyle_(button_left);
			//button_left.setAttribute("style", "float:left");
			return button_left;
		}
		
		function createSeparator() {
			var button_separator = document.createElement("div");
			thisthis.setButtonSepStyle_(button_separator);
			return button_separator;
		}
		
		// Button right
		function createRight() {
			var button_right = document.createElement("img");
			button_right.setAttribute("src", "../fr/images/control_right.png");
			button_right.setAttribute("width", "10");
			button_right.setAttribute("height", "25");
			thisthis.setButtonStyle_(button_right);
			//button_right.setAttribute("style", "float:left");
			return button_right;
		}
		
		var ajouter_un_spot_width = 112;
		if ( available_width >= ajouter_un_spot_width )
		{
			if ( admin )
			{
				available_width -= ajouter_un_spot_width; 
				
				// Add Button
				container_button.appendChild(createLeft());
				
				// Button
				var imageDiv = document.createElement("div");
				imageDiv.appendChild(document.createTextNode("Ajouter un Spot"));
				this.setButtonBgStyle_(imageDiv);
				GEvent.addDomListener(imageDiv, "click", function() {
					if ( add_spot_marker == null ) add_spot_marker = admin_addSpotMarker("add_spot_marker");
					add_spot = !add_spot;
					if ( add_spot ) _map.addOverlay(add_spot_marker);
					else			_map.removeOverlay(add_spot_marker);
				});
				container_button.appendChild(imageDiv);
				
				container_button.appendChild(createRight());
				container_button.appendChild(createSeparator());
			}
		}

		var fullscreen_width = 85;
		if ( available_width >= fullscreen_width )
		{
			available_width -= fullscreen_width;
			container_button.appendChild(createLeft());
			// Button
			var imageDiv = document.createElement("div");
			var buttom_fullscreen = document.createTextNode("Plein écran");
			imageDiv.appendChild(buttom_fullscreen);
			imageDiv.setAttribute("id", "button_fullscreen"); 
			this.setButtonBgStyle_(imageDiv);
			container_button.appendChild(imageDiv);
			GEvent.addDomListener(imageDiv, "click", function() {
				if ( fullscreen ) {
					var center = _map.getCenter();
					var map_div = document.getElementById(_map_div_id);
					var map_parent_div = document.getElementById(_map_parent_div_id);
					map_div.style.width  = map_parent_div.style.width;
					map_div.style.height = map_parent_div.style.height;
					map_parent_div.appendChild(map_div);
					document.getElementById('layout_center').style.display = "block";
					_map.checkResize();
					_map.setCenter(center);
					document.getElementById("button_fullscreen").replaceChild(document.createTextNode("Plein écran"), document.getElementById("button_fullscreen").firstChild);			
					fullscreen = false;
				} else {
					var center = _map.getCenter();
					var winW = 0; 
					var winH = 0;
					if (window.innerWidth && window.innerHeight) {
						winW = window.innerWidth;
						winH = window.innerHeight;
					} else if (document.documentElement && document.documentElement.clientWidth && document.documentElement.clientHeight) {
						winW = document.documentElement.clientWidth;
						winH = document.documentElement.clientHeight;
					} else if (document.body) {
						winW = document.body.clientWidth;
						winH = document.body.clientHeight;
					}
					var map_div = document.getElementById(_map_div_id);
					map_div.style.width  = winW + "px";
					map_div.style.height = winH + "px";
					document.getElementById("button_fullscreen").replaceChild(document.createTextNode("Réduire l'écran"), document.getElementById("button_fullscreen").firstChild);			
					document.body.appendChild(map_div);
					document.getElementById('layout_center').style.display = "none";
					_map.checkResize();
					
					_map.setCenter(center);
					fullscreen = true;
				}
			});
			container_button.appendChild(createRight());
		
			container_button.appendChild(createSeparator());
		}
		
		// 155px
		//-------
		if ( available_width >= 40 )
		{
			container_button.appendChild(createLeft());
			// Button
			var imageDiv = document.createElement("div");
			imageDiv.appendChild(document.createTextNode("Plan"));
			this.setButtonBgStyle_(imageDiv);
			container_button.appendChild(imageDiv);
			GEvent.addDomListener(imageDiv, "click", function() {
				_map.setMapType(G_NORMAL_MAP);
			});
			container_button.appendChild(createRight());
			
			container_button.appendChild(createLeft());
			// Button
			var imageDiv = document.createElement("div");
			imageDiv.appendChild(document.createTextNode("Satellite"));
			this.setButtonBgStyle_(imageDiv);
			container_button.appendChild(imageDiv);
			GEvent.addDomListener(imageDiv, "click", function() {
				_map.setMapType(G_SATELLITE_MAP);
			});
			container_button.appendChild(createRight());
			
			container_button.appendChild(createLeft());
			// Button
			var imageDiv = document.createElement("div");
			imageDiv.appendChild(document.createTextNode("Mixte"));
			container_button.appendChild(imageDiv);
			this.setButtonBgStyle_(imageDiv);
			GEvent.addDomListener(imageDiv, "click", function() {
				_map.setMapType(G_HYBRID_MAP);
			});
			container_button.appendChild(createRight());
		}
		
		// 155px
		//-------
		var logo_width = 155;
		if ( available_width >= logo_width )
		{ 
			available_width -= logo_width;
			
			container_button.appendChild(createSeparator());
	
			// rollerenligne
			var imageDiv = document.createElement("img");
			container_button.appendChild(imageDiv);
			imageDiv.setAttribute("src", "../fr/images/control_logo.png");
			imageDiv.setAttribute("width", "155");
			imageDiv.setAttribute("height", "25");
			this.setButtonStyle_(imageDiv);
			//imageDiv.setAttribute("style", "float:left; cursor:pointer");
			GEvent.addDomListener(imageDiv, "click", function() {
				window.location.href = "http://www.rollerenligne.com";
			});
		}
		
		_map.getContainer().appendChild(container_button);

		return container_button;
	}
	
	AddButtonControl.prototype.getDefaultPosition = function() {
		return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(2, 5));
	}
	
	// Sets the proper CSS for the given button element.
	AddButtonControl.prototype.setButtonStyle_ = function(button) {
		if ( navigator.appName == 'Microsoft Internet Explorer')
			button.style.styleFloat  = "left";
		else
			button.style.cssFloat  = "left";
	}
	// Sets the proper CSS for the given button element.
	AddButtonControl.prototype.setButtonSepStyle_ = function(button) {
		button.style.width = "6px";
		button.style.height = "25px";
		if ( navigator.appName == 'Microsoft Internet Explorer')
			button.style.styleFloat  = "left";
		else
			button.style.cssFloat  = "left";	}
	
	// Sets the proper CSS for the given button element.
	AddButtonControl.prototype.setButtonBgStyle_ = function(button) {
		button.style.cursor = "pointer";
		button.style.height = "25px";
		button.style.paddingTop = "3px";
		button.style.backgroundImage = "url(../fr/images/control_fd.png)";
		button.style.backgroundRepeat = "repeat-x";
		if ( navigator.appName == 'Microsoft Internet Explorer')
			button.style.styleFloat  = "left";
		else
			button.style.cssFloat  = "left";
	}

	this.addControl(new AddButtonControl());
	this.addControl(new AddZoomControl());
	
	// Enable double click zoom
	this.enableContinuousZoom();
	this.enableDoubleClickZoom();
}

GMap2.prototype.setMapTypeFromArg = function(_type) {
	switch ( _type ) {
		case 'm' : this.setMapType(G_NORMAL_MAP); break;
		case 'k' : this.setMapType(G_SATELLITE_MAP); break;
		case 'h' : this.setMapType(G_HYBRID_MAP); break;
		default  : this.setMapType(G_NORMAL_MAP);
	}
}
