//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"));
			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.locations=new Array();
		this.tooltip='';
		//this.centerAt=new henworx.MapCms.Address("Germany");
		this.currentLocation=null;
		 
		this.themeURL=''; 
		this.beforeInitialize=function(){};
		this.afterInitialize=function(){};
		this.beforeLoad=function(){};
		this.afterLoad=function(){};
		//Events. you can override these events  
		this.afterAddLocation=function(){};
		
		//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.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){
			// nomarker if there is no location
			//alert(typeof(_loc.latlng.lat()))
			if(!_loc.latlng.lat()) return false;
			markerOptions = {icon:_loc.icon,title:_loc.address.address};
			var marker = new GMarker(_loc.latlng,markerOptions);
			//marker.value=_loc.uniqueID;			
			
			marker.value=_loc.id;
			_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);
															  })
			this.map.addOverlay(marker);
			_loc.marker=marker;
			
			//gMapCmsDebug.append(_loc.address.address+":"+_loc.lat);//debug
		}
		//puts 
		this.putAllMarker=function(){
								for(i=0;i<this.locations.length;i++){
									this.putMarker(this.locations[i])
								}
							}
	
	this.findLocation=function(id){
		for(i=0;i<this.locations.length;i++){
			__loc=this.locations[i]
			if(__loc.id==id){
				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
		}
	}
	


