$(document).ready(function() {
	// Karte
	/*$.ajax({
		url: 'http://www.positionsreport.de/php/enterTrack.php?callSign=DL1JD',
		type: 'GET',
		success: function(res) {
			var matches = res.responseText.match(/loadPage\((.*?),'/im);
			coordinates = matches[1];
			mapit(coordinates);
			
			if($('#map_position').length){
				map_position(coordinates);
			}
		}
	});*/
	

	
	// Gallerie
	$('li.wGallery').each(function(i){
		var title = $(this).find('img').attr('title');
		var href = $(this).find('a').attr('href');
		$(this).wrap('<a href="'+href+'" />');
		$(this).append('<span>'+title+'</span>');
		if(i % 2 == 1){
			$(this).css('marginRight','0');
		}
	});
	
	// Ads
	//$('#slider li:first img').load(function(){
		$("#slider").easySlider({
			auto: true,
			continuous: true,
			pause: 4000,
			controlsShow: false,
		});
	//});
	
	$('.popup-link').click(function(){
		$(this).parent().find('.comments').toggle();
	});
});

/*
	Zoomt Google Map auf Deutschland und l?dt St?dte nach
*/
var map = null;
var geocoder;

function showGermany(names) {

	if (!GBrowserIsCompatible()) return false;
	map = new GMap2(document.getElementById("map"));
	geocoder = new GClientGeocoder();
	map.addControl(new GSmallMapControl());
	map.addControl(new GMapTypeControl());
	// Zoomen auf die Mitte von Deutschland
	GoogleAPI_Zoom('Germany',5);
//#	GoogleAPI_LoadXML('/page/search/choosecity.xml?q='+names);

	GEvent.addListener(map, "click", function(marker, point) {
		if (marker) {
			top.location.href=marker.linkurl;
		}
	});
}

function GoogleAPI_LoadXML(url) {

	GDownloadUrl(url, function(data, responseCode) {

		var xml = GXml.parse(data);
  	var markers = xml.documentElement.getElementsByTagName("marker");
		for (var i = 0; i < markers.length; i++) {
			var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
															parseFloat(markers[i].getAttribute("lng")));
			var marker = new GMarker(point);
			map.addOverlay(marker);
			marker.linkurl = 'city?q='+markers[i].getAttribute("linkq")+'&id='+markers[i].getAttribute("linkid");
		}

	});
}

/*
	Diese Funktion zoomt an die angegebene Adresse
	zoomlevel 1-13: um so gr??er um so n?her
*/
function GoogleAPI_Zoom(address,zoomlevel) {
	if (!GBrowserIsCompatible()) return false;


	// Zoomen auf die Mitte von Deutschland
	var geocoder = new GClientGeocoder();
	geocoder.getLatLng(
		address,
		function(point) {
			if (!point) {
				mapstyle = document.getElementById("map").style;
				mapstyle.display = 'none';
			} else {

				map.setCenter(point, zoomlevel);
			}
		}
	 );
}


function addCity(address,detaillink) {
	if (!GBrowserIsCompatible()) return false;
	geocoder.getLatLng(address,
		function (point) {
			if (point) {
				var marker = new GMarker(point);
				marker.linkurl = detaillink;
				map.addOverlay(marker);
			}
		}
	);
}

function remove_stuff(){
	// Karte
	$($('#map').children()[2]).css('display', 'none');
	$($('#map').children()[1]).css('display', 'none');
}

function mapit(a, b) {
	if (!GBrowserIsCompatible()) return false;
	if (map == null) {
		map = new GMap2(document.getElementById("map"));
		map.setUIToDefault();
		map.setMapType(G_SATELLITE_MAP);
		
		//console.log(coordinates);

		/*coordinates[1] = coordinates[1]+'.'+coordinates[2];
		coordinates[1] = coordinates[1]+0;
		
		coordinates[4] = coordinates[4]+'.'+coordinates[5];
		coordinates[4] = coordinates[4]+0;
		
		var c = new Array();
		c[0] = coordinates[0]+coordinates[1]/60;
		c[1] = coordinates[3]+coordinates[4]/60;
		
		console.log(c[0]);		
		console.log(c[1]);*/				
		
		//console.log(a);
		//console.log(b);
		
		map.setCenter(new google.maps.LatLng(a, b), 10);
		var marker = new GMarker(new google.maps.LatLng(a, b));
		
		map.addOverlay(marker);
		map.getContainer().style.overflow="hidden"; 
		map.setZoom(2);
		
		window.setTimeout("remove_stuff()", 5000);
	}
	

	
	/*GEvent.addListener(map, "click", function() {
		window.location.href = base_url+'/?page_id=194';
	});*/
	
	
	/*var geocoder = new GClientGeocoder();
	geocoder.getLatLng(
		address,
		function(point) {

			if (!point) {
				//mapstyle = document.getElementById("route").style;
				//mapstyle.display = 'none';
			} else {

				map.setCenter(point, 13);
				var marker = new GMarker(point);
				map.addOverlay(marker);
				marker.openInfoWindowHtml("<b>"+dealer+"</b><br>"+address);
			}
		}
	 );*/
 }

function showMap(address) {
	if (!GBrowserIsCompatible()) return false;
	map = new GMap2(document.getElementById("map"));
	map.addControl(new GSmallMapControl());
	map.addControl(new GMapTypeControl());
	geocoder = new GClientGeocoder();
	geocoder.getLatLng(address,showAddress);
}

function showAddress(point) {
	if (!GBrowserIsCompatible()) return false;
	map.setCenter(point, 10);
}




function addStation(address,stationname,detaillink) {
	if (!GBrowserIsCompatible()) return false;
	geocoder.getLatLng(address,
		function (point) {
			if (point) {
				var marker = new GMarker(point);
			  GEvent.addListener(marker, "click", function() {
			    marker.openInfoWindowHtml("<b>"+stationname+"</b><br>"+address+"<br><br><a href="+detaillink+">H?ndlerdetails anzeigen &gt;&gt;</a>");
			  });
				map.addOverlay(marker);
			}
		}
	);
}

function map_position(coordinates) {

	if (!GBrowserIsCompatible()) return false;

	var map = new GMap2(document.getElementById("map_position"));
	/*map.addControl(new GSmallMapControl());
	map.addControl(new GMapTypeControl());*/

	map.setUIToDefault();
	map.setMapType(G_SATELLITE_MAP);
	var c = coordinates.split(',');
	map.setCenter(new google.maps.LatLng(c[0],c[1]), 10);
	
	var marker = new GMarker(new google.maps.LatLng(c[0],c[1]));
	map.addOverlay(marker);
	map.getContainer().style.overflow="hidden"; 
}
