/*
 * NING aka Friends with Benefits community 
 */

var ning = {
    community_url : "http://community.momlogic.com/",
    login_url : "http://community.momlogic.com/main/authorization/signIn",
    logout_url : "http://community.momlogic.com/logout.php?next=",
    api: "http://community.momlogic.com/json/",
    methods: {
        IS_LOGGED_IN : "is_logged_in.php?"
    },
    user: {
        is_logged_in : false,
        screenName   : "",
        firstName    : "",
        lastName     : "",
        cityState    : "",
        country      : "",
        thumbnail    : "http://pdl.warnerbros.com/momlogic/us/cdn/v3/_ning/img/silhouette.gif",
        profileURL   : ""
    },

    init: function() {
        this.check_login();
    },

    no_cache: function() {
        return "&nocache=" + (new Date()).getTime() + Math.floor(Math.random()*20000) + "&callback=?";
    },

    check_login: function() {
        jQuery.getJSON(this.api + this.methods.IS_LOGGED_IN + this.no_cache(), this.handle_check_login);
    },
    handle_check_login: function(data) {
        ning.user.is_logged_in = data.is_logged_in;
        if ( data.is_logged_in ) {
            ning.user.screenName = data.screenName;
            ning.user.firstName  = data.firstName;
            ning.user.lastName   = data.lastName;
            ning.user.cityState  = data.cityState;
            ning.user.country    = data.country;
            if (data.thumbnail != "")
                ning.user.thumbnail = data.thumbnail;
            ning.user.profileURL = data.profileURL;
        }
        ning.trigger_ready(data.is_logged_in);
    },

    trigger_ready: function(is_logged_in) {
        var html = "";
        var presence = jQuery("#ning_presence_indicator");
        if ( is_logged_in ) {
            html += "<div class='thumbnail'><a href='" + this.user.profileURL + "' target='_blank'><img src='" + this.user.thumbnail + "' /></a></div>";
            html += "<div class='welcome'><div class='header'><img src='http://pdl.warnerbros.com/momlogic/us/cdn/v3/_ning/img/friends.gif' /></div>";
            html += "<div class='text'>Hi <a href='" + this.user.profileURL + "' target='_blank'>" + this.user.firstName  + "</a>!</div>";
        } else {
            html += "<div class='thumbnail'><a href='" + this.community_url + "' target='_blank'><img src='" + this.user.thumbnail + "' /></a></div>";
            html += "<div class='welcome'><div class='header'><img src='http://pdl.warnerbros.com/momlogic/us/cdn/v3/_ning/img/friends.gif' /></div>";
            html += "<div class='text'><a href='" + this.login_url + "' target='_blank'>Login</a></idv>";
        }
        html += "</div>";
        presence.html(html);
        presence.show();
        jQuery(document).trigger("ningReady");
    },

    test_ready: function(evt) {
        // alert(evt.data.test);
    }
};