document.write("<script type='text/javascript' src='http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAA7TPFX5-Ilniw0UQCRZLfhBRJ2RAzHZrqd9CTJtLBdYUsLknadBQxvmJkt9TNxFo0BaIzK2MwiCFGkw&amp;hl=de'></scr" + "ipt>");


var gmLoaded = false;

function createMap(mapConfig, callback)
{
	if(GBrowserIsCompatible()) 
	{	
		// ====== Create the Euclidean Projection for the flat map ======
		// == Constructor ==
		function EuclideanProjection(a)
		{
			this.pixelsPerLonDegree=[];
			this.pixelsPerLonRadian=[];
			this.pixelOrigo=[];
			this.tileBounds=[];
			var b=256;
			var c=1;
			for(var d=0;d < a;d++)
			{
				var e=b/2;
				this.pixelsPerLonDegree.push(b/360);
				this.pixelsPerLonRadian.push(b/(2*Math.PI));
				this.pixelOrigo.push(new GPoint(e,e));
				this.tileBounds.push(c);
				b*=2;
				c*=2
			}
		}

		// == Attach it to the GProjection() class ==
		EuclideanProjection.prototype = new GProjection();

		// == A method for converting latitudes and longitudes to pixel coordinates == 
		EuclideanProjection.prototype.fromLatLngToPixel = function(a,b)
		{
			var c=Math.round(this.pixelOrigo[b].x+a.lng()*this.pixelsPerLonDegree[b]);
			var d=Math.round(this.pixelOrigo[b].y+(-2*a.lat())*this.pixelsPerLonDegree[b]);
			return new GPoint(c,d)
		};

		// == a method for converting pixel coordinates to latitudes and longitudes ==
		EuclideanProjection.prototype.fromPixelToLatLng = function(a,b,c)
		{
			var d=(a.x-this.pixelOrigo[b].x)/this.pixelsPerLonDegree[b];
			var e=-0.5*(a.y-this.pixelOrigo[b].y)/this.pixelsPerLonDegree[b];
			return new GLatLng(e,d,c)
		};

		// == a method that checks if the y value is in range, and wraps the x value ==
		EuclideanProjection.prototype.tileCheckRange = function(a,b,c)
		{
			var d=this.tileBounds[b];
			if (a.y<0||a.y>=d)
			{
				return false;
			}
			if(a.x<0||a.x>=d)
			{
				a.x=a.x%d;
				if(a.x<0)
				{a.x+=d;}
			}
			return true
		}

		// == a method that returns the width of the tilespace ==      
		EuclideanProjection.prototype.getWrapWidth = function(zoom)
		{
			return this.tileBounds[zoom]*256;
		}

		// ===== Create a Map Type that uses the Euclidean projection =====

		var tilelayers = 	
		[
			new GTileLayer
			(
				new GCopyrightCollection(""),
				mapConfig.mapMinZoom,
				mapConfig.mapMaxZoom
			)
		];
		
		tilelayers[0].getCopyright = 	function(a,b) 
		{
			return 	{
						prefix:"&copy;", 
						copyrightTexts:[mapConfig.copyright]
					};
		}
	
		var prZoom = mapConfig.mapMaxZoom;
		if(prZoom < 18)
		{
			prZoom = 18;
		}
		
		var euclidean = new EuclideanProjection(prZoom);
		
		tilelayers[0].getTileUrl = function(tile,zoom) 
		{
			if ((zoom < mapConfig.mapMinZoom) || (zoom > mapConfig.mapMaxZoom))
			{
				return "http://www.rro.ch/2009/cms/themes/map/blanktile." + mapConfig.imgFormat;
			} 

			var ymax = 1 << zoom;
			var y = ymax - tile.y -1;

			var MaxX = Math.ceil(mapConfig.tilesMaxX/Math.pow(2, mapConfig.mapMaxZoom - zoom));
			var MaxY = Math.ceil(mapConfig.tilesMaxY/Math.pow(2, mapConfig.mapMaxZoom - zoom));

			if (tile.x < MaxX && y < MaxY && tile.x >= 0 && y >= 0) 
			{
				return mapConfig.tileUrl + zoom + "/" + tile.x + "/" + y + "." + mapConfig.imgFormat;
			}
			else 
			{
				return "http://www.rro.ch/2009/cms/themes/map/blanktile." + mapConfig.imgFormat;
			}
		}
		
		var custommap = new GMapType(tilelayers, euclidean, mapConfig.mapTypeName, {errorMessage:"Tile not found!"});

		var map = new GMap2(document.getElementById(mapConfig.containerID), {backgroundColor: '#fff'});
		map.addMapType(custommap);
		map.addControl(new GLargeMapControl3D());
		map.enableScrollWheelZoom();
		var iZ = mapConfig.initZoom;
		if(iZ < mapConfig.mapMinZoom)
		{
			iZ = mapConfig.mapMinZoom;
		}
		else if(iZ > mapConfig.mapMaxZoom)
		{
			iZ = mapConfig.mapMaxZoom;
		}
		map.setCenter(new GLatLng(mapConfig.initLat, mapConfig.initLng), iZ, custommap);
		
		callback(map);
	}
}

function showMap(mapConfig, callback)
{
	if(!gmLoaded)
	{
		gmLoaded = true;
		document.write("<script type='text/javascript' src='http://www.rro.ch/cms/ee/index.php/global/labeledmarker'></scr" + "ipt>");//
		document.write("<script type='text/javascript' src='http://www.rro.ch/cms/ee/index.php/global/elabel'></scr" + "ipt>");
	}
	
	jQuery(document).ready(function() {
		createMap(mapConfig, callback);
	});
	
	jQuery(window).bind('beforeunload', function() {GUnload();});
	
	/*
	document.observe ("dom:loaded", function() 
		{
			createMap(mapConfig, callback);
		}
	);
	
	Event.observe (window, "beforeunload", function() {GUnload();}
	);
	*/
}

jQuery(document).ready(function() {
	jQuery('#cf_rosswald').crossFader({
		height: 106,
		delay: 3000,
		overlay: 'http://www.rro.ch/cms/ee/themes//weather/overlay.png',
		href: 'javascript: showPancam(\'17\', \'Panoramakamera Rosswald\');'
	});
});

function createContent(map, latlng, content, contentOffsetX, contentOffsetY)
{
	var marker = new ELabel(latlng, content, "", new GSize(contentOffsetX, contentOffsetY));
        		map.addOverlay(marker);

	return marker;
}

function createClickableContent(map, latlng, content, info, contentOffsetX, contentOffsetY, infoOffsetX, infoOffsetY)
{
	var icon = new GIcon();
	icon.image = 'http://www.rro.ch/2009/cms/themes/map/blankpixel.png';
	icon.iconSize = new GSize(1, 1);
	icon.iconAnchor = new GPoint(0, 0);
	icon.infoWindowAnchor = new GPoint(infoOffsetX, infoOffsetY);
	
	opts = { 
	  "icon": icon,
	  "htmlStr":content,
	  "labelOffset": new GSize(contentOffsetX, contentOffsetY)
	};
	
	var marker = new ClickableLabel(latlng, opts);
	
	GEvent.addListener(marker, "click", function() {marker.openInfoWindowHtml(info);});
        
	map.addOverlay(marker);
	
	return marker;
}

function showLocation(map, latlng, label)
{
	if (label == "Vormittag" || label == "Nachmittag") {
		var content = 
			"<table cellpadding='0' cellspacing='0'>" +
				"<tr>" +
					"<td>&nbsp;&nbsp;</td>" +
					"<td class='gm_label' valign='middle'>" + label + "</td>"
				"</tr>" +
			"</table>";
	} else {
		var content = 
			"<table cellpadding='0' cellspacing='0'>" +
				"<tr>" +
					"<td valign='middle'><img src='http://www.rro.ch/walliswetter/img/point.png' /><td>" +
					"<td>&nbsp;&nbsp;</td>" +
					"<td class='gm_location' valign='middle'>" + label + "</td>"
				"</tr>" +
			"</table>";
	}
			
	var correctionLat = 0.81298828;
	var correctionLng = -1.49414063;
	var halfOfContentHeight = 9;
	var halfOfPointWidth = 6;
	
	latlng = new GLatLng(latlng.lat() + correctionLat, latlng.lng() + correctionLng);
	
	createContent(map, latlng, content, -halfOfPointWidth, halfOfContentHeight);
}

function showClickableLocation(map, latlng, label)
{
	var content = 
			"<table cellpadding='0' cellspacing='0'>" +
				"<tr>" +
					"<td valign='middle'><img src='http://www.rro.ch/walliswetter/img/point.png' /><td>" +
					"<td>&nbsp;&nbsp;</td>" +
					"<td class='gm_location' valign='middle'>" + label + "</td>"
				"</tr>" +
			"</table>";
			
	var info = 
			"<div style='width:260px;'>" +
				"<b><u>" + label + ":</u></b>" +
				"<div style='clear:both; margin-top:5px;'>" +
					"Hier stehen Detailinformationen zu " + label + " !" +
				"</div>" +
			"</div>";
	
	var correctionLat = 0.81298828;
	var correctionLng = -1.80175782;
	var correctionInfoWindowX = 8;
	var correctionInfoWindowY = -8;
	var halfOfContentHeight = 9;
	
	latlng = new GLatLng(latlng.lat() + correctionLat, latlng.lng() + correctionLng);
	
	createClickableContent(map, latlng, content, info, 0, -halfOfContentHeight, correctionInfoWindowX, correctionInfoWindowY);
}

function showForecast(map, latlng, min, max, windSpeed, windDirection, iconpath)
{
	var icons = iconpath.split(',');
	var content = 
		"<div style=\"background-color: transparent; background-image: url(http://www.rro.ch/2009/cms/themes/weather/transparency_80.png); background-repeat: repeat; width: 110px; height: 210px; color: #FFF; font-family: Arial; border: 1px dotted #CCC;\">"+
			"<div>"+
				"<div style=\"color: #000; width: 100px; text-align: center; font-size: 11px;\">Vormittag</div>"+
				"<div style=\"text-align: center;\"><img src=\"http://www.rro.ch/2009/cms/themes/weather/icons/"+icons[0]+".png\" style=\"width: 75px; height: 60px; border: none;\" alt=\"\" /></div>"+
				"<div style=\"margin-left: 7px;\">"+
					"<div style=\"float: left; background-color: #246BFB; height: 17px; min-width: 26px; text-align: center;\">"+min+"</div>"+
					"<div style=\"float: left; background-color: #FC5A05; height: 17px; min-width: 26px; text-align: center;\">"+max+"</div>"+
					"<div style=\"float: left; background-color: #23815D; height: 17px; min-width: 26px; text-align: center;\"><img src=\""+windDirection+"\" alt=\"\" /> "+windSpeed+" <img src=\"http://www.rro.ch/2009/cms/themes/weather/kmh.png\" alt=\"\" /></div>"+
				"</div>"+
			"</div>"+
			"<div style=\"padding-top: 10px; margin-top: 30px; border-top: 1px dotted #CCC;\">"+
				"<div style=\"color: #000; width: 110px; text-align: center; font-size: 11px;\">Nachmittag</div>"+
				"<div style=\"text-align: center; width: 110px;\"><img src=\"http://www.rro.ch/2009/cms/themes/weather/icons/"+icons[1]+".png\" style=\"width: 75px; height: 60px; border: none;\" alt=\"\" /></div>"+
				"<div style=\"margin-left: 7px;\">"+
					"<div style=\"float: left; background-color: #246BFB; height: 17px; min-width: 26px; text-align: center;\">"+min+"</div>"+
					"<div style=\"float: left; background-color: #FC5A05; height: 17px; min-width: 26px; text-align: center;\">"+max+"</div>"+
					"<div style=\"float: left; background-color: #23815D; height: 17px; min-width: 26px; text-align: center;\"><img src=\""+windDirection+"\" alt=\"\" /> "+windSpeed+" <img src=\"http://www.rro.ch/2009/cms/themes/weather/kmh.png\" alt=\"\" /></div>"+
				"</div>"+
			"</div>"+
		"</div>";
	
	createContent(map, latlng, content, -45, 35);
}
