/*
MZoomControl
Control to set the zoomlevel, allowing for predefined values.

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 MZoomControl());
	map.addControl(new MZoomControl(options?));

MZoomControl options:
	Position: GControlPosition()
	direction: 'V' or 'H' - Default: 'H';
	background: HTML color - Default: '#8080FF';
	foreground: HTML color - Default: '#EEEEEE';
	parent: DOM element;
	validValues: Array of number - default: [];
*/

/////////////////////////////////////////////////////////////////////////////


//////////// BEGIN MZoomControl /////////////////
function MZoomControl(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_LEFT, new GSize(5, 5));
}

MZoomControl.prototype = new GControl();

MZoomControl.prototype.initialize = function(map) {
	var that = this;
	this.map = map;

// REMOVE - REMOVE - REMOVE - REMOVE - REMOVE - REMOVE - REMOVE - REMOVE - REMOVE - REMOVE
// Remove this block after saving the file to your server
//	if (window.location.host != 'maps.forum.nu') {
//		alert('Please do not hotlink this script.\n Save this file to your own server instead and remove this message.');
//		return;
//	}
// END Remove
// END REMOVE - END REMOVE - END REMOVE - END REMOVE - END REMOVE - END REMOVE - END REMOVE

	if (this.validValues.length == 0) {
		var mapTypes = this.map.getMapTypes();
		for (var n = 0; n < mapTypes.length ; n++ ) {
			this.maxRes = Math.max(mapTypes[n].getMaximumResolution(),this.maxRes);
		}
		for (var n = 0; n < this.maxRes+1 ; n++ ) {
			this.validValues.push(n);
		}
	}

	GEvent.addListener(this.map, "zoomend", function(o,n) {that.mapZoomEnd(o,n)});
	GEvent.addListener(this.map, "maptypechanged", function(o,n) {that.mapTypeChanged()});

	this.container = document.createElement('div');

	var oTable = document.createElement('table');
	this.container.appendChild(oTable);

	oTable.setAttribute('cellSpacing','1');
	oTable.setAttribute('cellPadding','0');


	var oTbody = document.createElement('tbody');
	oTable.appendChild(oTbody);
	
	var oRow = document.createElement('tr');
	oTbody.appendChild(oRow);

//------ Zoom In -------------------	
	var oCell = document.createElement('td');
	oCell.id = 'MZC_in';
	oCell.innerHTML = '<img src="images/zoomin.jpeg">';
	this.setStyle2(oCell);
	GEvent.addDomListener(oCell, "click", function() {that.discreteZoomIn()});
	oRow.appendChild(oCell);
//-------------------------	

	for (var  n = 0 ; n < this.validValues.length ; n++ ) {
		var zoomValue = this.validValues[n];
		if (this.direction == 'V') {
			var zoomValue = this.validValues[this.validValues.length-1-n];
			var oRow = document.createElement('tr');
			oTbody.appendChild(oRow);
			oRow.id = 'MZR_'+zoomValue;

		}
		var oCell = this.createZoombutton(that,zoomValue);
		oRow.appendChild(oCell);
	}

//------ Zoom Out -------------------	
	var oCell = document.createElement('td');
	oCell.id = 'MZC_out';
	oCell.innerHTML = '<img src="images/zoomout.jpeg">';
	this.setStyle2(oCell);
	GEvent.addDomListener(oCell, "click", function() {that.discreteZoomOut()});
	if (this.direction == 'V') {
		var oRow = document.createElement('tr');
		oTbody.appendChild(oRow);
	}
	oRow.appendChild(oCell);
//-------------------------	


	if (this.parent) {
		this.parent.appendChild(this.container);
	}
	else {
		this.map.getContainer().appendChild(this.container);
	}
	that.mapZoomEnd(null,this.map.getZoom());

//this.container.innerHTML += '&nbsp;';
	return this.container;
}

MZoomControl.prototype.getDefaultPosition = function() {
	return this.position;
}


////////////////////////////

MZoomControl.prototype.discreteZoomIn = function() {
	var currentZoom = this.map.getZoom();
	for (var n = 0 ; n < this.validValues.length ; n++ ) {
		if (this.validValues[n] > currentZoom) {
			this.map.setCenter(this.map.getCenter(),this.validValues[n]);
			return;
		}
	}
	this.map.setCenter(this.map.getCenter(),this.validValues[this.validValues.length-1]);
}

MZoomControl.prototype.discreteZoomOut = function() {
	var currentZoom = this.map.getZoom();
	for (var n = this.validValues.length-1 ; n > -1 ; n-- ) {
		if (this.validValues[n] < currentZoom) {
			this.map.setCenter(this.map.getCenter(),this.validValues[n]);
			return;
		}
	}
	this.map.setCenter(this.map.getCenter(),this.validValues[0]);
}

MZoomControl.prototype.createZoombutton = function(that,n) {
	var oCell = document.createElement('td');
	oCell.id = 'MZC_'+n;
	oCell.innerHTML = n;
	this.setStyle(oCell);
	GEvent.addDomListener(oCell, "click", function() {that.setZoom(n)});
	return oCell;
}

MZoomControl.prototype.setZoom = function(z) {
	this.map.setCenter(this.map.getCenter(),z)
}

MZoomControl.prototype.mapTypeChanged = function() {
	for (var n = 0; n < this.maxRes+1 ; n++ ) {

		var oCell = document.getElementById('MZC_'+n); //Cell
		if (oCell) {
			oCell.style.display = '';
		}

		var oRow = document.getElementById('MZR_'+n); //Row
		if (oRow) {
			oRow.style.display = '';
		}

		if (n > this.map.getCurrentMapType().getMaximumResolution()) {
			oCell.style.display = 'none';
			if (this.direction == 'V') {
				oRow.style.display = 'none';
			}
		}
	}
}

MZoomControl.prototype.mapZoomEnd = function(o,n) {
	n = Math.floor(n);

	var z = 0;
	var oldCell = document.getElementById('MZC_'+z);
	while (oldCell) {
		this.setStyle(oldCell);
		z++;
		oldCell = document.getElementById('MZC_'+z);
	}
	
	
	var newCell = document.getElementById('MZC_'+n);
	if (oldCell) {
		this.setStyle(oldCell);		
	}
	if (newCell) {
		this.setStyleSel(newCell);		
	}
	else {
		if (n > o) {
			this.discreteZoomIn();
		}
		else {
			this.discreteZoomOut();
		}
	}
};



MZoomControl.prototype.setCommon = function(obj) {
  obj.style.textDecoration = 'none';
  obj.style.border = '1px solid black';
  obj.style.padding = '1px';
  obj.style.textAlign = 'center';
  obj.style.width = '20px';
  obj.style.cursor = 'pointer';
}

MZoomControl.prototype.setStyle = function(obj) {
	this.setCommon(obj);
	obj.style.color = this.foreground;
	obj.style.backgroundColor = this.background;
	obj.style.font = 'normal 10px Verdana';
}
MZoomControl.prototype.setStyleSel = function(obj) {
	this.setCommon(obj);
	obj.style.color = this.background;
	obj.style.backgroundColor = this.foreground;
	obj.style.font = 'bold 10px Verdana';
}
MZoomControl.prototype.setStyle2 = function(obj) {
	this.setCommon(obj);
	obj.style.color = '#EEEEEE';
	obj.style.backgroundColor = '#DDDDDD';
	obj.style.font = 'bold 10px Verdana';
}



MZoomControl.prototype.show = function () {
	this.container.style.display = '';
}

MZoomControl.prototype.hide = function () {
	this.container.style.display = 'none';
}

MZoomControl.prototype.toggle = function () {
	this.container.style.display = this.container.style.display == '' ? 'none' : '';
}


//////////// END MZoomControl /////////////////

