var DMC = function(){
	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(){
			if(window.location.hash === '#closedmc'){
				document.getElementById('DMC').style.left = '-3000px';
				window.location.hash = '';
			} else {
				setTimeout('DMC.checkForClose()', 1500);
			}
		}
	}
}();

var DmcLoader = function(link){
	var dmcInitLinkHref = link.href;
	
	link.href = "javascript:void(0);";
	if(dmcInitLinkHref.indexOf('if=true') > -1){
		link.onclick = loadDMC;
	}	

    function loadDMC(evt){
    	document.getElementById('DMC').src = dmcInitLinkHref + "&referrer=" + encodeURIComponent(window.location.href);
		document.getElementById('DMC').style.display = '';
		document.getElementById('DMC').style.left = '0px';
		window.scrollTo(0,0);
		setTimeout('DMC.checkForClose()', 1500);
		evt.returnValue = false;
		return false;
    }    
};