var DMC = function() {
    var dmcInitLinkSrc = '';
    var _currentOrderId = '';
    if (window.onload != null) {
        var prev = window.onload
        window.onload = function() {
            prev();
            init();
        }
    } else {
        window.onload = init;
    }

    var prototypeFW = document.createElement("script");
    prototypeFW.type = "text/javascript";
    prototypeFW.src = "http://www.discrevolt.com/dmc/store/includes/prototype.js";

    var head = document.getElementsByTagName("head")[0];
    if (navigator.userAgent.indexOf("MSIE") > -1) {
        prototypeFW.onreadystatechange = scriptLoadedClosureFix(prototypeFW.src);
    } else {
        prototypeFW.onload = scriptLoadedClosureFix(prototypeFW.src);
    }
    head.appendChild(prototypeFW);

    function scriptLoadedClosureFix(filename) {
        return function(evt) {
            if (navigator.userAgent.indexOf("MSIE") > -1) {
                //Ok, so in IE, the onreadystatechange event fires for any state change, we need to single out the one where
                //the script element is not 'loading'
                var theScripts = document.scripts;
                var scriptElmt;
                for (var i = 0; i < theScripts.length; i++) {
                    if (theScripts[i].src == filename) {
                        //we have the right script tag
                        scriptElmt = theScripts[i];
                        break;
                    }
                }

                //only do the onScriptLoaded if the script is loaded
                if (scriptElmt.readyState != 'loading') {
                    document.observe('dom:loaded', init);
                }
            } else {
                document.observe('dom:loaded', init);
            }
        }
    }

    function init() {
        createBlankIframe('DMC', 'about');
        var links = document.getElementsByTagName("a");
        for (var i = 0; i < links.length; i++)
            if (links[i].className === "dmcInitializer")
            new DmcLoader(links[i]);

        var imageMaps = document.getElementsByTagName("area");
        for (var i = 0; i < imageMaps.length; i++)
            if (imageMaps[i].className === "dmcInitializer")
            new DmcLoader(imageMaps[i]);
    }

    function createBlankIframe(id, name) {
        var iframe = document.createElement("iframe");
        iframe.id = id;
        iframe.width = "100%";
        iframe.height = "100%";
        iframe.name = name;
        iframe.src = "#";
        iframe.frameBorder = 'No';
        iframe.style.display = 'none';
        iframe.allowTransparency = true;

        document.body.insertBefore(iframe, document.body.firstChild);

        if (document.frames && document.frames[iframe.id] && document.frames[iframe.id].document) {
            document.frames[iframe.id].document.onreadystatechange = function(evt) {
                if ((document.frames[iframe.id].document.readyState) == 'complete') {
                    document.frames[iframe.id].document.body.style.border = 0;
                }
            }
        } else {
            iframe.style.border = 0;
        }
        return iframe;
    }

    return {
        checkForClose: function(artistId) {
            if (window.location.hash === '#closedmc') {
                document.getElementById('DMC').style.left = '-3000px';
                document.getElementById('DMC').style.display = 'none';
                if (document.getElementById('DMCKeepShop')) {
                    document.getElementById('DMCKeepShop').style.left = '-3000px';
                    document.getElementById('DMCKeepShop').style.display = 'none';
                }
                window.location.hash = '';
            } else if (window.location.hash.indexOf("#viewCart") > -1) {
                document.getElementById('DMC').src = 'https://secure.discrevolt.net/dmc/store/default.aspx?if=true&is=cart&o=' + _currentOrderId + '&a=' + artistId + '&referrer=' + encodeURIComponent(window.location.href);
                _currentOrderId = window.location.hash.substr(9);
                document.getElementById('DMCKeepShop').style.left = '-3000px';
                document.getElementById('DMCKeepShop').style.display = 'none';
                document.getElementById('DMC').style.display = '';
                document.getElementById('DMC').style.left = '0px';
                window.scrollTo(0, 0);
                window.location.hash = '';
                setTimeout('DMC.checkForClose("' + artistId + '")', 1500);
            } else if (window.location.hash.indexOf("#closeAndContinue") > -1) {
                _currentOrderId = window.location.hash.substr(17);
                document.getElementById('DMC').src = 'https://secure.discrevolt.net/dmc/store/empty.aspx';
                document.getElementById('DMC').style.left = '-3000px';
                document.getElementById('DMC').style.display = 'none';
                // look to see if we have an iframe already, if not create it
                if (!document.getElementById('DMCKeepShop')) {
                    createBlankIframe('DMCKeepShop');
                    document.getElementById('DMCKeepShop').width = "270px";
                    document.getElementById('DMCKeepShop').height = "80px";
                }
                document.getElementById('DMCKeepShop').src = 'https://secure.discrevolt.net/dmc/store/keepshopping.aspx?o=' + _currentOrderId + '&a=' + artistId + '&referrer=' + encodeURIComponent(window.location.href);
                document.getElementById('DMCKeepShop').style.display = '';
                document.getElementById('DMCKeepShop').style.right = '10px';
                document.getElementById('DMCKeepShop').style.left = '';
                window.location.hash = '';
                setTimeout('DMC.checkForClose("' + artistId + '")', 1500);
            } else {
                setTimeout('DMC.checkForClose("' + artistId + '")', 1500);
            }
        },
        currentOrderId: function() { return _currentOrderId; },
        initializeContent: function(wrappingElement) {
            if (wrappingElement) {
                wrappingElement.select(".dmcInitializer").each(function(item) {
                    new DmcLoader(item);
                });
            } else {
                $$(".dmcInitializer").each(function(item) {
                    new DmcLoader(item);
                });
            }
        }
    }
} ();

var DmcLoader = function(link) {
    var dmcInitLinkHref = link.href;
    var artistId;

    var tmpString = dmcInitLinkHref.substr(dmcInitLinkHref.indexOf('?') + 1);
    var paramArray = tmpString.split("&");
    for (var i = 0; i < paramArray.length; i++) {
        if (paramArray[i].substr(0, 2) == 'a=') {
            artistId = paramArray[i].substr(2);
            break;
        }
    }

    if (dmcInitLinkHref.indexOf('if=true') > -1) {
        link.onclick = loadDMC;
        link.href = "javascript:void(0);";
    }

    function loadDMC(evt) {
        if (document.getElementById('DMCKeepShop')) {
            document.getElementById('DMCKeepShop').style.left = '-3000px';
        }
        var iframeSrc = dmcInitLinkHref + "&referrer=" + encodeURIComponent(window.location.href)
        if (DMC.currentOrderId() != '') {
            iframeSrc += '&o=' + DMC.currentOrderId();
        }
        document.getElementById('DMC').src = iframeSrc;
        document.getElementById('DMC').style.display = '';
        document.getElementById('DMC').style.left = '0px';
        window.scrollTo(0, 0);
        setTimeout('DMC.checkForClose("' + artistId + '")', 1500);
        evt.returnValue = false;
        return false;
    }
};
