// This will stop image flickering in IE6 when elements with images are moved
try {
	document.execCommand("BackgroundImageCache", false, true);
} catch(e) {}

ShowMessage = Class.create({
	divOverlay: null,
	divWrap: null,
	divContent: null,
	divMessageBox: null,
						
	overlayOpacity: 0.8,
	overlayDuration: 0.2,
	
	dimensions : {
		viewport : {
			height : null,
			width : null,
			offsetTop : null,
			offsetLeft : null
		}
	},

	initialize: function() {
		this._browserDimensions();
		
		// Create overlay
		this.divOverlay = document.createElement('div');
		this.divOverlay.setAttribute('id','showmessage_overlay');
		this.divOverlay.style.display = 'none';
		
		this.divOverlay.style.backgroundColor = "#000000";
		this.divOverlay.style.top = '0px';
		this.divOverlay.style.left = '0px';
		this.divOverlay.style.width = '100%';
		this.divOverlay.style.height = '100%';
		this.divOverlay.style.zIndex = 50;
	
		// Set position
		this.divOverlay.style.position = 'absolute';
		
		// Add Contentwrapper
		this.divWrap = document.createElement('div');
		this.divWrap.setAttribute('id','showmessage_wrap');
		this.divWrap.style.display = 'none';
		this.divWrap.style.backgroundColor = "transparent";
		this.divWrap.style.textAlign = 'center'; 
		this.divWrap.style.position = 'absolute'; 
		this.divWrap.style.top = '0px';
		this.divWrap.style.left = '0px';
		this.divWrap.style.width = '100%';
		this.divWrap.style.zIndex = 51;
	
		// Add Content div
		this.divContent = document.createElement('div');;
		this.divContent.setAttribute('id','showmessage_content');
		this.divContent.style.position = 'relative'; 
		this.divContent.style.visible = 'none';
		this.divContent.style.backgroundColor = "#FFFFFF";
		this.divContent.style.backgroundImage = "url('/img/framework/noaccess.jpg')";
		this.divContent.style.margin = '0px auto';
		this.divContent.style.zIndex = 51;
		this.divContent.style.textAlign = 'center';
		
		// Add MessageBox div
		this.divMessageBox = document.createElement('div');;
		this.divMessageBox.setAttribute('id','showmessage_messagebox');
		this.divMessageBox.style.position = 'relative'; 
		this.divMessageBox.style.backgroundColor = "#FFFFFF";
		this.divMessageBox.style.margin = '0px auto';
		this.divMessageBox.style.border = '1px solid black';
		this.divMessageBox.style.zIndex = 52;
		this.divMessageBox.style.textAlign = 'center';
		this.divMessageBox.style.width = '500px';
		this.divMessageBox.style.marginTop = '390px';
		this.divMessageBox.style.padding = '5px';
//		this.divMessageBox.style.top = '0px';
//		this.divMessageBox.style.left = '0px';
		
	
		// Add to page
		document.body.appendChild(this.divOverlay);
		document.body.appendChild(this.divWrap);
		this.divWrap.appendChild(this.divContent);
		this.divContent.appendChild(this.divMessageBox);
	},

	// getPageScroll()
	// Returns array with x,y page scroll values.
	// Core code from - quirksmode.com
	//
	getPageScroll: function (){
	
		var xScroll, yScroll;
	
		if (self.pageYOffset) {
			yScroll = self.pageYOffset;
			xScroll = self.pageXOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
			yScroll = document.documentElement.scrollTop;
			xScroll = document.documentElement.scrollLeft;
		} else if (document.body) {// all other Explorers
			yScroll = document.body.scrollTop;
			xScroll = document.body.scrollLeft;	
		}
	
		arrayPageScroll = new Array(xScroll,yScroll) 
		return arrayPageScroll;
	},

	// Function by 
	// Returns array with page width, height and window width, height
	// by Lokesh Dhakar - http://www.huddletogether.com
	// Core code from - quirksmode.org
	// Edit for Firefox by pHaez
	getPageSize: function (){
		
		var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = document.body.scrollWidth;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
		if (self.innerHeight) {	// all except Explorer
			windowWidth = self.innerWidth;
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
	
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = windowWidth;
		} else {
			pageWidth = xScroll;
		}
	
	
		arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
		return arrayPageSize;
	},

	show: function(message) {
		// Fill Content
		this.divMessageBox.innerHTML = document.Labels.NoaccessMessage.escapeHTML() + document.Labels.RegisterLink;
		this.divMessageBox.innerHTML += '<br /><br /><a href="javascript:myShowMessage.close();">'+document.Labels.Close.escapeHTML() + '</a>';

		var arrayPageSize = this.getPageSize();
		var arrayPageScroll = this.getPageScroll();
	
		// set height to take up whole page
		this.divOverlay.style.height = (arrayPageSize[1] + 'px');
		this.divWrap.style.height = (arrayPageSize[1] + 'px');
	
		// Set content position
		this.divContent.style.top = arrayPageScroll[1] + 100 + 'px';
		
		// Show
		this.hideSelectBoxes();
		new Effect.Appear(this.divOverlay, { duration: this.overlayDuration, 
						  					 from: 0.0, 
											 to: this.overlayOpacity, 
											 afterFinish: this.showContentAfterOverlay.bindAsEventListener(this) });
		
	},
	
	showContentAfterOverlay: function(e) {
		Element.show(this.divWrap);
	},
	
	close: function () {
		this.divWrap.hide();
		new Effect.Fade(this.divOverlay, {duration: this.overlayDuration});
		this.showSelectBoxes();
	},
	
	showSelectBoxes: function (){
		var selects = document.getElementsByTagName("select");
		for (i = 0; i != selects.length; i++) {
			selects[i].style.visibility = "visible";
		}
	},
	
	// ---------------------------------------------------
	hideSelectBoxes: function (){
		var selects = document.getElementsByTagName("select");
		for (i = 0; i != selects.length; i++) {
			selects[i].style.visibility = "hidden";
		}
	},
	
	//
	//  Get the Browser Viewport Dimensions
	//
	_browserDimensions : function() {
		if (Prototype.Browser.IE) {
            this.dimensions.viewport.height = document.documentElement.clientHeight;
            this.dimensions.viewport.width = document.documentElement.clientWidth;   
        } else {
            this.dimensions.viewport.height = window.innerHeight;
            this.dimensions.viewport.width = document.width || document.body.offsetWidth;
        }
	}
});

Event.observe(window, 'load', showmessageInit, false);

//
//	Set up all of our links
//
var myShowMessage = null;
function showmessageInit() {
	myShowMessage = new ShowMessage();
}