Map = null;
try
{
	if(SearchChMap !== undefined)
	{
		SearchChMap.prototype.alert = function(t)
		{
	
		}
	}
}
catch(e)
{

}

var Ag = (function() {

	var overview_url = ' ';
	var map_pos;
	var overview_opts = {};
	var loaded_items = [];
	var front_items = [];
	var act_front_item = null;
	
	return {
				
		show_event_detail: function(id)
		{	
			this.add_poi(id);
			this.goto_poi(id);
			this.open_poi(id);
		},	
						
		show_location_detail: function(id)
		{	
			this.add_poi(id);
			this.goto_poi(id);
			this.open_poi(id);			
			
			$(".location_kinds").hide();
			$(".agenda_regions").hide();					
		},	
		
		show_location_directory: function()
		{
			var clicked = false;
			$(".location_teaser_front div").each(function() {
				
				if(!clicked)
				{
					$(this).click();
					clicked = true;
					return;
				}
			});
			this.loc_tabs_init();
		},	
		
		show_location_overview: function(map_pos)
		{				
			this.bind_location_items_to_minidetails('.location_kinds a');
			
			this.add_all();
			this.set_map_pos(map_pos);
			
			$(".location_kinds").hide();
			$(".agenda_regions").hide();		
		},
		
		loc_tabs: function(part)
		{
			switch(part)
			{
				case 'regionen':
					$('.tab_regionen').show();
					$('.tab_rubriken').hide();						
					break;
					
				case 'rubriken':
					$('.tab_regionen').hide();
					$('.tab_rubriken').show();						
					break;
			}
		},
		
		loc_tabs_init: function()
		{
			$('.tab_rubriken').show();
			$('.tab_regionen').hide();
		},		
		
		location_details_front: function(id)
		{
			var res = $('#location_details_front');
			res.hide();
			var url = '/agenda/locations/details_front/'+id;
			$.get(url, {}, function(data) {
				res.html(data).fadeIn(500);				
			});
		},
		
	
		/**
		 * EVENT
		**/			
				
		bind_location_items: function(class_name)
		{
			if(Map)
			{										
				var me = this;
				$(class_name).each(function() {	
					var id = $(this).attr("info");
					me.add_poi(id);
				});						
			}
		},
				
		bind_location_items_to_minidetails: function(class_name)
		{				
			if(Map)
			{								
				var me = this;
				Map.addEventListener("change", function(e) {
					me.MapChangeListener(e);
				});
			}		
		},		
							
		set_map_pos: function(map_pos) {
			if(Map) {								
				if(typeof map_pos == 'object')
				{
					this.map_pos = map_pos;
					this.center_map();
				}
			}							
		},				
				
		center_map: function() {
			if(Map) {								
				if(typeof this.map_pos == 'object')
				{
					Map.set(this.map_pos);
				}
			}
		},
		
		goto_poi: function(id) {
			if(Map) {								
				var poi = this.get_poi(id); 
				Map.set({ center:poi[1], zoom:15 });
			}
		},
		
		open_poi: function(id) {
			var poi = this.get_poi(id);			
			if(typeof(poi[6]) == 'object') { 
				poi[6].open();
			}
		},		
		
		get_poi: function(id) {
			var id = parseInt(id);
			for(i in locs) {
				var poi = locs[i];
				if(poi[0] == id) {
					return poi;		
				}
			}
		},
	
		is_visible: function(id)
		{
			var poi = this.get_mappoi(id);				
			if(poi != undefined)
			{
				if(poi.isVisible())
				{
					return true;
				}
			}
			
			return false;
		},
		
		get_mappoi: function(id)
		{
			var id = parseInt(id);
			for(i in locs)
			{
				var poi = locs[i];
				if(poi[0] == id)
				{
					return poi[6];		
				}
			}
		},				
		
		get_all_pois: function()
		{
			return this.loaded_items;
		},
		
		
		is_poi_loaded: function(id)
		{
			if(this.loaded_items == 'undefined' || this.loaded_items == null)
				this.loaded_items = [];
				
			var id = parseInt(id);
			for(i in this.loaded_items)
			{
				var poi = this.loaded_items[i];
				if(poi[0] == id)
				{
					return true;		
				}
			}
			return false;
		},		
		
		add_all: function(id)
		{		
			for(i in locs)
			{
				if(locs[i][7] == '1')
				{
					this.add_poi(locs[i][0]);
				}
			}
		},		
		
		add_poi: function(id)
		{
			if(this.is_poi_loaded(id))
			{
				return;
			}
		
			try
			{				
				var poi = this.get_poi(id);
				for(i in locs) {
					var poi = locs[i];
					if(poi[0] == id) {		
						if(poi[1].length > 5) {
							locs[i][6] = new SearchChPOI({ center:poi[1], title:poi[2],html:this.create_html(poi), icon:"/images/"+poi[4] });
							locs[i][6].gaymap_id = poi[0];
							locs[i][6].addEventListener("popupopen", "MapPopupOpenListener", this);												
							locs[i][6].addEventListener("popupclose", "MapPopupCloseListener", this);							
							Map.addPOI(locs[i][6]);
						}	
					}
				}				
				
				this.loaded_items[this.loaded_items.length] = poi;												
			}
			catch(e)
			{
				// console.log(e);
			}
		},
		

		create_html: function(poi) {
			try
			{
				var res = '';
				if(poi[3] !== 'undefined' && poi[3].length > 0)
				{
					var url = '/agenda/locations/details/'+poi[0]+'"/'+poi[2];
					res = '<b>' + poi[2] + '</b><br>'+poi[3]+'<br>[<a href="'+url+'">mehr...</a>]';
					
					// var url = '/agenda/locations/merken/'+poi[0];
					// res+= '  [<a href="'+url+'">merken</a>]';					
				}
			
				return res;
			}
			catch(e)
			{
			
			}
		},	
		
		open_details: function(id)
		{
			var p = this.get_poi(id);
			var parts = Map.getPermUrl().split('?');			
			var url = '/agenda/locations/details/'+p[0]+'/'+p[2]+'?center='+Map.get("center")+'&'+parts[1];
			document.location.href=url;
		},
		
		update_location_list: function()
		{
			var html = [];
							
			for(i in locs)
			{
				var p = locs[i];
				var id = p[0];
				if(this.is_visible(id))
				{
					$('#loc_'+id).show();
				}
				else
				{
					$('#loc_'+id).hide();
				}	
			}
		},		
		
		MapPopupOpenListener: function(e)
		{
			var id = e.gaymap_id;
			if(id == false)
				return;
	
			var item = $("#loc_"+id);
			if(item) {				
				item.css("background-color","yellow");				
			}
		},	
		
		MapPopupCloseListener: function(e)
		{
			var id = e.gaymap_id;
			if(id == false)
				return;
	
			var item = $("#loc_"+id);
			if(item) {				
				item.css("background-color","white");				
			}			
		},	
		
		MapChangeListener: function(e)
		{						
			this.update_location_list();
		}	
	};
})();