/************************
(c)2010 Webuddha, Holodyn Corporation
************************/

var wbPopAd = function( elm, config ){

  this.config = Object.extend({
    url:    '',
    top:    0,
    left:   0,
    width:  640,
    height: 480,
    offsetTop: 100,
    scroll: false,
    parent: null
    },config);
  this.ready      = true;
  this.overlay    = null;
  this.container  = null;
  this.frame      = null;
  this.buttons    = {};

  this.show = function(){
    if( this.ready ){
      this.showOverlay();
      this.showContainer();
    } else
      this.msg('Not Ready for Display');
  };

  /**************************************************************************/
  this.showOverlay = function(){
    if( !Object.isElement(this.overlay) ){
      this.overlay = document.createElement('div');
      Element.extend(this.overlay);
      this.overlay.addClassName('wbpopad_overlay').hide();
      // document.body.insertBefore(this.overlay,document.body.firstChild);
      document.body.appendChild(this.overlay);
      this.overlay.setStyle({
        'position': 'absolute',
        'top':      '0px',
        'left':     '0px'
      });
      this.overlay.control = this;
      Event.observe(this.overlay,'click',function(){
        this.control.close();
        });
    };
    this.positionOverlay();
    this.overlay.show();
    var wbPopAdRO = this;
    Event.observe(window,'resize',function(){
      wbPopAdRO.positionOverlay();
      });
    Event.observe(window,'scroll',function(){
      wbPopAdRO.positionOverlay();
      });
  };
  this.hideOverlay = function(){
    // if( Object.isElement(this.overlay) )
    //   this.overlay.hide();
  };
  this.positionOverlay = function(){
    if( Object.isElement(this.overlay) ){
      var dvs = this.getDimensions();
      var vps = document.viewport.getScrollOffsets();
      this.overlay.setStyle({
        'top':    vps.top + 'px',
        'width':  (dvs.width) + 'px',
        'height': (dvs.height) + 'px'
        });
      if( this.container && typeof(this.container.setStyle)=='function' ){
        this.container.setStyle({
          'top':   (vps.top + this.config.offsetTop) + 'px',
          'left':  ((dvs.width - this.container.getWidth())/2) + 'px'
          });
      }
    };
  };

  /**************************************************************************/
  this.showContainer = function(){
    if( !Object.isElement(this.container) ){
      /*
        DIV - Container
        */
      this.container = document.createElement('div');
        Element.extend(this.container);
        this.container.addClassName('wbpopad_container');
        // this.config.parent.appendChild(this.container);
        document.body.appendChild(this.container);

      var dvs = this.getDimensions();

      var topPos = (this.config.top=='center'?((dvs.height/2)-(this.config.height/2)): this.config.top);
      if( topPos < 0 ) topPos = 0;

      var leftPos = (this.config.left=='center'?((dvs.width/2)-(this.config.width/2)): this.config.left);
      var leftPos = ((dvs.width/2)-(this.config.width/2));
      if( leftPos < 0 ) leftPos = 0;

      var openScale = .25;
      // var centerTop = (dvs.height/2)-((this.config.height*openScale)/2);
      var centerTop = (this.config.height/2) + (this.config.offsetTop / 2);
      var centerLeft = (dvs.width/2)-((this.config.width*openScale)/2);

      this.container.setStyle({
        'position': 'absolute',
        'top':      centerTop + 'px',
        'left':     centerLeft + 'px',
        'width':    (this.config.width * openScale) + 'px',
        'height':   (this.config.height * openScale) + 'px',
        'overflow': 'hidden'
        }); // .hide();

      var onCompleteFxEl=this;
      var onCompleteFx = function(){
        var self=onCompleteFxEl;
        /*
          IFRAME
        self.container.innerHTML = self.container.innerHTML + '<iframe src="'+self.config.url+'" frameborder="0" style="overflow:'+(self.config.scroll?'auto':'hidden')+';width:'+self.config.width+'px;height:'+self.config.height+'px;"></iframe>';
          */
        self.container.innerHTML = self.container.innerHTML + '<img src="'+self.config.url+'" border="0" />';
        /*
          DIV/A/SPAN - Close Button
          */
        self.buttons.close = document.createElement('div');
          Element.extend(self.buttons.close);
          self.buttons.close.addClassName('wbpopad_button_close');
          self.container.appendChild(self.buttons.close);
        var aTag = document.createElement('a');
          Element.extend(aTag);
          aTag.control = self;
          Event.observe(aTag,'click',function(){
            this.control.close();
            });
          self.buttons.close.appendChild(aTag);
        var sTag = document.createElement('span');
          Element.extend(sTag);
          aTag.appendChild(sTag);
      }

      // new Effect.BlindDown( this.container );
      // new Effect.Grow( this.container, {'from': 0.7, 'to': 1.0, 'duration': 1.0, afterFinish: function(){ onCompleteFx(); }});
      // new Effect.Move( this.container, {'x': -100, y: 0, 'mode': 'relative'});

      // get curren width and height
      this.widthCurrent   = this.container.getWidth();
      this.heightCurrent  = this.container.getHeight();

      // get new width and height
      var borderSize = 0;
      var widthNew = (this.config.width + (borderSize * 2));
      var heightNew = (this.config.height + (borderSize * 2));

      // scalars based on change from old to new
      this.xScale = ( widthNew / this.widthCurrent) * 100;
      this.yScale = ( heightNew / this.heightCurrent) * 100;

      // calculate size difference between new and old image, and resize if necessary
      wDiff = this.widthCurrent - widthNew;
      hDiff = this.heightCurrent - heightNew;

      resizeSpeed = 6;
      resizeDuration = (11 - resizeSpeed) * 0.15;
      new Effect.Scale(this.container, this.yScale, {scaleFromCenter: true, scaleX: false, duration: resizeDuration, queue: 'front'});
      new Effect.Scale(this.container, this.xScale, {scaleFromCenter: true, scaleY: false, delay: resizeDuration, duration: resizeDuration, afterFinish:function(){onCompleteFx();}});
      // this.onCompleteFx();
    };
    // this.container.show();
  };
  this.hideContainer = function(){
    if( Object.isElement(this.container) )
      this.container.hide();
  };

  /**************************************************************************/
  this.close = function(){
    Element.remove(this.overlay);
    Element.remove(this.container);
    this.overlay = null;
    this.container = null;
  };

  /**************************************************************************/
  this.msg = function(msg){
    alert( 'wbPopAd: '+msg );
  };

  /**************************************************************************/
  /** getDimensions taken from xiWindow **/
  /**************************************************************************/
  this.getDimensions = function(parent){
    parent = parent || document.body;
    var windowWidth, windowHeight;
    var pageHeight, pageWidth;
    if (parent != document.body) {
      windowWidth = parent.getWidth();
      windowHeight = parent.getHeight();
      pageWidth = parent.scrollWidth;
      pageHeight = parent.scrollHeight;
    }
    else {
      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;
      }
      if (self.innerHeight) {  // all except Explorer
        windowWidth = self.innerWidth - 18;
        windowHeight = self.innerHeight; // - 18;
      } 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 = windowWidth;
      } else {
        pageWidth = xScroll;
      }
    }
    return {
      width:  pageWidth > windowWidth ? windowWidth : pageWidth,
      height: pageHeight > windowHeight ? windowHeight : pageHeight
      };
  };

  /* ---------------------- */
  if( this.config.parent == null )
    this.config.parent = document.body;
  if( this.config.url == '' )
    this.ready = false;

}

