﻿
// Write Ad
// use WriteAd(.. instead of ads.write(..

var adsKeywords = "";
var count = 0;

function WriteAd(pAdSize, pAlias) {
    if (pAlias == "1") {
        //Old ad script
        pAlias = "";
    }
    if (typeof(cosmoAdKeywords) == "undefined") {
        Ad.GetAd(pAdSize, pAlias, "");    
    } else {
        Ad.GetAd(pAdSize, pAlias, cosmoAdKeywords);
    }
}

function WriteAdOld(size, tiles, direction, zone) {
    count++;
    if (adsKeywords.length == 0) {
        var so = null;
        try {
            so = GetSharedObject("WNetworkModalSelection");
        }
        catch (er) {
        }
        adsKeywords = GetSortedCategories(so);
    }
    ads.write(size, tiles, direction, zone);
}

function WithKey(ary, k) {
    for (var i = 0; i < ary.length; i++) {
        if (ary[i] != null && ary[i].Key == k) {
            return ary[i].Value;
        }
    }
    return '';
}

var WNetworkCategories = [
    { Key: '7', Value: 'shopping' },
    { Key: '2', Value: 'beauty-style' },
    { Key: '3', Value: 'relationships' },
    { Key: '31', Value: 'home-garden' },
    { Key: '32', Value: 'money' },
    { Key: '33', Value: 'food-health' },
    { Key: 'M', Value: 'movies' }
];

function GetSortedCategories(so) {
    var cats = [];
    if (so == undefined || so == null || so == '') {
        for (var i = 0; i < WNetworkCategories.length; i++) {
            cats[i] = WNetworkCategories[i].Value;
        }
    }
    else {
        var catlist = so.split(',');
        for (var i = 0; i < catlist.length; i++) {
            cats[i] = WithKey(WNetworkCategories, catlist[i]);
        }
    }
    return cats.join();
}



