// $Id$

var Viewer = {
   url_ : undefined,
   w_ : 640,
   h_ : 480,
   
   
   open : function(url,width,height,bgcolor) {
      Viewer.url_ = url;
      if(width) Viewer.w_ = width;
      if(height) Viewer.h_ = height;
      if(!bgcolor) bgcolor = 0x000000;
      //
      var size = Viewer.getPageSize();
      var scrollOffset = Viewer.getScrollOffset();

      new Insertion.Bottom('container','<div id="bg"></div>');
      new Insertion.Bottom('container','<div id="viewer-container"><div id="viewer"></div></div>');
      $('viewer-container').setStyle({
            'display'   : 'block',
            'position'  : 'absolute',
            'zIndex'    : 2,
            'left'      : (size.windowWidth-Viewer.w_)/2+scrollOffset.xOffset+'px',
            'top'       : (size.windowHeight-Viewer.h_)/2+scrollOffset.yOffset+'px',
            'width'     : Viewer.w_+'px',
            'height'    : Viewer.h_+'px'
      });
      //
      $('bg').setStyle({
            'display'           : 'block',
            'position'          : 'absolute',
            'zIndex'            : 1,
            'left'              : '0px',
            'top'               : '0px',
            'width'             : size.pageWidth+'px',
            'height'            : size.pageHeight+'px',
            'backgroundColor'   : '#000',
            'filter'            : 'alpha(opacity=50)',
            'opacity'           : 0.5
      });
      //
      Event.observe(window,'resize',Viewer.onresize,false);
      Event.observe(window,'scroll',Viewer.onscroll,false);
      Event.observe('bg','click',Viewer.onclick);
      // flash
      var has_param = Viewer.url_.indexOf('?');
      var url_extract = has_param > 0 ? Viewer.url_.substr(0,has_param) : Viewer.url_;
      var ext = url_extract.substr(Viewer.url_.lastIndexOf('.')+1);
      swfobject.embedSWF("/ui/"+ext+".swf", "viewer", Viewer.w_, Viewer.h_, "9", null, {url:Viewer.url_});
//       var s = new SWFObject("/ui/"+ext+".swf", "viewer", Viewer.w_, Viewer.h_, "9", bgcolor);
//       s.addVariable("url",Viewer.url_);
//       s.write("viewer-container");
   },
   close : function() {
      Event.stopObserving(window,'resize',Viewer.onresize);
      Event.stopObserving(window,'scroll',Viewer.onscroll);
      Event.stopObserving('bg','click',Viewer.onclick);
      $('viewer').remove();
      $('bg').remove();
      $('viewer-container').remove();
   },
   onclick : function() {
      Viewer.close();
   },
   onscroll : function() { 
      Viewer.onresize();
   },
   onresize : function() {
      var size = Viewer.getPageSize();
      var scrollOffset = Viewer.getScrollOffset();
      $('bg').setStyle({
         width:size.pageWidth+'px',
         height:size.pageHeight+'px'});
      $('viewer-container').setStyle({
         left:(size.windowWidth-Viewer.w_)/2+scrollOffset.xOffset+'px',
         top:(size.windowHeight-Viewer.h_)/2+scrollOffset.yOffset+'px'});
   },
   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      :pageWidth,
         pageHeight     :pageHeight,
         windowWidth    :windowWidth,
         windowHeight   :windowHeight
      };
   },
   getScrollOffset : function() {
      var arrayPageScroll = document.viewport.getScrollOffsets();
      return { xOffset:arrayPageScroll[0],yOffset:arrayPageScroll[1] };
   }
   
}; 
