﻿/* GLOBAL VARIABLES */
var ids = new Array("forTheMarketingProfessional",
                    "forBusinessOperations",
                    "forMailroomOperations",
                    "forTheITProfessional",
                    "forSeniorExecutives",
                    "forSmallMediumBusinesses"); // These are <div> IDs

var contentIDs = new Array(32,
                           34,
                           36,
                           40,
                           38,
                           39); // These are Ektron CMS400.NET ContentBlock IDs

var unsegmentedID = 202;

/* PROTOTYPES/EXTENSIONS */
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };

Array.prototype.indexOf = function(val) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] == val) {
            return i;
        }
    }
    return -1;
};

/* DOCUMENT READY EVENT HANDLER */
$(document).ready(function() {
    // Fix transparent PNGs in IE6
    $("#mainContainer").pngfix();
    $("#mainContainerFooter").pngfix();

    // Make sure cookies are enabled
    if (!areCookiesEnabled()) {
        alert("This browser has cookies disabled.  This site will not function properly unless you enable cookies and refresh your browser.");
    }

    // Correctly position ShareThis box (doesn't calculate correctly in IE, so we'll figure it up here and set the position ourselves)
//    $(".stbutton:first").click(function() {
//        var x = findPosX(document.getElementById("mainContainer")) + 510;
//        var y = findPosY(document.getElementById("shareThisLink")) + 44;
//        $("#stwrapper").css("left", x + "px").css("top", y + "px");
//    });

    bindClickExitChecks();
    $(document).ajaxComplete(bindClickExitChecks); // To properly detect exits from links loaded via AJAX.
});

/* EXIT POPUP */

var leaving = true;
window.onbeforeunload = showSiteExitPopup;

function bindClickExitChecks() {
    $("a").unbind("click", exitCheck).bind("click", exitCheck); // Unbind first to prevent multiple bindings on ajaxComplete to existing <a> tags
}

function exitCheck(e) {
    var targ;
    if (!e) var e = window.event;
    if (e.target) targ = e.target;
    else if (e.srcElement) targ = e.srcElement;
    if (targ.nodeType == 3) // defeat Safari bug
        targ = targ.parentNode;
    checkUrl(targ.href);
}

function checkUrl(url) {
    leaving = (url.indexOf("http://") > -1 && url.indexOf("pbconnect.com") == -1 && url.indexOf("hsrclients") == -1);
}

function showSiteExitPopup() {
    if (leaving) {
        if (readCookie("PB_ExitSurvey") == null) {
            window.open("/ExitSurvey.aspx", "exitsurvey", "status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=0,width=524,height=700,scrollbars=1");
            createCookie("PB_ExitSurvey", "1", 360);
        }
    }
}

/* HELPER FUNCTIONS */

function areCookiesEnabled() {
    var tmp = new Date();
    var chk = (tmp.getTime() + '');
    document.cookie = "chkcookie=" + chk + "; path=/";
    return (document.cookie.indexOf(chk, 0) >= 0);
}

function findPosX(obj) {
    var curleft = 0;
    if (obj.offsetParent)
        while (1) {
        curleft += obj.offsetLeft;
        if (!obj.offsetParent)
            break;
        obj = obj.offsetParent;
    }
    else if (obj.x)
        curleft += obj.x;
    return curleft;
}

function findPosY(obj) {
    var curtop = 0;
    if (obj.offsetParent)
        while (1) {
        curtop += obj.offsetTop;
        if (!obj.offsetParent)
            break;
        obj = obj.offsetParent;
    }
    else if (obj.y)
        curtop += obj.y;
    return curtop;
}

function getWindowWH() {
    var myWidth = 0, myHeight = 0;
    if (typeof (window.innerWidth) == 'number') {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }
    return [myWidth, myHeight];
}

function getScrollXY() {
    var scrOfX = 0, scrOfY = 0;
    if (typeof (window.pageYOffset) == 'number') {
        //Netscape compliant
        scrOfY = window.pageYOffset;
        scrOfX = window.pageXOffset;
    } else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
        //DOM compliant
        scrOfY = document.body.scrollTop;
        scrOfX = document.body.scrollLeft;
    } else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
        //IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop;
        scrOfX = document.documentElement.scrollLeft;
    }
    return [scrOfX, scrOfY];
}

function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function getLastSelectedConnCenter() {
    var connCtr = readCookie("connCtr");
    if (connCtr == null) {
        connCtr = "unsegmented";
    }
    return connCtr;
}