//Namespace henworx
var henworx={};

//The concept
//henworx.MapCms Object
//henworx.MapCms.Location Object
//henworx.MapCms.Address Object

henworx.MapCms= function() {
			if(!document.getElementById("mapCms")){
				alert("Div id='mapCms' is needed to continue")
				return false;
			}
			_map=new GMap2(document.getElementById("mapCms"));
			//_map.setCenter(new GLatLng(53.5973774, 6.7180445), 10);
			if (GBrowserIsCompatible()) {
				_map.addControl(new GMapTypeControl());		
				_map.addControl(new GSmallMapControl());
				_map.addControl(new GMapTypeControl());
			}
		this.map=_map;		
		this.mapDiv=document.getElementById("mapCms");
		this.zoom=10;
		this.allLocations=new Array();
		this.locations=new Array();		
		this.centerAt=new henworx.MapCms.Address("Germany","53.5973774","6.7180445");
		this.currentLocation=null;
		 
		this.themeURL=gThemeUrl; 
		this.beforeInitialize=function(){};
		this.afterInitialize=function(){};
		this.beforePutAllMarkers=function(){};
		this.afterPutAllMarkers=function(){};
		this.beforeLoad=function(){};
		this.afterLoad=function(){};
		//Events. you can override these events  
		this.afterAddLocation=function(){};
		GEvent.addListener(_map, "drag", function (){henworx.MapCms.MapEvents.drag(_map, this); })
		//public function initialize
		this.initialize= function(_address,_zoom){
			this.beforeInitialize()
			if(typeof _zoom !='undefined')
				this.zoom=_zoom;
			
			if(typeof _address !='undefined'){
				//if address is string value convert it
				_address=new henworx.MapCms.Address(_address);
				this.centerAt=_address
			}
			this.setCenter(this.centerAt);
			this.afterInitialize();
		}
		
		//public function setcenter
		this.setCenter=function(_loc){
			//todo add event
			if(typeof _loc == 'undefined')
				this.map.setCenter(this.centerAt.latlng,this.zoom)
			else
				this.map.setCenter(_loc.latlng,this.zoom)
			//todo add event
		}
		
		//public function adds loation to map
		this.addLocation=function(_loc){
			//todo add event
			if(typeof _loc =='undefined' ){return false;}
			if(! _loc){return false;}
			if(typeof _loc.length!='undefined' ){
				_arr = new Array()
				this.locations=_arr.concat(this.locations,_loc)
			}else{
				this.locations[this.locations.length]=_loc;
			}
			//raise Event
			this.afterAddLocation(_loc.uniqueID,this.locations.length-1)
			
		}
		
		//public function puts a marker with default events for a given location 
		this.putMarker=function(_loc,no_event){
			/*
			this hack is used in case of linkrel the _loc is undefined. This is temporary solution. In reality _loc should not be undefined in case of linkrel.
			*/
			if(typeof(_loc)=='undefined') return false;
			// nomarker if there is no location
		if(typeof(no_event)=="undefined"){no_event=false}
			if(!_loc.latlng.lat()) return false;
			if(no_event){
				markerOptions = {icon:_loc.icon,title:_loc.title};
			}else{
				markerOptions = {icon:_loc.icon};
			}
			var marker = new GMarker(_loc.latlng,markerOptions);
			//marker.value=_loc.uniqueID;
			marker.value=_loc.id;
			
			if(!no_event){
				_markerEvent=henworx.MapCms.MarkerEvent //call without new so that user can override it whenever necessary
				
				GEvent.addListener(marker, "mouseover", function (){
																	this.currentLocation=_loc
																	_markerEvent.mouseOver.onMouseOver(_loc,marker,_map);
																	
																  })
				GEvent.addListener(marker, "mouseout", function (){
																	this.currentLocation=null
																	_markerEvent.mouseOut.onMouseOut(_loc,marker,_map);
																  })
				GEvent.addListener(marker, "click", function (){	this.currentLocation=_loc
																	_markerEvent.mouseClick.onMouseClick(_loc,marker,_map);
																  })
				GEvent.addListener(marker, "dblclick", function (){
																	this.currentLocation=_loc
																	_markerEvent.mouseDoubleClick.onMouseDoubleClick(_loc,marker,_map);
																  })
				GEvent.addListener(marker, "remove", function (){
																	marker.hide();
																  })
			}
			
			this.map.addOverlay(marker);			
			_loc.marker=marker;
		}
		//puts 
		this.putAllMarker=function(){
								if(typeof(this.beforePutAllMarkers)=='function') {this.beforePutAllMarkers();}								
								for(i=0;i<this.locations.length;i++){
									this.putMarker(this.locations[i])
								}																
								if(typeof(this.afterPutAllMarkers)=='function') {this.afterPutAllMarkers();}
							}
	
	this.findLocation=function(id, putMarker){
		for(i=0;i<this.allLocations.length;i++){
			__loc=this.allLocations[i]
			if(__loc.id==id){
				if(typeof(putMarker)=='boolean'){
						if(putMarker && __loc.marker.isHidden()){
							 __loc.marker.show();
							 this.map.addOverlay(__loc.marker);
						}
				}
				return __loc;
			}
		}
		
	}
}


/**/
// Marker Events

// the class is designed to be used as static
// please don't use new word to instantiate the class except for pause
// example create instance 
// markerEv= henworx.MapCms.MarkerEvent //static class tjerefore without word new

henworx.MapCms.MarkerEvent= function(){
}
	
	var iPauseTimer;
	henworx.MapCms.MarkerEvent.mousePause =function(){
		
		this.iPauseTimer=null;
		
		this.enable=function(_loc,_marker,_map){
			this.loc=_loc
			this.marker=_marker
			this.map=_map
			
			_this=this
			this.iPauseTimer=window.setTimeout(_this.onMousePause,500)
		}
	
		this.disable=function(){
			window.clearTimeout(this.iPauseTimer)
		}
		//interface
		this.onMousePause=function(){
			
		}
		
	}
	
	/* these function can be overrriden by user 
	*/
	//interfaces
	
	
	henworx.MapCms.MarkerEvent.mouseOver=function (){
		
		henworx.MapCms.MarkerEvent.mouseOver.onMouseOver=function(_loc,_marker,_map){
			//user shall verwrite this function as shown in example below
		}
		
	}
	henworx.MapCms.MarkerEvent.mouseOut=function (_loc,_marker,_map){
		henworx.MapCms.MarkerEvent.mouseOver.onMouseOut=function(_loc,_marker,_map){
			//user shall verwrite this function as shown in example below
		}
		
		
	}
	henworx.MapCms.MarkerEvent.mouseClick=function (_loc,_marker,_map){
		henworx.MapCms.MarkerEvent.mouseOver.onMouseClick=function(_loc,_marker,_map){
			//user shall verwrite this function as shown in example below
		}
	}
	henworx.MapCms.MarkerEvent.mouseDoubleClick=function (_loc,_marker,_map){
		henworx.MapCms.MarkerEvent.mouseOver.onMouseDoubleClick=function(_loc,_marker,_map){
			//user shall verwrite this function as shown in example below
		}
	}
	
	
henworx.MapCms.changeLoader = function() {
	if (jQuery('#loader').css('display') == 'none' || jQuery('#loader').css('display') == '') {
		jQuery('#loader').css({'width' : jQuery('#mapCms').css('width'), 'height' : jQuery('#mapCms').css('height'), 'display' : 'block'});
	} else {
		jQuery("#loader").fadeOut('300', function() {
			jQuery('#loader').css({'display' : 'none'});
		});
	}
}
