var END_OF_INPUT = -1;

var base64Chars = new Array(
    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
    'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
    'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
    'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
    'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
    'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
    'w', 'x', 'y', 'z', '0', '1', '2', '3',
    '4', '5', '6', '7', '8', '9', '+', '/'
);

var reverseBase64Chars = new Array();
for (var i = 0; i < base64Chars.length; i++) {
    reverseBase64Chars[base64Chars[i]] = i;
}

var base64Str;
var base64Count;
function setBase64Str(str) {
    base64Str = str;
    base64Count = 0;
}
function readBase64() {
    if (!base64Str) return END_OF_INPUT;
    if (base64Count >= base64Str.length) return END_OF_INPUT;
    var c = base64Str.charCodeAt(base64Count) & 0xff;
    base64Count++;
    return c;
}
function encodeBase64(str) {
    setBase64Str(str);
    var result = '';
    var inBuffer = new Array(3);
    var lineCount = 0;
    var done = false;
    while (!done && (inBuffer[0] = readBase64()) != END_OF_INPUT) {
        inBuffer[1] = readBase64();
        inBuffer[2] = readBase64();
        result += (base64Chars[inBuffer[0] >> 2]);
        if (inBuffer[1] != END_OF_INPUT) {
            result += (base64Chars[((inBuffer[0] << 4) & 0x30) | (inBuffer[1] >> 4)]);
            if (inBuffer[2] != END_OF_INPUT) {
                result += (base64Chars[((inBuffer[1] << 2) & 0x3c) | (inBuffer[2] >> 6)]);
                result += (base64Chars[inBuffer[2] & 0x3F]);
            } else {
                result += (base64Chars[((inBuffer[1] << 2) & 0x3c)]);
                result += ('=');
                done = true;
            }
        } else {
            result += (base64Chars[((inBuffer[0] << 4) & 0x30)]);
            result += ('=');
            result += ('=');
            done = true;
        }
        lineCount += 4;
        if (lineCount >= 76) {
            result += ('\n');
            lineCount = 0;
        }
    }
    return result;
}
function readReverseBase64() {
    if (!base64Str) return END_OF_INPUT;
    while (true) {
        if (base64Count >= base64Str.length) return END_OF_INPUT;
        var nextCharacter = base64Str.charAt(base64Count);
        base64Count++;
        if (reverseBase64Chars[nextCharacter]) {
            return reverseBase64Chars[nextCharacter];
        }
        if (nextCharacter == 'A') return 0;
    }
    return END_OF_INPUT;
}

function ntos(n) {
    n = n.toString(16);
    if (n.length == 1) n = "0" + n;
    n = "%" + n;
    return unescape(n);
}

function decodeBase64(str) {
    setBase64Str(str);
    var result = "";
    var inBuffer = new Array(4);
    var done = false;
    while (!done && (inBuffer[0] = readReverseBase64()) != END_OF_INPUT
        && (inBuffer[1] = readReverseBase64()) != END_OF_INPUT) {
        inBuffer[2] = readReverseBase64();
        inBuffer[3] = readReverseBase64();
        result += ntos((((inBuffer[0] << 2) & 0xff) | inBuffer[1] >> 4));
        if (inBuffer[2] != END_OF_INPUT) {
            result += ntos((((inBuffer[1] << 4) & 0xff) | inBuffer[2] >> 2));
            if (inBuffer[3] != END_OF_INPUT) {
                result += ntos((((inBuffer[2] << 6) & 0xff) | inBuffer[3]));
            } else {
                done = true;
            }
        } else {
            done = true;
        }
    }
    return result;
}

function pof_ads_$() {
    var elements = new Array();
    for (var i = 0; i < arguments.length; i++) {
        var element = arguments[i];
        if (typeof element == 'string') {
            if (document.getElementById || document.getElementsByName) {
                if (document.getElementById(element)) {
                    element = document.getElementById(element);
                } else if (document.getElementsByName(element)) {
                    element = document.getElementsByName(element)[0];
                }
            } else if (document.all) {
                element = document.all[element];
            }
        }
        if (arguments.length == 1)
            return element;
        elements.push(element);
    }
    return elements;
}

function ads_show(name, containerName) {
    if (typeof (ads_output[name]) == 'undefined') {
        return;
    } else {
        // if it is an html tag then it doesn't need decoding
        if (ads_output[name].substring(0, 1) == '<')
            pof_ads_$(containerName).innerHTML = ads_output[name];
        else    // decode and write
        {
            var pof_ads_iframe = document.createElement("iframe");
            pof_ads_iframe.setAttribute("frameBorder", "0");
            pof_ads_iframe.setAttribute("scrolling", "no");
            pof_ads_iframe.setAttribute("width", "100%");
            pof_ads_iframe.setAttribute("height", "100%");
            pof_ads_iframe.setAttribute("vspace", "0");
            pof_ads_iframe.setAttribute("hspace", "0");
            pof_ads_iframe.setAttribute("allowTransparency", "true");
            pof_ads_iframe.setAttribute("marginWidth", "0");
            pof_ads_iframe.setAttribute("marginHeight", "0");
            pof_ads_iframe.style.border = "none";
            pof_ads_iframe.style.margin = "0px";
            pof_ads_iframe.style.padding = "0px";
            pof_ads_iframe.style.width = "100%";
            pof_ads_iframe.style.height = "100%";
            pof_ads_$(containerName).appendChild(pof_ads_iframe);

            if (pof_ads_iframe.contentDocument)
                pof_ads_iframe.contentDocument.write("<html><head></head><body style=\"margin:0;padding:0\"><center>" + decodeBase64(ads_output[name]) + "</center></body></html>");
            else if (pof_ads_iframe.contentWindow)
                pof_ads_iframe.contentWindow.document.write("<html><head></head><body style=\"margin:0;padding:0\"><center>" + decodeBase64(ads_output[name]) + "</center></body></html>");
        }
    }
}

function ads_can_show(name) {
    try {
        if (typeof (ads_output[name]) == 'undefined')
            return false;
        return true;
    } catch (err) {
    
    }
    return false;
}

var FlashHeed = (function (window) {

    var document = window.document;

    var gsub = function (string, pattern, replacement) {
        var result = '', source = string, match;

        try {

            while (source.length > 0) {
                if (match = source.match(pattern)) {
                    result += source.slice(0, match.index);
                    result += replacement;
                    source = source.slice(match.index + match[0].length);
                } else {
                    result += source, source = '';
                }
            }

        } catch (err) {
            result = '';
        }
        return result;
    };

    var heed = function (el) {

        try {

            if (el === undefined || el === null) var el = document;


            var objects = el.getElementsByTagName('object');
            var len = objects.length;
            var i;

            for (i = 0; i < len; i++) {
                var o = objects[i];
                var params = o.getElementsByTagName('param');
                var params_length = params.length;
                var embeds = o.getElementsByTagName('embed');
                var embed = null;
                if (embeds.length > 0) var embed = embeds[0];

                // Handle embed tag (for non-IE)
                if (embed) {
                    // Need to set the embed wmode attribute
                    // In this case, we need to remove and re-add the child node
                    embed.setAttribute('wmode', 'transparent');
                    var nx = embed.nextSibling, pn = embed.parentNode;
                    pn.removeChild(embed);
                    pn.insertBefore(embed, nx);
                }

                // Handle param tags (for IE)
                var correct_wmode_found = false;
                var incorrect_wmode_found = false;

                for (var j = 0; j < params_length; j++) {
                    if (params[j].name === 'wmode') {
                        if (/transparent/i.test(params[j].value) || /opaque/i.test(params[j].value)) {
                            // an existing wmode with "transparent" or "opaque" is found
                            correct_wmode_found = true;
                        } else {
                            incorrect_wmode_found = true;
                        }
                    }
                }

                if (!correct_wmode_found || incorrect_wmode_found) {
                    var html = o.outerHTML;
                    var nx = o.nextSibling, pn = o.parentNode;

                    // Do a string replacement for a window param that IE injects be default to the innerhtml
                    html = gsub(html, /<param name="wmode".*?>/i, '');

                    // Add the correct transparent wmode param
                    html = gsub(html, /<\/object>/i, '<PARAM NAME="WMode" VALUE="Transparent"></object>');

                    // Totally remove the object tag from the dom
                    pn.removeChild(o);

                    // Add it to our new div, only to clobber it immediately.
                    // This is the only way we've found to force IE to unrender the object.
                    // We use a new container for this because you can't mess with the innerhtml
                    // of an object tag. (throws a runtime error)
                    var div = document.createElement("div");
                    div.appendChild(o);
                    div.innerHTML = '';

                    // Update it with our new HTML that has the correct param tag
                    div.innerHTML = html;

                    // Finally, insert this new div back in the original spot
                    pn.insertBefore(div, nx);
                }
            }

        } catch (err) {

        }
    }

    return {
        heed: heed
    }
})(window);
