
/*These two functions below is using on user-register.tpl.php page */
$(document).ready(function() {    
    $('#fb-register').hide()
    var href = window.location.href;
    var params = href.split('?destination=');
    if (params.length > 1) {
        set_cookie('url_destination', params[1] , 1, '/', '.thesmokinggun.com', false);
    } 
})

function onFacebookRender() {
    $('#fb-register').show()
}

window.fbAsyncInit = function() {
    // Handle event that's fired when all Facebook plugins have completely rendered
    FB.Event.subscribe('xfbml.render', function() {
        if (window.onFacebookRender) {
            onFacebookRender();
        }
    });

    // Handle event that's fired when a user is determined to be logged in to Facebook
    // Initialize the Facebook SDK
    FB.init({
        appId  : '154325684596001', // TSG Facebook app ID
        status : true, // check login status
        cookie : true, // enable cookies to allow the server to access the session
        xfbml  : true  // parse XFBML
    });
    
    // Ask Facebook whether or not user is logged in to Facebook
    FB.getLoginStatus(function(response) {
        if (window.onFacebookGetLoginStatus) {
            onFacebookGetLoginStatus(response)
        }    
    });
    
    FB.Event.subscribe('auth.login', function(response) {
        user_id = response.authResponse.userID;
        access_token = response.authResponse.accessToken; 

        if ((user_id && access_token)) {
            display_login_notification(); 
            showOverlay();
            fblogin();
        }
    });

};

(function() {
    // Load the Facebook SDK asynchronously 
    var e = document.createElement('script');
    e.onerror = function() {
        // If an error occurrs while laoding the Facebook SDK, Facebook is probably blocked.
        // Display a notification to the user that Facebook is blocked
        var fb_like_block = document.getElementById('fb-like-block');
        if (fb_like_block) {
            fb_like_block.style.background = '#3B5998';
        }
        var facebook_blocked = document.getElementById('facebook-blocked');
        if (facebook_blocked) {
            facebook_blocked.style.visibility = 'visible';
        }
        var fb_like_box = document.getElementById('fb-like-box');
        if (fb_like_box) {
            fb_like_box.style.border = 'none';
        }
    }
    e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
    e.async = true;
    document.getElementById('fb-root').appendChild(e);
}()); 

function set_cookie(name, value, expires, path, domain, secure) {
    // set time, it's in milliseconds
    var today = new Date();
    today.setTime( today.getTime() );

    if (expires) {
        expires = expires * 1000 * 60 * 60 * 24;
    }
    
    var expires_date = new Date(today.getTime() + (expires));
    document.cookie = name + "=" +  escape(value) +
    ((expires) ? ";expires=" + expires_date.toGMTString() : "") +
    ((path) ? ";path=" + path : "") +
    ((domain) ? ";domain=" + domain : "") +
    ((secure) ? ";secure" : "" );
}

function Get_Cookie( name ) {

    var start = document.cookie.indexOf( name + "=" );
    var len = start + name.length + 1;
    if ( ( !start ) &&
    ( name != document.cookie.substring( 0, name.length ) ) )
    {
        return null;
    }
    if ( start == -1 ) return null;
    var end = document.cookie.indexOf( ";", len );
    if ( end == -1 ) end = document.cookie.length;
    return unescape( document.cookie.substring( len, end ) );
}

