// JavaScript Document
var mapCmsUmgebungDisplayed = new Array();
var mapCmsUmgebungCurrentView = new Array();
henworx.MapCms.UmgebungXml={}

henworx.MapCms.UmgebungXml.Click=function(uid){
	
	this.umgbungLocation = new Array();	
	this.umgebungMarksers = new Array();
	//this.umgebungMarkerManager = new GMarkerManager(gMapCms.map, {borderPadding:1});
	this.init = function(){	
		
		//check if the linkrel is enabled if yest restore the linkrel marker 	
		if(typeof(objlinkrel)=="object"){					
			//objlinkrel.putMarker();	//this doesnot work due to conflict.
			 no_event=true				
			 gMapCms.putMarker(objlinkrel.linkrelLocation,no_event);
		}	
				
		
		//check if the same catid is already opened
		henworx.MapCms.Status(_lang.getWord("Loading..."))
					
		if(typeof(mapCmsUmgebungDisplayed[uid]) != 'undefined'){
			
			//hide it if it is already on the map else put it on map.
			if(mapCmsUmgebungCurrentView[uid].status == 'on'){
				this.umgbungLocation = mapCmsUmgebungDisplayed[uid];
				this.hideAllMarker();
				mapCmsUmgebungCurrentView[uid] = {status:'off'};
			}
			else{
				this.umgbungLocation = mapCmsUmgebungDisplayed[uid];		
				//this.putAllMarker();
				umgebungXmlPutAllMarkers(this.umgbungLocation);
				mapCmsUmgebungCurrentView[uid] = {status:'on'};
			}
				
		}
		else{	
			
			this.umgbungLocation=this.getUmgebungFromXML(uid);				
			if(this.umgbungLocation.length>0){			
				mapCmsUmgebungDisplayed[uid] = this.umgbungLocation			
				//this.putAllMarker();
				umgebungXmlPutAllMarkers(this.umgbungLocation);
				mapCmsUmgebungCurrentView[uid] = {status:'on'};
			}
			else{				
				this.umgbungLocation.length=0;
			}
			
		}	
		henworx.MapCms.Status('');
	}
	
	this.hideAllMarker = function(){
		for(i=0;i<this.umgbungLocation.length;i++){
			this.umgbungLocation[i].marker.hide();
		}
	}
	
	 
	 this.getUmgebungFromXML = function (uid){
		
		xml = requestURL(gPluginUrl+'/includes/get-umgebung.php?type='+uid,false,true);	
		
		if(xml == null ){ return false;}
		arrXml = xml2array(xml);	
		if(typeof(arrXml) == 'undefined'){ return false; };
		_umg_type_emblem = 'linkrel';
		for(i=0; i<iumgebungXmlType.length; i++){
			if(iumgebungXmlType[i][0] == uid){	
				_umg_type_emblem = iumgebungXmlType[i][1];
				break;
			}
		}
		var myLoc
		
		var arrLocations=new Array();
		var arrMarkers = new Array();
		for(i in arrXml.locations.location){
			o=arrXml.locations.location[i];
			mAdd= new henworx.MapCms.Address(o.title+" ",o.lat,o.lng);
			myLoc= new henworx.MapCms.Location(mAdd);
			with(myLoc){			
				setTitle(o.title);							
				setEmblem(_umg_type_emblem);
			}
			arrLocations[arrLocations.length]=myLoc;
			_markerOptions = {icon:myLoc.icon,title:myLoc.title};
			marker = new GMarker(myLoc.latlng,_markerOptions);
			arrMarkers.push(marker);
			//gMapCms.map.addOverlay(marker);
		}
		//return arrLocations;
		return arrMarkers;
	}
	 
	 this.init();
}

/*
Click event for Umgebung
*/
function umgebungXmlOnClick(uid){	
	umgebungData = new henworx.MapCms.UmgebungXml.Click(uid)	
}

/* Put all markers according to array of locations sent as parameters
	- use this function to put markers with no any events on marker
*/
function umgebungXmlPutAllMarkers(umgbungMarkers){
	/*
	for(i=0;i<umgbungLocations.length;i++){
		umgebungXmlPutMarker(umgbungLocations[i]);
	}
	*/
	var _mm = new GMarkerManager(gMapCms.map, {borderPadding:1});
	_mm.addMarkers(umgbungMarkers,8,17);
	_mm.refresh();
}

/*put marker with no event */
function umgebungXmlPutMarker(_loc){
	no_event=true				
	gMapCms.putMarker(_loc,no_event);	
}


/*restore the marksers for all umgebungs*/
function restoreUmgebungXmlMarksers(){
	if(typeof(iumgebungXmlType) != 'undefined'){		
		if(iumgebungXmlType.length>0){			
			for(i=0; i<iumgebungXmlType.length; i++){
				uid = iumgebungXmlType[i][0]; //get id				
				if(typeof(mapCmsUmgebungDisplayed[uid]) != 'undefined'){	
					if(mapCmsUmgebungCurrentView[uid].status == 'on'){
						umgebungXmlPutAllMarkers(mapCmsUmgebungDisplayed[uid]);						
					}
				}
			}
		}
	}	
}

/*change umgebung xml link image to show on|off status*/
function umgebungXmlStatusImage(uid){
	lid = document.getElementById('mapcms_umgebung_xml_link_'+uid);	
}
