Ozein.WindowBox = Class.create();
Ozein.WindowBox.prototype = {
  initialize: function(node, options) {
    this.setOptions(options);
    this.windowBox = node;
    var parts = document.getElementsByAttribute('ozein_windowbox', '*', node);
    if (node.getAttribute('ozein_windowbox')) {
      parts.push(node);
    }
    for(var i=0; i<parts.length; ++i) {
      var partsName = parts[i].getAttribute('ozein_windowbox');
      var funcName = 'set'+partsName;
      if (this[funcName]) this[funcName](parts[i]);
    }
    node.close = this.close.bind(this);
  },
  setOptions: function(options) {
    this.options = {
      closeEffect: Effect.Fade
    }
    Object.extend(this.options, options || {});
  },
  setCloseBtn: function(node) {
    Event.observe(node, 'click', this.close.bindAsEventListener(this));
  },
  setDraggable: function(node) {
    new Draggable(this.windowBox, {handle: node, zindex:1100, reverteffect:null, endeffect:null});
  },
  close: function() {
    if (this.options.closeEffect) {
      new this.options.closeEffect(this.windowBox);
    } else {
      this.windowBox.hide();
    }
  }
}

Ozein.ShowWindowBox = Class.create();
Ozein.ShowWindowBox.prototype = {
  initialize: function(node, options) {
    this.setOptions(options);
    if(this.options.trigger) {
        Event.observe(node, this.options.trigger, this.show.bindAsEventListener(this));
    }
  },
  setOptions: function(options) {
    this.options = {
      target: null,
      position: 'center',
      showEffect: Effect.Appear,
      trigger: 'click',
      modal: false,
      coveredColor: '#000',
      coveredOpacity: 0.8,
      releaseModalWhenClick: false,
      alwaysCenterPos: true,
      offsetX: 0,
      offsetY: 0,
      zIndex: 1001,
      coverzIndex: 1000,
      iframeId: null
    }
    Object.extend(this.options, options || {});
  },
  show: function(e) {
    var op = this.options;
    if (op.target) {
      var node = $(op.target);
      
      if (op.position == 'near') {
        node.style.top = (Event.pointerY(e) - this.options.offsetY) + "px";
        node.style.left = (Event.pointerX(e) - this.options.offsetX) + "px";
      } else if (op.position == 'center') {
        Ozein.Screen.setCenterPosition(node);
      }
      node.style.zIndex = this.options.zIndex;
      
      if (this.options.showEffect) {
        new this.options.showEffect(node);
      } else {
        node.show();
      }
      
      if (op.iframeId) {
      	ifrm = $(op.iframeId);
      	ifrm.style.top = node.style.top;
      	ifrm.style.left = node.style.left;
      	ifrm.style.width = node.getWidth()+"px";
      	ifrm.style.height = node.getHeight()+"px";
      	ifrm.style.zIndex = op.zIndex - 1;
      	ifrm.show();
      }
      
      if (this.options.modal) {
        this._prepareCover();
      }
    }
    if(e) Event.stop(e);
  },
  _prepareCover: function() {
    var cv = this._coverScreen;
    if (!cv) {
      cv = document.createElement('div');
      this._coverScreen = cv;
      Element.setStyle(cv, {
        backgroundColor: this.options.coveredColor,
        opacity: this.options.coveredOpacity,
        width: '100%',
        position: 'absolute',
        zIndex: this.options.coverzIndex
      });
      if (this.options.releaseModalWhenClick) {
        Event.observe(cv, 'click', $(this.options.target).close);
      }
      document.body.appendChild(cv);
    }
    this._covering();
    Element.show(cv);
    setTimeout(this._covering.bind(this), 100);
  },
  _covering: function() {
    var node = $(this.options.target);
    if (node.visible()) {
      var scr = Ozein.Screen.getScrollOffset();
      var size = Ozein.Screen.getSize();
      Element.setStyle(this._coverScreen, {
        width: '100%',
        height: size[3] + 'px',
        left: 0,
        top: scr[1] + 'px'
      });
      this._coverScreen.show();
      if (this.options.alwaysCenterPos) {
        Ozein.Screen.setCenterPosition(node);
      }
      setTimeout(this._covering.bind(this), 1);
    } else {
      this._coverScreen.hide();
      clearTimeout();
    }
  }
}
