var map, clusterLayer, tour;
var qs;
var tourid;
var myLatlng;
var myImage="";
var ueberschrift="";
var beschreibung="";
var mytours=new  Array();
var infoBox;
var root_url="http://www.noevog.at";
var mapTypeIds;
var clickednr;
var artikelid,tt;
var noepol2;
var idpoi;

 function initializeMap(divtag) {
	 alp.gmap3(function (gm) {
        mapTypeIds = ['alpstein_map', 'alpstein_hybrid',  gm.MapTypeId.TERRAIN];
		if(console) console.log("48.30")
        map = new alp.gmap3.Map($(divtag), {
               center : new gm.LatLng(48.30, 16.18196289),
               zoom : 8,
               mapTypeId : mapTypeIds[ 2 ],
			   disableDefaultUI: true,
			    navigationControl: true,
				 mapTypeControl: true,
				 scaleControl: true,
				mapTypeControlOptions : { mapTypeIds: mapTypeIds }
				
              });
		
		
		
		 // initialize clusterLayer Object with project key
       clusterLayer = new alp.gmap3.api.ClusterLayer( { proj : 'ar-noevog', initCat : 'hideall' });
	   clusterLayer.init_cb( function() { 
	  
         // clusterLayer.hideAllCats(); 
         // clusterLayer.setCatSelected('-730826425675674231', true); //wandern
		 // clusterLayer.setCatSelected('-730826421380706931', true); //rad
		
		var qspoi=new QS();//wenn poi als parameter übergeben wird dann poi gleich öffnen
		if (qspoi.get('idpoi')!=""){
			idpoi=parseInt(qspoi.get('idpoi'));
		}else{
			idpoi=0;
		}
		
		donau('#0e72b5');
		KarteGrenze();
		
		google.maps.event.addListener(map, 'zoom_changed', function() {
			if(map.getZoom()!=8){
				noepol2.setMap(null);
			}else{
				noepol2.setMap(map);
			}
		 });
		
		var qstour=new QS(); //wenn karte über url und parameter tour aufgerufen wird dann tour anzeigen auf karte
		
		if (parseInt(qstour.get('tour'))>0){
			tourid=parseInt(qstour.get('tour'));
			showOnMap();
		}
        });
	
		// add clusterLayer to map
        clusterLayer.setMap(map);

		
    });
  }
  



function donau(farbe){
	var pdonau = new google.maps.Polyline({
	  path: donauarr,
	 strokeColor: farbe,
	 strokeOpacity: 1.0,
	 clickable: false,
	 strokeWeight: 3,
	 zindex:2
	});
	pdonau.setMap(map)
}

function KarteGrenze(){	


	var arrwhite = [
				new google.maps.LatLng(51.044157,11.975098),
				new google.maps.LatLng(51.057969,51.044157),
				new google.maps.LatLng(45.923644,18.962402),
				new google.maps.LatLng(45.831861,11.66748),
				new google.maps.LatLng(51.044157,11.975098)
			];

	noepol2 = new google.maps.Polygon({
	 paths: noearr,
	 strokeColor: "#333333",
	 fillColor: "#ffffff",
	 strokeOpacity: 0.7,
	 clickable: false,
	 strokeWeight: 3,
	 fillOpacity: 0.5

	});
	

	noepol2.setMap(map);
}

function showOnMap(){

		
		var qsid=new QS(); 
		var qstt=new QS();
		artikelid=qsid.get('id');
		if (typeof(artikelid)=="undefined"){
			artikelid=""
		}
		tt=qsid.get('tt');
		if (typeof(tt)=="undefined"){
			tt=""
		}
		$('boxKarteAuswahl').setStyle('display','block');
		 
			
			
			multislide = new multislide('ms_container_bahn',
				{
				    slide_duration: 600,
				    slide_transition: 'quint:out',
				    class_toggler: 'control',

				    onExpand: function (el) {

				        el.set('html','<div class="toplink2"><img src="img/leer.gif" height="5" width="9" alt="ausblenden" /></div>');
				    },

				    onCollapse: function (el) {
				        el.set('html','<div class="toplink1"><img src="img/leer.gif" height="5" width="9" alt="anzeigen" /></div>');
				    }
				});
			if (parseInt(tourid)!=19) {
				multislide.expand_all();
			}
			
			multislide2 = new multislide2('ms_container',
				{
				    slide_duration: 600,
				    slide_transition: 'quint:out',
				    class_toggler: 'control',

				    onExpand: function (el) {

				        el.set('html','<div class="toplink2"><img src="img/leer.gif" height="5" width="9" alt="ausblenden" /></div>');
				    },

				    onCollapse: function (el) {
				        el.set('html','<div class="toplink1"><img src="img/leer.gif" height="5" width="9" alt="anzeigen" /></div>');
				    }
				});
			
			if($('boxbus')){
				multislide3 = new multislide3('ms_container_bus',
					{
						slide_duration: 600,
						slide_transition: 'quint:out',
						class_toggler: 'control',

						onExpand: function (el) {

							el.set('html','<div class="toplink2"><img src="img/leer.gif" height="5" width="9" alt="ausblenden" /></div>');
						},

						onCollapse: function (el) {
							el.set('html','<div class="toplink1"><img src="img/leer.gif" height="5" width="9" alt="anzeigen" /></div>');
						}
					});
				if (parseInt(tourid)>=19){ //bei klick auf wieselbus im menü, gleich wieselbus kartenmenü öffnen
					multislide3.expand_all();
					
					// Hier die Wieselbus Strecken anzeigen
					//alert('ja');
					
					map.setMapTypeId( mapTypeIds[ 2 ]);
	
					showLines(poiline101,"#036AA5"); // wieselbus a
					showLines(poiline102,"#036AA5"); // wieselbus b
					showLines(poiline103,"#036AA5"); // wieselbus c
					showLines(poiline104,"#036AA5"); // wieselbus d
					showLines(poiline105,"#036AA5"); // wieselbus e
					showLines(poiline106,"#036AA5"); // wieselbus f
					showLines(poiline107,"#036AA5"); // wieselbus g
					showLines(poiline108,"#036AA5"); // wieselbus h
					showLines(poiline109,"#036AA5"); // wieselbus k
					showLines(poiline110,"#036AA5"); // wieselbus L
					showLines(poiline111,"#036AA5"); // wieselbus m
					
					// Wieselbus Linie A 
					var area101 = [
						new google.maps.LatLng(48.329961,16.366882),
						new google.maps.LatLng(48.370118,16.463013),
						new google.maps.LatLng(48.390184,16.65802),
						new google.maps.LatLng(48.376504,16.700592),
						new google.maps.LatLng(48.355519,16.77063),
						new google.maps.LatLng(48.322656,16.75827),
						new google.maps.LatLng(48.323569,16.642914),
						new google.maps.LatLng(48.328135,16.480865),
						new google.maps.LatLng(48.309871,16.392975),
						new google.maps.LatLng(48.329961,16.366882)
					];						
					showArea(area101,'<div class="blase-bahnlinie"><h3>Linie A</h3><a href="default.asp?bahnkarte=1&tour=20&id='+artikelid+'&tt='+tt+'" class="extlink">mehr Infos anzeigen</a></div>');
					// Ende Wieselbus Strecken
				}
				
			}
			
			if ((parseInt(tourid)>0) && (parseInt(tourid)!=8) ) { //dann wird die jeweilige tour + bahnmenü gleich angezeigt
			
				var BahnItems = $$('#boxbahn .box-item');
				var BusItems = $$('#boxbus .box-item');
				if (BahnItems[(parseInt(tourid)-1)]){
					BahnItems[(parseInt(tourid)-1)].fireEvent('click');
				}else if ((parseInt(tourid)-21)>=0){
					if(BusItems[(parseInt(tourid)-21)]){
						BusItems[(parseInt(tourid)-21)].fireEvent('click');
					}
				}
		
			}
			
			else{//sonst touren über gmaps laden

				map.setMapTypeId( mapTypeIds[ 2 ]);
				
				showLines(poiline1,"#1e549e");
				showLines(poiline2,"#ad681f");
				//showLines(poiline3,"#0091b9");
				showLines(poiline4,"#ffcc33");
				showLines(poiline5,"#d10019");
				showLines(poiline6,"#34802b");
				showLines(poiline7,"#34802b");
				showLines(poiline8,"#86a001");
			    showLines(poiline9,"#00ccff"); //gemeindealpe
				showLines(poiline10,"#00ccff"); //gemeindealpe
				showLines(poiline11,"#00ccff"); //gemeindealpe
				

			//citybahn waidhofen
			var area1 = [
				new google.maps.LatLng(47.982568,14.91394),
				new google.maps.LatLng(47.875829,14.848022),
				new google.maps.LatLng(47.949466,14.644775),
				new google.maps.LatLng(48.052382,14.716187),
				new google.maps.LatLng(47.982568,14.91394)
			 ];		 
			//mariazellerbahn
		/*	var area2 = [
				new google.maps.LatLng(47.967859,15.205078),
				new google.maps.LatLng(47.739323,15.216064),
				new google.maps.LatLng(47.735629,15.402832),
				new google.maps.LatLng(47.931066,15.430298),
				new google.maps.LatLng(48.210032,15.765381),
				new google.maps.LatLng(48.297812,15.595093),
				new google.maps.LatLng(47.967859,15.205078)
			 ];*/
			 //mariazeller neu:
			 var area2 = [
				new google.maps.LatLng(47.768305275216825, 15.301511291503857),
				new google.maps.LatLng(47.87895100398706, 15.315244201660107),
				new google.maps.LatLng(47.94706499272296, 15.257565979003857),
				new google.maps.LatLng(48.21130410143529, 15.609128479003857),
				new google.maps.LatLng(48.1600305567491, 15.691525939941357),
				new google.maps.LatLng(47.94154557682069, 15.337216857910107),
				new google.maps.LatLng(47.73691277036002, 15.364682678222607)
			 ];
	
			 
			 
			 //ötscherland-express
			 /*var area3 = [
				new google.maps.LatLng(47.978891,15.095215),
				new google.maps.LatLng(47.938427,15.172119),
				new google.maps.LatLng(47.816843,15.03479),
				new google.maps.LatLng(47.883197,14.919434),
				new google.maps.LatLng(47.978891,15.095215)
			 ];*/
			//Wachaubahn
			var area7 = [
				new google.maps.LatLng(48.414619,15.650024),
				new google.maps.LatLng(48.217353,15.325928),
				new google.maps.LatLng(48.286848,15.216064),
				new google.maps.LatLng(48.458352,15.496216),
				new google.maps.LatLng(48.414619,15.650024)
			];
			//Waldviertler Schmalspurbahnen
			var area6 = [
				new google.maps.LatLng(48.980217,15.007324),
				new google.maps.LatLng(48.592959,14.738159),
				new google.maps.LatLng(48.523881,15.007324),
				new google.maps.LatLng(48.890004,15.183105),
				new google.maps.LatLng(48.980217,15.007324)
			];
			//schneebergbahn
			var area4 = [
				new google.maps.LatLng(47.829752,15.916443),
				new google.maps.LatLng(47.77256,15.971375),
				new google.maps.LatLng(47.713458,15.845032),
				new google.maps.LatLng(47.757791,15.779114),
				new google.maps.LatLng(47.829752,15.916443)
			];
			//reblaus express
			var area5 = [
				new google.maps.LatLng(48.917085,15.603333),
				new google.maps.LatLng(48.788771,16.045532),
				new google.maps.LatLng(48.692773,15.946655),
				new google.maps.LatLng(48.823141,15.512695),
				new google.maps.LatLng(48.917085,15.603333)
			 ];	
				
			//gemeindealpe
			var area9 = [
				new google.maps.LatLng(47.81460923829068, 15.290567878723095),
				new google.maps.LatLng(47.80308035346132, 15.292799476623486),
				new google.maps.LatLng(47.81045913456968, 15.241987709045361),
				new google.maps.LatLng(47.8161078053479, 15.24353266143794)
			 ];	


			
			showArea(area1,'<div class="blase-bahnlinie"><h3>Citybahn Waidhofen</h3><a href="default.asp?bahnkarte=1&tour=1&id='+artikelid+'&tt='+tt+'" class="extlink">mehr Infos anzeigen</a></div>');
			showArea(area2,'<div class="blase-bahnlinie"><h3>Mariazellerbahn</h3><a href="default.asp?bahnkarte=1&tour=2&id='+artikelid+'&tt='+tt+'" class="extlink">mehr Infos anzeigen</a></div>');
			//showArea(area3,'<div class="blase-bahnlinie"><h3>Ötscherland-Express</h3><a href="default.asp?bahnkarte=1&tour=3&id='+artikelid+'&tt='+tt+'" class="extlink">mehr Infos anzeigen</a></div>');
			showArea(area4,'<div class="blase-bahnlinie"><h3>Schneebergbahn</h3><a href="default.asp?bahnkarte=1&tour=4&id='+artikelid+'&tt='+tt+'" class="extlink">mehr Infos anzeigen</a></div>');
			showArea(area5,'<div class="blase-bahnlinie"><h3>Reblaus-Express</h3><a href="default.asp?bahnkarte=1&tour=5&id='+artikelid+'&tt='+tt+'" class="extlink">mehr Infos anzeigen</a></div>');
			showArea(area6,'<div class="blase-bahnlinie"><h3>Waldviertler Schmalspurbahnen</h3><a href="default.asp?bahnkarte=1&tour=6&id='+artikelid+'&tt='+tt+'" class="extlink">mehr Infos anzeigen</a></div>');
			showArea(area7,'<div class="blase-bahnlinie"><h3>Wachaubahn</h3><a href="default.asp?bahnkarte=1&tour=7&id='+artikelid+'&tt='+tt+'" class="extlink">mehr Infos anzeigen</a></div>');
			showArea(area9,'<div class="blase-bahnlinie"><h3>Gemeindealpe</h3><a href="default.asp?tt=NOEVOG_R143" class="extlink">mehr Infos zur Gemeindealpe</a></div>');

			

			
			}
			//google.maps.event.trigger.delay((map, 'resize'),300);
			//map.setZoom(map.getZoom());
			
} 

function showLines(arrpoi,color){

	var polyPoints = Array();
	for (var p=0;p<arrpoi.length;p++) {
	   var point = new google.maps.LatLng(arrpoi[p][1],arrpoi[p][0]);
	   polyPoints.push(point);
	 }

   var line1 = new google.maps.Polyline({
		 path: polyPoints,
		 strokeColor: color,
		 strokeOpacity: 1.0,
		 clickable: false,
		 strokeWeight: 5,
		 zindex:5
	});
	 line1.setMap(map); 
}

function showArea(arrpoi,content){

	var polyPoints = Array();
	for (var p=0;p<arrpoi.length;p++) {
	   var point = new google.maps.LatLng(arrpoi[p][1],arrpoi[p][0]);
	   polyPoints.push(point);
	 }
	 var bahnarea;

    bahnarea = new google.maps.Polygon({
		paths: arrpoi,
		strokeColor: "#FFffff",
		strokeOpacity: 0,
		strokeWeight: 1,
		fillColor: "#FF0000",
		fillOpacity: 0
	  });

	var infobox2;
	 google.maps.event.addListener(bahnarea, 'click', function(event) {
		if (typeof(infobox2)!="undefined"){
			infobox2.setMap(null);
		}
		infobox2 = new InfoBox({latlng: event.latLng, map: map, html: content,height:80, width:215,offsetVertical: -80,offsetHorizontal: 0});
	 }); 
	 bahnarea.setMap(map); 
}

function showmytour(mytourid){ 
    clusterLayer.setCatSelected(mytourid, true);
 }
  
function clearmytour(mytourid){ 
	clusterLayer.setCatSelected(mytourid, false);
}
  
function showbahn(bahnid,nr){ //zeige die angeklickte tour
	
	mytours[nr] = new alp.gmap3.api.TourPoiLayer(bahnid);
	mytours[nr].setMap(map);
	mytours[nr].listen( 'poiClick', TourMarkerClick );
	mytours[nr].listen( 'poiClick', TourMarkerClick );
	if (idpoi!=0){
		//alp.gmap3.api.iwOpenOOI( map,"-8247592877030521658");
	}
	clickednr=nr;
	timing.delay(2000); //wait 5 seconds and execute my function.
}
function timing(){ // bei manchen strecken muss der kartenausschnitt verschoben werden, weil die sonst unter der menübox liegen
	
	switch(clickednr){
		case "20":
			map.panBy(80,0)
			break;
			
		case "23":
			map.panBy(115,0)
			break;
		
		case "28":
			map.panBy(115,0)
			break;
		case "29":
			map.panBy(115,0)
			break;
		case "30":
			map.panBy(115,0)
			break;	
		default:
			map.panBy(50,0);
			break;
		
	}

}
function clearbahn(bahnid,nr){
	mytours[nr].setMap(null);
}

function iconTausch(){
	var allIcons=$('karteNoevog').getElements('img');
	var counti=1;
	if (allIcons.length>0){
			allIcons.each(function(i, index){
				srcvalue = i.getProperty('src'); 
			if (srcvalue.indexOf("busStop.mapscreen.png")!=-1){
				i.set('src','img/icon_wiesel.png'); 
			}			
			if (counti==1){			
			//	alert(srcvalue.indexOf("busStop.mapscreen.png")!=-1);
			}

			counti++;	
		});
	}
	//alert(counti);
	//http://res.oastatic.com/icons/alpstein/busStop/maroon/busStop.mapscreen.png

}

function TourMarkerClick(markerInfo){
	myImage="";
	ueberschrift="";
	beschreibung="";
	 if ((markerInfo.title)!="undefined"){
		ueberschrift=decodeURI(markerInfo.title);
	 }
	 if ((markerInfo.images)!=null){
		 myImage='<div class="blase-bahninfo-bild"><img src="http://www.outdooractive.com/img/110/80/'+markerInfo.images.image[0].id+'/.jpg" alt="'+ueberschrift+'"></div>'
	 }
	
	  if (typeof(markerInfo.longText)!="undefined"){
		beschreibung=markerInfo.longText;
	 }
	 myhtml='<div class="blase-bahninfo">'+myImage+'<div class="blase-bahninfo-content"><h3>'+ueberschrift+'</h3>';
	 myhtml+=''+beschreibung+'</div><br style="clear:both;" />';
	 myhtml+='</div>';
	 var c = eval("[" + markerInfo.geometry+"]");
	 var mylatlng = new google.maps.LatLng(c[1],c[0]);
	// alp.gmap3.api.iwOpen(map,latlng, myhtml);
	
	if (typeof(infoBox)!="undefined"){
		infoBox.setMap(null);
	}
	 infoBox = new InfoBox({latlng: mylatlng, map: map, html: myhtml});

}
 
  window.addEvent('domready', function(){
  
	
	
    if ($('boxbahn')) {
		
        
		
		
		//Listener hinzufügen Bahnstrecken
		var BahnItems = $$('#boxbahn .box-item');
		if (BahnItems.length>0){
			BahnItems.each(function(i, index){
				i.addEvent('click', function(ev){
					if(i.getProperty('class')=='box-item'){
						i.setProperty('class','box-item aktiviert');
						if(i.get('tourid')!=""){
							if (i.get('tourid')=="8"){
							
							}else{
							showbahn(i.get('tourid'),i.get('bahn'));
							}
						}
						if(i.get('tourid2')!="" && i.get('tourid2')!=null){
							//alert(i.get('tourid2'));
							showbahn(i.get('tourid2'),i.get('bahn2'));
						}
					}else{
						i.setProperty('class','box-item');
						if(i.get('tourid')!=""){
						    if (i.get('tourid')=="8"){
							
							}else{
							clearbahn(i.get('tourid'),i.get('bahn'));
							}
						}
						if(i.get('tourid2')!=""  && i.get('tourid2')!=null){
							clearbahn(i.get('tourid2'),i.get('bahn2'));
						}
						
					}
				});

			});
		}
		
		//Listener hinzufügen Rad und Wandern
		var TourItems = $$('#boxradwandern .box-item');
		if (TourItems.length>0){
			TourItems.each(function(i, index){
				i.addEvent('click', function(ev){
					if(i.getProperty('class')=='box-item'){
						i.setProperty('class','box-item aktiviert');
						showmytour(i.get('tourid'));
					}else{
						i.setProperty('class','box-item');
						clearmytour(i.get('tourid'));
					}
				});

			});
		}
		
		
		
		//Listener hinzufügen Busstrecken
		var BusItems = $$('#boxbus .box-item');
		if (BusItems.length>0){
			BusItems.each(function(i, index){
				i.addEvent('click', function(ev){
					if(i.getProperty('class')=='box-item'){
						i.setProperty('class','box-item aktiviert');
						if(i.get('tourid')!=""){
							showbahn(i.get('tourid'),i.get('bahn'));
							
						}
						if(i.get('tourid2')!="" && i.get('tourid2')!=null){
							//alert(i.get('tourid2'));
							showbahn(i.get('tourid2'),i.get('bahn2'));
						}
						iconTausch.delay(2500); //nachdem die alp
					}else{
						i.setProperty('class','box-item');
						if(i.get('tourid')!=""){
							clearbahn(i.get('tourid'),i.get('bahn'));
						}
						if(i.get('tourid2')!=""  && i.get('tourid2')!=null){
							clearbahn(i.get('tourid2'),i.get('bahn2'));
						}
						
					}
				});

			});
		}
		
		
		
    }
	
	
	
    if ($('karteNoevog')){

		initializeMap("karteNoevog");
		
	}
	

	
});


window.addEvent('domready', function(){

	if ($('mmenu2')){ //menüpunkt interkative karte
		if($('karteNoevog')){ //wenn div für karte vorhanden
			
		
			//bei klick karte sichtbar machen, headerbilder verstecken und tour auslesen
			$('mmenu2').addEvent('click', function(e){
				if($('karteNoevog').getStyle('display')=='none'){
					$('karteNoevog').setStyle('display','block');
					if ($('myGallery')) {
					$('myGallery').setStyle('display','none');
					}
					tourid=$('mmenu2tour').getProperty('tour');
					 
					 
					 //initializeMap();
					google.maps.event.trigger(map, 'resize');
					if(console) console.log("30")
					map.setCenter(new google.maps.LatLng(48.30, 16.18196289));
					
					map.setZoom(8);
					showOnMap();
					

				}
			});
			
		}
	}
	
	

});
	

