/*
* ExtMapTypeControl Class 
*  Copyright (c) 2007, Google 
*  Author: Pamela Fox, others
* 
* 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.
*
* This class lets you add a control to the map which mimics GMapTypeControl
*  and allows for the addition of a traffic button/traffic key.
*/

/*
* Modified by Dominik Piekarski Nov 2008
*/

/**
*	Erstellt einen Legenden-Button
*
*
*
*/
var MINZOOM = 5; //min out zoom 
var MAXZOOM = 15; //max in zoom
var DEFZOOM = (typeof param_defzoom != "number")?6:param_defzoom;

function mapMenue() {
  
}


mapMenue.prototype = new GControl();

/**
 * Is called by GMap2's addOverlay method. Creates the button 
 *  and appends to the map div.
 * @param {GMap2} map The map that has had this mapMenue added to it.
 * @return {DOM Object} Div that holds the control
 */ 
mapMenue.prototype.initialize = function(map) {
  var container = document.createElement("div");
  var me = this;
  
  var legendeDiv = me.createButton_("Legende");
  var toernDiv = me.createButton_("Toernplaner");
  var listeDiv = me.createButton_("Kartenliste");
  var sucheDiv = me.createButton_("Suche");
//  var dealerDiv = me.createButton_("Dealerlocator");
  var hybridDiv = me.createButton_("Hybrid");
  var gelaendeDiv = me.createButton_("Gelaende");
  
  
  me.assignButtonEvent_(legendeDiv, map, 0, [toernDiv, listeDiv, sucheDiv]);
  me.assignButtonEvent_(listeDiv, map, 0, [legendeDiv, toernDiv, sucheDiv]);
  me.assignButtonEvent_(toernDiv, map, 0, [legendeDiv, listeDiv, sucheDiv]);
  me.assignButtonEvent_(sucheDiv, map, 0, [legendeDiv, listeDiv, toernDiv]);
  //me.assignButtonEvent_(dealerDiv, map, 0, [legendeDiv, listeDiv, toernDiv, sucheDiv]);
  me.assignButtonEvent_(hybridDiv, map, "hyb", [gelaendeDiv]);
  me.assignButtonEvent_(gelaendeDiv, map, "phys", [hybridDiv]);
//  GEvent.addListener(map, "maptypechanged", function() {
//    if (map.menueSelected == "LEGENDE") {
//      GEvent.trigger(legendeDiv, "click"); 
//    } else if (map.menueSelected == "LISTE") {
//      GEvent.trigger(listeDiv, "click");
//    } else if (map.menueSelected == "TOERNPLANER") {
//      GEvent.trigger(toernDiv, "click");
//    }else if (map.menueSelected == "SUCHE") {
//      GEvent.trigger(sucheDiv, "click");
//    }else if (map.menueSelected == "DEALER") {
//      GEvent.trigger(dealerDiv, "click");
//    }else if (map.menueSelected == "GELAENDE") {
//      GEvent.trigger(gelaendeDiv, "click");
//    }else if (map.menueSelected == "HYBRID") {
//      GEvent.trigger(hybridDiv, "click");
//    }
//  });
  
  GEvent.trigger(gelaendeDiv, "click");
  GEvent.trigger(legendeDiv, "click");
  
  //dealerDiv.style.marginRight= "10px";
  sucheDiv.style.marginRight= "15px";
  container.appendChild(legendeDiv);
  container.appendChild(toernDiv);
  container.appendChild(listeDiv);
  container.appendChild(sucheDiv);
  //container.appendChild(dealerDiv);
  container.appendChild(gelaendeDiv);
  container.appendChild(hybridDiv);
  
  
  
  map.getContainer().appendChild(container);
  
  return container;
}

/*
 * Creates simple buttons with text nodes. 
 * @param {String} text Text to display in button
 * @return {DOM Object} The div for the button.
 */
mapMenue.prototype.createButton_ = function(text) {
  
	var buttonDiv = document.createElement("div");
	var textDiv = document.createElement("div");
  
	if(text == "Legende"){
		buttonDiv.onclick = function(){closeMenuesBut("Legende");Legende.showHide();};
	}else if(text == "Toernplaner"){
		buttonDiv.onclick = function(){closeMenuesBut("Toernplaner");Toernplaner.showHide();};
	}else if(text == "Suche"){
		buttonDiv.onclick = function(){closeMenuesBut("Suche");Suche.showHide();};
	}else if(text == "Kartenliste"){
		buttonDiv.onclick = function(){closeMenuesBut("Kartenliste");Kartenliste.showHide();};
//	}else if(text == "Dealerlocator"){
//		buttonDiv.onclick = function(){closeMenuesBut("Dealerlocator");Dealerlocator.showHide();};
	}
	
	buttonDiv.type = text;
	this.setButtonStyle_(buttonDiv, text);
	buttonDiv.style.cssFloat = "left";
	buttonDiv.style.styleFloat = "left";
	
	textDiv.appendChild(document.createTextNode(""));
	textDiv.style.width = "6em";
	buttonDiv.appendChild(textDiv);
	
	return buttonDiv;
}


/*
 * Assigns events to MapType buttons to change maptype
 *  and toggle button styles correctly for all buttons
 *  when button is clicked.
 *  @param {DOM Object} div Button's div to assign click to
 *  @param {GMap2} Map object to change maptype of.
 *  @param {Object} mapType GMapType to change map to when clicked
 *  @param {Array} otherDivs Array of other button divs to toggle off
 */  
mapMenue.prototype.assignButtonEvent_ = function(div, map, type, otherDivs) {
  var me = this;
  
  GEvent.addDomListener(div, "click", function() {
    
    if(type == "phys"){
		map.setMapType(G_PHYSICAL_MAP);
		var m = map.getCurrentMapType();
		m.getMinimumResolution = function(){return MINZOOM;};
		m.getMaximumResolution = function(){return MAXZOOM;};
		me.toggleButton_(otherDivs[0], false);
		me.toggleButton_(div, true);
		return;
	}else if(type == "hyb"){
		map.setMapType(G_HYBRID_MAP);
		var m = map.getCurrentMapType();
		m.getMinimumResolution = function(){return MINZOOM;};
//		m.getMaximumResolution = function(){return MAXZOOM;};
		me.toggleButton_(otherDivs[0], false);
		me.toggleButton_(div, true);
		return;
	}else if(type == 0){
   		for (var i = 0; i < otherDivs.length; i++){
  	       me.toggleButton_(otherDivs[i], false);
   		}
   		me.toggleButton_(div, true);
   	}
	
  });
}

/*
 * Changes style of button to appear on/off depending on boolean passed in.
 * @param {DOM Object} div  Button div to change style of
 * @param {Boolean} boolCheck Used to decide to use on style or off style
 */
mapMenue.prototype.toggleButton_ = function(div, boolCheck) {
  
  var url = BASEURL+"img/layout/kartenmenue/"+div.type;
  if(boolCheck)
  	url += "_a";
  
  url += ".png";
  div.style.background = "url('"+url+"')";
}

/*
 * Required by GMaps API for controls. 
 * @return {GControlPosition} Default location for control
 */
mapMenue.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(70, 0));
}

/*
 * Sets the proper CSS for the given button element.
 * @param {DOM Object} button Button div to set style for
 */
mapMenue.prototype.setButtonStyle_ = function(button, label) {
  button.style.color = "#000000";
  button.style.backgroundColor = "white";
  button.style.background = "url('"+BASEURL+"img/layout/kartenmenue/"+label + ".png')";
  button.style.width = "86px";
  button.style.height = "21px";
  button.style.font = "small Arial";
  button.style.border = "0px solid black";
  button.style.padding = "0px";
  button.style.margin= "0px";
  button.style.textAlign = "center";
  button.style.fontSize = "12px"; 
  button.style.cursor = "pointer";
}


function closeMenuesBut(openedDivId){
	
	if(openedDivId != "Legende")
		Legende.hide();
	
	if(openedDivId != "Toernplaner")
		Toernplaner.hide();
		
	if(openedDivId != "Kartenliste")
		Kartenliste.hide();
	
	if(openedDivId != "Suche")
		Suche.hide();
	
//	if(openedDivId != "Dealerlocator")
//		Dealerlocator.hide();
	
}

