$(document).ready(function(){
	//starte googlemaps
	onLoad();
	
	$('.fillwithtext').each(function(i)
		{
			var text = $(this).attr('filltext');
			var origcolor = this.style.color;
			
			if($(this).attr('value') == '')
			{
				$(this).attr('value', text);
				this.style.color = '#333';
			}
			
			$(this).focus(function(event)
				{
					if($(this).attr('value') == text)
					{
						$(this).attr('value', '');
						this.style.color = origcolor;
					}
				});
		});
	
	$('form').submit(function(event){
			$('.fillwithtext').each(function(i)
			{
				var text = $(this).attr('filltext');
				
				if($(this).attr('value') == text)
					{$(this).attr('value', '');}
			});
		});
	
	/*function submitAForm(id)
	{
		var form = $(id);
		$.ajax({
			type: "POST",
			url: form.attr('action'),
			data: form.serialize(),
			success: function(data){$('html').html(data);},
			error: function(){alert('error submitting the form '+id);}
		});
	}
	
	$('#sortingorderdropdown').change(function(){
			submitAForm('#update_search_results_form');
		});*/
	
	$('#addEnterpriseFormLink').click(function(event){
		event.preventDefault();
		
		$('#enterpriseBox').slideUp('fast');
		$('#enterpriseMessage').slideUp('fast');
		
		$("#enterpriseBox").load('http://faireseiten.ch/home/addEntry/true', {}, function(){
			$("#enterpriseBox").slideDown('slow'); createEvents();});
	});
	
	$('#manageEnterprisesLink').click(function(event){
		event.preventDefault();
		
		$('#enterpriseBox').slideUp('fast');
		$('#enterpriseMessage').slideUp('fast');
		
		loadEnterpriseTable();
	});
	
	function loadEnterpriseTable()
	{
		$("#enterpriseBox").load('http://faireseiten.ch/home/entryList/true', {}, function(){
			$("#enterpriseBox").slideDown('slow'); createEvents();});
	};

	$.ajaxSetup({type: "POST"});
	$('#searchbox').autocomplete("http://faireseiten.ch/home/searchEntrys/true/",
								 {matchContains: true,
								 formatItem: formatSearch,
								 formatResult: formatSearchResults,
								 selectFirst: false,
								 type: "POST",
								 extraParams: {
										near				: function(){return $('input[name=near]').attr('checked');},
										SortingOrder		: function(){return $('input[name=sorting]').attr('value');},
										Lebensmittel		: true,
										Textilien			: true,
										Diverses			: true
		}}).result(function(event, item){
			location.href="http://faireseiten.ch/home/displayEntry/"+item[1]+"/";
		});
	
	function formatSearch(row)
	{
		return '<strong>'+row[0]+'</strong><br />'+row[2]+'<br />'+row[3];
	}
	
	function formatSearchResults(row)
	{
		return row;
	}
	
	
	
	function createEvents()
	{
		$("a.editEntryLink").fancybox({
			'frameWidth': 600,
			'frameHeight': 600,
			'callbackOnShow': addJSForEditForm
			});
		
		
		$('.tablesorter').tablesorter();
		
		var form = $('#insertNewEntry');
		form.submit(function(event){
			event.preventDefault();
			
			$.post('/home/receiveAddEntry/true',form.serialize(),function(html){
				if(html == 'gespeichert')
				{
					alert('gespeichert');
					form.reset();
				}
				else
					{alert(html);}
				});
		});
		
		$("a.deleteEntryLink").click(function(event){
			event.preventDefault();
			
			var entryID = $(this).attr('entryID');
			
			var answer = confirm("Diesen Eintrag wirklich löschen?");
			if(answer)
			{
				$.post('/home/deleteEntry/'+entryID+'/true', {}, function(html){
					if(html == "success")
					{
						//slide the row out (encapsule each td-content in a div an slide them out)
						$('#entry_'+entryID).children('td').each(function(){
							$(this).wrapInner('<div />').children('div').slideUp("slow", function(){
								$('#entry_'+entryID).remove();
							});
						});
						updateMap();
					}
					else
						alert(html);
				})
			}
		});
	};
});