/*
    1) pops window for subscriptions offers on arrival
    2) cookies viewer for specified duration on arrival
    3) checks and pops exiting window if unloading window to new domain
    4) assumes all links to other sites open new windows so does not check these
*/

var SubscriptionPopups = {

    /* pop window sizes and locations ... */
    exitingPopPath   : "/nocount/exiting.html"
,    exitingPopHeight : 295
,    exitingPopWidth  : 460
,    arrivalPopPath   : "/nocount/arrival.html"
,    arrivalPopHeight : 295
,    arrivalPopWidth  : 460
,   arrivalMode      : "popup" // value is either layer or popup.
,   subsLayer        : "" // reference to the subs layer object.

    /* offsets for pop windows from center ...*/
,    arrivalLeftOffset : -50
,    arrivalTopOffset  : -50
,    exitingLeftOffset : 50
,    exitingTopOffset  : 50

    /* pop window names ... */
,    exitingPopName : "exitingpop"
,    arrivalPopName : "arrivalpop"

    /* cookie duration time ... */
,    duration : 24 /* hours */

    /* delay for arrival pop */
,    delay : 3 /* seconds delay between click and unload */

    /* set the workarea domain ... */
,    workarea : "advance"

    /* switches ... */
,    turnOffStaging   : false /* SET TO TRUE */
,    turnOnExitingPop : false /* SET TO FALSE */

    /* cookie names ...*/
,    exitingCookieName : "ExitingCookie"
,    arrivalCookieName : "ArrivalCookie"

    /* don't pop an exiting pop if we're going here */
,    suppressedDomains : ["buysub.com", "condenastdirect.com", "magazinestoresubscriptions.com"]

    /* add additional domains to exclusion list  */
,   suppressUrl : function (url) {
        this.suppressedDomains.push(url);
    }

    /* sets the pop path to be absolute if we are on boards. */
,   setPopPath : function() {
        var domain = location.host.toString();
        if (domain.indexOf("boards") > -1 || domain.indexOf("stag-forums") > -1) {
            // point boards to www.
            domain = domain.replace("boards", "www");
            domain = domain.replace("stag-forums", "www");
            this.exitingPopPath = location.protocol.toString() + "//" + domain + this.exitingPopPath;
            this.arrivalPopPath = location.protocol.toString() + "//" + domain + this.arrivalPopPath;
        }
    }

,   suppressedEntryPops : ["/sweeps", "/registration", "/services/newsletters"]

    /* add additional URL mappings to entry-pop exclusion list  */
,   suppressEntry : function (s) {
        this.suppressedEntryPops.push(s);
    }

    /* checks paths excluded from popping an entry pop */
,   checkEntryExcludes : function () {
        var path = location.pathname;
        var a = this.suppressedEntryPops;
        for (var i=0; i<a.length; i++) {
            if (path.indexOf(a[i]) != -1) {
                return true;
            }
        }
        return false;
    }

    /* arrival subscription pop ...  */
,    arrival : function () {
        /*don't display popup if suppressed*/
        if (typeof(noArrivalPopFlag) != 'undefined' && noArrivalPopFlag) { return; }
        if (this.checkEntryExcludes()) { return; }

        /* check for cookie */
        var cookie = this.getCookie(this.arrivalCookieName);
        if (cookie == true) { return; }

        /* set exiting cookie */
        this.setArrivalCookie();

        // set the pop path if we are on boards.
        this.setPopPath();

        var domain = location.host.toString();
        var referrer = document.referrer.toString();

        /* prevents triggering when loading new page on site */
        if (typeof referrer == "string" && referrer != "" && typeof domain == "string" && domain != "") {
            if (domain.indexOf("www") > -1)       { domain = domain.substring(domain.indexOf("www.") + 5); }
            if (domain.indexOf("boards") > -1)    { domain = domain.substring(domain.indexOf("boards.") + 8); }

            if (referrer.indexOf("http") != -1)   { referrer = referrer.substring(referrer.indexOf("http://") + 8); }
            if (referrer.indexOf("boards") != -1) { referrer = referrer.substring(referrer.indexOf("boards.") + 8); }
            if (referrer.indexOf("www") != -1)    { referrer = referrer.substring(referrer.indexOf("www.") + 5); }
            if (referrer.indexOf("/") != -1)      { referrer = referrer.substring(0, referrer.indexOf("/")); }

            if (referrer.indexOf(domain) != -1)   { return; }
            if (domain.indexOf(referrer) != -1)   { return; }
        }

        /* prevents functioning on development servers */
        var workarea = (domain.indexOf(this.workarea) != -1);
        if (workarea && this.turnOffStaging) { return; }

         // Safari has problems with the layer floating over Flash so use the popup.
         var agt=navigator.userAgent.toLowerCase();
         if(agt.indexOf("safari")!=-1 || (typeof document.childNodes!="undefined" && typeof document.all=="undefined" && typeof navigator.taintEnabled=="undefined")){ this.arrivalMode = "popup"; }

        /* pop window or layer */
        if (this.arrivalMode == "layer") {
            this.arrivalAction();
        } else {
            setTimeout("SubscriptionPopups.arrivalAction()", (this.delay * 1000));
        }
    }

    /* arrival subscription pop ...  */
,    arrivalAction : function () {
        var agt=navigator.userAgent.toLowerCase();

        // Adding extra padding so Safari doesn't cut off the page content
        if(agt.indexOf("safari")!=-1 || (typeof document.childNodes!="undefined" && typeof document.all=="undefined" && typeof navigator.taintEnabled=="undefined")){
            this.arrivalPopHeight = this.arrivalPopHeight + 20;
            this.arrivalPopWidth = this.arrivalPopWidth + 20;
        }

        var options = "height=" + this.arrivalPopHeight;
        options += ",width=" + this.arrivalPopWidth + ",scrollbars=no";

        if(screen.height) {
            options += ",top=" + (screen.height/2 - this.arrivalPopHeight/2 + this.arrivalTopOffset);
            options += ",left=" + (screen.width/2 - this.arrivalPopWidth/2 + this.arrivalLeftOffset);
        }

        if (this.arrivalMode == "layer") {
            this.subsLayer = new AdLayer(this.arrivalPopPath, this.arrivalPopName,options);
        } else {
            var pop = window.open('/nocount/arrival.html', this.arrivalPopName,options);
            if (pop != null) { pop.focus(); }
        }
    }

    /* exiting subscription pop ...  */
,    exiting : function() {
        this.turnOnExitingPop = true;
    }

    /* exiting subscription pop ...  */
,    popExiting : function() {

        /*don't display popup if suppressed */
        noExitingPop = typeof(noExitingPopFlag) != 'undefined' && noExitingPopFlag;
        if (noExitingPop) { return; }

        /* check if exiting pop is engaged */
        if (!this.turnOnExitingPop) { return; }

        /* check for cookie */
        var cookie = this.getCookie(this.exitingCookieName);
        if (cookie == true) { return; }

        /* set exiting cookie */
        this.setExitingCookie();

        /* prevents functioning on development servers */
        var domain = location.host.toString();
        var workarea = (domain.indexOf(this.workarea) != -1);
        if (workarea && this.turnOffStaging) { return; }

        /* pop window */
        this.exitingAction();
    }

,    exitingAction : function() {
        var agt=navigator.userAgent.toLowerCase();

        // Adding extra padding so Safari doesn't cut off the page content
        if(agt.indexOf("safari")!=-1 || (typeof document.childNodes!="undefined" && typeof document.all=="undefined" && typeof navigator.taintEnabled=="undefined")){
            this.exitingPopHeight = this.exitingPopHeight + 20;
            this.exitingPopWidth = this.exitingPopWidth + 20;
        }

        var options = "height=" + this.exitingPopHeight;
        options += ",width=" + this.exitingPopWidth + ",scrollbars=no";

        if(screen.height) {
            options += ",top=" + (screen.height/2 - this.exitingPopHeight/2 + this.exitingTopOffset);
            options += ",left=" + (screen.width/2 - this.exitingPopWidth/2 + this.exitingLeftOffset);
        }

        var pop = window.open(this.exitingPopPath, this.exitingPopName,options);
        if (pop != null) { pop.focus(); }
    }

,    clicked : 0

,    checkExiting : function(e) {

        if (!this.turnOnExitingPop) { return; }

        var activity = null;
        var source = null;

        if (typeof(e) =='undefined') { e = window.event; }
        activity = e.type;

        if (activity && activity == "click") {
            this.clicked = 1;

            if (typeof(e.target) != 'undefined') {
                source = e.target;
            } else if (typeof(e.srcElement) != 'undefined') {
                source = e.srcElement;
            }

            /* SAFARI BUG FIX */
            if (source.nodeType && source.nodeType == 3) { source = source.parentNode; }

            if (typeof source.href == "string") {

                   // IE fix. IE reports the image src in the href property if the image is linked up.
                if (source.nodeName == "IMG") {
                    source = source.parentNode; // Assuming anchor tags are the parent of the image.
                    // exit if image is not linked up.
                    if (typeof(source.href) == 'undefined') { return; }
                }
                source = source.href;
                if (source.indexOf("javascript") > -1) { return; }
                if (this.checkExcludes(source)) { return; }

                var domain = location.host.toString();
                var workarea = (domain.indexOf(this.workarea) != -1);
                if (!workarea) {
                    var domainSplit = domain.split(".");
                    domain = domainSplit[domainSplit.length-2] + "." + domainSplit[domainSplit.length-1];
                }

                if (domain != "" && source.indexOf(domain) > -1) { return; }

                this.popExiting();
            }

            return;
        }

        /* if just clicked return */
        if (this.clicked == 1) { this.clicked = 0; return; }

        /* if unloading from location pop */
        this.popExiting();
    }

    /* checks excluded paths */
,   checkExcludes : function (src) {
        for (var i=0; i<this.suppressedDomains.length; i++) {
            if (src.search(this.suppressedDomains[i]) != -1) {
                return true;
            }
        }
        return false;
    }

    /* set cookie for variable duration ... */
,    setCookie : function(name) {
        var domain = location.host.toString();
        var workarea = (domain.indexOf(this.workarea) != -1);
        if (!workarea) {
          // Set cookies for the entire domain, not just subdomains.
          var domainSplit = domain.split(".");
          domain = domainSplit[domainSplit.length-2] + "." + domainSplit[domainSplit.length-1];
        }
        var date = new Date( (new Date()).getTime() + this.duration*3600000 );
        var current = name + "=" + escape(name)+"; ";
        current += "expires=" + date.toGMTString()+"; "; /* set expiry date */
        current += "path=/; "; /* set path to top directory */
        if (!workarea) {
            current+="domain="+domain+"; "; /* set domain */
        }
        document.cookie = current;
    }

    /* set cookie for variable duration ...*/
,    setArrivalCookie : function() {
        this.setCookie(this.arrivalCookieName);
    }

    /* set cookie for variable duration ... */
,    setExitingCookie : function() {
        this.setCookie(this.exitingCookieName);
    }

    /* returns cookie ... */
,    getCookie : function(name) {
        var cookie = unescape(document.cookie);
        if (document.cookie.indexOf(name) > -1) { return true; }
        return false;
    }

    /* delete cookie ... */
,    deleteCookie : function(name) {
        var domain = location.host.toString();
        var current = name + "=" + escape(name) + "; ";
        current += "expires=Fri, 02-Jan-1970 00:00:00 GMT; "; /* set expiry date */
        current += "path=/; "; /* set path to top directory */
        /*current+="domain="+domain+"; "; /* set domain */
        document.cookie = current;
    }

    /* delete cookie ... */
,    deleteExitingCookie : function(name) {
        this.deleteCookie(this.exitingCookieName);
    }

    /* delete cookie ... */
,    deleteArrivalCookie : function(name) {
        this.deleteCookie(this.arrivalCookieName);
    }


    /* set clicked ... */
,    setClicked : function(url) {
        this.clicked = 1;
        document.location.href = url;
    }

} // END: SubscriptionPopups

// This is the function used to close the subs layer.
function AdLayer_closeLayer() {
    SubscriptionPopups.subsLayer.closeAdLayer();
}

// This is a callback. Safari can't have layers floated over Flash so this callback will hide divs with Flash
// if they are under the layer.
function AdLayer_hideFlash() {
    SubscriptionPopups.subsLayer.hideFlash();
}
