// JavaScript Document

function splashScreen() {
  this.config = {
	  overlayDuration : 0.7,
	  popupOpenDuration : 1,
	  popupHeaderHeight: 22
  };
	
  var objBody = $$('body')[0];
  objBody.appendChild(Builder.node('div',{id:'splashScreenOverlay'}));

  objBody.appendChild(Builder.node('div',{id:'splashScreenContainer'}, [
            Builder.node('iframe',{id:'splashScreenDocument',frameborder:'0',marginheight:'0',marginwidth:'0',scrolling:'no'})
	    ]
  ));
  
  this.bindedScrollAction=null;
  this.bindedResizeAction=null;
  this.onScrollEffect=null;
  
  $('splashScreenOverlay').hide();
  $('splashScreenContainer').hide();
  $('splashScreenDocument').hide();
}

splashScreen.prototype.setContainerGeometry=function(w,h) {
  var wsize=this.getPageSize();
  var viewportSize=this.getViewPortWH();
  var scrollOffset=this.getScrollXY();
  $('splashScreenContainer').setStyle({
	width: w+'px',
	height: h+'px',
	left: ((wsize[0]-w)/2)+'px',
	top: ((viewportSize[1]-h)/2+scrollOffset[1])+'px'
  });
  return true;
}

splashScreen.prototype.ouvre=function(url,w,h) {
  var wsize=this.getPageSize();
  $('splashScreenOverlay').setStyle({
	width: wsize[0]+'px',
	height: wsize[1]+'px'
  });
    
  $('splashScreenDocument').hide();
  $('splashScreenDocument').setStyle({
	top: '0px',
	left: '0px',
	width: w+'px',
	height: (h+this.config.popupHeaderHeight)+'px'
  });
  $('splashScreenDocument').src=url;
  
  new Effect.Appear($('splashScreenOverlay'), { duration: this.config.overlayDuration, from: 0.0, to: 0.5 });
  
  this.setContainerGeometry(10,10);
  $('splashScreenContainer').show();

  var xScale = (w/$('splashScreenContainer').getWidth())*100;
  var yScale = ((h+this.config.popupHeaderHeight)/$('splashScreenContainer').getHeight())*100;
  var d=this.config.popupOpenDuration/2;

  new Effect.Scale($('splashScreenContainer'), xScale, {scaleY: false, scaleFromCenter: true, duration: d });
  new Effect.Scale($('splashScreenContainer'), yScale, {scaleX: false, scaleFromCenter: true, duration: d, delay: d});

  new Effect.Appear($('splashScreenDocument'), { duration: 0.5, from: 0.0, to: 1, delay: this.config.popupOpenDuration });

  if (this.bindedScrollAction==null)
    this.bindedScrollAction=this.scrollAction.bindAsEventListener(this);
  Event.observe(window,'scroll', this.bindedScrollAction);
  if (this.bindedResizeAction==null)
    this.bindedResizeAction=this.resizeAction.bindAsEventListener(this);
  Event.observe(window,'resize', this.bindedResizeAction);
  
}
splashScreen.prototype.ferme=function() {
  Event.stopObserving(window,'scroll', this.bindedScrollAction);   
  Event.stopObserving(window,'resize', this.bindedResizeAction);   
  $('splashScreenDocument').src='/blank.html';
  $('splashScreenOverlay').hide();
  $('splashScreenContainer').hide();
}

splashScreen.prototype.scrollAction=function(event) {
  var h=$('splashScreenContainer').getHeight();
  var viewportSize=this.getViewPortWH();
  var scrollOffset=this.getScrollXY();
  var l=parseInt($('splashScreenContainer').getStyle('left'));
  //$('splashScreenContainer').setStyle({ top: ((viewportSize[1]-h)/2+scrollOffset[1])+'px' });
  if (this.onScrollEffect!=null) this.onScrollEffect.cancel();
  this.onScrollEffect=new Effect.Move($('splashScreenContainer'),{x: l, y: ((viewportSize[1]-h)/2+scrollOffset[1]), mode: 'absolute'});
  return true;
}
splashScreen.prototype.resizeAction=function(event) {
  var wsize=this.getPageSize();
  $('splashScreenOverlay').setStyle({
	width: wsize[0]+'px',
	height: wsize[1]+'px'
  });
  var viewportSize=this.getViewPortWH();
  var scrollOffset=this.getScrollXY();
  var l=((wsize[0]-$('splashScreenContainer').getWidth())/2);
  var t=((viewportSize[1]-$('splashScreenContainer').getHeight())/2+scrollOffset[1])
  if (this.onScrollEffect!=null) this.onScrollEffect.cancel();
  this.onScrollEffect=new Effect.Move($('splashScreenContainer'),{x: l, y: t, mode: 'absolute'});

  //var h=parseInt($('splashScreenContainer').getStyle('left'));

  //this.setContainerGeometry(10,10);

  /*
  var containerSize=$('splashScreenContainer').getDimensions();
alert(containerSize);
var h=$('splashScreenContainer').getHeight();
  var viewportSize=this.getViewPortWH();
  var scrollOffset=this.getScrollXY();
  var l=parseInt($('splashScreenContainer').getStyle('left'));
  //$('splashScreenContainer').setStyle({ top: ((viewportSize[1]-h)/2+scrollOffset[1])+'px' });
  if (this.onScrollEffect!=null) this.onScrollEffect.cancel();
  this.onScrollEffect=new Effect.Move($('splashScreenContainer'),{x: l, y: ((viewportSize[1]-h)/2+scrollOffset[1]), mode: 'absolute'});
  */
  return true;
}

splashScreen.prototype.getScrollXY=function() {  
  // http://www.howtocreate.co.uk/tutorials/javascript/browserwindow
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}
splashScreen.prototype.getViewPortWH=function() {  
  // http://www.howtocreate.co.uk/tutorials/javascript/browserwindow
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return [ myWidth, myHeight ];
}

splashScreen.prototype.getPageSize=function() {  
  var xScroll, yScroll;
  if (window.innerHeight && window.scrollMaxY) {	
    xScroll = window.innerWidth + window.scrollMaxX;
    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
    if(document.documentElement.clientWidth){
      windowWidth = document.documentElement.clientWidth; 
    } else {
      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 = xScroll;		
  } else {
    pageWidth = windowWidth;
  }

  return [pageWidth,pageHeight];
};
