$(document).ready(function() {
    
    /* Expand Edit /faa/images */
    
    $("#editimgs").live("click", function() {
    	$("#hideupload").slideDown();
    	return false;
    });  
    
    /* Create Inline Ovelays */
    
    $('a.modal').live("click", function() {
    	var link = $(this).attr('href');
    	//clearoverlays();
		$('body').append("<div class='inlinecontent loading'><img src='/faa/images/closebox.png' class='close' /><div class='message'></div></div>");
		$('.inlinecontent .message').load(link, function() {
			$('.inlinecontent').removeClass('loading');
            $('.inlinecontent').css({ 'left' : ($(window).width() / 2 - ($('.inlinecontent').width() / 2)) - 20 });
            $('.inlinecontent').css({ 'margin-left' : 0 });			
			$('.inlinecontent .message').hide().slideDown(); 
		});
		return false;
	});
    
    /* Submit Inline Forms */
    
    $('.inlinecontent form').live("submit", function() {
        var link = $(this).attr('action');
        $.ajax({
            type: "POST",
            url: link,
            data: $(this).serialize(),
            dataType: "html",
            success: function(html, status) {
                $('.message').parent().slideUp();              
            }
        });        
        return false;    
    });
	
	$('.close').live("click", function() {
		$(this).parent().slideUp(function() {
			$(this).remove();
		});
	});  
	
	/* Expand Edit House */
	
	$('#editinfo').live("click", function() {
		link = $(this).attr('rel');
		$.ajax({
			type: "GET",
			url: link,
			dataType: "html",
			success: function(html, status) {
				$('.widecolumn .details').slideUp(function() {
					$('.widecolumn .details').html(html).slideDown();
				});
			}
		});
		return false;
	});
	
	/*$('#slider').s3Slider({
		timeOut: 5000
	});*/
    
    /* House Actions */
    
    $('.actions a').live("click", function() {
        button = $(this);
        link = button.attr('href');
        $.ajax({
            type: "GET",
            url: link,
            dataType: "html",
            success: function(html, status) {
                // What to do next
                if (button.hasClass('hide-remove')) {
                    button.parent().parent().fadeOut();
                } else {
                    button.addClass('ticked');
                }
            }
        });        
        return false;    
    });
    
    /**
    * Fancy users search for composing messages to multiple people.
    * Acts like Facebook, in that tpying in the input performs an
    * AJAX search for your friends, you select somebody and a new
    * hidden input is injected into the form containing the user_id
    * of said user.
    */
    
    var delayed;
    
    $('#friend-search-container').live("mouseenter", function() {
		$(this).addClass('focused');
    }).live("mouseleave", function() {
		$(this).removeClass('focused');
    });
    
    $('#friend-search').live("keyup", function() {
		clearTimeout(delayed);
		
		var $input = $(this);
		var term = $(this).val();
		var length = $(this).val().length;
		
		if (length > 0) {
			delayed = setTimeout(function() {
				$.ajax({
					url: 'http://192.168.0.50/faa/friend/search', 
					type: 'POST',
					data: {'search' : term},
					dataType: 'json',
					success: function(data) {
						$('#friend-list').show();
						$.each(data, function(i, item) {
							$('#friend-list').prepend('<li><a href="#" rel"' + item.user_id + '" class="friend-link">' + item.first_name + ' ' + item.last_name + '</a></li>');	
						});
					}
				});				
			}, 500);
		}

		return false;
    });
    
    $('.friend-link').live("click", function() {
		id = $(this).attr('rel');
		name = $(this).html();
		
		$('#friend-search-container').prepend('<span class="friend">' + name + '</span>');
		
		return false;
    });
	
});

