﻿var _cookieLifetime = 48; // hours before the popup should display again
var _popupDelay = 30; // seconds before the popup should appear
var _chatOpen = 9; // GMT Hour when chat opens
var _chatClose = 21; // GMT Hour when chat closes

var __popupStatus = 0;

google.load('jquery', '1.3.1');
google.load('swfobject', '2.1');
google.setOnLoadCallback(initChat);


function loadPopup() {
    if (__popupStatus == 0) {
        $('#backgroundPopup').css({
            'opacity': '0.7'
        });
        $('#backgroundPopup').fadeIn('slow');
        $('#popupChat').fadeIn('slow');
        __popupStatus = 1;
    }
}
function disablePopup() {
    if (__popupStatus == 1) {
        $('#backgroundPopup').fadeOut('slow');
        $('#popupChat').fadeOut('slow');
        
        // Set a cookie to disable the popup
        $.cookies.set('displayUKChat', 'false', { hoursToLive: _cookieLifetime });        
        
        __popupStatus = 0;
    }
}
function centerPopup() {
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight = $('#popupChat').height();
    var popupWidth = $('#popupChat').width();
    $('#popupChat').css({
        'position': 'absolute',
        'top': windowHeight / 2 - popupHeight / 2,
        'left': windowWidth / 2 - popupWidth / 2
    });

    /* IE6 Hack */
    $('#backgroundPopup').css({
        'height': windowHeight
    });
}
function loadChat() {
    var url = 'https://portal.arise.com/Portal/Support/Chat%20with%20an%20agent.asp';
    var opts = 'width=600px,height=550px,status=no,menubar=no,scrollbars=no,titlebar=no,resizable=yes,toolbar=no,location=no';
    window.open(url, 'LiveChat', opts);
    disablePopup();

}

function shouldDisplayPopup() {
    var isChatOpen = false;
    var GMTHour = new Date().getUTCHours();

    // Chat operations are closed at this time
    if (GMTHour < _chatOpen || GMTHour >= _chatClose)
        return false;
    
    // Cookies are disabled, so always show the popup
    if ($.cookies.test() == false)
        return true;
    
    // Cookie found, so the popup has already been displayed previously
    var cookieValue = $.cookies.get('displayUKChat');
    if (cookieValue && cookieValue == 'false')
        return false;

    return true;
}

function initChat() {
    $(document).ready(function() {
        $.getScript("jquery.cookies.2.0.1.js", function() {
            if (shouldDisplayPopup()) {
                setTimeout('centerPopup();loadPopup();', _popupDelay * 1000);
            }
        });

        //Hi! Do you have any questions I can answer now? 
        $('#popupChatHeader').text('Let\'s Chat!');
        $('#chatArea').html('Hi! Do you have any questions I can answer now?<br /><br />' +
        '<a href="javascript:void(0);" onclick="javascript:loadChat();">Yes</a>&nbsp;&nbsp;' +
        '<a href="javascript:void(0);" onclick="javascript:disablePopup();">No, Thanks</a>');

        $('#popupChatClose').click(function() {
            disablePopup();
        });
        $('#backgroundPopup').click(function() {
            disablePopup();
        });
        $(document).keypress(function(e) {
            if (e.keyCode == 27 && popupStatus == 1) {
                disablePopup();
            }
        });
    });
}