getViewportDimensions=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 {width:pageWidth,height:pageHeight,winWidth:windowWidth, winHeight:windowHeight};
	}

setOverLayerDim=function(){
  dims=getViewportDimensions();
  //alert(dims.winWidth+'::'+dims.winHeight);
  
  $('overlayer').setStyle(
    {
      width:dims.width+'px',
      height:dims.height+'px'
    }
  );
}

setWindowPos=function(){
  scroll_offsets=document.viewport.getScrollOffsets();
  //alert(scroll_offsets.top+'::'+scroll_offsets.left)
  dim=getViewportDimensions();
  t=20;
  //if($())
  if($('window').getHeight() < dim.winHeight){
    t=Math.abs(dim.winHeight-$('window').getHeight())/2+scroll_offsets.top;
  }
  if($('window_container').visible())
    new Effect.Morph('window_container',{
      style:{top:t+'px'},
      duration:0.5
    });
  else
    $('window_container').setStyle({top:t+'px'})
}

showOverlayer=function(){
  setOverLayerDim();
  $('overlayer').visible() ? $('overlayer').hide() : $('overlayer').show()
}

loadWindow=function(event){
  Event.stop(event);
  showOverlayer();
  win_trigger=$(event.findElement('a.window-trigger'));
  ajax_url=win_trigger.readAttribute('href');
  win_title=win_trigger.readAttribute('title');
  
  if(win_title !='')
    $('window_title').update(win_title);
  new Ajax.Updater('window',ajax_url+'&ajax=1',{
    onComplete:function(){
      new Effect.Appear('window_container',
          {
          duration:0.5,
          afterFinish:setWindowPos
          }
      );
    },
    method:'post',
    evalScripts:true
  });
  
}

unloadWindow=function(event){
  if(event)
    Event.stop(event);
  new Effect.Fade('window_container',{
    duration:0.5,
    afterFinish:function(){
      $('window').update('');
      showOverlayer();
      $('window_container').setStyle({top:'0px'})
    }
  })
  
}

document.observe('dom:loaded',function(){
  $$('a.window-trigger').each(function(wt){
      Event.observe(wt,'click',loadWindow);
  });
  
   
  Event.observe($$('a.window-close').first(),'click',unloadWindow);
  Event.observe(window,'resize',setWindowPos);
  Event.observe(window,'resize',setOverLayerDim);
  Event.observe(window,'scroll',setWindowPos);
});