/*
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.
------------------------------------------------------------
*/
//////////// 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 : '#000000';
	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 = mapTypes.length-1 ; n >= 0 ; n-- ) {
			
		if(mapTypes[n].getName()=='Map'){
			// change 'map' name to 'Google Map'
			mapTypes[n]= new GMapType(mapTypes[n].getTileLayers(), mapTypes[n].getProjection(), 'Google Map');
		}
		//restrict the maptypes range of view, 
		//override the getMinimumResolution() and getMaximumResolution() methods
		mapTypes[n].getMinimumResolution = function() {return 6;}
		mapTypes[n].getMaximumResolution = function() {return 12;}

		//create a button to toggle the maptype and add it to the control
		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);
	}
	
	//add the county lines checkbox
		//create a checkbox to toggle the maptype and add it to the control
		var label = 'Counties';
		if (this.direction == 'V' || n == 0) {
			var oRow = document.createElement('tr');
			oTbody.appendChild(oRow);
			oRow.id = 'MMTR_counties';
		}
		var oCell = this.createCheckbox(this.map,label);
		oCell.innerHTML+=" ";  // This line for f*****g IE, else it willnot render the control
		oRow.appendChild(oCell);
		
	
	//add buttons to the container map
	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;
	label = label.replace(/ /g,'&nbsp;');
	oCell.innerHTML = label;
	this.setStyle(oCell);
	GEvent.addDomListener(oCell, "click", function() {that.setMapType(n)});
	return oCell;
}

MMapTypeControl.prototype.createButton2 = function(map,label) {
	var oCell = document.createElement('td');
	oCell.id = 'MMTC_counties';
	label = label.replace(/ /g,'&nbsp;');
	oCell.innerHTML = label;
	this.setStyle(oCell);
	GEvent.addDomListener(oCell, "click", function() {map.toggleCounties()});
	return oCell;
}

MMapTypeControl.prototype.createCheckbox = function(map,label) {
	var oCell = document.createElement('td');
	oCell.id = 'MMTC_counties';
	label = label.replace(/ /g,'&nbsp;');
	oCell.style.font = 'bold 10px Verdana';
  	oCell.style.textAlign = 'right';
  	oCell.style.color = this.background;
  	oCell.style.background = '#f3f3ef';
  	oCell.style.opacity = 0.8;
	oCell.style.filter = 'alpha(opacity=80)';

	oCell.innerHTML = label+'&nbsp;<input type="checkbox" onclick="map.toggleCounties()" />';
	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);
			}
		}
	}

	//show or hide the percipitation legend
	if(currentMapType.getName()=='Precipitation') { 
		this.map.legend.show(); 
		document.getElementById('pl').setAttribute('class', 'ps'); //show the legend while printing
	}
	else{ 
		this.map.legend.hide(); 
		this.map.legendName = 'none';
		document.getElementById('pl').setAttribute('class', 'ph'); //hid the legend while printing
	}
}

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 = '0';
  obj.style.padding = '1px';
  obj.style.textAlign = 'center';
  //if you are trying to change the button width, you will need to change the image
  obj.style.width = '75px';
  obj.style.height = '22px';
  obj.style.cursor = 'pointer';
}

MMapTypeControl.prototype.setStyle = function(obj) {
	this.setCommon(obj);
	obj.style.color = this.background;
	//obj.style.backgroundColor = this.background;
	obj.style.background = 'transparent url(images/button75.png) no-repeat center';
	obj.style.font = 'normal 10px Verdana';
}
MMapTypeControl.prototype.setStyleSel = function(obj) {
	this.setCommon(obj);
	obj.style.color = this.background;
	//obj.style.backgroundColor = this.foreground;
	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 /////////////////

