	function make_inline_editors(){
		$('.inlineedit').each(function(i){
			$(this).editable($(this).attr('url'),{
				data : "{'-2':'Very bad','-1':'Bad','0':'Neutral','1':'Good','2':'Very good','selected':'0'}",
				type : 'select',
				submit : 'Save'
			});
			$(this).mouseover(function() {
				$(this).css('background-color', '#949DFF').css('cursor', 'pointer');
			});
			$(this).mouseout(function() {
				$(this).css('background-color', '').css('cursor', 'auto');
			});
		});
	}

	var unfinishedwait = false;
	var unfinished_blockupdate=false;
	function unfinished_update(){
		//updates unfinished activities
		// and starts itself again after a delay
		if (!unfinished_blockupdate){
			unfinishedwait = true;
			$('#messages').load(message_url,function(){
				unfinishedwait = false;
			});
			setTimeout(function(){ unfinished_update() },300000);
		}
	}

	var suggestionwait = false;
	function suggestions_update(){
		if ($('#suggestions').length){
			//we are on a page with suggestions
			//starts updating suggestions every 5 minutes
			suggestionwait = true;
			$('#suggestions').load(suggestion_url,function(){
				suggestionwait = false;
			});
			setTimeout(function(){ suggestions_update() },300000);
		} else {
			//this page does not have a suggestion div
		}
	}
	nextexe=null;
	function updatefriendfeed(s,plus,obj){
		if ($('#friendfeed').length){
			if (!friendfeed_block){
				if (obj!=null){
					addloading(obj);
				}
				if (s!=0){
					selection = s;
				}
				p = p+plus;
				$('#friendfeed').load(friendfeed_url + '?p=' + p + '&s=' + selection,function(){
					make_inline_editors();
					$("[id^='feedselect_']").removeClass('feedbutton_selected');
					$('#feedselect_' + selection).addClass('feedbutton_selected');
					if (obj!=null){
						removeloading(obj);
					}
				});
				clearTimeout(nextexe);
				nextexe=setTimeout(function(){ updatefriendfeed(selection,0) },300000);
			}
		}
		return false;
	}
	

	function doajax(url,obj){
		/*
		 * adds a loading indicator to end of obj
		 * gets url via ajax call
		 * updates suggestions
		 * updates unfinished activities
		 * updates friendfeed
		 */
		addloading(obj);
		$.get(url,function(data){
			suggestions_update();
			unfinished_update();
			updatefriendfeed(selection,0,null);
			checkstillwaiting();
		});
		return false;
	}

	function checkstillwaiting(){
		if (unfinishedwait || suggestionwait){
			setTimeout(function(){ checkstillwaiting() },100);
		} else {
			removeloading();
			show_conf();
		}
	}

	function addloading(obj){
		$(obj).append('<img class="loading" src="/site_media/img/loading.gif" style="padding: 2px; vertical-align: bottom;" alt="loading"/>');
	}
	function removeloading(obj){
		if (obj==null){
			obj = $("*");
		}
		$(obj).children(".loading").remove();
	}

	function doajaxdelete(url,obj){
		conf = confirm("Do you really want to delete that?");
		if (conf){
			return doajax(url,obj);
		} else {
			return false;
		}
	}

	function show_conf(){
		if(confmessage_url){
			$('#inside_conf').load(confmessage_url,function(){
				if (!$('#inside_conf').is(":empty")){
					$('#conf_message').hide()
					$('#conf_message').slideDown(200);
					setTimeout(function(){ $('#conf_message').slideUp(200); },4000);
				}
			});
		}
	}

	function formatItem(row,pos,nr,search){
		thisparse = jQuery.parseJSON(search);
		return thisparse.disp;
	}

	function formatResult(row,search,nr){
		thisparse = jQuery.parseJSON(search);
		return thisparse.term;
	}
	
	searchac = true;
	function init_autocomplete_search(){
		if ($("#id_q").length){
			searchac=false;
			$("#id_q").autocomplete(search_url + "?autocomplete",{
				cacheLength: 1,
				delay : 200,
				max : 100,
				selectFirst: false,
				formatItem : formatItem,
				formatResult : formatResult
			});
			$("#id_q").result(handle_search_selection);
		}
	}

	function handle_search_selection(event,data,selected){
		thisparse = jQuery.parseJSON(selected);
		if (thisparse!=null){
			if (thisparse.url!=null){
				location.href=thisparse.url;
				return true;
			}
		}
		return false;
	}

	function do_search(){
		return !searchac;
	}
	
	$(document).ready(
		function (){
			make_inline_editors();
			show_conf(true);
			init_autocomplete_search();
		}
	);

	function switchcomment(boxid){
		switchstuffandstopfeed(boxid);
		$.scrollTo('#' + boxid,{
								'duration':300,
								'offset':-50
								}
		);
		return false;
	}

	function switchstuffandstopfeed(boxid){
		$('#' + boxid).toggle();
		friendfeed_block = true;
		return false;
	}

	function switchstuffandstopmessages(boxid){
		$('#' + boxid).toggle();
		unfinished_blockupdate = true;
		return false;
	}

	sfHover = function() {
		try {
			var sfEls = document.getElementById("nav").getElementsByTagName("LI");
		} catch(err) {
			return false;
		}
		
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" sfhover";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			}
		}
	}
	if (window.attachEvent) window.attachEvent("onload", sfHover);
