var map;
var isPlaying = false;
var col;
var style;
var fontSize;

function readText(target) {
	var p = document.getElementById("soundPlayer");
	if(p != null) {
		p.readText(target);
		swapSpeaker();
	}
}

function swapSpeaker() {
	isPlaying = !isPlaying;
	setSpeakerIcon();
}

function initSpeaker(col, filePath) {

	this.col = col;
	var speakerSpan = document.getElementById("speaker");
	var nimg = document.createElement("img");
	nimg.width = 17;
	nimg.height = 17;
	nimg.alt = "Text vorlesen";
	nimg.id = "speakerIcon";
	var nhref = document.createElement("a");
	var target = filePath;
	nhref.onclick = function() {
		readText(target);
		this.blur();
		return false;
	}
	nhref.appendChild(nimg);
	// nhref.href="";
	speakerSpan.appendChild(nhref);
	setSpeakerIcon();
}

function setSpeakerIcon() {
	var speaker = document.getElementById("speakerIcon");
	speaker.src = getIconPath(isPlaying);
}

function getIconPath(isPlaying) {
	var path ="pics/sound_" + col + (isPlaying ? "_off" : "") + ".gif";
	return path;
}

function load() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		map.setCenter(new GLatLng(50.94449,6.955998), 14);		
		var zControl = new GSmallMapControl();
		map.addControl(zControl, new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7, 40)));
		map.addControl(new GMapTypeControl());
		map.setMapType(G_HYBRID_MAP);
		showAddressMarker();
	}
}

function loadEn() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		map.setCenter(new GLatLng(50.94449,6.955998), 14);
		var zControl = new GSmallMapControl();
		map.addControl(zControl, new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7, 40)));
		map.addControl(new GMapTypeControl());
		map.setMapType(G_HYBRID_MAP);
		showAddressMarkerEn();
	}
}

function showAddressMarker() {
	var marker = new GMarker(new GLatLng(50.94449,6.955998));
	map.addOverlay(marker);
    
	GEvent.addListener(map, "click", function(marker, point) {
		if (marker) {
		      var txt = "<div id=\"mapText\"><strong>Aclewe GmbH</strong><br />Marzellenstraße 43a<br />50668 Köln";
		      txt += "<br /><br /><form action=\"http://maps.google.com/maps\" method=\"get\" target=\"_blank\">Ihre Startadresse:<br />";
		      txt += "<input type=\"text\" SIZE=30 MAXLENGTH=40 name=\"saddr\" id=\"saddr\" value=\"\" /><br>";
		      txt += "<INPUT value=\"Route hierher berechnen\" TYPE=\"SUBMIT\">";
		      txt += "<input type=\"hidden\" name=\"daddr\" value=\"Marzellenstraße 43a, 50668 Köln\" /></form></div>";		      
		      marker.openInfoWindowHtml(txt);
		}
	}); 
}

function showAddressMarkerEn() {
      var marker = new GMarker(new GLatLng(50.9407, 6.9426));
      map.addOverlay(marker);
      
	GEvent.addListener(map, "click", function(marker, point) {
		if (marker) {
		      var txt = "<div id=\"mapText\"><strong>Aclewe GmbH</strong><br />Marzellenstraße 43a<br />50668 Cologne<br />Germany";
		      txt += "<br /><br /><form action=\"http://maps.google.com/maps\" method=\"get\" target=\"_blank\">Your starting location:<br />";
		      txt += "<input type=\"text\" SIZE=30 MAXLENGTH=40 name=\"saddr\" id=\"saddr\" value=\"\" /><br>";
		      txt += "<INPUT value=\"Get direction\" TYPE=\"SUBMIT\">";
		      txt += "<input type=\"hidden\" name=\"daddr\" value=\"Marzellenstraße 43a, 50668 Köln\" /></form></div>";		      
		      marker.openInfoWindowHtml(txt);
		}
	}); 
}

// styleswitcher

function setActiveStyleSheet(st) {
	style = st;
	var i, a, main;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
		if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
			a.disabled = true;
			if(a.getAttribute("title") == style) a.disabled = false;
		}
	}
	createCookie();
	try {
		var adder = "";
		if(style == "black") adder += "_black";
		swapPics(adder);
		
	} catch (e) {
	}
	
	try {
		var m = document.getElementById("missionStatement");
		if(m != null) m.setColor(st);				
	} catch (e)  {
	}
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

function createCookie() {
	var date = new Date();
	// var days = 365;
	date.setTime(date.getTime()+(60*60*1000));	// days*24*60*60*1000
	var expires = "; expires="+date.toGMTString();  
  	document.cookie = "style=" + style + expires+"; path=/"; 
	document.cookie = "fontSize=" + fontSize + expires+"; path=/"; 
	
//	alert("writing fs : " + fontSize + " / style : " + style);
}

function readCookie() {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  var styleDef = "style=";
  var fontSizeDef = "fontSize=";
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);	// strip whitespaces
	if (c.indexOf(styleDef) == 0) style = c.substring(styleDef.length,c.length);
	if (c.indexOf(fontSizeDef) == 0) fontSize = c.substring(fontSizeDef.length,c.length);
  }
//	alert("reading fs : " + fontSize + " / style : " + style);
  return null;
}


function initPage() {
	readCookie();
	if(!style) {
		// style = getPreferredStyleSheet();
		var st = Math.round(Math.random());
		style = (st ==0) ? "white" : "black";
	}
	if(!fontSize) fontSize = 0;
	setActiveStyleSheet(style);
	if(isNaN(fontSize)) fontSize = 0;
	var linkObj = document.getElementById("switch");
	linkObj.href = 'styles_fontSize_' + fontSize + '.css';
}

window.onunload = function(e) {
	style = getActiveStyleSheet();
	createCookie();
}


function reduce() {
	fontSize--;
	if(fontSize < 0) fontSize = 0;
	setFontSize();
}

function enlarge() {
	fontSize++;
	if(fontSize > 2) fontSize = 2;
	setFontSize();
}

function setFontSize() {
	createCookie();
	document.location.reload();
}