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);
    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,
      nearNode: null,
      dispNode: null,
      balloonOffsetX: 0,
      balloonOffsetY: 25,
      heightTyosei: 0
    }
    Object.extend(this.options, options || {});
  },
  show: function(e) {
    var op = this.options;
    if (op.target) {
      var node = $(op.target);
      if (op.position == 'near') {
      } else if (op.position == 'center') {
        Ozein.Screen.setCenterPosition(node);
      } else if (op.position == 'nearhear') {
        var PointX = Event.pointerX(e);
        var PointY = Event.pointerY(e);
        var targetNode;
        var yTyousei;
        if (op.dispNode) {
            targetNode = op.dispNode;
        }else{
            targetNode = $(op.nearNode);
            yTyousei = -120;
        }
        var retY = Ozein.Screen.setNearPosition(node, targetNode, yTyousei);
        var currentPos = Position.realOffset(document.getElementsByTagName('body')[0])[1];
        if (currentPos > retY) {
            new Effect.ScrollTo(document.getElementsByTagName('body')[0]);
        }
      }
      node.style.zIndex = 1001;
      if (this.options.showEffect) {
        new this.options.showEffect(node);
      } else {
        node.show();
      }
      if (this.options.modal) {
        this._prepareCover();
      }
    }
    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: 1000
      });
      if (this.options.releaseModalWhenClick) {
        Event.observe(cv, 'click', $(this.options.target).close);
      }
      document.body.appendChild(cv);
    }
    this._covering();
    Element.show(cv);
    if (this.options.heightTyosei) {
      $(this.options.target).style.top = (parseFloat(Ozein.Screen.getScrollOffset()[1]) + parseFloat(this.options.heightTyosei)) + 'px';
    }
    setTimeout(this._covering.bind(this), 100);
  },
  _covering: function() {
    var node = $(this.options.target);
    if (!$(this.options.target)) {
      this._coverScreen.hide();
      clearTimeout();
      return;
    }
    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();
    }
  }
}
