/*
MMapTypeControl
Control to set the map type.

Copyright 2008 - Marcelo Montagna  - http://maps.forum.nu

Free to use as long as copyright notices are left unchanged.
Please save the file to your own server. Do not link directly,
or unexpected things might happen to your control :-)

Note: This script contains code to prevent hotlinking. (marked with 'REMOVE')
You need to remove it when saving the file to your server.

------------------------------------------------------------
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
------------------------------------------------------------


Usage:
	map.addControl(new MMapTypeControl());
	map.addControl(new MMapTypeControl(options?));

MMapTypeControl options:
	Position: GControlPosition()
	background: HTML color - Default: '#8080FF';
	foreground: HTML color - Default: '#EEEEEE';
	parent: DOM element;
	direction: 'V' or 'H' - Default: 'H';
*/

/////////////////////////////////////////////////////////////////////////////



//////////// BEGIN MMapTypeControl /////////////////
function MMapTypeControl(MOptions) {
	MOptions = MOptions ? MOptions : {};
	this.parent = MOptions.container ? MOptions.container : null;
	this.direction = MOptions.direction ? MOptions.direction : 'H';
	this.background = MOptions.background ? MOptions.background : '#8080FF';
	this.foreground = MOptions.foreground ? MOptions.foreground : '#EEEEEE';
	this.validValues = MOptions.validValues ? MOptions.validValues : [];
	this.maxRes = 0;
	this.position = MOptions.position ? MOptions.position : new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(5, 5));
}

MMapTypeControl.prototype = new GControl();

MMapTypeControl.prototype.initialize = function(map) {
	var that = this;
	this.map = map;

	GEvent.addListener(map, "addmaptype", function(type) {that.mapAddType(type)});
	GEvent.addListener(map, "removemaptype", function(type) {that.mapRemoveType(type)});
	GEvent.addListener(map, "maptypechanged", function(o,n) {that.mapTypeChanged()});

	var container = document.createElement('div');
	this.container = container;
	var oTable = document.createElement('table');
	oTable.id = 'MMTT_TABLE';
	container.appendChild(oTable);

	oTable.setAttribute('cellSpacing','0');
	oTable.setAttribute('cellPadding','0');

	var oTbody = document.createElement('tbody');
	oTable.appendChild(oTbody);

	var mapTypes = this.map.getMapTypes();
	for (var  n = 0 ; n < mapTypes.length ; n++ ) {
		var label = mapTypes[n].getName();
		if (this.direction == 'V' || n == 0) {
			var oRow = document.createElement('tr');
			oTbody.appendChild(oRow);
			oRow.id = 'MMTR_'+n;

		}
		var oCell = this.createButton(that,n,label);
	oCell.innerHTML+=" ";  // This line for f*****g IE, else it willnot render the control

		oRow.appendChild(oCell);
	}

	if (this.parent) {
		this.parent.appendChild(container);
	}
	else {
		this.map.getContainer().appendChild(container);
	}

//	this.mapTypeChanged();

	return container;
}

MMapTypeControl.prototype.getDefaultPosition = function() {
	if (this.parent) {
		return null;
	}
	return this.position;
}








////////////////////////////


MMapTypeControl.prototype.createButton = function(that,n,label) {
	var oCell = document.createElement('td');
	oCell.id = 'MMTC_'+n;   
    // alert(n); de 0 à 1
	label = label.replace(/ /g,'&nbsp;');
	oCell.innerHTML = label;
	this.setStyle(oCell);
	GEvent.addDomListener(oCell, "click", function() {that.setMapType(n)});
	return oCell;
}

MMapTypeControl.prototype.setMapType = function(n) {
	this.map.setMapType(this.map.getMapTypes()[n]);
}

MMapTypeControl.prototype.mapTypeChanged = function() {
	var mapTypes = this.map.getMapTypes();
	var currentMapType = this.map.getCurrentMapType();

	for (var n = 0; n < mapTypes.length ; n++ ) {
		var oCell = document.getElementById('MMTC_'+n); //Cell
		if (oCell) {
			this.setStyle(oCell);
			if (currentMapType == mapTypes[n]) {
				this.setStyleSel(oCell);
			}
		}
	}
}



MMapTypeControl.prototype.mapAddType = function(type) {
	var mapTypes = this.map.getMapTypes();
	var ix = mapTypes.length-1;
	var label = mapTypes[ix].getName();

	if (this.direction == 'V') {
		var oTable = document.getElementById('MMTT_TABLE');
		var oRow = document.createElement('tr');
		oTable.appendChild(oRow);
		oRow.id = 'MMTR_'+ix;
	}
	else {
		var oRow = document.getElementById('MMTR_0');
	}

	var oCell = this.createButton(this,ix,label);
	oRow.appendChild(oCell);

};

MMapTypeControl.prototype.mapRemoveType = function(type) {
}



////////// Styles /////////////////////



MMapTypeControl.prototype.setCommon = function(obj) {
  obj.style.textDecoration = 'none';
  // obj.style.border = '1px solid #636363';
  obj.style.padding = '0 0 3px 0';
  // obj.style.margin = '0 8px 0 0';
  obj.style.textAlign = 'center';
  obj.style.width = '69px';
  obj.style.height = '26px';
  obj.style.cursor = 'pointer';
}

MMapTypeControl.prototype.setStyle = function(obj) {
	this.setCommon(obj);
	obj.style.background = "url('http://www.pierrefrey.com/images/contacts/gmaps/cartouche_sat_normal.png') center center";
	obj.style.color = '#636363';
	obj.style.backgroundColor = "transparent";
	obj.style.font = 'normal 10px Verdana';
}
MMapTypeControl.prototype.setStyleSel = function(obj) {
	this.setCommon(obj);
	obj.style.color = "#FFF";
	obj.style.background = "url('http://www.pierrefrey.com/images/contacts/gmaps/cartouche_sat_over.png') center center";
	obj.style.backgroundColor = "transparent";
	obj.style.font = 'normal 10px Verdana';
}
MMapTypeControl.prototype.setStyle2 = function(obj) {
	this.setCommon(obj);
	obj.style.color = '#EEEEEE';
	obj.style.backgroundColor = '#EEEEEE';
	obj.style.font = 'bold 10px Verdana';
}



MMapTypeControl.prototype.show = function () {
	this.container.style.display = '';
}

MMapTypeControl.prototype.hide = function () {
	this.container.style.display = 'none';
}

MMapTypeControl.prototype.toggle = function () {
	this.container.style.display = this.container.style.display == '' ? 'none' : '';
}


//////////// END MMapTypeControl /////////////////

