/*
* The facebook_onload statement is printed out in the PHP. If the user's logged in
* status has changed since the last page load, then refresh the page to pick up
* the change.
*
* This helps enforce the concept of "single sign on", so that if a user is signed into
* Facebook when they visit your site, they will be automatically logged in -
* without any need to click the login button.
*
* @param already_logged_into_facebook  reports whether the server thinks the user
*                                      is logged in, based on their cookies
*
*/
function facebook_onload(already_logged_into_facebook) {
    // user state is either: has a session, or does not.
    // if the state has changed, detect that and reload.
    FB.ensureInit(function() {
        FB.Facebook.get_sessionState().waitUntilReady(function(session) {
            var is_now_logged_into_facebook = session ? true : false;

            // if the new state is the same as the old (i.e., nothing changed)
            // then do nothing
            if (is_now_logged_into_facebook == already_logged_into_facebook) {

                return;
            }

            // otherwise, refresh to pick up the state change
            refresh_page();
        });
    });
}

/*
* Our <fb:login-button> specifies this function in its onlogin attribute,
* which is triggered after the user authenticates the app in the Connect
* dialog and the Facebook session has been set in the cookies.
*/
function facebook_onlogin_ready() {
    // In this app, we redirect the user back to index.php. The server will read
    // the cookie and see that the user is logged in, and will deliver a new page
    // with content appropriate for a logged-in user.
    //
    // However, a more complex app could use this function to do AJAX calls
    // and/or in-place replacement of page contents to avoid a full page refresh.
    refresh_page();
}

/*
* Do a page refresh after login state changes.
* This is the easiest but not the only way to pick up changes.
* If you have a small amount of Facebook-specific content on a large page,
* then you could change it in Javascript without refresh.
*/
function refresh_page() {
    var i = 0;
    var sessionkey = FB.Facebook.apiClient.get_session() ? FB.Facebook.apiClient.get_session().session_key : null;
    var uid = FB.Facebook.apiClient.get_session() ? FB.Facebook.apiClient.get_session().uid : null;
    //    for (i = 0; i <= sessionkey.toString().length; i++) {
    //        if (sessionkey.toString().indexOf(".") != "-1") {
    //           sessionkey = sessionkey.replace(".","^");


    //        }
    //    }



    //window.location = 'open_login/1011409250/3.frPKSRBEkZ80kBi3Yxd3ug__.86400.1230019200-1011409250';
    RedirectPagevalue_1('/open_login/' + uid + '/' + sessionkey);


}


/*
* Prompts the user to grant a permission to the application.
*/
function facebook_prompt_permission(permission) {
    FB.ensureInit(function() {
        FB.Connect.showPermissionDialog(permission);
    });
}

/*
* Show the feed form. This would be typically called in response to the
* onclick handler of a "Publish" button, or in the onload event after
* the user submits a form with info that should be published.
*
*/
function facebook_feeds(temp_id, temp_data, temp_data1, fulltext, imgsrc, image_href) {
  
   

    FB.ensureInit(function() {
        FB.Connect.get_status().waitUntilReady(function(status) {
            if (status != FB.ConnectState.userNotLoggedIn) {
                // If the user is currently logged into Facebook, but has not
                // authorized the app, then go ahead and show them the feed dialog + upsell

                var temp_data_rep = temp_data.replace(/~/g, "'");
                var temp_data1_rep = temp_data1.replace(/~/g, "'");
                var fulltext_rep = fulltext.replace(/~/g, "'");
                var image_href_rep = image_href.replace(/~/g, "'");
                
                var comment_data = { "company": temp_data_rep, "city": temp_data1_rep, "FullText": fulltext_rep, "images": [{ "src": imgsrc, "href": image_href_rep}] };
                FB.Connect.showFeedDialog(temp_id, comment_data, null, null, null, FB.RequireConnect.promptConnect);
                return false;

            }
        });
    });
}


function popupFacebookInvite(userid) {

    FB.ensureInit(function() {
        FB.Connect.get_status().waitUntilReady(function(status) {
            if (status != FB.ConnectState.userNotLoggedIn) {
                // If the user is currently logged into Facebook, but has not
                // authorized the app, then go ahead and show them the feed dialog + upsell
                FB.IFrameUtil.CanvasUtilServer.run(true);
                var _d2 = document.createElement("div");
                _d2.setAttribute("iframeHeight", "460px");
                _d2.setAttribute("iframeWidth", "630px");
                var _d3 = new FB.UI.PopupDialog("Invite Your Friends to Join Filmjamr", _d2, false, false);
                _d3.setContentWidth(630);
                _d3.setContentHeight(560);
                _d3.set_placement(FB.UI.PopupPlacement.center);
                _d2.setAttribute("fbml", "<fb:fbml>" + "<fb:request-form style=\"width:630px; height:560px;\" action=\"" + document.location.href + "\"\tmethod=\"POST\" invite=\"false\" type=\"The Filmjamr Friend\" " + "content=\"I'd like to add you as a friend on Filmjamr: <fb:req-choice url='http://www.filmjamr.com/invite/registration/" + userid + "' label='Confirm' />\">" + "<fb:multi-friend-selector\tshowborder=\"false\" actiontext=\"Invite your friends to Filmjamr\" rows= \"5\" bypass=\"cancel\"\tshowborder=\"false\" />" + "</fb:request-form>" + "</fb:fbml>");
                _d3.show();
                FB_RequireFeatures(["XFBML"], function() {
                    var _d4 = new FB.XFBML.ServerFbml(_d2);
                    FB.XFBML.Host.addElement(_d4);

                });

            }
        });
    });

}




function handle_logout() {


    FB.Connect.logoutAndRedirect("/Logout");
}



function facebook_login_popup(value) {


    switch (value) {

        case "show":
            {
                document.getElementById("Associate_filmjamr_facebook").style.display = 'none';
                document.getElementById("comm_user_welcome_div").style.display = 'block';
                BackDiv_call();
                return false;
                break;
            }
        case "Film_user":
            {


                
                document.getElementById("ctl00_ContentPlaceHolder1_lbl_eror").value = "";
                document.getElementById("ctl00_ContentPlaceHolder1_Text_password").value = "";
                document.getElementById("comm_user_welcome_div").style.display = 'none';
                document.getElementById("Associate_filmjamr_facebook").style.display = 'block';
                
                
                BackDiv_call();
                return false;
                break;
            }
        case "Film_user_back":
            {
                document.getElementById("ALogcNeedCont").style.display = 'none';
                document.getElementById("ctl00_ContentPlaceHolder1_Text_password").value = "";
                document.getElementById("Associate_filmjamr_facebook").style.display = 'none';
                document.getElementById("comm_user_welcome_div").style.display = 'block';


                BackDiv_call();
                return false;
                break;
            }
//        case "enter":
//            {

//                if (document.getElementById("ctl00_ContentPlaceHolder1_Fb_FUsername").value == "") {
//                    alert("Please enter user name.")
//                    return false;
//                }

//                else if (document.getElementById("ctl00_ContentPlaceHolder1_chk_box").checked == false) {
//                    alert("Please check term and services.")
//                    return false;
//                }
//                break;
//            }
        case "close":
            {
                if (document.getElementById("comm_user_welcome_div") == null) {

                    document.getElementById("comm_popup_username").style.display = 'none';
                }
//                else if (document.getElementById("comm_popup_username") == null) {
//                    document.getElementById("comm_user_welcome_div").style.display = 'none';
//                }

                document.getElementById("BackDIV").style.display = 'none';
                //FB.Connect.logoutAndRedirect("/signin");
                // return false;
                break;
            }
        default:
            {
                document.getElementById("comm_user_welcome_div").style.display = 'block';
//                document.getElementById("comm_popup_username").style.display = 'block';
                return false;
                break;
            }


    }



}



function Reassociation_popup(value) {

    if (value == "close") {
        document.getElementById("Reassociation").style.display = 'none';
        document.getElementById("BackDIV").style.display = 'none';
        FB.Connect.logoutAndRedirect("/signin");

    }
    else if (value == "fb_unlink") {


        document.getElementById("facebook_unlink").style.display = 'block';
        BackDiv_call();
        return false;
    }
    else if (value == "fb_unlink_ok") {
        document.getElementById("facebook_unlink").style.display = 'none';
        document.getElementById("BackDIV").style.display = 'none';
        return false;

    }
    else {
        document.getElementById("Reassociation").style.display = 'block';
        BackDiv_call();
        return false;
    }


}