﻿
/*
* Ad: an individual ad package to be displayed.
*/


var testSite = "http://www.wnetwork.com";
var debug = true;

function Ad(pAdSize, pAlias, pKeys) {

    this.AdBaseUri = "http://adserver.adtechus.com";
    this.DeliveryType = "addyn";
    this.TagVersion = "3.0";


    this.NetWorkID = "5233.1";
    this.AdSize = pAdSize;
    this.AdGroupId;



    this.AdUrl = "";
    this.Alias = pAlias;
    this.Key = pKeys;
    this.TheDate = new Date();

    this.AdGroupId = Ad.GetAdGroupId();
    this.BackUpIds = {
        '1x1': "1196217",
        '1x2': "1196215",
        '100x100': "1196209",
        '120x90': "1196212",
        '130x50': "1196214",
        '150x90': "1196211",
        '160x600': "1196218",
        '170x76': "1196216",
        '180x150': "1196213",
        '180x180': "1196219",
        '200x100': "1196228",
        '240x100': "1196226",
        '240x90': "1196222",
        '25x25': "1196210",
        '300x250': "1196223",
        '300x40': "1196225",
        '500x500': "1196224",
        '545x90': "1196227",
        '592x90': "1196221",
        '728x90': "1196220",
        '950x100': "1196230",
        '950x150': "1196229"
    }

    this.AdBackupID = Ad.GetAdBackupId(pAdSize);
}

Ad.GetAd = function(pAdSize, pAlias, pKeys) {

    if (pKeys == "" || pKeys == "undefined" || pAlias == null)
        pKeys = "";

    if (pAlias == "" || pAlias == "undefined" || pAlias == null) {
        pAlias = GetAlias() + "_" + pAdSize;
    }

    //console.log(pAlias);

    var NewAd = new Ad(pAdSize, pAlias, pKeys);
    var AdIndex = Ad.SetInstance(NewAd);

    NewAd.AdUrl = NewAd.AdBaseUri + "/" + NewAd.DeliveryType + "|" + NewAd.TagVersion + "|" + NewAd.NetWorkID + "|" + NewAd.AdBackupID + "|0|-1|size=" + NewAd.AdSize;
    NewAd.AdUrl += ";loc=100;alias=" + NewAd.Alias + ";key=" + NewAd.Key + ";grp=" + NewAd.AdGroupId + ";";

    //**************DEBUG************************

    //    if (debug) 
    //    {
    //        $("#leaderboard").append("<div style='position:absolute;z-index:2;white-space: nowrap;'>" + NewAd.AdUrl + "</div>");        
    //    }


    //**************DEBUG************************


    document.write('<scr' + 'ipt language="javascript1.1" src="' + NewAd.AdUrl + '"></scri' + 'pt>');

}


Ad.GetInstance = function(i) {
    if (!Ad.__instance) {
        throw "Trying to get an instance of the Ad before it has been initialized";
        return;
    }
    else if (i != null && i >= 0) {
        return Ad.__instance[i];
    }
    else {
        return Ad.__instance[Ad.__instance.length - 1];
    }
}

Ad.GetAdGroupId = function() {
    if (Ad.AdGroupId == null) {
        Ad.AdGroupId = Math.round(Math.random() * 1000);
    }

    return Ad.AdGroupId;
}

/********************************************************
********* Function added by Boris Iriarte on Jan 7th 2010
*********************************************************/
var BackUpIds = {
    '1x1': "1196217",
    '1x2': "1196215",
    '100x100': "1196209",
    '120x90': "1196212",
    '130x50': "1196214",
    '150x90': "1196211",
    '160x600': "1196218",
    '170x76': "1196216",
    '180x150': "1196213",
    '180x180': "1196219",
    '200x100': "1196228",
    '240x100': "1196226",
    '240x90': "1196222",
    '25x25': "1196210",
    '300x250': "1196223",
    '300x40': "1196225",
    '500x500': "1196224",
    '545x90': "1196227",
    '592x90': "1196221",
    '728x90': "1196220",
    '950x100': "1196230",
    '950x150': "1196229"
}


Ad.GetAdBackupId = function(size) {
    //iterate through BackUpIds hash table
    for (name in BackUpIds) {
        if (name == size) {
            return BackUpIds[name];
        }
    }
}

Ad.GetAdUrl = function() {
    return Ad.GetInstance(this.AdIndex).AdUrl;
}

Ad.SetKey = function(keys) {
    var allKeys = keys.replace("|", "+");
    Ad.GetInstance(this.AdIndex).Keys += allKeys;
}

Ad.SetInstance = function(pAd) {
    if (!Ad.__instance) {
        Ad.__instance = new Array();
    }

    Ad.__instance.push(pAd);

    return Ad.__instance.length - 1;
}

//*****************************************************************************************
//******************** Function modified by Boris Iriarte on Jan 13th 2009 ****************
//******************** to implement the AdTech display considering each web site section **
//*****************************************************************************************

function GetAlias() {

    var siteName;
    var siteSection;
    var pUri = GetUri(window.location);

    //**************DEBUG************************

    //$("#container_debug").append("DirectoryPath:" + pUri.directoryPath[1].toLowerCase() + " length: " + pUri.directoryPath.length);
    //$("#container_debug").append("URIPath:" + pUri.domain + " length: " + pUri.domain.length);

    //**************DEBUG************************
    var ruta = window.location.toString();

    if (pUri) {

        //console.dir(pUri);

        switch (pUri.authority) { //Changed by Osvel (pUri.domain[2].toLowerCase()) {

            /**********  Modification made to display the Adtech banner en different 
            ***********  web sites depending in (spreadheet logic)

            /***********************************************************************/ 
            /************************* case wnetwork.com   ************************
            /**********************************************************************/ 
            case "wnetwork.staging.edentity.ca":
            case "wnetwork.live.edentity.ca":
            case "www.wnetwork.com":
            case "wnetwork.com":
            case "wnetwork.dev":
                siteName = "wnetwork";
                siteSection = "other";

                if (pUri.domain[1] != undefined && pUri.domain[1].toLowerCase().indexOf("wexperts") == 0) {
                    //WExperts site
                    siteSection = "wnetwork_experts";
                    break;
                }

                if (pUri.directoryPath.length > 2 || (pUri.fileName != "undefined" && pUri.fileName != "")) {

                    var ss = "";

                    if (pUri.directoryPath[1].toLowerCase() != "") {
                        ss = pUri.directoryPath[1].toLowerCase();
                    }
                    else if (pUri.fileName != "") {
                        ss = pUri.fileName.toLowerCase().replace(/.aspx/, "");
                    }
                    switch (ss) {
                        case "shows":
                            siteSection = "shows";
                            break;
                        case "beauty-and-style":
                            siteSection = "beauty&style";
                            break;
                        case "relationships":
                            siteSection = "relationships";
                            break;
                        case "shopping":
                            siteSection = "shopping";
                            break;
                        case "home-and-garden":
                            siteSection = "home&garden";
                            break;
                        case "money":
                            siteSection = "money";
                            break;
                        case "food-and-health":
                            siteSection = "food&health";
                            break;
                        case "video":
                            siteSection = "video";
                            break;
                        case "movies":
                            siteSection = "movies";
                            break;
                        case "home":
                            siteSection = "home";
                            break;
                        case "temp":
                            siteSection = "test";
                            break;
                    };
                }

                if (pUri.directoryPath[1].toLowerCase() == "fun-stuff") {

                    var ss = "";

                    if (pUri.directoryPath[2].toLowerCase() != "") {
                        ss = pUri.directoryPath[2].toLowerCase();
                    }
                    else if (pUri.fileName != "") {
                        ss = pUri.fileName.toLowerCase().replace(/.aspx/, "");
                    }
                    switch (ss) {
                        case "games":
                            siteSection = "games";
                            break;
                        case "contests":
                            siteSection = "contest";
                            break;
                    };
                }

                break;

            /***********************************************************************/ 
            /************************* case cosmotv.ca *****************
            /**********************************************************************/ 
            case "cosmotv.staging.edentity.ca":
            case "cosmotv.live.edentity.ca":
            case "cosmotv.ca":
            case "www.cosmotv.ca":
            case "cosmotv.dev":
            case "stage.cosmotv.ca":
                siteName = "cosmotv";
                siteSection = "other";

                if (pUri.directoryPath.length > 2 || (pUri.fileName != "undefined" && pUri.fileName != "")) {

                    var ss = "";

                    if (pUri.directoryPath[1].toLowerCase() != "") {
                        ss = pUri.directoryPath[1].toLowerCase();
                    }
                    else if (pUri.fileName != "") {
                        ss = pUri.fileName.toLowerCase().replace(/.aspx/, "");
                    }
                    switch (ss) {
                        case "video":
                            siteSection = "video";
                            break;
                        case "on-tv":
                            if (pUri.fileName != "undefined" && pUri.fileName != "" &&
                                pUri.fileName.toLowerCase().replace(/.aspx/, "") == "oh-so-cosmo") {
                                siteSection = "ohsocosmo";
                            }
                            if (pUri.directoryPath.length > 3 && pUri.directoryPath[3].toLowerCase() == "oh-so-cosmo") {
                                siteSection = "ohsocosmo";
                            }
                            break;
                        case "home":
                            siteSection = "home";
                            break;
                    }
                }

                if (pUri.directoryPath[1].toLowerCase() == "fun-stuff") {

                    var ss = "";

                    if (pUri.directoryPath[2].toLowerCase() != "") {
                        ss = pUri.directoryPath[2].toLowerCase();
                    }
                    else if (pUri.fileName != "") {
                        ss = pUri.fileName.toLowerCase().replace(/.aspx/, "");
                    }
                    switch (ss) {
                        case "compliments":
                            siteSection = "compliments";
                            break;
                    }
                }

                break;
        };
    }

    return siteName + "_" + siteSection;
}

function GetUri(pUri) {

    /* parseUri JS v0.1, by Steven Levithan modified by Rob Clarkson <http://stevenlevithan.com> Splits any well-formed URI into the following parts (all are optional): */

    var UriPartNames = ["source", "protocol", "authority", "domain", "port", "path", "directoryPath", "fileName", "query", "anchor"],
			UriParts = new RegExp("^(?:([^:/?#.]+):)?(?://)?(([^:/?#]*)(?::(\\d*))?)((/(?:[^?#](?![^?#/]*\\.[^?#/.]+(?:[\\?#]|$)))*/?)?([^?#/]*))?(?:\\?([^#]*))?(?:#(.*))?").exec(pUri),
			Uri = {};

    for (var i = 0; i < 10; i++) {
        Uri[UriPartNames[i]] = (UriParts[i] ? UriParts[i] : "");
    }


    if (Uri.directoryPath.length > 0) {
        Uri.directoryPath = Uri.directoryPath.replace(/\/?$/, "/");
        Uri.directoryPath = Uri.directoryPath.split("/");
    }

    if (Uri.domain) {

        if (Uri.domain.indexOf("localhost") != -1)
            Uri.domain = (testSite + "").match(/^https?:\/\/(\w*\.)?([^\/]+\.[\w]{2,4})\/?(\w*)\/?/i);
        else
            Uri.domain = (window.location + "").match(/^https?:\/\/(\w*\.)?([^\/]+\.[\w]{2,4})\/?(\w*)\/?/i);
    }

    return Uri;
}


