var FB_LoggedInEvents = Array();
var FB_LoggedOutEvents = Array();
var FB_LoadedEvents = Array();
var FB_Connected = false;

FB_LoggedInEvents.push(FB_ProcessLoggedInHeader);
FB_LoggedOutEvents.push(FB_ProcessLoggedOutHeader);

jQuery(document).ready(function ($) {
    $("#signup_header_email").watermark("Email Address");
    $("#signup_header_zip").watermark("Zip").numeric();
    $("#signup_header").validate({
        rules: {
            email: {
                required: true,
                email: true
            },
            zip: {
                digits: true,
                required: true
            }
        },
        invalidHandler: function () {
            $.watermark.showAll()
        }
    });
    $("#facebook_login_header").click(facebook_login_OnClick);
    $("#facebook_logout_header").click(facebook_logout_OnClick);

    if($('#home-slider')){
      $('#home-slider').cycle({
          timeout: 9000,
          speed: 2500,
          fx: 'fade'
      });
    }
});

window.fbAsyncInit = function () {
    FB.Event.subscribe("auth.statusChange", function (a) {
        //console.log(a);
        if (a.status == 'connected')
            FB_LoggedIn();
    });
    //FB.Event.subscribe("auth.logout", function (a) { });
    //FB.Event.subscribe("fb.log", function (a) { });
    FB.init({
        appId: FACEBOOK_APP_ID,
        status: true,
        cookie: true,
        xfbml: true,
        channelUrl : 'http://www.minnesotaformarriage.com/channel.html',
        oauth  : true
    });
    FB.getLoginStatus(function (response) {
        //console.log(response);
        for (var index = 0; index < FB_LoadedEvents.length; index++) {
            FB_LoadedEvents[index](response);
        }
    });




};
(function () {
    var a = document.createElement("script");
    a.src = document.location.protocol + "//connect.facebook.net/en_US/all.js";
    a.async = true;
    document.getElementById("fb-root").appendChild(a)
} ());

function facebook_login_OnClick() {
    FB.login(function (response) {
        if (response.authResponse) {
    // Logged In
    }
    }, {
        scope: "email,user_location"
    });
    return false;
}

function facebook_logout_OnClick()
{
    FB.logout(function(response) {
        FB_Connected = false;
        for (var index = 0; index < FB_LoggedOutEvents.length; index++) {
            FB_LoggedOutEvents[index]();
        }
    });

    return false;
}

function FB_LoggedIn() {
    FB_Connected = true;
//console.log('1');
    var queryString = 'select uid, name, first_name, last_name, email, current_location, sex, timezone, locale, verified from user where uid=me()';

    var query = FB.Data.query(queryString);

    query.wait(function (rows) {
        //console.log(rows);
        for (var index = 0; index < FB_LoggedInEvents.length; index++) {
            FB_LoggedInEvents[index](rows[0]);
        }
    })
}
function FB_ProcessLoggedInHeader(user) {
    var $ = jQuery;

    $('#facebook_login_header').fadeOut('fast', function() {
        $('#facebook_logout_header').fadeIn('fast') ;
    });

    $("#signup_header_email").val(user.email);

    if (user.current_location != null) {
        if (user.current_location.zip.length > 0) {
            if ($('#signup_header_zip').val().length == 0)
                $('#signup_header_zip').val(user.current_location.zip);
        }
    }
};

function FB_ProcessLoggedOutHeader() {
    var $ = jQuery;

    $('#facebook_logout_header').fadeOut('fast', function() {
        $('#facebook_login_header').fadeIn('fast') ;
    });
}
