﻿window.fbAsyncInit = function() {
    FB.init({ appId: '121629294579174', status: true, cookie: true, xfbml: true }); 
        
            /* All the events registered */                
        FB.Event.subscribe('auth.login', function(response) {                    
            // do something with response                     
            login();	    
        });                 
        FB.Event.subscribe('auth.logout', function(response) {                     
            // do something with response                     
            logout();                 
        });                   
        FB.getLoginStatus(function(response) {   
            if (response.status == 'connected') {
                // logged in and connected user, someone you know
                login();
            }
        });
    };
    (function() {
        var e = document.createElement('script');
        e.type = 'text/javascript';
        e.src = document.location.protocol +
            '//connect.facebook.net/en_US/all.js';
        e.async = true;
        document.getElementById('fb-root').appendChild(e);
    } ());

    function login() {
        FB.api('/me', function(response) {
            document.getElementById('login').style.display = "block";
            document.getElementById('name').style.display = "block";
            document.getElementById('login').innerHTML = "Using arc website as " + response.name;
        });
    }
    function logout() {
        document.getElementById('login').style.display = "none";
        document.getElementById('name').style.display = "none";
        document.getElementById('facebookRegister').style.display = "none";
    }
                   
    //stream publish method             
    function streamPublish(name, description, hrefTitle, hrefLink, userPrompt){                 
        FB.ui(
        {
            method: 'stream.publish',                     
            message: '',                     
            attachment: {                         
                name: name,                         
                caption: '',                         
                description: (description),                         
                href: hrefLink                     
            },                     
            action_links: [                         
                { text: hrefTitle, href: hrefLink }                     
            ],                     
                user_prompt_message: userPrompt                 
            },                 
            function(response) {                   
        });               
     }
     function showStream() {
         FB.api('/me', function(response) {
             //console.log(response.id);
         streamPublish(response.name + ' is using arc Bury St Edmunds', 'arc Bury St Edmunds for fashion and a great day out!', 'hrefTitle', 'http://www.arc-burystedmunds.com/', "Share arc-burystedmunds.com");
         });
    }
    function share() {
        var share = {
            method: 'stream.share',
            u: 'http://www.arc-burystedmunds.com/'
        };
        FB.ui(share, function(response) { console.log(response); });
    }
    function graphStreamPublish() {
        var body = 'Using arc Bury St Edmunds website';
        FB.api('/me/feed', 'post', { message: body }, function(response) {
            if (!response || response.error) {
                alert('Error occured');
            } else {
                alert('Post ID: ' + response.id);
            }
        });
    }
    function fqlQuery() {
        FB.api('/me', function(response) {
        var query = FB.Data.query('select first_name, last_name, birthday, email, hometown_location, sex from user where uid={0}', response.id);
            query.wait(function(rows) {
            document.getElementById("ctl00_ContentPlaceHolder1_txtFirstname").value = rows[0].first_name;
            document.getElementById("ctl00_ContentPlaceHolder1_txtSurname").value = rows[0].last_name;
            document.getElementById("ctl00_ContentPlaceHolder1_txtDOB").value = rows[0].birthday;
            document.getElementById("ctl00_ContentPlaceHolder1_txtEmail").value = rows[0].email;
            document.getElementById("ctl00_ContentPlaceHolder1_txtCity").value = rows[0].hometown_location;
            setDropDownList(document.getElementById("ctl00_ContentPlaceHolder1_ddGender"), rows[0].sex);
            });
        }); 
    }
    function setStatus() {
        status1 = document.getElementById('status').value;
        FB.api(
            {
            method: 'status.set',
            status: status1
        },
        function(response) {
            if (response == 0) {
                alert('Your facebook status not updated. Give Status Update Permission.');
            }
            else {
                alert('Your facebook status updated');
            }
        }
    );
}         
