function display_message(tag, message){
	var li = $("<li class=\""+tag+"\">"+message+"</li>").hide();
	$("#messages ul").append(li);
	li.fadeIn();

	if(tag=="success"){
	    setTimeout(function() {
	    	li.fadeOut(null, function() {
	        	li.remove();
	    	});
	    }, 3000);		
	}
}


$("#messages li").live("click", function(){
	$(this).remove()
});


$(document).ready(function()
{

    $.ajaxSetup({
        beforeSend: function(xhr, settings) {
            if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
                // Only send the token to relative URLs i.e. locally.
                xhr.setRequestHeader("X-CSRFToken",
                $("input[name=csrfmiddlewaretoken]").val());
            }
        }
    });
    
    
   /* $.extend($.ui.dialog.prototype.options, {
        modal: true,
        draggable: false,
        resizable: false,
        show: 'fade',
        hide: 'fade',
        width: '580px',        
        close: function(event, ui){$(this).remove();},
    }); */

    $("embed").attr("wmode", "opaque").wrap('<div>'); 

	$(".auto-hide").fadeTo(0, 0);
	
	$(".auto-hide-container").hover(function(){
		$(this).find(".auto-hide").fadeTo("fast", 1);
	},function(){
		$(this).find(".auto-hide").fadeTo("fast", 0);		
	});
	       
       
	var search_form_delay;

	$("#search_form input").keypress(function(){
		clearTimeout(search_form_delay);
		search_form_delay = setTimeout(function(){
			$.ajax({
				type: "POST",
				url: "/dialogs/autocomplete",
				data: JSON.stringify($("#search_form").serializeObject()),
				dataType: 'json',
				contentType: "application/json; charset=utf-8",
				success: function(data){
					if(data.length > 0)
					{
						var content = "<ul>";
						for (entry in data)
						{
							content += "<li><a href=\""+data[entry]["url"]+"\"><div class=\"icon small\"><div class=\"logo\">";
							if(data[entry]["logo"]){
							    content += "<img src=\""+data[entry]["logo"]+"\">";
							}
							content +="</div></div></a><h3><a href=\""+data[entry]["url"]+"\">"+data[entry]["title"]+"</a></h3>"+gettext(data[entry]["category"])+"</li>"; 		
						}			
						content += "</ul>";
						if (data.length>=5)
						{
						    content += "<p><a href=\"/search/?query="+escape($("#search_form input").val())+"\">"+gettext("Show all results")+"</a></p>";
						}
						$("#search_form input").popover({
							id: "search_form_popover",
							header: $("<div>Suggestions</div>"),
							content: $(content)
						}).trigger("showPopover");										
					}
					else
					{
						$("#search_form_popover").remove();
					}
					
				}
			});
		}, 500);
	});    
	
	$("#search_form").submit(function(){
		document.location = ('/' + encodeURIComponent($("#search_form input").val()));
		return false;
	});  
       
       
	$("#show_activity_popover").click(function(){
		$.ajax({
			type: "GET",
			url: "/activity",
			dataType: 'json',
			contentType: "application/json; charset=utf-8",
			success: function(data){
				var content;
				if(data.length > 0)
				{
					content = "<ul>";
					for (action in data)
					{
						content += "<li><h3><a href=\""+data[action]["url"]+"\">"+data[action]["actor"]+" "+gettext(data[action]["verb"])+" "+data[action]["target"]+"</a></h3><sup>"+data[action]["timestamp"]+" ago</sup></li>"; 		
					}			
					content += "</ul>";	
				}
				content += "<p><a href=\"/activity\">"+gettext("Show all activity")+"</a></p>";	
				$("#show_activity_popover").popover({
					id: "activity_popover",
					header: $("<div>Activity</div>"),
					content: $(content)
				}).trigger("showPopover");				
			}
		});
	});  
	
	
	$("#show_user_popover").click(function(){

				content = "<ul>";
				content += "<li><a href=\"/accounts/password/change/\">"+gettext("Change password")+"</a></li>"; 
				content += "<li><a href=\"/accounts/logout/\">"+gettext("Log out")+"</a></li>"; 		
			    content += "</ul>";	

				$("#show_user_popover").popover({
					id: "user_popover",
					header: $("<div>User</div>"),
					content: $(content)
				}).trigger("showPopover");				

	});	
	
	
     
       
        
    $("#show_add_entry_dialog").click(function(){
        /*$("<div id=\"add_entry_dialog\"></div>").load("/dialogs/add-entry").dialog({
            title: gettext("Add an entry"),
            buttons: [{
                text: gettext("Submit"),
                click: function(){
                    $.ajax({
                        type: "POST",
                        url: "/dialogs/add-entry",
                        data: JSON.stringify($("#add_entry_form").serializeObject()),
                        dataType: 'json',
                        contentType: "application/json; charset=utf-8",
                        success: function(){
                            $("#add_entry_dialog").dialog("close");
                            display_message("success", gettext("Entry submitted successfully"));                            
                        },
                        error: function(xhr){
                            $("#add_entry_dialog div.error").remove();
                            $("#add_entry_dialog .error").removeClass("error");                                 
                            $.each($.parseJSON(xhr.responseText), function(field_id, error_message)
                            {
                               $("#id_"+field_id).addClass("error").after("<div class=\"error\">"+error_message+"</div>");
                            });
                        }                        
                    });
                }
            }]
        });*/
       if(is_authenticated)
       {
           $.overlay({ajax:"/dialogs/add-entry"});    
       }
       else
       {
           $("#show_login_dialog").click();
       }     
    }); 
    
    
    $("#show_login_dialog").click(function(){
       document.location.href=("/accounts/login/?next="+encodeURIComponent(document.location.href))
       //$.overlay({ajax:"/account/login"});         
    }); 
    

    $("#show_signup_dialog").click(function(){
       document.location.href=("/accounts/login/?next="+encodeURIComponent(document.location.href))
    });        
     
    
});
