function Proposal() {
	var editmode = false;
	var prop_name;
	var prop_url;
	var opts = {};
	var selected = [];
	
	var selects;		
}	

Proposal.prototype.bind_proposal = function() 
{
	var me = this;
	$("#"+me.prop_name+"_selector_search").keyup(function(key) {		
		var val = $(this).val();
		if(val.length < 1)
			return;
			
		var inp_field = $(this); 
		$.getJSON(me.prop_url, {input:val}, function(data) {				
			me.create_list(data);
		});
	});	
}
	
Proposal.prototype.create_list = function(data)
{	
	var me = this;		
	var res = new Array();					
	res[res.length] = "<ul class=proposal>";
	if(data != null)
	{
		for(i in data)
		{
			item = data[i];
			res[res.length] = "<li info=\""+item.id+"\">" + item.title +"</li>";
			if(res.length > 20)
			{
				break;
			}
		}
		res[res.length] = "</ul>"
		$("#"+me.prop_name+"_selector_proposals").html(res.join(""));
	
		$("#"+me.prop_name+"_selector_proposals li").each(function() {
			$(this).click(function() {
				$("#"+me.prop_name+"_selector_proposals").empty();
				var id = parseInt($(this).attr("info"));
				if(me.is_selected(id) == true)
				{								
					me.remove(id);
				}
				else
				{							
					me.add(id, $(this).html());
				}
				me.set_mode_view();
				
			}).mouseover(function() {
				$(this).css("cursor","pointer");
			});
		});	
	} 
	else
	{
		$("#"+me.prop_name+"_selector_proposals").empty();
		return;	
	}	
}	
			
Proposal.prototype.add = function(id,title)
{			
	
	if(this.opts.select == "single")
	{
		this.selected = [];
	}

	var id = parseInt(id);
	this.selected[this.selected.length] = {id:id, title:title, status:"on"};
	this.write_info();
}
			
Proposal.prototype.remove = function(id)
{						
	var t = this.get_selected();
	
	var id = parseInt(id);
	var items = this.get_selected();
	for(i = 0; i < items.length; ++i)
	{
		var index = items[i];
		var item = this.selected[index];				
		if(item.id == id)
		{
			item.status = "off";
			this.write_info();
		}
	}
}
			
Proposal.prototype.write_info = function() 
{	
	var me = this;
	var res = [];
	var ids = [];
	var items = this.get_selected();
	for(i = 0; i < items.length; ++i)
	{
		var index = items[i];
		var item = this.selected[index];
		if(typeof( item ) == 'object')
		{
			res[res.length] = '<span info="' + item.id +'">' + item.title + '</span>';
			ids[ids.length] = item.id;	
		}
	}
	$("#"+me.prop_name+"_selector_info").html(res.join(''));
	$("#"+me.prop_name+"_selector_ids").val(ids.join(','));
	
	$("#"+me.prop_name+"_selector_info span").each(function() {
		$(this).click(function(){								
			me.remove($(this).attr("info"));					
		}).mouseover(function() {
			$(this).css("cursor","pointer");
		});				
	});
}
			
Proposal.prototype.get_item = function(id)
{		
	var id = parseInt(id);
	var items = this.get_selected();
	for(i = 0; i < items.length; ++i)
	{
		var item = items[i];				
		if(item.id == id)
		{
			return item;
		}
	}
}
		
Proposal.prototype.is_selected = function(id)
{
	var item = this.get_item(id);
	if(typeof(item) == 'object')
	{
		return tue;
	}
}
			
Proposal.prototype.get_selected = function()
{
	var res = [];
	for(i in this.selected)
	{
		var item = this.selected[i];
		if(typeof( item ) == 'object')
		{
			if(item.status == 'on')
			{
				res[res.length] = i;
			}				
		}
	}
	return res;			
}
			
Proposal.prototype.select = function(init_data) 
{		
	var me = this;	
	for(i in init_data)
	{
		var item = init_data[i];
		if(typeof( item ) == 'object')
		{
			var item = init_data[i];			
			this.add(item.id, item.title);
		}
	}
}

Proposal.prototype.gui_init = function()
{
	var me = this;
	$("#"+me.prop_name+"_selector_add").click(function() {
		if(me.editmode == true) {
			me.set_mode_view();		
		}
		else {
			me.set_mode_edit();
		}		
	}).mouseover(function() {
		$(this).css({cursor:"pointer"});
	});
}

Proposal.prototype.set_mode_view = function()
{
	var inp = $("#"+this.prop_name+"_selector_search");
	this.editmode = false;
	inp.hide(100);
	inp.val(" ");
	inp.focus();
	$("#"+this.prop_name+"_selector_add").attr("src","/images/down.gif");
	$("#"+this.prop_name+"_selector_proposals").empty();
}

Proposal.prototype.set_mode_edit = function()
{
	$("#"+this.prop_name+"_selector_add").attr("src","/images/up.gif");
	var inp = $("#"+this.prop_name+"_selector_search");
	inp.val(" ");
	this.editmode = true;

	if(this.selects.length > 0) {		
		this.create_list(this.selects);
	}
	else {
		inp.show(100);
	}
}

Proposal.prototype.set_selects = function(items)
{
	selects = [];
	for(i in items) {
		var id = i;
		var title = items[i];
		selects[selects.length] = {'id':id, 'title':title};
	}
	
	this.selects = selects;
}
		
Proposal.prototype.init = function(prop_name, init_data, opts, selects)
{
	this.selected = [];	
	this.editmode = false;
	this.opts = opts;
	this.prop_name = prop_name;
	this.prop_url = opts.url;
	this.set_selects(selects);

	this.gui_init();
	
	this.select(init_data);	
	this.bind_proposal();
	this.write_info();	
		
	return this;
}

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();
		}	
	};
})();

var pepi = (function() {
	var images = [];
	
	return {				
		init: function(json_data) {
			this.images = json_data;
			this.detail(0);
		},
	
		detail: function(i) {		
			if(i > this.images.length) {
				i = 0;
			}
			var img_url = this.images[i];
			if(img_url == 'undefined')
			{
				console.log(this.images);
			}	
		
			var img = this.create_detail_image(i, this.images[i]);			
			$("#pepi_detail").html(img);		
		},			
		
		create_detail_image: function(i, img_url) {		
			var img = '<img src="'+img_url+'" alt="detailbild" border="0" onclick="pepi.detail('+(parseInt(i)+1)+'); return false;">';
			return img;
		}
	};
})();

var singles = (function() {

	return {
				
		delete_img: function(id)
		{			
			$.get('/singles/edit/delete_img', {'id':id}, function(data) {
				var x = $('li[info='+id+']');
				x.remove();
			});	
		},
		
		vote: function(id, score)
		{
			console.log(id,score);
			
			
			
			$.get('/singles/vote/'+id+'/'+score,{}, function(data) {
 
			});
		}			
	};
})();


//$(document).ready(function() {
//	
//	var stars = [];
//	var star = '<img src="/images/icons/star.png">';
//	
//	$('.voting_cont div').each(function() {			
//		stars[stars.length] = $(this);		
//	});	
//	
//	$('.voting_cont div').mouseenter(function(event) {
//		var middle = $(this).attr('info');
//		
//		for(i in stars) {
//			var item = $(stars[i]);
//			var val = item.attr('info');					
//			if(val > middle) {				
//				item.html(null);					
//			}
//			else {
//				item.html(star);				
//			}
//		}
//	}).click(function() {
//		var middle = $(this).attr('info');
//		console.log('vote score:' + middle);
//		
//	});
//});