﻿/*
* Facebook Connect JavaScript Library 
* 
* Date: 2009-02-23 20:42:16 -0500 (Mon, 23 Feb 2009)
* Revision: 1.0
*
* Author: Joshua Drew - jd@storyworldwide.com
*
* Description: 
*/

//Name : UpdateAfterConnect
//Description : After user has registered they will be prompeted to link their Facebook account with their NPL account
//From Page : My Challenge / Status
function UpdateAfterConnectMyChallenge() 
{
    var uid = FB.Facebook.apiClient.get_session().uid;
    var nid = $.cookie("npl_settings");
    var url = "/Services/PureLifeService.asmx/Link";
    var dataToBeSent = "{var1: '" + uid + "', var2: '" + nid + "'}";
    
    $.ajax({
        type: "POST",
        url: url,
        data: dataToBeSent,
        contentType: "application/json; charset=utf-8",
        dataType: "json"        
    });
    
    var uid = FB.Facebook.apiClient.get_session().uid;
    var sql = "SELECT first_name, name, current_location, pic_with_logo FROM user WHERE uid = " + uid; 
    var name = "";
    var location = "";
    var fname = "";
    var pic = "";
    FB.Facebook.apiClient.fql_query(sql,function(result, ex){
        pic = result[0]['pic_with_logo'];
        fname = result[0]['first_name'];
        name = result[0]['name'];
        location = result[0]['current_location']['city'] + ', ' + result[0]['current_location']['state'];
        $("#ctl00_ContentPlaceHolder1_lblFname").html(fname);    
        $("#ctl00_ContentPlaceHolder1_lblFname2").html(fname);
        $("#ctl00_ContentPlaceHolder1_ProfileInfo1_lblFname2").html(fname);
        $("#profile-name").html(name);
        
        if (location.indexOf("undefined",0) < 0) {
           $("#profile-address").html(location);
        }
        
        $("#profile-info-img").html(
            "<img src=\""+pic+"\" style=\"margin: 0pt auto; display: block;\" />"
        );
        
    });

    $("#profile-facebookconnect").html("Connected to Facebook");
    $("#profile-facebookconnect-link").css({ display: 'block' });
       
    
    FB.XFBML.Host.parseDomTree();               
}
//Name : UpdateAfterConnectRegistration
//Description : After user has registered they will be prompeted to link their Facebook account with their NPL account
//Called From : Challenge Set Up
function UpdateAfterConnectRegistration() 
{
    
    var uid = FB.Facebook.apiClient.get_session().uid;
    var nid = $.cookie("npl_settings");
    var url = "/Services/PureLifeService.asmx/Link";
    var dataToBeSent = "{var1: '" + uid + "', var2: '" + nid + "'}";
    
    $.ajax({
        type: "POST",
        url: url,
        data: dataToBeSent,
        contentType: "application/json; charset=utf-8",
        dataType: "json"        
    });

    $("#begin-challenge-facebook").html( 
        "<span>" + 
        "Welcome, <fb:name uid=loggedinuser useyou=false></fb:name>. <br />You are signed in with your Facebook account." + 
        "</span>"); 
    
    $("#profile-facebookconnect").html("Connected to Facebook");
    
    var uid = FB.Facebook.apiClient.get_session().uid;
    var sql = "SELECT name, current_location FROM user WHERE uid = " + uid; 
    FB.Facebook.apiClient.fql_query(sql,function(result, ex){
        var name = result[0]['name'];
        var location = result[0]['current_location']['city'] + ', ' + result[0]['current_location']['state'];
        $("#profile-name").html(name);
        
        if (location.indexOf("undefined",0) < 0) {
           $("#profile-address").html(location);
        }
    });
    
    $("#profile-info-img").html(
        "<fb:profile-pic size=small uid=loggedinuser facebook-logo=true></fb:profile-pic>"
        );
    
    FB.XFBML.Host.parseDomTree();  
    
    
    FB.ensureInit(function () { 
        var comment_data = {"images":[{'src':'http://'+gbl_host+'/images/facebook/logo1.jpg', 'href':'http://www.purelifebestlife.com'}] }; 
        FB.Connect.showFeedDialog(gbl_regfeedid, comment_data, null, null, null, FB.RequireConnect.promptConnect); 
    });
    
}
//Name : UpdateAfterConnect
//Description : 
//Called From : 
function UpdateAfterConnect() 
{
    var uid = FB.Facebook.apiClient.get_session().uid;
    var nid = $.cookie("npl_settings");
    var url = "/Services/PureLifeService.asmx/Link";
    var dataToBeSent = "{var1: '" + uid + "', var2: '" + nid + "'}";
    
    $.ajax({
        type: "POST",
        url: url,
        data: dataToBeSent,
        contentType: "application/json; charset=utf-8",
        dataType: "json"        
    });

    $("#begin-challenge-facebook").html( 
        "<span>" + 
        "Welcome, <fb:name uid=loggedinuser useyou=false></fb:name>. <br />You are signed in with your Facebook account." + 
        "</span>"); 
    
    $("#profile-facebookconnect").html("Connected to Facebook");
    
    var uid = FB.Facebook.apiClient.get_session().uid;
    var sql = "SELECT name, current_location FROM user WHERE uid = " + uid; 
    FB.Facebook.apiClient.fql_query(sql,function(result, ex){
        var name = result[0]['name'];
        var location = result[0]['current_location']['city'] + ', ' + result[0]['current_location']['state'];
        $("#profile-name").html(name);
        if (location.length > 0){
            $("#profile-address").html(location);
        }
    });
    
    $("#profile-info-img").html(
        "<fb:profile-pic size=small uid=loggedinuser facebook-logo=true></fb:profile-pic>"
        );
    
    FB.XFBML.Host.parseDomTree();  
    
    
    //FB.ensureInit(function () { 
    //    var comment_data = {"images":[{'src':'http://'+gbl_host+'/images/facebook/logo1.jpg', 'href':'http://www.purelifebestlife.com'}] }; 
    //    FB.Connect.showFeedDialog(gbl_regfeedid, comment_data, null, null, null, FB.RequireConnect.promptConnect); 
    //});
    
}
//Name : UpdateAfterConnectLogin
//Description: Function is called AFTER the user logins via Facebook Connect.  
//Called From : From Home Page
function UpdateAfterConnectLogin() 
{    
    //Lets make sure they are logged in
    var nid = $.cookie("npl_settings");
    var url = "/Services/PureLifeService.asmx/IsLinked";
    if (nid != null) 
    {
        //this means there is a FB & NPL cookie.
        $.ajax({
            type: "POST",
            url: url,
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(response) {
                if (response.d == "true"){
                    var pic = "";
                    var uid = FB.Facebook.apiClient.get_session().uid;
                    var sql = "SELECT pic_square_with_logo FROM user WHERE uid = " + uid; 
                    FB.Facebook.apiClient.fql_query(sql,function(result, ex){
                        pic = result[0]['pic_square_with_logo'];                        
                        
                        $("#login-content").html( 
                            "<div id=\"imgbox\" style=\"float:left;\">" + 
                                "<img src=\""+ pic +"\" />" + 
                            "</div>" + 
                            "<div style=\"float:left; width:150px; margin-left:5px;\">" + 
                                "<span class=\"grey-text med\">" + 
                                "Welcome, <fb:name uid=loggedinuser useyou=false></fb:name>. <br />You are signed in with your Facebook account.<br />" + 
                                "</span>" + 
                                "<a href=\"/Profile/My-Status.aspx\" class=\"blue-text med\">Manage My Challenges</a>" +
                            "</div>"                            
                        ); 
            
                        FB.XFBML.Host.parseDomTree(); 
                    });
                    
                    $("#utility-nav").css("width", "300px"); 
                   
                }
            
            }      
        });
        
    }
}
//Name : ProcessConnectLogin
//Description: Function is called AFTER the user logins via Facebook Connect to determine if user is registred or not.
//Called From: FB Connect Button on home page
function ProcessConnectLogin() 
{
    window.location = "/Facebook/ProcessLogin.aspx";
}
//Name: ProcessConnectLogout
//Description: Logs the user OUT of NPLBL & FB
//Called From: Logout Link in utility nav
function ProcessConnectLogout()
{
    var url = "/Services/PureLifeService.asmx/Logout";
    $.ajax({
        type: "POST",
        url: url,
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json"  
    });
    
    FB.Connect.logoutAndRedirect("/Home.aspx");
   
}

//Name: PublishFeedAfterPillarComplete
//Description: Opens up wall feed when user is completes pillar
function PublishFeedAfterPillarComplete()
{
    var imgDR = "http://www.purelifebestlife.com/images/facebook/Drink-Right.gif";
    var imgER = "http://www.purelifebestlife.com/images/facebook/Eat-Right.gif";
    var imgGA = "http://www.purelifebestlife.com/images/facebook/Get-Active.gif";
    var imgGG = "http://www.purelifebestlife.com/images/facebook/Get-Green.gif";
    var imghref = "http://www.purelifebestlife.com";
    
    var imagesArray = [{ "src": "http://www.purelifebestlife.com/images/facebook/logo1.jpg", "href": imghref },
    		    { "src": imgDR, "href": imghref },
    		    { "src": imgER, "href": imghref },
    		    { "src": imgGA, "href": imghref },
    		    { "src": imgGG, "href": imghref}];

    pillar = "|Drink Right|Eat Right|Get Active|Get Green";

    if ($("#imgbadge-get-green").length < 1) {
        imagesArray.splice(4, 1);
        pillar = pillar.replace("|Get Green", "");
    }
    if ($("#imgbadge-get-active").length < 1) {
        imagesArray.splice(3, 1);
        pillar = pillar.replace("|Get Active", "");
    }
    if ($("#imgbadge-eat-right").length < 1) {
        imagesArray.splice(2, 1);
        pillar = pillar.replace("|Eat Right", "");
    }
    if ($("#imgbadge-drink-right").length < 1) {
        imagesArray.splice(1, 1);
        pillar = pillar.replace("|Drink Right", "");
    }

    if (pillar.charAt(0) == "|")
        pillar = pillar.substring(1, pillar.length);

    if (pillar.split('|').length == 2) {
        pillar = pillar.replace("|", " and ");
    }
    else {
        var lastIndex = pillar.lastIndexOf("|");
        pillar = pillar.substring(0, lastIndex) + " and " + pillar.substring(lastIndex + 1, pillar.length);

        while (pillar.indexOf("|") != -1) {
            pillar = pillar.replace("|", ", ");
        }        
    }

    FB.ensureInit(function() {
        var pillarbadge = pillar;
        var comment_data = { "pillar": pillar, "badge": pillar + " badge", "images": imagesArray };
        FB.Connect.showFeedDialog(gbl_pillarfeedid, comment_data, null, null, null, FB.RequireConnect.promptConnect, null, "Tell your friends of your accomplishment");

    });
}

//Name: PublishFeedMotivationMessage
//Description: Opens up wall feed for the user to post to a friends wall
function PublishFeedMotivationMessage(friendid)
{
    var url = location.protocol + "//" + location.host + "/Profile/My-Friends.aspx";
    var url2 = location.protocol + "//" + location.host;

    var comment_data = {"images":[{"src":"http://www.purelifebestlife.com/images/facebook/fb_post_thumbsUP.jpg", "href":"http://www.purelifebestlife.com"}, {"src": "http://www.purelifebestlife.com/images/facebook/fb_post_icon.jpg", "href":"http://www.purelifebestlife.com"}]}

    FB.ensureInit(function() {
        FB.Connect.showFeedDialog('97250817793', comment_data, null, friendid, null, FB.RequireConnect.promptConnect, null, "Tell your friends of your accomplishment.");
    });
}

//Name: InviteFriends
//Description: Opens up dialog to invite friends
function InviteFriends() 
{
    var url = location.protocol + "//" + location.host + "/Profile/My-Friends.aspx";
    var url2 = location.protocol + "//" + location.host;
    FB.ensureInit(function() {      
          var dialog = new FB.UI.FBMLPopupDialog('Invite your friends to join The Pure Life / Best Life Challenge', '');
              var fbml = '<fb:fbml><fb:request-form action="' + url + '"' 
                  + ' method="POST"' 
                  + ' invite="true"' 
                  + ' type="Pure Life / Best Life Challenge"' 
                  + ' content="I’d like you to take the Pure Life / Best Life Challenge with me. Together we can take small steps toward living a better, healthier life. Sign up now and let’s do this together!  Visit ' + url2 
                  + '<fb:req-choice url=\'' + url2  + '\' label=\'Yes, I want to get join\' />">' 
                  + '<fb:multi-friend-selector rows="3" cols="5" showborder="false" actiontext="Invite your friends"/>' 
                  + '</fb:request-form></fb:fbml>';
              dialog.setFBMLContent(fbml);
              dialog.setContentWidth(650); 
              dialog.setContentHeight(400);
              dialog.show();

    });    
}

//Name: LoginContainer
//Description: Swaps out HTML login container with logged in user info
function LoginContainer() 
{
    /*  Get facebook cookie */
    var uid;
    try {
        uid = FB.Facebook.apiClient.get_session().uid;
    }
    catch(err) {
        uid = null
    }
    var nid = $.cookie("npl_settings");
    
    if (uid == null) {
        if (nid != null) {
            
            var url = "/Services/PureLifeService.asmx/UserContent";
            var dataToBeSent = "{}";
    
            $.ajax({
                type: "POST",
                url: url,
                data: dataToBeSent,
                contentType: "application/json; charset=utf-8",
                dataType: "json" ,
                success: function(response) {
                    $("#login-content").html(response.d); 
                }     
            });
                
        }
    }
}
