var online_id=[];
var online_un=[];

var event_timer;
var checking=false;

var isOnline;

function do_login(form) {
//	alert('login');
	$('.login-error').hide();
	var error="";
	if (form.un.value.length<4)
	{
		error="Ditt användarnamn är för kort!";
		form.un.focus();
	}
	else if (form.pw.value.length<4)
	{
		error="Ditt lösenord är för kort!";
		form.pw.focus();
	}
	else {
		// Gör inloggning
		$.post("execute_login.php",{ action: 'login' , un: form.un.value, pw: form.pw.value, rememberme: form.rememberme.checked },function(response,textstatus,jqxhr) {
//			alert(form.un.value+' '+form.pw.value+' '+form.rememberme.checked);
//			alert(response);
			process_loginattempt(response);
		});
	}
	if (error!="")
	{
		$('.login-error').text(error);
		$('.login-error').show('fast');
	}
	return false;
}

function process_loginattempt(response) {
	var error="";
	if (response=="login_ok")
	{
		location.reload();
	}
	else if (response=="login_error")
	{
		error="Felaktiga inloggningsuppgifter!";
	}
	else if (response=="login_error_nonverified")
	{
		error="Ditt konto är inte aktiverat än! Kolla din epost!";
	}

	if (error!="")
	{
		$('.login-error').hide();
		$('.login-error').text(error);
		$('.login-error').show('fast');
	}
}

function check_availability(type,value) {
	if (value.length>3)
	{
		$.post("execute_account.php",{action:"check_available",type:type,value:value},function(response) {
			var retvals=response.split(" ");
			if (retvals[0]=="username")
			{
				if (retvals[1]=="available")
				{
					$('#unreg_error').hide();
					$('[name=un_available]').val(1);
				}
				else {
					$('#unreg_error').text('Användarnamnet upptaget');
					$('#unreg_error').show('fast');
					$('[name=un_available]').val(0);
				}
			}
			else if (retvals[0]=="email")
			{
				if (retvals[1]=="available")
				{
					$('#emailreg_error').hide();
					$('[name=email_available]').val(1);
				}
				else {
					$('#emailreg_error').text('Epostadressen har redan ett konto');
					$('#emailreg_error').show('fast');
					$('[name=email_available]').val(0);
				}
			}
		});
	}
}

function toggle_visibility(divid) {
	if ($('#'+divid).is(":visible"))
	{
		$('#'+divid).hide('fast');
	}
	else {
		$('#'+divid).show('fast');
	}
	return false;
}

function mail_unpw(form) {
	if (validate_form(form))
	{
		alert('validerade');
	}
	return false;
}

function try_register(form) {
	if (validate_form(form))
	{
		check_availability("username",$('#unreg').val());
		check_availability("email",$('#emailreg').val());
		error=false;
		if (form.bananer.value!=form.c1.value-form.c2.value+2)
		{
			$('#bananer_error').text('Hur står det till med matten? Prova igen!');
			$('#bananer_error').show('fast');
		}
		if (/[^a-zA-Z0-9åäöÅÄÖ]/.test(form.unreg.value))
		{
			error=true;
			$('#unreg_error').text('Endast svenska bokstäver och/eller siffror i användarnamnet');
			$('#unreg_error').show('fast');
		}
		else if (form.unreg.value.length>20)
		{
			error=true;
			$('#unreg_error').text('Ditt användarnamn är för långt');
			$('#unreg_error').show('fast');
		}
		if (form.pwreg_1.value!=form.pwreg_2.value)
		{
			error=true;
			$('#pwreg_error').text('Lösenorden stämmer inte överens');
			$('#pwreg_error').show('fast');
		}
		else {
			if ($('[name=un_available]').val()==1 && $('[name=email_available]').val()==1)
			{
				$.post('execute_account.php',$(form).serialize(),function(data,status,jqxhr) {
					//alert(data);
					if (data=="ok")
					{
						$('#register_form').html('Såja! Kolla nu din mail. Ett mail har nu skickats till dig, innehållandes en länk. Klicka på den för att aktivera ditt konto.');

					}
					else {
						$('#register_form').html('Jaha nu har det hänt nån bugg här. Vänligen kontakta webmastern så ska jag reda ut det här.');
					}
				});
			}
			else {
				alert('error');
			}
		}
	}
	return false;
}

function load_div(page,query,divid) {
	if (query=="")
	{
		$('#'+divid).load('show_inc.php?p='+page);
	}
	else {
		$('#'+divid).load('show_inc.php?p='+page+'&'+query);
	}
	return false;
}

function send_mail(form,with_fancybox) {
	if (validate_form(form))
	{
		$('input[type=submit]', form).attr('disabled', 'disabled');
//		$(form,"input[type='submit']").attr("disabled","true");
		$.post('execute_mail.php', $(form).serialize(),function(data,status,jqxhr) {
			if (status=="success")
			{
				if (with_fancybox)
				{
					$.fancybox.close();
				}
				else {
					$('#mail_compose').html("Meddelande skickat.");
				}
			}
			else {
//				alert(status);
				$('input[type=submit]', form).removeAttr('disabled');
			}
		});
		return false;
	}
	return false;
}

function validate_and_send_form(form,script,reload) {
//	alert('validate and send');
	if (validate_form(form))
	{
		send_fancybox_form(form,script,reload);
	}
	return false;
}

function send_fancybox_form(form,script,reload) {
	$.post(script+'.php', $(form).serialize(),function(data,status,jqxhr) {
		if (status=="success")
		{
//			alert(data);
//			if (confirm('Debug: stäng fancyboxen?'))
//			{
				$.fancybox.close();
//			}
			if (reload)
			{
				location.reload();
			}
		}
		else {
			alert(status);
		}
	});
	return false;
}

function generate_img_descr_edit_form(currentIndex) {
	$('#fancybox-title-over').load("show_inc.php?p=form_img_descr&current_index="+currentIndex);
	return false;
}

function delete_gallery_img(currentIndex) {
	if (confirm("Ta bort denna bild från ditt galleri?"))
	{
		$.post('execute_pic_edit.php?action=delete_pic&current_index='+currentIndex,function(data,status,jqxhr) {
			if (status=="success")
			{
//				alert(data);
				$.fancybox.close();
				location.reload();
			}
			else {
				alert(status);
			}
		});
	}
	return false;
}

function validate_calendar_add_form(form) {
	$('#map_error').hide();
	if (validate_form(form))
	{
		if ($('#id_place').val()==0 && ($('#x_coord').val()+$('#y_coord').val())==0)
		{
			alert('error - måste välja plats');
			$('#map_error').text('Du måste välja en plats för evenemanget');
			$('#map_error').show('fast');
			return false;
		}
	}
	else {
		return false;
	}
}

function validate_form(form) {
	if (!form.validator)
		return true;
	retval=true;

	var lines=form.validator.value.split("|");
	for (i=0;i<lines.length ;i++ )
	{
		var chkvars=lines[i].split(";");
		//alert(lines[i]);
		var fieldObj=$('[name='+chkvars[0]+']');
		var errorObj=$('#'+chkvars[3]);
		if (errorObj)
		{
			errorObj.hide();
		}
		var val=fieldObj.val();
		if (val.length<chkvars[1])
		{
			if (errorObj)
			{
				errorObj.text(chkvars[4]);
				errorObj.show('fast');
			}
			retval=false;
		}
		switch (chkvars[2])
		{
		case "email":
			if (!validate_email(val))
			{
				if (errorObj)
				{
					errorObj.text(chkvars[4]);
					errorObj.show('fast');
				}
				retval=false;
			}
			break;
		case "date":
			//alert('kolla date');
			if (!validateDate(val,$('[name='+chkvars[5]+']').val()-1,$('[name='+chkvars[6]+']').val()))
			{
				retval=false;
				if (errorObj)
				{
					errorObj.text(chkvars[4]);
					errorObj.show('fast');
				}
			}
			break;
		case "checkbox":
			if (!fieldObj.is(':checked'))
			{
				retval=false;
				if (errorObj)
				{
					errorObj.text(chkvars[4]);
					errorObj.show('fast');
				}			
			}
			break;
		case "":
			break;
		}
	}

	return retval;
}

function validate_email(email) {
	if (email.search(/^[^@]+@[^@]+\.[a-z]{2,}$/i)==-1)
		return false;
	else
		return true;
}

function validateDate(day,month,year) {
	if (day!="")
	{
		//alert(day+' '+month+' '+year);
		var dteDate=new Date(year,month,day);
		return ((day==dteDate.getDate()) && (month==dteDate.getMonth()) && (year==dteDate.getFullYear()));
	}
	else
		return true;
}

function show_remaining_chars(field,maxlen,showid) {
	$("#"+showid).text(maxlen-$(field).val().length);
}

// Mapstuff


//var mapCropBaseX;
//var mapCropBaseY;
//var isMapCropMoving=false;

function preventDefaultEvent(e) {
	if (e && e.preventDefault)
	{
		e.preventDefault();
	}
	return false;
}

function setCropEvent(event) {
	$('#mapleft').bind('click',function(event) {
		moveCrop(event);
	});
}

function mapStartCropMov(event) {
	preventDefaultEvent(event);
//	$('#mapleft').unbind('click');
	moveCrop(event);
	$('#mapleft').bind('mousemove',function(event) {
		moveCrop(event);
	});
	return true;
}

function mapStopCropMov(event,addplaces) {
	preventDefaultEvent(event);
	$('#status').text('stop moving ');
	$('#mapleft').unbind('mousemove');
	$('#mapleft').bind('mousedown',function(event) {
		mapStartCropMov(event);
	});
	mapUpdateMain();
	return true;
}

function mapUpdateMain() {
	$('#mapposition').hide();
	$('#submit_btn').hide();

//	alert($('#mapcrop').position().top);

//	var scale_x=3960/18;
//	var scale_y=5940/$('#mapleft').height();
//	alert('left='+left+' top='+top);

	x1=$('#mapcrop').position().left*16;
	y1=$('#mapcrop').position().top*16;

	$('#x_map').val(x1);
	$('#y_map').val(y1);

	$('#xd_map').val('');
	$('#yd_map').val('');

	x2=x1+439;
	y2=y1+439;

//	alert("x1="+x1+"&y1="+y1+"&x2="+x2+"&y2="+y2);
//	$('#status').text("_img/_maps/get_map.php?w_img=440&h_img=440&x1="+x1+"&y1="+y1+"&x2="+x2+"&y2="+y2);
	
//	alert("_img/get_map.php?w_img=440&h_img=440&x1="+x1+"&y1="+y1+"&x2="+x2+"&y2="+y2);	
	$('#mapholder').css("background-image","url(_img/get_map.php?w_img=440&h_img=440&x1="+x1+"&y1="+y1+"&x2="+x2+"&y2="+y2+")");
//	alert('uppdatera stora');
}

function moveCrop(event) {
	$('#status').text('event'+event.pageX);
	if (!event)
	{
		event=window.event;
	}
	preventDefaultEvent(event);
	left=Math.round(event.pageX-$('#mapleft').offset().left-($('#mapcrop').width()/2));
	top=Math.round(event.pageY-$('#mapleft').offset().top-($('#mapcrop').height()/2));

	if (left<0)
	{
		left=0;
	}
	else if (left>$('#mapleft').width()-$('#mapcrop').width())
	{
		left=$('#mapleft').width()-$('#mapcrop').width();
	}
	if (top<0)
	{
		top=0;
	}
	else if (top>$('#mapleft').height()-$('#mapcrop').height())
	{
		top=$('#mapleft').height()-$('#mapcrop').height();
	}
	$("#mapcrop").css("left",left);
	$("#mapcrop").css("top",top);


//	$('#status').text('moving '+event.pageX+' '+event.pageY);
	return true;
}

function clickLeftMapPosition(event) {
	preventDefaultEvent(event);
	var leftLeft=Math.round(event.pageX-$('#mapleft').offset().left-12);
	var topLeft=Math.round(event.pageY-$('#mapleft').offset().top-12);
//	alert(leftLeft+' '+topLeft);
	if (leftLeft<0)
	{
		leftLeft=0;
	}
	else if (leftLeft>195)
	{
		leftLeft=195;
	}
	if (topLeft<0)
	{
		topLeft=0;
	}
	else if (topLeft>305)
	{
		topLeft=305;
	}
//	topTop=topLeft;
//	leftLeft=leftLeft;

	$("#mapcrop").css("left",leftLeft);
	$("#mapcrop").css("top",topLeft);
//	alert('top='+topTop+' left='+leftLeft);
	mapUpdateMain();
}

function clickMainMapPosition(event) {
	preventDefaultEvent(event);
	var leftMain=Math.round(event.pageX-$('#mapholder').offset().left);
	var topMain=Math.round(event.pageY-$('#mapholder').offset().top);
	$('#xd_map').val(leftMain);
	$('#yd_map').val(topMain);

	$('#mapposition').css("left",leftMain-10);
	$('#mapposition').css("top",topMain-10);

	$('#mapposition').show();
	$('#submit_btn').show();

	$('#status').text("leftMain="+leftMain+" topMain="+topMain);
}

function show_map_img(divid,id_place) {
	if (id_place>0)
	{
		$('#'+divid).css("background","url('get_uploaded_image.php?dir=maps&file=places_"+id_place+"&type=gif')");
	}
	else {
		$('#'+divid).css("background","");
		$('#x_coord').val('');
		$('#y_coord').val('');
	}
}

function try_send_mapform(form,type) {
	if ($('#xd_map').val()!='' && $('#yd_map').val()!='')
	{
		if (type=="places" || type=="calendar")
		{
			var x=parseInt($('#xd_map').val())+parseInt($('#x_map').val());
			var y=parseInt($('#yd_map').val())+parseInt($('#y_map').val());
			send_fancybox_form(form,'execute_mappick',false);
			$('#x_coord').val(x);
			$('#y_coord').val(y);
//			alert("url('_img/get_dynamic_map.php?x="+x+"&y="+y+"')");
//			alert("_img/get_dynamic_map.php?x="+x+"&y="+y);
			$('#place_map').css("background","url('_img/get_dynamic_map.php?x="+x+"&y="+y+"')");
			if (type=="calendar")
			{
				$('#id_place option[value=0]').attr('selected','selected');
			}
		}
		else if (type=="personal")
		{
			send_fancybox_form(form,'execute_mappick',true);
		}
	}
	return false;
}

function edit_gallery_pic_description(index) {
	alert(index);
}

/* Mugshot cropping */

function set_mugshot_image(id_pic,width,height) {
/*	$('#img_crop_holder').width(width);
	$('#img_crop_holder').height(height);
*/
	$('#img_crop_holder').load("show_inc.php?p=get_mugshot_croparea&id_pic="+id_pic,function(data,status,xhr) {
		if (status=="success")
		{
			mugshot_prepare(id_pic);
		}
	});
	return false;
}

function mugshot_prepare(id_pic) {
	jQuery('#cropbox').Jcrop({
		aspectRatio:1,
		minSize:	[160,160],
		onSelect:	setCropCoords
	});		
	$('#id_pic').val(id_pic);
}

function setCropCoords(c) {
	if (1+c.x2-c.x>=160 && 1+c.y2-c.y>=160)
	{
		$('#x_crop').val(c.x);
		$('#y_crop').val(c.y);
		$('#x2_crop').val(c.x2);
		$('#y2_crop').val(c.y2);
		$('#submit_crop').show();
	}
	else {
		$('#x_crop').val('');
		$('#y_crop').val('');
		$('#x2_crop').val('');
		$('#y2_crop').val('');
		$('#submit_crop').hide();
	}
}


function get_online_list() {
	$.getJSON('services/json_online.php',function(data) {
		update_online_list(data);
	});
}

function update_online_list(data) {
	// Initialisera ny onlinelista
	online_id=[];
	online_un=[];
	i=0;
	$.each(data,function(id,un) {
		online_id[i]=id;
		online_un[i]=un;
		i++;
	});

	// Gå först igenom synliga listan - ta bort de element som inte är online i arrayen
	$('.online-list li').each(function(index) {
		id_list=this.id.substring(11);

		index=jQuery.inArray(id_list,online_id);
		if (index==-1)
		{
			$(this).hide('slow',function() {
				$(this).remove();
			});
		}
		else {
			online_id[index]="";
			online_un[index]="";
		}
	});

	// Lägg till nya
	for (i=0;i<online_id.length ;i++ )
	{
		if (online_id[i]!="")
		{
			$('<li id="onlineuser_'+online_id[i]+'"><a href="?p=profile&pid='+online_id[i]+'">'+online_un[i]+'</a></li>').hide().prependTo('.online-list').slideDown("slow");
		}
	}
}

function init_check_events() {
	event_timer=setInterval("check_events()",5000);
	check_events();
}

function check_events() {
	if (!checking)
	{
		// Sätt kollflaggan
		checking=true;
		// Hämta event-JSON
		$.getJSON('services/json_events.php',function(data) {
			$.each(data,function(key,val) {
				switch(key) {
					case "mail":
						aNewItems_counter[0]=val;
						break;
					case "friends":
						aNewItems_counter[1]=val;
						break;
					case "forum":
						aNewItems_counter[2]=val;
						break;
				}
			});
			checking=false;
			setNewMessages();
		});
		// Hämta och uppdatera onlinelista
		get_online_list();
	}
}

function submenu_click(item,file,divid) {
	$('ul.menu li.on').removeClass("on");
	$(item).parent('li').addClass("on");
	$('#'+divid).load('show_inc.php?p='+file);
	return false;
}

function try_post_forum(form) {
	if (validate_form(form))
	{
//		id_thread=$('#id_thread').val()
//		alert(id_thread);
		$.post('execute_forum.php', $(form).serialize(),function(id_thread,status,jqxhr) {
			if (status=="success")
			{
//				alert(data);
				
				$('#forum_mainpanel').load('show_inc.php?p=forum_thread&id_thread='+id_thread);
			}
			else {
				alert(status);
			}
		});
	}
	return false;
}

function show_forum_cat(id_subcat) {
	$('ul.menu li.on').removeClass("on");
	if (id_subcat>0)
	{
		$('#subcat_'+id_subcat).addClass("on");
		load_div('forum_threads','id_cat='+id_subcat,'forum_mainpanel');
		$.post('execute_forum.php',{action:'set_cats',id_subcat:id_subcat});
	}
	else {
		load_div('forum_latest','','forum_mainpanel');
		$.post('execute_forum.php',{action:'set_cats',id_subcat:0});
	}
	return false;
}

// Mapstuff 2

var base_x;
var base_y;
var curr_x;
var curr_y;

function mapStartMovement(event) {
	$('#map_holder .place_dot').remove();
	preventDefaultEvent(event);
	mapOverviewShow(false);
	map_moving=true;
	base_x=event.pageX-$('#map_holder').offset().left;
	base_y=event.pageY-$('#map_holder').offset().top;
	container_top=-$('#map_container').position().top;
	container_left=-$('#map_container').position().left;

	$('#map_container').bind('mousemove',function(event) {
		mapMove(event);
	});
}

function mapStopMovement(event) {
	preventDefaultEvent(event);
	map_moving=false;
	$('#map_container').unbind('mousemove');
	update_main_map();
}

function mapMove(event) {
	preventDefaultEvent(event);
	curr_x=event.pageX-$('#map_holder').offset().left;
	curr_y=event.pageY-$('#map_holder').offset().top;
	container_left-=2*(curr_x-base_x);
	container_top-=2*(curr_y-base_y);
	if (container_left<0)
	{
		container_left=0;
	}
	else if (container_left>3090)
	{
		container_left=3090;
	}
	if (container_top<0)
	{
		container_top=0;
	}
	else if (container_top>4850)
	{
		container_top=4850;
	}

	update_container_and_box_positions();

	base_x=curr_x;
	base_y=curr_y;
}

function update_container_and_box_positions() {
	$('#map_container').css("left",-container_left);
	$('#map_container').css("top",-container_top);
	box_left=Math.round(container_left*150/3520);
	box_top=Math.round(container_top*225/5280);
	$('#map_overview_box').css("left",box_left);
	$('#map_overview_box').css("top",box_top);
}

function update_overview_box() {
	$('#map_overview_box').css("left",box_left);
	$('#map_overview_box').css("top",box_top);
}

function update_main_map() {
	$('#map_overview_holder').unbind('mousemove');
	x1=container_left;
	y1=container_top;
	x2=x1+429;
	y2=y1+429;
	check_map_tile(x1,y1);
	check_map_tile(x2,y1);
	check_map_tile(x1,y2);
	check_map_tile(x2,y2);
	set_places(x1,y1,x2,y2);
	$.post('execute_map_store_coords.php',{x:x1,y:y1});
}

function set_places(x1,y1,x2,y2) {
	$('#places_onmap_list').empty();
	$('#places_onmap_list').append('<ul>');
	$.getJSON('services/json_places.php',{ x1:x1,y1:y1,x2:x2,y2:y2},function(data) {
		$.each(data,function(id,obj) {
			_div=$('<div>');
			_div.css('top',(obj.pos_y-y1-10)+'px');
			_div.css('left',(obj.pos_x-x1-10)+'px');
			_div.attr('title',obj.name);
			_div.addClass('place_dot');
			_div.html(obj.name);
			_div.attr('id','placedot_'+obj.id);

//			_div.show();
			$('#map_holder').append(_div);
			_div.bind('click',function(event) {
				window.location='?p=place&id='+obj.id;
			});
			_div.bind('mouseover',function(event) {
				$('#placedot_'+obj.id).addClass('hilite');
				mapOverviewShow(false);
			});
			_div.bind('mouseout',function(event) {
				$('#placedot_'+obj.id).removeClass('hilite');
			});

			_ahref=$('<a>');
			_ahref.attr("href","?p=place&id="+obj.id)
			_ahref.text(obj.name);
			_li=$('<li>');
			_li.html(_ahref);

			_li.bind('mouseover',function(event) {
				$('#placedot_'+obj.id).addClass('hilite');
			});
			_li.bind('mouseout',function(event) {
				$('#placedot_'+obj.id).removeClass('hilite');
			});
			$('#places_onmap_list').append(_li);

		});
//		update_online_list(data);
	});
	$('#places_onmap_list').append('</ul>');

}

function check_map_tile(x,y) {
	xt=Math.floor(x/440);
	yt=Math.floor(y/440);
	tile=yt*8+xt;
	if (tiles_loaded[tile]!=true)
	{
		tiles_loaded[tile]=true;
		var top=yt*440;
		var left=xt*440;
		_im=$('<img>');
		_im.hide();
		_im.width(440);
		_im.height(440);
		_im.css('position','absolute');
		_im.css('top',top+'px');
		_im.css('left',left+'px');

		_im.bind("load",function() {
			$(this).show();
		});
		$('#map_container').append(_im);
		_im.attr('src','_img/_maps_tiles/map_'+yt+'_'+xt+'.gif');
	}
}

function mapOverviewShow(show) {
	if (show)
	{
		$('#map_overview_holder').show("fast");
		$('#map_overview_mini').hide("fast");
	}
	else {
		$('#map_overview_holder').hide("fast");
		$('#map_overview_mini').show("fast");
	}

}

function mapOverviewMouseDown(event) {
	preventDefaultEvent(event);
	$('#map_holder .place_dot').remove();
	$('#map_overview_holder').unbind('mousedown');
	setOverviewBoxPosition(event);
	$('#map_overview_holder').bind('mousemove',function(event) {
		setOverviewBoxPosition(event);
	});
	$('#map_overview_holder').bind('mouseup',function(event) {
		mapOverviewMouseUp(event);
	});
	$('#map_overview_holder').bind('mouseout',function(event) {
		mapOverviewMouseUp(event);
	});
}

function mapOverviewMouseUp(event) {
	preventDefaultEvent(event);
	$('#map_overview_holder').unbind('mousemove');
	$('#map_overview_holder').unbind('mouseup');
	$('#map_overview_holder').unbind('mouseout');
	$('#map_overview_holder').bind('mousedown',function(event) {
		mapOverviewMouseDown(event);
	});
	$('#map_overview_box').unbind('mousemove');
	update_main_map();
}

function setOverviewBoxPosition(event) {
	preventDefaultEvent(event);
	var overviewOffset=$('#map_overview_holder').offset();
	box_left=Math.round(event.pageX-overviewOffset.left)-8;
	box_top=Math.round(event.pageY-overviewOffset.top)-8;
	if (box_left<0)
	{
		box_left=0;
	}
	else if (box_left>131)
	{
		box_left=131;
	}
	if (box_top<0)
	{
		box_top=0;
	}
	else if (box_top>207)
	{
		box_top=207;
	}
	update_overview_box();
	container_left=Math.round(box_left*3520/150);
	container_top=Math.round(box_top*5280/225);
	$('#map_container').css("left",-container_left);
	$('#map_container').css("top",-container_top);
}

// Place search functions

function setPlaceSearchTimeout(formfield) {
	if (formfield.value.length>2)
	{

		clearTimeout(searchPlaceTimeoutId);
		searchPlaceTimeoutId=setTimeout('doPlaceSearch()',500);
	}
	else {
		$('#places_search_list').hide();
		$('#places_search_list').empty();
	}

}

function doPlaceSearch() {
	name=$('#places_search_field').val();
	$('#places_search_list').show();
	$('#places_search_list').empty();
	$.getJSON('services/json_places_name.php',{name:name},function(data) {
		$('<h3>').appendTo('#places_search_list').text("Sökresultat:");
		$('#places_search_list').append('<ul>');
		$.each(data,function(id,obj) {
			_ahref=$('<a>');
			_ahref.text(obj.name);
			_li=$('<li>');
			_li.css('cursor','pointer');
			_li.html(_ahref);
			_li.bind('click',function(event) {
				map_center_on_place(obj);
			});
			$('#places_search_list').append(_li);
		});
		$('#places_search_list').append('</ul>');
	});
}

function submit_search(target,url) {
	search=$('#searchform input[name=search_field]').val();
	$('#'+target).load('show_inc.php?p='+url+'&q='+search);

	return false;
}

function map_center_on_place(obj) {
	$('#map_holder .place_dot').remove();
	container_left=obj.pos_x-215;
	container_top=obj.pos_y-215;
	update_container_and_box_positions();
	mapOverviewShow(true);
	update_main_map();
}

function place_cat_select(catid) {
	var catObj=$('#place_cat_'+catid);
	if (catObj.hasClass("on"))
	{
		catObj.removeClass("on");
		catObj.appendTo("#panel_mapcats_off");
		$.post('execute_map_set_placecat.php',{action:"hide",catid:catid});
		$('#place_cat_all').hide();
	}
	else {
		catObj.addClass("on");
		catObj.appendTo("#panel_mapcats_on");
		$.post('execute_map_set_placecat.php',{action:"show",catid:catid});
	}
	$('#map_holder .place_dot').remove();
	update_main_map();



//	if (state)
//	{
//		$('#place_cat_'+catid).addClass("on").appendTo("#panel_mapcats_on");
//		$('#place_cat_'+catid).unbind();
//		$('#place_cat_'+catid).bind('click',function() {
//			place_cat_select(catid,false);
//		});
//	}
//	else {
//		$('#place_cat_'+catid).removeClass("on").appendTo("#panel_mapcats_off");
//		$('#place_cat_'+catid).unbind();
//		$('#place_cat_'+catid).bind('click',function() {
//			place_cat_select(catid,false);
//		});
//	}


//	alert(catObj.parent().attr("id"));
//	if ($('#place_cat_'+catid).parent())
//	{
//	}
//	if (state)
//	{
//		$('#place_cat_'+catid).addClass("on").appendTo("#panel_mapcats_on");
//	}
//	else {
//		$('#place_cat_'+catid).removeClass("on").appendTo("#panel_mapcats_off");
//	}
}

//function show_date_string(datestring,divid) {
//	$.get('services/get_date_string.php',{datestring:datestring},function(response) {
//		alert(response);
//	});
//}


