// Globals
var _selected_privacy_icon_id;

// On Ready
$(document).ready(function() {
    
    // Add a Friend Functionality.
    $('.add_friend').unbind().live("click", function(e) {
        e.preventDefault();
        var post_url = $(this).attr('href');
        var link_obj = $(this);
        
        $.ajax({
            type:"GET",
            url:post_url,
            success: function(dialog) {
        		var $dialog = $(dialog);
                $dialog.dialog({
                	title:"Personalize friend request",
                	width: 400,
                    modal: true,
                	open:function(){
                		$("#add_friend_form").bind("submit", function(e) {
                			e.preventDefault();
                			
                			$.ajax({
                	            type:"POST",
                	            url:post_url,
                	            data:$("#add_friend_form").serialize(),
                	            success: function(response) {
                					link_obj.after("<span class=\""+link_obj.attr("class")+"\">Request Sent</span>").remove();
                					$dialog.html(response);
                					$("#cancel_link").bind("click", function(e) {e.preventDefault();$dialog.dialog('close');});
                				}
                			});
                		});
                		$("#cancel_link").bind("click", function(e) {e.preventDefault();$dialog.dialog('close');});
                	},
                	close: function() { 
                		$dialog.dialog('destroy');
                		$dialog.remove(); 
                	}
                });
            }
        });
    });
    
    $('.approve_friend').unbind().live("click", function(e) {
        e.preventDefault();
        var post_url = $(this).attr('href');
        var link_obj = $(this);
        
        $.ajax({
            type: "POST",
            url: post_url,
            data: { approve_friend: true },
            dataType: "json",
            success: function(title_content) {
                link_obj.text('APPROVED!');
                link_obj.closest('.mmf_icon_list').fadeOut();
                friend_response_dialog(title_content);
            }
        });
    });
    
    $('.deny_friend').unbind().live("click", function(e) {
        e.preventDefault();
        var post_url = $(this).attr('href');
        var link_obj = $(this);
        
        $.ajax({
            type: "POST",
            url: post_url,
            data: { deny_friend: true },
            dataType: "json",
            success: function(title_content) {
                link_obj.text('DENIED.');
                link_obj.closest('.mmf_icon_list').fadeOut();
                friend_response_dialog(title_content);
            }
        });
        
    });
    
    $('.remove_friend').unbind().live("click", function(e) {
        e.preventDefault();
        var post_url = $(this).attr('href');
        var link_obj = $(this);
        
        $.ajax({
            type:"POST",
            url:post_url,
            data:{ get_user_info: true },
            dataType:"json",
            success: function(user_info) {
                remove_friend_prompt(user_info, post_url, link_obj);
            }
        });
    });
    
    $('.remove_from_list').unbind().live("click", function(e) {
        e.preventDefault();
        var post_url = $(this).attr('href');
        var link_obj = $(this);
        
        $.ajax({
            type : "POST",
            url : post_url,
            success : function(data) {
                link_obj.text('REMOVED');
            }
        });
    });
    
    // Cancel Friend Request.
    $('.cancel_friend').unbind().live("click", function(e) {
        e.preventDefault();
        var post_url = $(this).attr('href');
        var link_obj = $(this);
        
        $.ajax({type:"POST", url:post_url, data:{ cancel_friend_request: true }, dataType:"json",
            success: function(title_content) {
                link_obj.text('FRIEND REQUEST CANCELED.');
                link_obj.closest('.mmf_icon_list').fadeOut();
                friend_response_dialog(title_content);
            }
        });
    });
    
});

function remove_friend_prompt(user_info, post_url, link_obj) {
    var remove_friend_dialog = $('<div style="font-size:14px;">Are you sure you want to remove '+user_info.content + ' as your friend?</div>')
        .dialog({
            autoOpen: false,
            title: 'Remove Friend',
            buttons: {
                "Remove Friend":function() {
                    $(this).dialog("close");
                    $.ajax({ type:"POST", url:post_url, data:{ remove_friend: true }, dataType:"json",
                        success: function(title_content) {
                            link_obj.text('REMOVED.');
                            link_obj.closest('.mmf_icon_list').fadeOut();
                            friend_response_dialog(title_content);
                        }
                    });
                },
                "Cancel":function() { 
                    $(this).dialog("close"); 
                }
            },
			width: 500,
			zIndex: 9999,
			modal: true,
            close: function(event, ui) { $(this).dialog('destroy');$(this).remove(); }
        });
    remove_friend_dialog.dialog('open');
}

function friend_response_dialog(title_content){
    var response_dialog = $('<div style="font-size:14px;">' + title_content.content + '</div>')
        .dialog({
            autoOpen: false,
            title: title_content.title,
            buttons: {"Close Window":function() { $(this).dialog("close"); } },
            width: 420,
            modal: true,
            close: function(event, ui) { $(this).dialog('destroy');$(this).remove();}
        });
    response_dialog.dialog('open');
}

function togglePrivacy(content_type_id, object_id, set_privacy_to){

    set_privacy = set_privacy_to | '';
    if (set_privacy === 0) {
        set_privacy = '';
    }

    _selected_privacy_icon_id = "privacy_icon_" + content_type_id + "_" + object_id;

    $.ajax({
        url: _MMF_GLOBAL.page_urls.privacy_toggle, 
        data: { 
            content_type_id: content_type_id,
            object_id: object_id,
            set_privacy_to: set_privacy
        },
        success: function(data, textStatus, XMLHttpRequest){
            try {
                var privacy_setting = data.output.privacy_setting;
                if (privacy_setting === 0) {
                    $("#"+_selected_privacy_icon_id).attr("class", "mmf_icon mmf_private_icon");
                    $("#"+_selected_privacy_icon_id).attr("title", "Private - Click to Toggle Privacy Setting");
                } else if (privacy_setting === 3) {
                    $("#"+_selected_privacy_icon_id).attr("class", "mmf_icon mmf_public_icon");
                    $("#"+_selected_privacy_icon_id).attr("title", "Public - Click to Toggle Privacy Setting");
                } else if (privacy_setting === 1) {
                    $("#"+_selected_privacy_icon_id).attr("class", "mmf_icon mmf_private_friend_icon");
                    $("#"+_selected_privacy_icon_id).attr("title", "Friends Only - Click to Toggle Privacy Setting");
                } else if (privacy_setting === 6) {
                  $("#"+_selected_privacy_icon_id).attr("class", "mmf_icon mmf_accesible_icon");
                  $("#"+_selected_privacy_icon_id).attr("title", "Accessible - Click to Toggle Privacy Setting");
                }
            } catch(err) {
                mmfDialog(_MMF_GLOBAL.text.privacy_toggle_error_title, _MMF_GLOBAL.text.privacy_toggle_error_msg, "error");
            }
            
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            mmfDialog(_MMF_GLOBAL.text.privacy_toggle_error_title, _MMF_GLOBAL.text.privacy_toggle_error_msg, "error");
        }
    });
   
}




