UserIndex = {
	init: function(){
		$('ul.profile_tiles li').hover(UserIndex.toggle_profile_tile_hover, UserIndex.toggle_profile_tile_hover);
	},
	toggle_profile_tile_hover: function(e){
		$(this).toggleClass("hover");
		e.preventDefault();
	}
}

$(document).ready(UserIndex.init);


Message = {
  init: function(){
    if (document.getElementById("missive_receiver")) {
	    //$('#missive_receiver').val('');
	    $('#missive_receiver').autocomplete('/dashboard/messages/autocomplete/', {
	      	minChars: 1,
			autoFill: true
	    });
	}
  }
}

$(document).ready(Message.init);

Profile = {
	init: function(){
	    if (!logged_in)
		    $('a.login_required').click(function(){
			    $(this).overlay({});
			    return false;
		    });
		$('#profile_bio').click(Profile.send_bio_request);
		$('#profile_bio_music').click(Profile.send_bio_request);
	},
	send_bio_request:function(){
		if ($(this).hasClass("login_required"))
			return false
		var what = "#" + this.id.replace('profile_','') + "_form";
		var params = $(what + ' form').serialize();
		$.post("/dashboard/messages/compose", params, function(data){
			$(what).html(data)
			// if (data.indexOf("message sent") == -1)
			// 	$(what).html("<p>Unable to send message.</p>");
			// else
			// 	$(what).html("<p>Message sent!</p>");
		});
		return false;
	},
	do_fan_request: function(link, url, callback){
	    var div = jQuery(link).parent('div.friend_ignore');
	    if (!div.hasClass('requested')) {
	        div.addClass('requested');
	        div.addClass('progress');
	        jQuery.post(url, {authenticity_token: jQuery('#content input[name=authenticity_token]').val()}, function(response){
    	        if (response == "success") {
    	            callback.apply();
    	        }
    	        div.removeClass('progress');
	        });
	    }
	    return false;
	},
	confirm_friend: function(id, link, is_fan_page){
	    var url = "/dashboard/friends/create/" + id + "?context=module"; // context is module because we want the same thing to happen on back end
	    Profile.do_fan_request(link, url, function(){
	        var fan_tile = jQuery('#fan_' + id);
	        fan_tile.addClass('friended');	 
	        if (!is_fan_page) {
    	        var friend_tile = jQuery(document.createElement("LI")),
    	            image_tile = jQuery('#fan_' + id + ' div.rounded').html();
    	        friend_tile
    	            .attr("id", "friend_" + id)
    	            .addClass("rounded")
    	            .html(image_tile);
    	        if (jQuery('#friends li').size() < 1) {
    	            jQuery('#friends').html('').append("<ul class='thumbs'></ul>");
    	        }
    	        jQuery('#friends ul').prepend(friend_tile);
    	        Profile.move_fan_to_friends();
	        }
	    });
	},
	ignore_friend: function(id, link, context){
	    var url = "/dashboard/friends/ignore/" + id;
	    Profile.do_fan_request(link, url, function(){
	        Profile.decrement_fan_count();
	        jQuery('#fan_' + id).fadeOut();
	    });
	},
	move_fan_to_friends : function(){
	    Profile.decrement_fan_count();
	    Profile.increment_friend_count();
	},
	increment_friend_count: function(){
	    var friends = jQuery('#friends_link');
	    if (friends.html().replace(/\s+/,"").length > 0) {
	        friends = friends.children("span");
	        var friends_count = parseInt(friends.html()) + 1;
    	    friends.html(friends_count);
	    } else {
	        friends.html('See all <span>1</span> friends');
	    }
	},
	decrement_fan_count: function(){
	    var fans = jQuery('#fans_link span'), 
	        fan_count = parseInt(fans.html());
	    fans.html(fan_count-1);
	}
}

$(document).ready(Profile.init);