var TGMap = {};

TGMap.map;
TGMap.geo;
TGMap.delay = 100;
TGMap.nextAddress = 0;
TGMap.playerData = [];
TGMap.geoData = [];
TGMap.markers = {};

TGMap.initMap = function()
{
	if(GBrowserIsCompatible())
	{
		TGMap.map = new GMap2(document.getElementById('googleMap'));
		TGMap.map.setMapType(G_PHYSICAL_MAP);
		TGMap.map.addControl(new GSmallMapControl());
		TGMap.map.setCenter(new GLatLng(47.268080,9.883970), 9);
		TGMap.map.savePosition();
		TGMap.geo = new GClientGeocoder();
	}
	return true;
}

TGMap.createMarker = function(point, name, icon, html)
{
	if(icon)
	{
		var icon = new GIcon();
		icon.iconSize = new GSize(15, 19);
		icon.iconAnchor = new GPoint(7, 8);
		icon.infoWindowAnchor = new GPoint(7, 8);
		
		var randomIcons = new Array('schelleMarker.png', 'eichelMarker.png', 'herzMarker.png', 'laubMarker.png', 'laubMarker.png');
		icon.image = 'media/jassen/' + randomIcons[Math.floor(Math.random() * 4)];
	}
	  //HW Begin Neu
		//html = "<div id=\"gmapmarker\">	<div class=\"InfoLeft\">&nbsp;</div>	<div class=\"InfoMiddle\"><div class=\"InfoContent\">" + html + "</div></div><div class=\"InfoRight\">&nbsp;</div></div>";	
		html = "<div id=\"gmapmarker\">	<div class=\"InfoTop\">&nbsp;</div>	<div class=\"InfoCenter\"><div class=\"InfoContent\">" + html + "</div></div><div class=\"InfoBottom\">&nbsp;</div></div>";
		TGMap.markers[name] = new GMarker(point, icon);
		marker = setTwMarkers(TGMap.map,point,name,html);   
		TGMap.markers[name] = marker;  
		
		GEvent.addListener(TGMap.markers[name], 'click', function() {
			$('#playerProfile').hide();
			//alert('Hoehe: ' + $('#GInfoWindow').height());
			//window.setTimeout($('#gmapmarker').css("top","-" + $('#GInfoWindow').height() + "px"),1000);
			//$('#gmapmarker').css("top","-" + $('#GInfoWindow').height() + "px");
			//TGMap.markers[name].openInfoWindowHtml(html);			
			var pageTracker = _gat._getTracker("UA-3628762-1");
			pageTracker._initData();
			pageTracker._trackPageview();
		});
		GEvent.addListener(TGMap.markers[name], 'infowindowclose', function() {
			alert('infowindowclose');
			TGMap.map.returnToSavedPosition();
		});
	    //markers.push(marker);  
	    return marker;  
	  //HW Ende
		/*	 Alte Version     
			TGMap.markers[name] = new GMarker(point, icon);
		
			if(html)
			html = "<div id=\"gmapmarker\">	<div class=\"InfoBoxLeft\">&nbsp;</div>	<div class=\"InfoBoxMiddle\"><div class=\"InfoBoxContent\">	<div class=\"Text\">  <div class=\"Ort\">Bregenz</div>  <div class=\"Title\"><a href=\"http://www.laendlefirma.at/bahnhof-apotheke.html\">Bahnhof-Apotheke</a></div>  </div></div></div><div class=\"InfoBoxRight\">&nbsp;</div></div>";	
			{
				GEvent.addListener(TGMap.markers[name], 'click', function() {
					$('#playerProfile').hide();
					TGMap.markers[name].openInfoWindowHtml(html);
					
					var pageTracker = _gat._getTracker("UA-3628762-1");
					pageTracker._initData();
					pageTracker._trackPageview();
				});
				GEvent.addListener(TGMap.markers[name], 'infowindowclose', function() {
					TGMap.map.returnToSavedPosition();
				});
			}
			return TGMap.markers[name];
			
		*/
}

TGMap.getHTML = function(obj)
{

		if(obj.length > 2)
		{
			var html = '<div id="GInfoWindow">';
			html += '<a href="javascript:void(null);" onclick="document.getElementById(\'gmapmarker\').style.display = \'none\'; TGMap.map.returnToSavedPosition();" class="xclose" style="padding-right:8px;"  title="Schliessen">&nbsp;</a>'; 
			html += '<h4>Spieler aus ' + obj[0]['city'] + '</h4>';
			html += '<ul>';
			for(var i = 1; i < obj.length; ++i)
			{
				html += '<li><a href="javascript:void(null);" onclick="TGMap.city2User(' + obj[i].userID + ', \'' + obj[i].city + '\');">' + obj[i].userName + ' (' + obj[i].points + ') ';
				html += '</a></li>';
			}
			html += '</ul>';
			html += '</div>';
		} else {
			var html = '<div id="GInfoWindow">';
			html += '<a href="javascript:void(null);" onclick="document.getElementById(\'gmapmarker\').style.display = \'none\'; TGMap.map.returnToSavedPosition();" class="xclose" style="padding-right:8px;" title="Schliessen">&nbsp;</a>'; 
			html += '<h4>' + obj[1].userName + ' aus ' + obj[0].city.substr(0, 1).toUpperCase() + obj[0].city.substr(1, obj[0].city.length) + '</h4>';
			html += '<img src="' + obj[1].avatar + '" style="display:block; float:left; margin:5px 5px 0 15px;" />';
			html += '<table cellpadding="2" cellspacing="0"><tr><td>Punkte:</td><td>' + obj[1].points + '</td>';
			html += '</tr><tr><td>Spiele:</td><td>' + obj[1].games + '</td></tr><tr><td>Gewonnen:</td><td>';
			html += obj[1].winRate + '%</td></tr><tr><td>Abgebrochen:</td><td>' + obj[1].abortedRate + '%</td></tr>';
			if (obj[1].slogan != '')
			{
				html += '<tr><td valign="top" colspan="2">Motto:<br>' + obj[1].slogan + '</td></tr>';
			}
			html += '</tr></table></div>';
		}



	return html;
}

TGMap.city2User = function(userID, city)
{
	$.ajax({
		type:		'GET',
		url:		'?classname=TPlayer&method=city2User&userID=' + userID,
		dataType:	'html',
		global:		false,
		success:	function(response)
		{
			var b2list = '<a href="javascript:void(null);" class="back2list" onclick="TGMap.user2City(\'' + city + '\');"><< zur&uuml;ck zur Liste</a>';
			$('#GInfoWindow').html(response + b2list);
			$('#GInfoWindow').css("padding","0px 10px 10px 15px");	
			$('#GInfoWindow h4').css("margin","0px 0px 10px 0px");	

		}	
	});
	return true;
}

TGMap.user2City = function(city)
{
	//GEvent.trigger(markers[city], 'click');
	GEvent.trigger(TGMap.markers[city], 'click');
}

TGMap.nextAdd = function()
{
	if(TGMap.nextAddress < TGMap.geoData.length)
	{
		setTimeout('TGMap.getLatLngFromAddress("' + TGMap.geoData[TGMap.nextAddress][0].city + '", TGMap.nextAdd, ' + TGMap.nextAddress + ')', TGMap.delay);
		TGMap.nextAddress++;
	} else
	{
		$('#loading').hide();
	}
}

TGMap.getLatLngFromAddress = function(address, next, playerDataID)
{
	TGMap.geo.getLocations(address, function(result){
		if(result.Status.code == G_GEO_SUCCESS)
		{
			var point = result.Placemark[0].Point.coordinates;
			point = new GLatLng(point[1], point[0]);
			var html = TGMap.getHTML(TGMap.geoData[playerDataID]);
			marker = TGMap.createMarker(point, address, true, html);
			TGMap.map.addOverlay(marker);
		} else
		{
			if(result.Status.code == G_GEO_TOO_MANY_QUERIES)
			{
				TGMap.nextAddress--;
				TGMap.delay++;
			} else
			{
				/*
				 * debug
				 **/
				//GLog.write(result.Status.code);
				 /**/
			}
		}
		TGMap.nextAdd();
	});
}

TGMap.drawPlayerData = function()
{
	//alert (TGMap.playerData.length);
	for(var i = 0; i < TGMap.playerData.length; ++i)
	{	
		if (TGMap.playerData[i]) //HW IE Fix
		{
			html = TGMap.getHTML(TGMap.playerData[i]);
			if(TGMap.playerData[i][0].Lat != '')
			{
				marker = TGMap.createMarker(new GLatLng(TGMap.playerData[i][0].Lat, TGMap.playerData[i][0].Lng), TGMap.playerData[i][0].city, true, html);
				TGMap.map.addOverlay(marker);
			} else {
				TGMap.geoData.push(TGMap.playerData[i]);					
			}
		}
	}
	
	// to disable global geo reverse engineering comment the if / else part
	
	//if(TGMap.geoData.length > 0)
  //		TGMap.nextAdd();
	//else
		$('#loading').hide();
}


$(document).ready(function() {
	TGMap.initMap();
	$.ajax({
		type:		'GET',
		url:		'?classname=TPlayer&method=getGMDataPlayerOnline',
		dataType:	'JSON',
		global:		false,
		success:	function(response)
		{
			TGMap.playerData = eval(response);
			// check if map is already loaded, otherwise retry a second later
			if(TGMap.map)
				TGMap.drawPlayerData();
			else
				setTimeout("TGMap.drawPlayerData();", 1000);
		}	
	});
	return true;
});


