// == Helper ==
function getWindowheight() { 
var windowheight = 0; 
if (typeof(window.innerheight) == 'number') { 
windowheight = window.innerheight; 
} 
else { 
if (document.documentElement && document.documentElement.clientheight) { 
windowheight = document.documentElement.clientheight; 
} 
else { 
if (document.body && document.body.clientheight) { 
windowheight = document.body.clientheight; 
} 
} 
} 
return windowheight; 
} 
function myHeight() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  //window.alert( 'Width = ' + myWidth );
  //window.alert( 'Height = ' + myHeight );
  return myHeight;
}

// == Tango.info ==

function createMarker1(point,name,html) {

	var icon = new GIcon();
/*	icon.image = 'http://www.tango.info/x/png/gmap/mm_20_red.png';*/
/*	if (imgcolor){
		icon.image = 'http://www.tango.info/x/png/gmap/mm_20_'+imgcolor+'.png';
	}*/
	
/*	icon.shadow = 'http://www.tango.info/x/png/gmap/mm_20_shadow.png';
	icon.iconSize = new GSize(12, 20);
	icon.shadowSize = new GSize(22, 20);
	icon.iconAnchor = new GPoint(6, 20);
	icon.infoWindowAnchor = new GPoint(5, 1);*/


    var marker = new GMarker(point);
    GEvent.addListener(marker, 'click', function() {
      marker.openInfoWindowHtml(html);
    });
    //return marker;
}
      
function createMarker(latlng,name,html,location_code) {
	image = 'http://www.tango.info/x/png/gmap/mm_20_red.png';
	
	/*var icon = new GIcon();
	


	if (window.geo_location==location_code){icon.image = 'http://www.tango.info/x/png/gmap/mm_20_green.png';
	}

	icon.iconSize = new GSize(12, 20);
	icon.iconAnchor = new GPoint(6, 20);
	icon.infoWindowAnchor = new GPoint(5, 1);

	loc2=location_code.substr(6,8)
	html='<span class="ti_location_code">&nbsp;'+loc2+'&nbsp;</span>&nbsp;<a href="'+location_code+'">'+name+'</a>';
	var marker = new GMarker(point,icon);
	*/
	loc2=location_code.substr(6,8)
image = 'http://chart.apis.google.com/chart?chst=d_map_spin&chld=0.7|0|FFFFFF|12|_|'+loc2+'';
	
	var marker = new google.maps.Marker({
        position: latlng,
        map: map,
        icon : image,
        title : loc2+' '+name
        })
	markersArray.push(marker);
	
	// === store the name so that the tooltip function can use it ===
	marker.tooltip = '<div class="tooltip">'+name+'</div>';
	google.maps.event.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(html);
	});
	/*gmarkers[i] = marker;
	htmls[i] = html;
	side_bar_html += '<a href="javascript:myclick(' + i + ')" onmouseover="mymouseover('+i+')" onmouseout="mymouseout()">' + name + '</a><br>';
	i++;*/
	/*map.addOverlay(marker);*/
	
	//  ======  The new marker "mouseover" and "mouseout" listeners  ======
	/*google.maps.event.addListener(marker,"mouseover", function() {
		showTooltip(marker);
	});        
	google.maps.event.addListener(marker,"mouseout", function() {
	tooltip.style.visibility="hidden"
	});
	*/
	
    //return marker;
}
function showTooltip(marker) {
	tooltip.innerHTML = marker.tooltip;
	var point=map.getCurrentMapType().getProjection().fromLatLngToPixel(map.fromDivPixelToLatLng(new GPoint(0,0),true),map.getZoom());
	var offset=map.getCurrentMapType().getProjection().fromLatLngToPixel(marker.getPoint(),map.getZoom());
	var anchor=marker.getIcon().iconAnchor;
	var width=marker.getIcon().iconSize.width;
	var height=tooltip.clientHeight;
	var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(offset.x - point.x - anchor.x + width, offset.y - point.y -anchor.y -height)); 
	pos.apply(tooltip);
	tooltip.style.visibility="visible";
}


function calcDateDiff(x,y,format){
	var px = Date.UTC(x.substr(0,4), x.substr(5,2), x.substr(8,2));
	var py = Date.UTC(y.substr(0,4), y.substr(5,2), y.substr(8,2));
	
	//var diff=py/1000/60/60/24 +"-"+ px/1000/60/60/24 + "..."+(py-px)/1000/60/60/24;
	var diff=py-px;
	
	//alert (px);
	//diff=6000000;
	if (format=="days"){
		var re=diff/1000/60/60/24;
	}

	return re;
}

/* maps.tango.info */
function createMarker3(point, markeri) {

	var city=markeri.getAttribute("city");
	
	var cc=markeri.getAttribute("cc");
	var lc=markeri.getAttribute("lc");
	var tmpi="<img src='http://www.tango.info/x/png/flags/018x012/"+cc+".png' />";
	
	var thtml="<a href='//maps.tango.info/" + cc + "'>"+tmpi+"</a> <a href='/"+ cc+lc + "'>" + city +"</a>";
	
	var fname=markeri.getAttribute("fname");
	
	if (!fname){
		var c_links=markeri.getAttribute("c_links");
		var c_deejays=markeri.getAttribute("c_deejays");
		var c_teachers=markeri.getAttribute("c_teachers");
		var c_festivals=markeri.getAttribute("c_festivals");
		
		thtml+="<br /><a href='//www.tango.info/festivals/"+cc+lc+"'>festivals: "+c_festivals+"</a>";
		thtml+="<br /><a href='//www.tango.info/deejays/"+cc+lc+"'>deejays: "+c_deejays+"</a>";
		thtml+="<br /><a href='//www.tango.info/teachers/"+cc+lc+"'>teachers: "+c_teachers+"</a>";
		thtml+="<br /><a href='//www.tango.info/links/"+cc+lc+"'>links: "+c_links+"</a>";
		thtml+="<br /><a href='//www.tango.info/"+cc+lc+"'>more info</a>";
		
	}

	if (fname){

		var start=markeri.getAttribute("start");
		var stop=markeri.getAttribute("stop");
		var fcodefull=markeri.getAttribute("fcodefull");
		var days2start=calcDateDiff(iso_8601_now,start,"days");
	
		thtml+="<br />"+fname;
		//alert (stop);
		thtml+="<br />" + start + "--" + stop + " ("+ days2start +" days left)";
		thtml+="<br /><a href='//festivals.tango.info/"+fcodefull+"'>more info</a>";
	
		var imgcolor="blue";
		if (days2start<90){var imgcolor="green";}
		if (days2start<31){var imgcolor="yellow";}
		if (days2start<8){var imgcolor="red";}
	}

	//thtml=cc+thtml;
	
	
	var icon = new GIcon();
	icon.image = "http://www.tango.info/x/png/gmap/mm_20_red.png";
	if (imgcolor){
		icon.image = "http://www.tango.info/x/png/gmap/mm_20_"+imgcolor+".png";
	}
	
/*	icon.shadow = "http://www.tango.info/x/png/gmap/mm_20_shadow.png";
	icon.shadowSize = new GSize(22, 20);
*/	
	icon.iconSize = new GSize(12, 20);
	icon.iconAnchor = new GPoint(6, 20);
	icon.infoWindowAnchor = new GPoint(5, 1);
	
	  var marker = new GMarker(point,icon);
	  GEvent.addListener(marker, "click", 
	  	function() {
			marker.openInfoWindowHtml(thtml);
		});
	  return marker;
}
function createLocations(map) {

	map.clearOverlays(); // Browser beschweren sich damit über langsamen Code
	
	var bounds = map.getBounds();
	var southWest = bounds.getSouthWest();
	var northEast = bounds.getNorthEast();
	var b_east = northEast.lng();
	var b_west = southWest.lng();
	var b_north = northEast.lat();
	var b_south = southWest.lat();
	var level = map.getZoom();
	
	var icon = new GIcon();
	icon.image = "http://www.tango.info/x/png/gmap/mm_20_red.png";
/*	icon.shadow = "http://www.tango.info/x/png/gmap/mm_20_shadow.png";
	icon.shadowSize = new GSize(22, 20);
*/
	icon.iconSize = new GSize(12, 20);

	icon.iconAnchor = new GPoint(6, 20);
	icon.infoWindowAnchor = new GPoint(5, 1);
	
	
	        // Add 10 markers to the map at random locations
       /*
        var bounds = map.getBounds();
        var southWest = bounds.getSouthWest();
        var northEast = bounds.getNorthEast();
        var lngSpan = northEast.lng() - southWest.lng();
        var latSpan = northEast.lat() - southWest.lat();
        for (var i = 0; i < 10; i++) {
          var point = new GLatLng(southWest.lat() + latSpan * Math.random(),
                                  southWest.lng() + lngSpan * Math.random());
          map.addOverlay(new GMarker(point));
        }
	*/
	gdownload="/?b_west="+b_west+"&b_east="+b_east+"&b_north="+b_north+"&b_south="+b_south+"&level="+level+"&ti_path="+ti_path;
	//alert (gdownload);
	//http://maps.tango.info/?b_west=-179&b_east=179&b_north=80&b_south=-80&level=2&ti_path=/festivals
	
	
	GDownloadUrl(gdownload, function(data, responseCode) {
  // To ensure against HTTP errors that result in null or bad data,
  // always check status code is equal to 200 before processing the data
  if(responseCode == 200) {
    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")));
	    //map.addOverlay(new GMarker(point, icon));
	    map.addOverlay(createMarker3(point, markers[i]));
    }
  } else if(responseCode == -1) {
    alert("Data request timed out. Please try later.");
  } else { 
    alert("Request resulted in error. Check XML file is retrievable.");
  }
});
	
}


