/*-----------------------------------------------------------------------------------------------
 * Unobtrusive popup links
 * 
 *      Usage:  Add the following code in the head of your HTML document:
 *
 *       <script src="/scripts/splash.js" type="text/javascript"></script>
 *       <script type="text/javascript">
 *            function initSplash(){
 *                var mySplash = new Splash("http://www.myServer.com/splashPage.html");
 *            }
 *            Event.observe(window, 'load', initSplash, false);
 *        </script>
 *
 *   Requires:  prototype.js version 1.6+
 * Created by:  Mark Adams
 *-----------------------------------------------------------------------------------------------*/


var Splash = Class.create({

    initialize: function(href) {
        this.href = href;
        this.addSplashMarkup();
        this.loadSplashContent();
    },

    addSplashMarkup: function() {
        var objBody = document.getElementsByTagName('body').item(0);
        
        var objOverlay = document.createElement('div');
        objOverlay.setAttribute('id', 'splashOverlay');
        objOverlay.style.display = 'block';
        objOverlay.style.position = 'absolute';
        objOverlay.style.top = '0px';
        objOverlay.style.left = '0px';
        objOverlay.style.width = '100%';
        if (!Prototype.Browser.IE) {
            objOverlay.setOpacity(0.8);
        }
        objOverlay.style.filter = 'alpha(opacity=80)';
        var pageSize = this.getPageSize();
        objOverlay.style.height = Math.max(pageSize.pageHeight, 0) + 'px';
        objOverlay.style.backgroundColor = 'black';
        objOverlay.style.zIndex = '5001';
        objBody.appendChild(objOverlay);

        var objContent = document.createElement('div');
        objContent.setAttribute('id', 'splashContent');
        objContent.style.display = 'block';
        objContent.style.position = 'absolute';
        objContent.style.top = '15%';
        objContent.style.left = '15%';
        objContent.style.width = '700px';
        objContent.style.height = '285px';
        objContent.style.padding = '16px';
        objContent.style.border = '2px solid #D74119';
        objContent.style.backgroundColor = 'white';
        objContent.style.zIndex = '5002';
        objContent.style.overflow = 'auto';
        objBody.appendChild(objContent);
    },

	loadSplashContent: function() {
		var myAjax = new Ajax.Request(this.href, {
            method: 'get',
            parameters: "",
            onSuccess: this.processSplashContent.bindAsEventListener(this)
        });
		
	},

	processSplashContent: function(response){
		$('splashContent').innerHTML = response.responseText;
        $('splashClose').observe('click',this.closeSplash);
		this.displaySplash();
	},

    displaySplash: function() {
        $$('select').each(function(el){el.style.visibility="hidden";});
        $('splashOverlay').show();
        $('splashContent').show(); 
    },

    closeSplash: function(event) {
        $('splashOverlay').hide();
        $('splashContent').hide();        
        $$('select').each(function(el){el.style.visibility="visible";});
        Event.stop(event); 
        return false;
    },

	//
	// getPageSize()
	// Returns array with page width, height and window width, height
	// Core code from - quirksmode.org
	// Edit for Firefox by pHaez
	//
	getPageSize: function(){
		var scrollX,scrollY,windowX,windowY,pageX,pageY;
		if (window.innerHeight && window.scrollMaxY) {	
			scrollX = document.body.scrollWidth;
			scrollY = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			scrollX = document.body.scrollWidth;
			scrollY = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			scrollX = document.body.offsetWidth;
			scrollY = document.body.offsetHeight;
		}
		
		if (self.innerHeight) {	// all except Explorer
			windowX = self.innerWidth;
			windowY = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowX = document.documentElement.clientWidth;
			windowY = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowX = document.body.clientWidth;
			windowY = document.body.clientHeight;
		}	
		
		pageY = (scrollY < windowY) ? windowY : scrollY; // for small pages with total height less then height of the viewport
		pageX = (scrollX < windowX) ? windowX : scrollX; // for small pages with total width less then width of the viewport
	
		return {pageWidth:pageX,pageHeight:pageY,winWidth:windowX,winHeight:windowY};
	}

});
