﻿window.Adk2 = window.Adk2 || {};
(function () {
    var consts = { tpl: '<iframe height="{height}" width="{width}" frameborder="0" border="0" scrolling="no" src="http://ads.adk2.com/player.html?a={website}&size={width}x{height}{cid}{tags}"></iframe>' },
        websites = eval('[{"peruanisimoss.blogspot.com": 6321424, "default": 6293356, "referrer": 6299394, "translate.googleusercontent.com": 6284504, "acercatealperu.blogspot.com": 6307164, "teveperuana.info": 6299393, "webcache.googleusercontent.com": 6317094, "ads.adk2.com": 6314115, "http:": 6283911, "face-teve.blogspot.com": 6289602}]')[0],
        wwwDop = {},
        key, onLoad, oHead, oScript;

    for(key in websites){
        wwwDop['www.' + key] = websites[key];
    }

    for(key in wwwDop){
        websites[key] = wwwDop[key];
    }

    onLoad = function (cb) {
        var $ = jQuery;
        if (!Adk2.integrationAttached){
            Adk2.integrationAttached = true;
            $(document).ready(onLoad);
        }

        // find and replace all tags

        var selector = ($.browser.msie && $.browser.version === '8.0') ? '*' : 'adk2\\:ad';
        $(selector).each(function(){
            try{
            if (this.nodeName && this.nodeName.toLowerCase() !== 'adk2:ad'){
                return;
            }
            }catch(e){}

            var size = $(this).attr('size'),
                width = size.split('x')[0],
                height = size.split('x')[1],
                tags = $(this).attr('tags'),
                cid = $(this).attr('cid'),
                a = '_',
                res = '';

            if (tags){
                tags = tags.split(',');
                tags = jQuery.map(tags, function(el){
                    return jQuery.trim(el);
                });
                tags = tags.join('|');
            }

            if (typeof(websites) === 'object') {
                a = websites[window.location.host] || websites['default'] || '_';
            }

            res = consts.tpl.replace('{website}', a).replace(/{width}/gi, width).replace(/{height}/gi, height).replace('{cid}', cid ? '&cid=' +cid : '').replace('{tags}', tags ? '&tags=' + tags :'' );
            $(this).replaceWith(res);
        });
        cb ? cb() : '';
    }

    if (window.jQuery) {
        onLoad(null);
        return;
    }

    /* no jquery on page */
    oHead = document.getElementsByTagName("head")[0];
    oScript = document.createElement("script");
    oScript.type = 'text/javascript';
    oScript.src = "https://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js";
    oScript.onload = oScript.onreadystatechange = function () {
        if (this.readyState && this.readyState != 'complete' && this.readyState != 'loaded') { return; }
        oScript.onload = oScript.onreadystatechange = null;
        onLoad(function() {
            //Jquery was not on the page so release $
            jQuery.noConflict(); // Relinquish jQuery's control of the $ variable in-case there are other libraries o
        });
    };
    oHead.appendChild(oScript);
})();
