function sellfy_close_frame() {
    document.body.removeChild(sellfy_iframe);   
}
window.addEventListener("message", function (event) {
    if (event.data === "close_frame") {
        sellfy_close_frame();
    }
});
function sellfy_show_iframe(e) {
    e.preventDefault();
    var loading = document.createElement('img');
    loading.id = "sellfy-loading";
    loading.src =  "data:image/gif;base64,[encoded data, 12572 characters]";
    loading.style.position = "fixed";
    loading.style.top = "50%";
    loading.style.left = "50%";
    loading.style.zIndex = "9999";
    loading.style.padding = "5px";
    loading.style.border = "1px solid #CCC";
    loading.style.borderRadius = "4px";
    loading.style.margin = "-21px 0 0 -21px";
    loading.style.backgroundColor = "rgba(255, 255, 255, 0.5)";
    document.body.appendChild(loading);
    
    var href = e.currentTarget.getAttribute("data-href");
    if ("href" in e.currentTarget.attributes) {
        href = e.currentTarget.attributes.href.value;
    }
    sellfy_iframe = document.createElement("iframe");
    sellfy_iframe.setAttribute("src", href + "?window=iframe");
    sellfy_iframe.style.width = "100%";
    sellfy_iframe.style.height = "100%";
    sellfy_iframe.style.top = "0";
    sellfy_iframe.style.bottom = "0";
    sellfy_iframe.style.left = "0";
    sellfy_iframe.style.right = "0";
    sellfy_iframe.style.border = "none";
    sellfy_iframe.style.visibility = "hidden";
    sellfy_iframe.style.backgroundColor = "none transparent";
    sellfy_iframe.style.overflow = "hidden";
    sellfy_iframe.style.position = "fixed";
    sellfy_iframe.style.zIndex = "9999";
    sellfy_iframe.onload = function() {
        this.style.visibility = "visible";
        var load = document.getElementById("sellfy-loading");
        if(load) {
            document.body.removeChild(load);
        }
    }
    document.body.appendChild(sellfy_iframe);
    return false;
}
function sellfy_init_buttons() {
    var allHTMLTags = document.getElementsByTagName("*"), index = 0;
    if (typeof window.slfy_btns === 'undefined') {
        window.slfy_btns = [];
    }
    for (i = 0; i < allHTMLTags.length; i++) {
        var classes = allHTMLTags[i].className.split(" ");
        if (classes[0] === "sellfy-buy-button") {
            var activated = allHTMLTags[i].className.indexOf('slfy_activated') !== -1 ? true : false;
            if (!activated) {
                allHTMLTags[i].className += " slfy_activated";
                window.slfy_btns.push(allHTMLTags[i]);
            }
        }
    }
    function loadBtn() {
        if (!window.slfy_btns.length) {
            return false;
        }
        var button = window.slfy_btns.shift();
        if (button.className.indexOf("slfy_load_started") !== -1) {
            if (window.slfy_btns.length) {
                loadBtn();
            }
        }
        button.className += " slfy_load_started";
        var url = button.getAttribute("data-href");
        if ("href" in button.attributes) {
            url = button.attributes.href.value;
        }
        var id = url.substring(url.indexOf("/p/") + 3).replace("/", "");
        url = url.substring(0, url.indexOf("/p/") + 1);
        if (id !== 'undefined' && button.className.indexOf('sellfy-buy-button-custom') === -1) {
            var bW = 162, bH = 32;
            button.style.width = bW + "px";
            button.style.height = bH + "px";
            button.innerHTML = '<iframe name="sellfy_button_iframe" id="sellfy_button_iframe" style="width:170px; max-width:170px!important; height:32px; max-height: 32px !important;  overflow:hidden; margin: 0" src="" allowtransparency="true" scrolling="no" frameborder="0"/>';
            var ifr = button.getElementsByTagName('iframe')[0], invDiv = document.createElement('div');
            ifr.src = url + 'buttons/' + id + '/';
            invDiv.style.width = "162px";
            invDiv.style.height = "32px";
            invDiv.style.position = "absolute";
            invDiv.style.left = "0";
            invDiv.style.top = "0";
            invDiv.style.opacity = "0";
            button.appendChild(invDiv);
        }
        if (button.className.indexOf('in-new-page') === -1) {
            button.addEventListener("click", sellfy_show_iframe, false);
        }
        button.style.position = "relative";
        button.style.overflow = "hidden";
        button.style.display = "inline-block";
        loadBtn();
    }
    loadBtn();
}
sellfy_init_buttons();