// JavaScript Document
var $jQ = jQuery.noConflict();
var gMap;
var arrUmgebungTypes = new Array();
var arrAllUmgebungs = new Array();
var gCurrentUmgebungMarker;
if(!henworx){
	var henworx={};
	henworx.MapCms = {};
	
}

henworx.MapCms.Umgebung={}
henworx.MapCms.Umgebung.afterInit=function(){}
function loadUmgebung(obj_array) {
	
	if (GBrowserIsCompatible()) {
		gMap = new GMap2(document.getElementById("map"));
		//gMap.setCenter(new GLatLng(gStartAddress.lat, gStartAddress.lng),12);
		gMap.setCenter(new GLatLng('51.1656910', '10.4515260'),12);
		gMap.addControl(new GMapTypeControl());
		gMap.addControl(new GSmallMapControl());
		gMap.disableDoubleClickZoom();
		//map.enableScrollWheelZoom();

	}
}

function swapImage(this_id,the_url,the_active_img,the_inactive_img){
	
	
	//only change if not active
	if(!$jQ('#'+this_id).hasClass('active')){
		//the_img_obj = $jQ('#'+this_id).children('img');	
		the_img_obj = $jQ('#'+this_id+" img:first")
				
		if(the_img_obj.attr('src') == the_url+the_inactive_img){
			/*
			//detect the name of img
			the_img_name_arr = the_inactive_img.split('.');
			if(the_img_name_arr.length == 2){
				$jQ(the_img_obj).attr('src',the_url+the_img_name_arr[0]+'_active.'+the_img_name_arr[1]);
				
			}else{
				//TODO: handel later if the name of image contains . like (bild1.thumb.jpg)
			}
			*/
			$jQ(the_img_obj).attr('src',the_url+the_active_img);
		}
		else{			
			$jQ(the_img_obj).attr('src',the_url+the_inactive_img);
		}
	}
	
		
}
function locateUmgebung(umgebung_cat_id,this_id){
	jQuery('#'+this_id).toggleClass('active');
	if($jQ('#'+this_id).hasClass('active')){
		showUmgebung(umgebung_cat_id);
	}
	else{
		hideUmgebung(umgebung_cat_id);
	}
	
}


//add landmarkder icon on center of the map to be draggable on required location
function setlandMark(type,lat,lng,store_only,img_url,active_img,inactive_img) {
	
	_lang = henworx.MapCms.Lang;				
	
	if(!gMap)
		gMap = gMapCms.map;
	
	if(!arrAllUmgebungs){		
		arrAllUmgebungs=new Array();
		
	}
	if(!arrAllUmgebungs[type]){
				arrAllUmgebungs[type]=new Array();
				
	}
	
	var baseIcon = new GIcon();
			
			baseIcon.image = img_url+"/"+active_img;
			baseIcon.iconSize = new GSize(30, 30);
			baseIcon.iconAnchor = new GPoint(15, 15);
			baseIcon.infoWindowAnchor = new GPoint(30, 0);
		
		var indIcon = new GIcon(baseIcon);
		if(!store_only){
			markerOptions = { icon:indIcon, draggable: true };
		}
		else{
			markerOptions = { icon:indIcon, draggable: false };
		}
		////load mode create marker from latlng
		if(lat &&  lng){
				var marker = new GMarker(new GLatLng(lat,lng),markerOptions);
		}
		else
			var marker = new GMarker(gMap.getCenter(),markerOptions);
		
		marker.value=arrAllUmgebungs[type].length
		//load mode so store in array
		//if(lat &&  lng){
		arrAllUmgebungs[type][marker.value]=marker;
		//}
		
		if(!store_only){
			//we don't need to drag if we are stroing for fuure use from admin section
			//drag events
									
			GEvent.addListener(marker, "dragstart", function () {old_latlng = marker.getLatLng();});
			GEvent.addListener(marker, "dragend", function ()
			{
				
				
				arrAllUmgebungs[type][marker.value]=marker;
				
			});
			
			GEvent.addListener(marker, "click", function ()
			{
				
				gCurrentUmgebungMarker = marker;
				//mInfowindowOption= GInfoWindowOptions;
				//mInfowindowOption.maxWidth =250;
				marker.openInfoWindowHtml(_lang.getWord('Relocate this marker to')+' <br />'+_lang.getWord('Address')+': <input type="text" name="umgebung_relocate_address" id="umgebung_relocate_address" onkeypress="if(detectEnterKey(event) == \'EnterPressed\'){ relocateCurrentUmgebung(); return false; }" /> <br /><label class="umgebung_label_blank" >&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </label> <input type="button" name="submit_relocate" class="button-secondary" value="'+_lang.getWord('Relocate')+'" onclick="relocateCurrentUmgebung()" /> <br />'+
										  '<br/> <a href="javascript:void(0)" onclick="deleteCurrentUmgebung(); return false;" class="delete" >'+_lang.getWord('Delete')+'</a>');
			});
						
			//we don't need to show if we are stroing for fture use
			
			
		}
		gMap.addOverlay(marker);
		if(store_only==true){
			marker.hide();
		}

		
 }
 function deleteCurrentUmgebung(){
	 _lang = henworx.MapCms.Lang;
	 if(confirm(_lang.getWord('Are you sure to delete this marker ?'))){
		 marker = gCurrentUmgebungMarker;
		//remove from the array
		
		for(i=0; i<arrUmgebungTypes.length; i++){
			
			_type = arrUmgebungTypes[i];
			
			for(j=0; j<arrAllUmgebungs[_type].length; j++){
				
				if(	arrAllUmgebungs[_type][j] == marker){
					arrAllUmgebungs[_type][j] = "";
					
				}
			}
		}
		//remove from the map
		gMap.removeOverlay(marker);
	 }
 }
 
function relocateCurrentUmgebung(){
	currentUmgebungAddress = document.getElementById('umgebung_relocate_address').value;
	ff=new GClientGeocoder();
	try{
			ff.getLatLng(currentUmgebungAddress,callBack_findMarker);
	}
	catch(ex){
		
	}
	function callBack_findMarker(latlng){
		
		if( typeof latlng != 'undefined'){
			marker = gCurrentUmgebungMarker;
			setUmgebungMagCenter(latlng);
			marker.setPoint(latlng);
		}
	}
}

function setUmgebungMagCenter(latlng){
	//set center;
	gMap.panTo(latlng);
}

/*
* Pressing Enter key will work pressing submit button
*/
function detectEnterKey(e,callbackFunction)
{
     var key;     
     if(window.event)
          key = window.event.keyCode; //IE
     else
          key = e.which; //firefox     

     if(key == 13){
	 	
		if(callbackFunction != null)
			callbackFunction();
		else
			return "EnterPressed";;
	 }else
		return key;
}

function umgebungJumpToAddress(theFormField){
	newUmgebungAddress = document.getElementById(theFormField).value;	
	ff=new GClientGeocoder();
	ff.getLatLng(newUmgebungAddress,callBack_findLocation);
	
	function callBack_findLocation(latlng){
		setUmgebungMagCenter(latlng);
	}
}

function showUmgebung(type)	 {
	if(!arrAllUmgebungs[type]) { return false; }
	for(j=0; j<	arrAllUmgebungs[type].length; j++){
			marker=arrAllUmgebungs[type][j];
			marker.show();
	}
	
}
function hideUmgebung(type){
	if(!arrAllUmgebungs[type]) { return false; }
	for(j=0; j<	arrAllUmgebungs[type].length; j++){
				marker=arrAllUmgebungs[type][j];
				marker.hide();
	 }
}
function saveUmgebungLocations(url){
	//type
	type_array_string = '';	
	value_array_string = '';
	for(i=0; i<arrUmgebungTypes.length; i++){
		type=arrUmgebungTypes[i];
		//separate each types with |
		type_array_string += type+"|";
		
		if(!arrAllUmgebungs[type]){
				arrAllUmgebungs[type]=new Array();
		}
			for(j=0; j<	arrAllUmgebungs[type].length; j++){
				marker=arrAllUmgebungs[type][j];
				if(marker != ""){ //check if it is not deleted
					latlng=marker.getLatLng();
					lat=latlng.lat();
					lng=latlng.lng();
					//seprate values for one types with #
					value_array_string += lat+","+lng+"*";
				}
			}
		//seprate other types of values with |
		value_array_string +="|"
		
	}
	
	
	
	jQuery("#mapcms_umgebung_debug").load(url+'?mode=update_data&type='+type_array_string+'&values='+value_array_string,
{mode: 'update_data' , type: type_array_string , values: value_array_string},
function() {  }
);
	
}
/*Umgebung Ends
*/
