function sellfy_saveDocumentOverflow(){
    if (document.currentStyle){
        documentOverflow = document.documentElement.currentStyle.overflowY;
    } else if (document.defaultView && document.defaultView.getComputedStyle) {
        documentOverflow = document.defaultView.getComputedStyle(document.documentElement).overflowY;
    } else {
        documentOverflow = '';
    }
}
function sellfy_setDocumentOverflow(value){
    if(typeof value === 'undefined'){
        value = documentOverflow;
    }
    document.documentElement.style.overflowY = value;
}
function sellfy_close_frame() {
    if(document.body.contains(sellfy_iframe_wrap)){
        document.body.removeChild(sellfy_iframe_wrap);
    }
    sellfy_setDocumentOverflow();
}
if(window.addEventListener){
    window.addEventListener('message', function (event) {
        if (event.data === 'close_frame') {
            sellfy_close_frame();
        }
    });
} else {
    window.attachEvent('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;
    }
    href = href.replace(/\/$/,'');
    sellfy_iframe = document.createElement('iframe');
    sellfy_iframe.setAttribute('src', href + '/iframe/');
    sellfy_iframe.style.width = '100%';
    sellfy_iframe.style.height = '100%';
    sellfy_iframe.style.display = 'block';
    sellfy_iframe.style.backgroundColor = 'none transparent';
    sellfy_iframe.style.visibility = 'hidden';
    sellfy_iframe.style.border = 'none';
    sellfy_iframe.onload = function() {
        this.style.visibility = 'visible';
        var load = document.getElementById('sellfy-loading');
        if(load) {
            document.body.removeChild(load);
        }
    };
    sellfy_iframe_wrap = document.createElement('div');
    sellfy_iframe_wrap.style.width = '100%';
    sellfy_iframe_wrap.style.height = '100%';
    sellfy_iframe_wrap.style.top = '0';
    sellfy_iframe_wrap.style.bottom = '0';
    sellfy_iframe_wrap.style.left = '0';
    sellfy_iframe_wrap.style.right = '0';
    sellfy_iframe_wrap.style.position = 'fixed';
    sellfy_iframe_wrap.style.overflow = 'auto';
    sellfy_iframe_wrap.style.zIndex = '9999';
    sellfy_iframe_wrap.style.WebkitOverflowScrolling = 'touch';
    document.body.appendChild(sellfy_iframe_wrap);
    sellfy_iframe_wrap.appendChild(sellfy_iframe);
    sellfy_setDocumentOverflow('hidden');
    return false;
}
function sellfy_init_buttons() {
    sellfy_saveDocumentOverflow();
    var allHTMLTags = document.getElementsByTagName('*');
    if (typeof window.slfy_btns === 'undefined') {
        window.slfy_btns = [];
    }
    for (i = 0; i < allHTMLTags.length; i++) {
        var classes = allHTMLTags[i].className.toString().split(' ');
        if (classes[0] === 'sellfy-buy-button') {
            var activated = allHTMLTags[i].className.toString().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();
