﻿var jQueryScriptOutputted = false;
var jQueryScriptWg = false;
var rda_options = {
    rda_campaign: (typeof (this.rda_campaign) != 'undefined') ? this.rda_campaign : "",
    rda_cat: (typeof (this.rda_cat) != 'undefined') ? encodeURIComponent(this.rda_cat.replace('\t')) : "",
    rda_type: (typeof (this.rda_type) != 'undefined') ? this.rda_type : "",
    rda_subid: (typeof (this.rda_subid) != 'undefined') ? this.rda_subid : "",
    rda_url: (typeof (this.rda_url) != 'undefined') ? encodeURIComponent(this.rda_url.replace('\t')) : window.location.href,
    rda_domain: (typeof (this.rda_domain) != 'undefined') ? this.rda_domain : "",
    rda_guid: ""
}

var WinbuyerRdaNamespace = function () {

    Get_Cookie = function (name) {
        var start = document.cookie.indexOf(name + "=");
        var len = start + name.length + 1;
        if ((!start) &&
        (name != document.cookie.substring(0, name.length))) {
            return null;
        }
        if (start == -1) return null;
        var end = document.cookie.indexOf(";", len);
        if (end == -1) end = document.cookie.length;
        return unescape(document.cookie.substring(len, end));
    }

    S4 = function () { return ((1 + Math.random(new Date().getSeconds())) * parseInt('10000', 16)).toString(16).substring(1, 5); }
    generateGuid = function () { return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4()).toUpperCase(); }

    rda_options.rda_guid = Get_Cookie("rda_guid");
    if (rda_options.rda_guid == null) {
        rda_options.rda_guid = generateGuid();
        document.cookie = 'rda_guid=' + rda_options.rda_guid + '; path=/';
    }

    jQuery.getJSON("http://rda.winbuyer.com/ads/RdaHandler.ashx?callback=?",
                            rda_options,
                            function (response, status) {
                                jQuery("<div id=" + rda_options.rda_guid + "></div>").insertAfter(jQuery("script[src*='ShowAD']")).hide();
                                if (response.isScript == "1") {
                                    var RdaScr = escape(response.source.replace(/script/gi, "RDATRIRCS").replace(/</gi, "RDALEFTSIGN").replace(/>/gi, "RDARIGHTSIGN"));
                                    jQuery("<iframe name='ifr" + rda_options.rda_guid + "' id='ifr" + rda_options.rda_guid + "' src = 'http://rda.winbuyer.com/ads/ShowIframe.aspx?scr=" + RdaScr + "' height='" + response.height + "' width='" + response.width + "' frameborder='0' marginwidth='0' marginheight='0' vspace='0' hspace='0' allowtransparency='true' scrolling='no'></iframe>").appendTo("#" + rda_options.rda_guid);
                                }
                                else
                                    jQuery("#" + rda_options.rda_guid).html(response.source);
                                jQuery("#" + rda_options.rda_guid).slideDown(500);
                            });
};

getRdaScript = function () {
    if (typeof (jQuery) == 'undefined') {
        if (!jQueryScriptOutputted) {
            jQueryScriptOutputted = true;
            jQueryScriptWg = true;
            var s = document.createElement('script'); s.setAttribute('src', 'http://wpc.2994.edgecastcdn.net/002994/rpl/includes/jquery-1.4.2.min.js'); s.setAttribute('type', 'text/javascript'); document.getElementsByTagName('head')[0].appendChild(s);
        }
        setTimeout("getRdaScript()", 50);
    } else {
        if (jQueryScriptWg)
            jQuery.noConflict();
        WinbuyerRda = new WinbuyerRdaNamespace();
    }
}
getRdaScript();
