// Initialisiere 
var map, tourbounds, markers, lastpolyline;
var touricons = new Array();
var points = new Array();
var markers = new Array();

//Suche - Warten, Progress
function showProgress()
{	
	
	hideblock("block_criteria");
	hideblock("block_region");

	document.getElementById("tour_wait").style.display = "block";
	document.getElementById("waitimage").innerHTML = "<img src='images/touren/loading.gif' border='0'>"; 
	window.scroll(0,0)
}

// Suche - Block anzeigen
function showblock(name)
{	
	document.getElementById(name + "_titleopen").style.display="block";
	document.getElementById(name + "_titleclosed").style.display="none";
	document.getElementById(name + "_content").style.display="block";
}



// Suche - Block verbergen
function hideblock(name)
{	
	document.getElementById(name + "_titleopen").style.display="none";
	document.getElementById(name + "_titleclosed").style.display="block";
	document.getElementById(name + "_content").style.display="none";
}

// Suche - Karte anzeigen
function showmap()
{
	document.getElementById("tour_map").style.display="block";
	document.getElementById("page_showmap").style.display="none";
    document.getElementById("page_hidemap").style.display="block";
}

// Suche - Karte verbergen 
function hidemap()
{
	document.getElementById("tour_map").style.display="none";
	document.getElementById("page_showmap").style.display="block";
     document.getElementById("page_hidemap").style.display="none";
}

//Liste - Lade Hotelliste 
function loadlist(){
	
	//anzahl gemerkte Betriebe
	updateTourcounter();
	
	//checkboxes deativieren
	var checkboxes = document.getElementsByName("Feld_Tourcheck");
	for (var i=0; i < checkboxes.length; i++) {
		checkboxes[i].checked = false;
		document.getElementById("tour_item_" + checkboxes[i].value).style.backgroundColor = "";	
	}
	
	//checkboxes aktivieren
	var tourlist = readCookie();
	
	if (tourlist != null) {
		var liste = tourlist.split("|");
		if (liste != null){		
			for (var i=0; i < checkboxes.length; i++) {
				for (var j=0; j < liste.length; j++) {
					if (liste[j] == checkboxes[i].value) {
						checkboxes[i].checked = true;
						document.getElementById("tour_item_" + checkboxes[i].value).style.backgroundColor = "#F8EFD5";	
					}
				}
			}
		}		
	}
	
}


//Tourliste - hinzufügen
function updateTour(checked,id)
{
	
	var tourlist = readCookie();
	
	// Hinzufügen oder löschen
	if (checked == true) {
		tourlist = tourlist + id + "|"
		createCookie(tourlist,1);
		document.getElementById("tour_item_" + id).style.backgroundColor = "#F8EFD5";		
	}
	else {
		deleteTour(id);
		document.getElementById("tour_item_" + id).style.backgroundColor = "";	
	}
	
	updateTourcounter();

}

//Tourliste - hinzfügen
function checkTour(id){
	updateTour(true,id);
	loadlist();
}

//Tourliste - entfernen
function deleteTour(id){
	var tourlist = readCookie();
	tourlist = tourlist.replace(id + "|", "");
	createCookie(tourlist,1);
}

//Tourliste - löschen 
function clearlist(){
	eraseCookie();
	loadlist();
}


//Tourliste - zähler
function updateTourcounter(){
	
	var tourlist = readCookie();
	var tourcounter = document.getElementById("tourlist_counter");
	
	if (tourlist != null) {
		var liste = tourlist.split("|");
		if (liste != null) {var counter = liste.length-1;}
		if (document.getElementById("options") !=null) {document.getElementById("options").style.display="block";}
	}
	else {
		var counter = 0;
		if (document.getElementById("options") !=null) {document.getElementById("options").style.display="none";}
	}
	
	if (tourcounter != null) {tourcounter.innerHTML = counter;}
	
}


//Cookie erstellen
function createCookie(value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = "TOURENLISTE="+value+expires+"; path=/";
}

//Cookie lesen
function readCookie() {
	var nameEQ = "TOURENLISTE=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

//Cookie löschen
function eraseCookie() {
	createCookie("",-1);
}

// Suche - Regionsblock löschen 
function clearRegionBlock()
{
	document.getElementsByName("Feld_AlleOrte")[0].checked=true;
	
	var checkboxes = document.getElementsByName("Feld_Region");
	for (var j=0; j < checkboxes.length; j++) {
		checkboxes[j].checked = true;
	}
	
	toggleregion(true,'BRIXEN;WESTENDORF;KIRCHBERG');
	toggleregion(true,'STJOHANNJ;OBERNDORFJ;KIRCHDORF;ERPFENDORF');
	toggleregion(true,'HOPFGARTEN;ITTER;WOERGL;ANGERBERG;KIRCHBICHL;MARIASTEIN;ANGATH');

}

// Suche - Region auswählen
function toggleregion(checked,orte){
	
	var liste = orte.split(";");
	var checkboxes = document.getElementsByName("Feld_Ort");
	
	for (var i = 0; i < liste.length; i++) {
		for (var j=0; j < checkboxes.length; j++) {
			if (checkboxes[j].value == liste[i]) {
				checkboxes[j].checked = checked;
			}
		}		
	}
	
}

// Suche - Alle Regionen auswählen
function checkregions(checked){
	
	if (checked){
		
		var checkboxes = document.getElementsByName("Feld_Region");
			for (var j=0; j < checkboxes.length; j++) {
				checkboxes[j].checked = true;
			}
		
		toggleregion(true,'BRIXEN;WESTENDORF;KIRCHBERG');
		toggleregion(true,'STJOHANNJ;OBERNDORFJ;KIRCHDORF;ERPFENDORF');
		toggleregion(true,'HOPFGARTEN;ITTER;WOERGL;ANGERBERG;KIRCHBICHL;MARIASTEIN;ANGATH');	
	}
	
}

function clearTourBlock()
{
	
	document.getElementById("distanz").value = "0";
	document.getElementById("distanztext").innerHTML = "0";
	
	document.getElementById("schwierigkeit").value = "0";
	changeslider('slider0',0);
	
	document.getElementById("schwierigkeit").value = "0";
	changeslider('slider1',0);
	
	document.getElementById("dauer").value = "0";
	changeslider('slider2',0);
	
	document.getElementById("hoehenmeter").value = "0";
	changeslider('slider3',0);
	
}

function clearTourAll()
{
	
	clearTourBlock();
	clearRegionBlock();
	
}
		
 function loadtourmap() {
				
				 if (GBrowserIsCompatible()) {

			        // Google Karte - Basiseinstellungen
			        map = new GMap2(document.getElementById("map"));
			        map.setCenter(new GLatLng( 47.500412,12.297702), 11);
			        map.setMapType(G_PHYSICAL_MAP); 	
			        
			        //Bounds
			        tourbounds = new GLatLngBounds();
			        
			        // Controls
		      		map.addControl(new GLargeMapControl());
		     		map.addControl(new GMapTypeControl());	
		     		
		     		// Icons definieren
			      	touricons[1] = new GIcon();
					touricons[1].iconSize = new GSize(41, 34);
					touricons[1].iconAnchor = new GPoint(10, 31);
					touricons[1].image = "/de/images/touren/difficulty1.png";	
					touricons[1].infoWindowAnchor = new GPoint(10, 12);
					
					touricons[2] = new GIcon();
					touricons[2].iconSize = new GSize(41, 34);
					touricons[2].iconAnchor = new GPoint(10, 31);
					touricons[2].image = "/de/images/touren/difficulty2.png";	
					touricons[2].infoWindowAnchor = new GPoint(10, 12);
					
					touricons[3] = new GIcon();
					touricons[3].iconSize = new GSize(41, 34);
					touricons[3].iconAnchor = new GPoint(10, 31);
					touricons[3].image = "/de/images/touren/difficulty3.png";	
					touricons[3].infoWindowAnchor = new GPoint(10, 12);
					
					touricons[4] = new GIcon();
					touricons[4].iconSize =new GSize(41, 34);
					touricons[4].iconAnchor = new GPoint(10, 31);
					touricons[4].image = "/de/images/touren/difficulty4.png";	
					touricons[4].infoWindowAnchor = new GPoint(10, 12);      
					
					// Touren
					loadtourlist();
					//map.setZoom(map.getBoundsZoomLevel(tourbounds));
       				map.setCenter(tourbounds.getCenter());
		     		
		     	}
		  }	
		  
		  function addtourmarker(id,difficulty, lat, lon, titel, html){
					
					var touricon
					
					// Icon
					switch (difficulty){
						case "1": {  touricon=touricons[1]; break }
						case "2": {  touricon=touricons[2]; break }
						case "3": {  touricon=touricons[3]; break }
						case "4": {  touricon=touricons[4]; break }
					}
					
					// Marker
					var point = new GLatLng(lat,lon);
					var marker = new GMarker(point,{icon:touricon,title:titel});
					map.addOverlay(marker); 
					markers[id]=marker;
					
					// Infowindow Mouseover
					GEvent.addListener(marker, "click", function() {
					    marker.openInfoWindowHtml(html);
					    map.setCenter(marker.point); 
					 });

					 //GEvent.addListener(marker, "mouseover", function() {
					   //showtourline(id,difficulty);
					 //});
					 
					// Bounds erweitern
					tourbounds.extend(point);
					
				}	
				
				function showtourline(id,difficulty) {
					
					var color = "#7AA618";
					
					switch (difficulty){
							case "1": { color="#0E1BC7"; break }
							case "2": { color="#E2001A"; break }
							case "3": { color="#000000"; break }
							case "4": { color="#FEDA0E"; break }
					}
					
					if (lastpolyline != null) {map.removeOverlay(lastpolyline);}					
					var polyline = new GPolyline(points[id],color, 4, 0.80);
					lastpolyline = polyline
					map.addOverlay(polyline);
					map.setZoom(14); 		
					window.scrollTo(0, document.getElementById("map").offsetTop);
					GEvent.trigger(markers[id], "click");	
					
				}
				
				function showtourmarker(marker){					
					GEvent.trigger(marker, "mouseover");
				
		}  		    		
