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.6, to:0.8 });
		new Effect.Appear("notice", { duration: 0.4, queue: 'end'});
	}
  },  
  
  
  //
  // Disable notice
  //
  disableNotice:function() {
	WindowUtilities.clearCenteredDiv();
	WindowUtilities.disableFade();
	WindowUtilities.noticeEnabled = false;
  },

  openPicture:function(url) {
  	if (this.noticeEnabled == false) {
	  	this.noticeEnabled = true;
		this.createCenteredDiv();
		
		var im = new Image();
		im.src = url;
		setTimeout(function() {WindowUtilities.isPictureLoaded(im);}, 100);
		
		this.enableFade();
		new Effect.Appear("fade", { duration: 0.6, to:0.8 });
		$('notice').style.opacity = "1.0";
		$('notice').innerHTML = "<div id='photo_loading'>En chargement..</div>";
		Event.observe(document.getElementById("notice"), "click", function() {WindowUtilities.disableNotice();});
	}
  },
	
  isPictureLoaded:function(im) {
	if (im.complete) {
		var image = document.createElement("img");
		image.id = "photo_enlarge";
		image.src = im.src;
		
		var image_close = document.createElement('div');
		image_close.innerHTML = "Close";
		image_close.id = "photo_close";
		
		var image_container = document.createElement('div');
		image_container.id = "photo_container";
		image_container.style.opacity = "0.0";
		image_container.style.position = "absolute";
		image_container.style.left = image_container.style.top = "50%";
		image_container.style.marginLeft = "-"+(im.width/2)+"px";
		image_container.style.marginTop = "-"+(im.height/2)+"px";
		image_container.appendChild(image);
		image_container.appendChild(image_close);
		
		
		$('notice').innerHTML = "";
		$('notice').insertBefore(image_container,$('notice').firstChild);
		
		new Effect.Appear("photo_container", { duration: 0.4, queue:'end'});
	}
	else {
		setTimeout(function() {WindowUtilities.isPictureLoaded(im);}, 100);
	}
  }
}


function openPicture(url) {
	WindowUtilities.openPicture(url);
}

function disableNotice() {
	WindowUtilities.disableNotice();
}