Whaddado.Social = Whaddado.Social || {};


Whaddado.Social.init = function()
{

     $("input[name^=update_social]").click(Whaddado.Social.updateSocial);

     $(".friend_request").live("click",Whaddado.Social.handleFriendRequest);
  
};
Whaddado.Social.handleFriendRequest = function()
{
       // check to see if the user is logged in
    var link_to_handle_friend_request = $(this).attr("href");
    $.ajax({
        type :  "GET",
        url  :  Whaddado.base_url+'action/check_if_logged_in/add_friend',
        dataType : "json",
        success: function(jsonResponse)
        {
            if (jsonResponse.is_logged == true)
            {

                $.ajax({
                    type :  'GET',
                    url  :   link_to_handle_friend_request,
                    dataType : "json",
                    success: function(jsonResponse)
                    {
                        if (jsonResponse.success)
                        {
                            $("#friend_request_"+jsonResponse.userId).hide("slow",function(){
                                $(this).html(jsonResponse.message).show("slow");
                            })

                        }
                    }
                });


            return false;
        }
        else
        {
            // show the login_register window if the sign in box div is defined
            // else redirect to the login/register page
            if (  $('#sign-in-box').length != 0 )
            {
                $("#currentUrl").val(link_to_handle_friend_request);
                $('#sign-in-box').dialog('open');
            }
            else
            {
                window.location =  Whaddado.base_url+'action/login';
            }
        }

    }
    });


    return false;
}

Whaddado.Social.updateSocial = function()
{
    social_site =  $(this).attr("name").match(/\d/)[0];    
    update_status = Whaddado.Social.notUpdateStatus;    
    if  ( $(this).val() == Whaddado.Social.updateStatus )
    {
        if (social_site == Whaddado.Social.socialSiteFacebook)
        {
             FB.Connect.showPermissionDialog("publish_stream");
        }
        
        update_status = Whaddado.Social.updateStatus;
    }
   

     var dataGET = {"social_site"   : social_site ,
                    "update_status" : update_status };
    
     $.ajax({
           type :  "POST",
           url  :  "/profile/update_social/",
           data :  dataGET,
           dataType : "json",
           success: function(jsonResponse)
           {

           }
       });
}

$(document).ready(Whaddado.Social.init);



