$(document).ready( function() {
	function call( elem ) {
		elem.text("...");
		$.getJSON( elem.attr('href'), function( data ) {
			if ( data ) {
				elem.replaceWith("request sent");
			} else {
				elem.replaceWith("request failed");
			}
			return data;
		});
	}
	
    $("a.del").click( function(e) {
        e.preventDefault();
        var title = $(this).attr('title');
        if (!confirm( title?title:"Really delete item?")) return false;
        $.post(this.href);
        $(this).parents("*[id]:first").fadeOut("slow");
    });
    $("a.toggle").click( function( e ) { 
    	e.preventDefault(); 
    	$(this).next().slideToggle(); 
    });
    
    $("a.request").click( function( e ) {
    	e.preventDefault(); 
    	var elem = $(this);
		var title = elem.attr('title');
		if (!confirm( title?title:"Really request item?")) return false;
		return call( elem );
    });
    
    $("a.call").click( function( e ) {
    	e.preventDefault(); 
    	var elem = $(this);
		call( elem );
		elem.parent().remove();
    });
    
    $("a.warn").click( function( e ) {
    	$(this).toggleClass('inlet-open');
    	var theform = $(this).next();
    	theform.toggle('fast');
    	return false;    	
    });
    $("a.warn").next().submit( function() {
    	var form = $(this);
    	var link = form.prev();    	
    	var comment = $("textarea", form).val();
    	$.post(
    			form.attr('action'), 
    			{ comment : comment },
    			function( resp ) {
    				if (resp) link.text( parseInt(link.text(), 10)+1 );
    			},
    			'json'
    	);
    	link.click();    	
    	return false;
    });
    
    
    $("div#vote a").click( function( e ) {
		e.preventDefault();
		var elem = $(this);
		$.getJSON( elem.attr('href'), function( data ) {
			if ( data ) {
				elem.next().text( parseInt(elem.next().text())+1 );
			}
		});
    });
});