var isIE = navigator.appVersion.match(/MSIE/) == "MSIE";

var WindowUtilities = {
  // From script.aculo.us
  getWindowScroll: function() {
    var w = window;
      var T, L, W, H;
      with (w.document) {
        if (w.document.documentElement && documentElement.scrollTop) {
          T = documentElement.scrollTop;
          L = documentElement.scrollLeft;
        } else if (w.document.body) {
          T = body.scrollTop;
          L = body.scrollLeft;
        }
        if (w.innerWidth) {
          W = w.innerWidth;
          H = w.innerHeight;
        } else if (w.document.documentElement && documentElement.clientWidth) {
          W = documentElement.clientWidth;
          H = documentElement.clientHeight;
        } else {
          W = body.offsetWidth;
          H = body.offsetHeight
        }
      }
      return { top: T, left: L, width: W, height: H };
    
  }, 
  
  
  //
  // getPageSize()
  // Returns array with page width, height and window width, height
  // 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;
  	}	
  	var pageHeight, pageWidth;

  	// 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;
  	}

  	return {pageWidth: pageWidth ,pageHeight: pageHeight , windowWidth: windowWidth, windowHeight: windowHeight};
  },
  
  
  //
  // Enable fade
  //
  enableFade:function() {
	var size = this.getPageSize();
	
	var fade = document.createElement("div");
	fade.id = "fade";
	fade.style.height = size.pageHeight+"px";
	fade.style.opacity = "0";
	
	document.getElementsByTagName("body")[0].appendChild(fade);
	document.getElementsByTagName("body")[0].insertBefore(fade,document.getElementsByTagName("body")[0].firstChild);
	
	Event.observe(window, "resize", this.updateSizeFade);
  },
  
  
  //
  // Disable fade
  //
  disableFade:function() {
	document.getElementsByTagName("body")[0].removeChild(document.getElementById("fade"));
  },
  
  
  //
  // Resize fade
  //
  updateSizeFade:function() {
	var size = WindowUtilities.getPageSize();
	document.getElementById("fade").style.height = size.pageHeight+"px";
  },
  
  
  // 
  // Create centered div on top
  //
  createCenteredDiv:function(content) {
    var windowScroll = this.getWindowScroll(); 
    var pageSize = this.getPageSize();
    
	var centeredDiv = document.createElement("div");
	centeredDiv.id = "notice";
	centeredDiv.innerHTML = content;
	centeredDiv.style.height = pageSize.windowHeight +"px";
	centeredDiv.style.top = windowScroll.top +"px";
	centeredDiv.style.opacity = "0";
	
	document.getElementsByTagName("body")[0].appendChild(centeredDiv);
	document.getElementsByTagName("body")[0].insertBefore(centeredDiv,document.getElementsByTagName("body")[0].firstChild);
	
	Event.observe(window, "scroll", this.updateCenteredDiv);
	Event.observe(window, "resize", this.updateCenteredDiv);
  },
  
  
  //
  // Update centered div
  //
  updateCenteredDiv:function() {
	var windowScroll = WindowUtilities.getWindowScroll(); 
	var size = WindowUtilities.getPageSize();
    document.getElementById("notice").style.height = size.windowHeight + "px";  
  	document.getElementById("notice").style.top = windowScroll.top + "px";  
  },
  
  
  //
  // Clear centered div
  //
  clearCenteredDiv:function() {
	document.getElementsByTagName("body")[0].removeChild(document.getElementById("notice"));  
  },
  
  
  //
  // Show a notice
  //
  noticeEnabled:false,
  notice:function(content) {
  	if (this.noticeEnabled == false) {
	  	this.noticeEnabled = true;
		this.createCenteredDiv(content);
		this.enableFade();
		new Effect.Appear("fade", { duration: 0.4, to:0.7 });
		new Effect.Appear("notice", { duration: 0.4, queue: 'end'});
		Event.observe(document.getElementById("notice"), "click", function() {WindowUtilities.disableNotice();});
	}
  },  
  
  
  //
  // Disable notice
  //
  disableNotice:function() {
	WindowUtilities.clearCenteredDiv();
	WindowUtilities.disableFade();
	WindowUtilities.noticeEnabled = false;
  }
}
