﻿var chatPopupDebug = false;

jQuery(document).ready(function () {

    //alert(111);

    var chatOnline = chatPopupDebug;
    if (jQuery('img[src$="/DesktopModules/CuteSoft_Client/CuteChat/images/141x44-online.gif"]').size() > 0)
        chatOnline = true;

    if (chatOnline) {
        if (jQuery.cookie('NoChatPopup') != '1' || chatPopupDebug) {
            setTimeout('chatPopupOpen()', 1000 * (chatPopupDebug ? 5 : 60)); //set timeout to one min;
        }
    }



    jQuery("#btnFaceBookLogin").css("cursor", "pointer");
    jQuery("#btnFaceBookLogin").click(checkFacebookLogin);

});

function chatPopupOpen() {
    

    var html = '<div class="lbwrap" id="chatPopupBox">'+
  '<div class="lbcontent">'+
   '<div class="lbline"><a href="javascript:void(0)" title="close" href="javascript:void(0)" onclick="chatPopupClose()" class="close"><img src="/Portals/0/Skins/YourCover/images/chat-help/close.png" width="22" height="22" alt="close" /></a></div>' +
    '<div class="lbinner">'+
     '<p>Need help?</p>'+
      '<a href="javascript:void(0)" onclick="chatPopupClose(); OpenLiveSupport();" title="Yes, please" class="yes"><img src="/Portals/0/Skins/YourCover/images/chat-help/yes.gif" width="84" height="28" alt="" /></a><a href="javascript:void(0)" onclick="chatPopupClose()" title="No, thank you"><img src="/Portals/0/Skins/YourCover/images/chat-help/no.gif" width="103" height="28" alt="" /></a></div>' +
  '</div>'+
'</div>';

    var popup = jQuery(html);

    popup.css("position", "absolute");
    popup.css("z-index", "30000");
    popup.css("top", (jQuery(window).height() - popup.height()) / 2 + jQuery(window).scrollTop() + "px");
    popup.css("left", (jQuery(window).width() - popup.width()) / 2 + jQuery(window).scrollLeft() - 300 + "px");

    if (window.disableLiveChatPopup != undefined) {
        if (window.disableLiveChatPopup == true) {
            //popup.css("top", ( popup.height()) / 2  + "px");
            popup.css("top", (100 + "px"));
            popup.css("left", (jQuery(window).width() - popup.width()) / 2 + jQuery(window).scrollLeft() + "px");
        }
    }
   
    popup.hide();
//    alert(popup);

    
    

    overlay = jQuery('<div id="chatPopupOverlay"><div>');

    overlay.css("position", "absolute");
    overlay.css("z-index", "29000");
    overlay.css("top", "0px");
    overlay.css("left", "0px");
    overlay.width(jQuery(window).width());
    overlay.height(jQuery(document).height());
    overlay.css("opacity", "0.5");
    overlay.css("background-color", "black");
    overlay.hide();
    

    jQuery(document.body).append(overlay);
    jQuery(document.body).append(popup);

    overlay.fadeIn(500);
    popup.slideDown(1000);
    

    //jQuery.cookie('NoChatPopup', '1');
}

function chatPopupClose() {
    jQuery.cookie('NoChatPopup', '1');
    jQuery('#chatPopupBox').remove();
    jQuery('#chatPopupOverlay').remove();


}

window.checkFacebookLogin = function () {


    FB.getLoginStatus(function (response) {
        if (response.authResponse) {
            // logged in and connected user, someone you know
            if (response.status == "connected") {

                var query = FB.Data.query('select name, uid, email, first_name, last_name from user where uid={0}',
                           response.authResponse.userID);
                query.wait(function (rows) {
                    //alert('email is ' + rows[0].email);
                    if (!rows[0].email) {
                        facebookLogin();
                    }
                    else {  //call ajax handler
                        jQuery.post("/FacebookLogin.aspx", rows[0], function (data) {
                            if (data == "OK") {
                                if (typeof (IsLoginPopupMode) != "undefined" && IsLoginPopupMode) {
                                    jQuery("#loginFrame").attr("src", "/Default.aspx?TabID=75");
                                }
                                else {
                                    window.location = "/";
                                }
                            }
                            else
                                alert("Error occured during authorization process");
                        });
                    }
                });
            }
            else {
                facebookLogin();
            }
        } else {
            // no user session available, someone you dont know
            facebookLogin();
        }
    });
}

function facebookLogin()
{
    FB.login(function (response) {
        if (response.authResponse) {
            //console.log('Welcome!  Fetching your information.... ');
            checkFacebookLogin();
        } else {
            //console.log('User cancelled login or did not fully authorize.');
        }
    }, { scope: 'email' });
}

