if(typeof(AUTO123) == "undefined") AUTO123 = new Object();

AUTO123.Windows = function() {
  return {
    windowSpeed: 0.6,
    windowCurve: 100,
    
    openWindow: function(windowid, width, height, lefttop, curve, onComplete) {
      var w = document.getElementById(windowid);
      if(w != null) {
        if(lefttop == null) {
          lefttop = [0, 0];
        }
        var offs = this.getScreenOffsets();
        lt = [ lefttop[0] + offs[0], lefttop[1] + offs[1] ];
        this._showWindow(windowid, width, height, lt, curve, onComplete);
      }
    },
    
    openWindowCentered: function(windowid, width, height, curve, onComplete) {
      var w = document.getElementById(windowid);
      if(w != null) {
        var xy = this.getScreenCenter();
        var lt = [ xy[0] - (width / 2), xy[1] - (height / 2) ];
        this._showWindow(windowid, width, height, lt, curve, onComplete);
      }
    },
    
    _showWindow: function(windowid, width, height, lt, curve, onComplete) {
      var w = document.getElementById(windowid);
      if(w != null) {
        YAHOO.util.Dom.setStyle(w, "opacity", 0);        
        YAHOO.util.Dom.setStyle(w, "width", width + "px");
        w.style.position = "absolute";
        YAHOO.util.Dom.setXY(windowid, lt);
        if(w.style.display == "none") w.style.display = "";
      
        var a_attrs = {
          width: { from: 1, to: width },
          height: { from: 1, to: height },
          opacity: { from: 0, to: 0.95 }
        };
        var m_attrs = {
          points: {
            to: lt,
            control: curve ? this.getCurvePoints(w, lt) : lt
          }          
        };
        
        var wc = document.getElementById(windowid + "-content");
        if(wc != null) {
          YAHOO.util.Dom.setStyle(wc, "height", "100%");
          var attr = wc.getAttribute("overflow");
          if(attr != null) {
            YAHOO.util.Dom.setStyle(wc, "overflow", "hidden");
          }
        }
        
        var anim = new YAHOO.util.Anim(windowid, a_attrs, this.windowSpeed, YAHOO.util.Easing.easeNone);
        var motion = new YAHOO.util.Motion(windowid, m_attrs, this.windowSpeed, YAHOO.util.Easing.easeOut);
        
        var motionBegin = function() {
          YAHOO.util.Dom.setXY(windowid, lt);
          if(YAHOO.util.Dom.hasClass(windowid, "dim")) {
            AUTO123.Windows.dimScreen();
          }
        };
        
        var animEnd = function() {
          var wc = document.getElementById(windowid + "-content");
          if(wc != null) {
            var attr = wc.getAttribute("overflow");
            if(attr != null) {
              YAHOO.util.Dom.setStyle(wc, "overflow", attr);
            }
          }
          if(onComplete != null) onComplete();
        }
        
        anim.onComplete.subscribe(animEnd);
        motion.onStart.subscribe(motionBegin);
        
        anim.animate();
        motion.animate();
      }
    },
    
    hideWindow: function(windowid, destid) {
      var xy = [ 0, 0 ];
      
      if(destid == null) destid = windowid + "-close-anchor";
      if(destid != null) {
        var d = document.getElementById(destid);
        if(d != null) {
          xy = YAHOO.util.Dom.getXY(d);
        }
      }
      
      var w = document.getElementById(windowid);
      if(w != null) {
        var tt = parseInt(YAHOO.util.Dom.getStyle(windowid, "height"));
        if(isNaN(tt))
          tt = 0;
        else
          tt = tt / 2;
      
        var a_attrs = {
          height: { to: 1 },
          opacity: { to: 0 }
        };
        
        if(d != null) {
          var m_attrs = {
            points: {
              to: xy,
              control: this.getCurvePoints(w, xy)
            }
          };
        }
        else {
          var m_attrs = {
            points: { by: [0, tt || 50] }
          };
        }
        
        var wc = document.getElementById(windowid + "-content");
        if(wc != null) {
          var attr = wc.getAttribute("overflow");
          if(attr != null) {
            YAHOO.util.Dom.setStyle(wc, "overflow", "hidden");
          }
        }
        
        w.style.position = "absolute";
        if(w.style.display == "none") w.style.display = "";
        
        if(YAHOO.util.Dom.hasClass(windowid, "dim")) {
          this.undimScreen();
        }
        
        var anim = new YAHOO.util.Anim(windowid, a_attrs, this.windowSpeed/2, YAHOO.util.Easing.easeNone);
        var motion = new YAHOO.util.Motion(windowid, m_attrs, this.windowSpeed/2, YAHOO.util.Easing.easeIn);
        
        var motionEnd = function() { YAHOO.util.Dom.setXY(windowid, [-4000,-4000]); };
        motion.onComplete.subscribe(motionEnd);
        anim.animate();
        motion.animate();
      }
    },
    
    getCurvePoints: function(e, xy) {
      var elem_xy = YAHOO.util.Dom.getXY(e);
      var curveLeft = xy[0] + (this.windowCurve * ((elem_xy[0] < xy[0]) ? 1:-1));
      var curveTop = xy[1] + (this.windowCurve * ((elem_xy[1] < xy[1]) ? 1:-1));
      return [[curveLeft < 0 ? 0 : curveLeft, curveTop < 0 ? 0 : curveTop]];
    },
    
    getClientDimensions: function() {
      var _w = YAHOO.util.Dom.getViewportWidth();
      var _h = YAHOO.util.Dom.getViewportHeight();
      return [ _w, _h ];
    },
    
    getScreenOffsets: function() {
      var sl = (document.compatMode != 'CSS1Compat') ? document.body.scrollLeft : document.documentElement.scrollLeft;
      var st = (document.compatMode != 'CSS1Compat') ? document.body.scrollTop : document.documentElement.scrollTop;
      return [ sl, st ];
    },
    
    getScreenCenter: function() {
      var wh = this.getClientDimensions();
      var offs = this.getScreenOffsets();
      var _l = offs[0] + (wh[0] / 2);
      var _t = offs[1] + (wh[1] / 2);
      return [ _l, _t ];
    },
    
    dimScreen: function() {
      var fb = document.getElementById("x-fader-blk");
      if(fb == null) {
        fb = document.createElement("div");
        fb.setAttribute("id", "x-fader-blk");
        document.body.appendChild(fb);
        YAHOO.util.Dom.setStyle("x-fader-blk", "backgroundColor", "#333333");
        YAHOO.util.Dom.setStyle("x-fader-blk", "position", "absolute");
        YAHOO.util.Dom.setStyle("x-fader-blk", "left", "0");
        YAHOO.util.Dom.setStyle("x-fader-blk", "top", "0");
      }
      
      if(fb != null) {
        YAHOO.util.Dom.setStyle("x-fader-blk", "display", "block");
        YAHOO.util.Dom.setStyle("x-fader-blk", "opacity", 0);
        YAHOO.util.Dom.setStyle("x-fader-blk", "width", YAHOO.util.Dom.getDocumentWidth() + "px");
        YAHOO.util.Dom.setStyle("x-fader-blk", "height", YAHOO.util.Dom.getDocumentHeight() + "px");
        var a = new YAHOO.util.Anim("x-fader-blk", { opacity: { from: 0, to: 0.5 } }, this.windowSpeed / 4, YAHOO.util.Easing.easeNone);
        a.animate();
      }
    },
    
    undimScreen: function() {
      var fb = document.getElementById("fadeblock");
      if(fb != null) {
        var comp = function() {
          YAHOO.util.Dom.setStyle("x-fader-blk", "display", "none");
        };
        
        var a = new YAHOO.util.Anim("x-fader-blk", { opacity: { to: 0 } }, this.windowSpeed / 4, YAHOO.util.Easing.easeNone);
        a.onComplete.subscribe(comp);
        a.animate();
      }
    }
  }
}();

