/* LUCKY KEYS */

function subscriptionsClass() {
    var giftPath = "https://w1.buysub.com/servlet/GiftsGateway?cds_mag_code=LKY&cds_page_id=804&cds_response_key=";

    this["promoRailGift"]=makeLink(giftPath+"GWDILC03");
    this["headerGift"]=makeLink(giftPath+"GWDILC02");
    this["contactGiftSubscribe"]=makeLink(giftPath+"GWDILC01");
    this["giftMagSubHome"]=makeLink(giftPath+"GYDIMTMS");
    
    this["subscriptionQuestions"]=makeLink("https://w1.buysub.com/pubs/N3/LKY/Customer_Care_Landing_Page.jsp?cds_page_id=47521&cds_mag_code=LKY");
    this["autosubFooter"]=makeLink("https://w1.buysub.com/servlet/OrdersGateway?cds_mag_code=LKY&cds_page_id=800&cds_response_key=IWDNL1EF");
    this["headerRenew"]=makeLink("https://w1.buysub.com/pubs/N3/LKY/Customer_Care_Landing_Page.jsp?cds_page_id=47521&cds_mag_code=LKY");
    //this["menuSubscribe"]=makeLink("http://clk.atdmt.com/NYC/go/vngnccdt0040000002nyc/direct;wi.1;hi.1/01/" + Math.random()*10000000000000000);     
    //this["menuSubscribe"]=makeLink("https://w1.buysub.com/servlet/OrdersGateway?cds_mag_code=LKY&cds_page_id=47181&cds_response_key=I8CNAAV2");
    this["intlMagSubHome"]=makeLink("https://w1.buysub.com/servlet/OrdersGateway?cds_mag_code=LKY&cds_page_id=800&cds_response_key=I2HNT7CU");
    this["customerService"]=makeLink("https://w1.buysub.com/servlet/CSGateway?cds_mag_code=LKY&cds_page_id=938");

    this["menuSubscribe"]=prepopLink("I8DNAADB"); /* subscription center */
    this["headerSubscribe"]=prepopLink("I8DNAADC"); /* subscription center */
    this["headerSubscribeExtra"]=prepopLink("I8DNAADD"); /* header */
    this["arrivalPop"]=prepopLink("I8DNAADE");
    this["exitingPop"]=prepopLink("I8DNAADF");
    this["promoRailSubscribe"]=prepopLink("I8DNAADG");
    this["contactSubscribe"]=prepopLink("I8DNAADH");
    this["magSubHome"]=prepopLink("I8DNAADI");
    this["footerSubscribe"]=prepopLink("I8DNAADJ"); /* zrail */
    this["zRailSubscribe"]=prepopLink("I8DNAADJ"); /* zrail */
    
    this["arrivalLayer"]=prepopLink("I8INAAIM"); /*Floating Ad Image*/
    
        
    this.checkCdsAdmin = checkCdsAdmin;
        
    return;
    
    function prepopLink(responseKey) {
        //var boards = 'boards.';
       	//var url = (location.host.indexOf(boards) != -1 ? 'http://' + location.host.substr(boards.length, location.host.length) : "") + '/user/regsub/prepop.post?cds_mag_code=LKY&cds_page_id=799&cds_response_key='+responseKey;
        //comment out the following line to use prepop gateway
        var url = 'https://w1.buysub.com/servlet/OrdersGateway?cds_mag_code=LKY&cds_page_id=799&cds_response_key='+responseKey;
        return makeLink(url);
    }
    
    function makeLink(url) {
        //only teenvogue has gateway for ageVerification
        return checkCdsAdmin(url);//gateway + escape(url);
    }
    
    function checkCdsAdmin(url) {
        if (location.href.indexOf('www.') != -1) {
            //if in staging, we use admin.buysub, else keep w1.buysub
            url = url.replace('://admin.', '://w1.');
        }
        return url;
    }
    
}

var subscriptions=new subscriptionsClass();

/* Used to specify urls you don't want exit pops opening on. */
function definePopExclusions() {
  if (typeof(SubscriptionPopups) != 'undefined') {
    SubscriptionPopups.suppressUrl(this.path);
    SubscriptionPopups.suppressUrl(this.giftPath);
  }
}

function getLinks(name) {
    if (typeof name == "string") {
        var url = "";
        if (name.indexOf('://') != -1) {
            //name is the URL itself
            url = name;
        } else {
            name=name.substring(name.lastIndexOf('/')+1);
            if (typeof this[name] == "string") {
                url = this[name]; 
            }
        }
        
        if (url != "") {
            var token = document.cookie.split('browsertoken=');
            if (token.length>1) {
                token = token[1].split(';')[0];
                //token += '|' + location.host;
                url += '&cds_offsite_id=' + token;
            }
            url = this.checkCdsAdmin(url);
            window.open(url, 'subsWin', 'top=0,left=0,resizable=yes,scrollbars=yes,status=yes,toolbar=yes,location=yes');
        }
    }
    
    return false;
}

function assignLinks() {
    this.definePopExclusions();
    for (var i=0; i<document.links.length; i++) {
        var name="";
        if (typeof document.links[i].name == "string") {
            /* works only in newer browsers !!! */
            name=document.links[i].name;
        } else {
            /* for older browsers !!! */
            name=document.links[i].href;
        }
        if (name == null) { continue; }
        if (typeof name != "string") { continue; }
        if (name.length < 5) { continue; }
        if (name.indexOf('javascript') != -1) { continue; }

        if (typeof name == "string") {
            name=name.substring(name.lastIndexOf('/')+1);
            if (typeof this[name] == "string") {
                anchor.href=this[name];
                anchor.target="_blank";
            }
            
        } /* end if */

    } /* end for */
} /* end assignLinks */

subscriptionsClass.prototype.definePopExclusions=definePopExclusions;
subscriptionsClass.prototype.assign=assignLinks; 
subscriptionsClass.prototype.getlink=getLinks;
