/**
   Written by Robert Plank
   Drag class based on DOM-Drag by Aaron Boodman
*/

var close = new Image(); close.src = "close.gif";
var pclose = new Image(); pclose.src = "pclose.gif";

var theRoot, theHandle, theContent, theButtons;
var popbody, formcode, email;

function press(e) {
   e.src = eval("p" + e.id + ".src;");
}

function depress(e) {
   e.src = eval(e.id + ".src");
}

var w3c = (document.getElementById) ? true : false;
var ns4 = (document.layers) ? true : false;
var ie5 = (w3c && document.all) ? true : false;
var ns6 = (w3c && !document.all) ? true : false;
var other = (document.all && !w3c) ? true : false;

var Drag = {

   x : null,
   y : null,

   w : null,
   h : null,

   mx : null,
   my : null,

   visible : true,

   obj : null,

   ceil : Math.max,
   floor : Math.min,

   init : function(o, oRoot, x, y) {

            /*   if (popOnce) {
      if (getCookie(cid) != "") return;
      d.cookie = cid + "=yes";
   }*/
      o.onmousedown   = this.startDrag;
      o.onmousemove = Drag.trackMouse;

      o.hmode         = true ;
      o.vmode         = true ;

      o.root = oRoot && oRoot != null ? oRoot : o ;

      if (o.hmode  && isNaN(parseInt(o.root.style.left  ))) o.root.style.left   = "0px";
      if (o.vmode  && isNaN(parseInt(o.root.style.top   ))) o.root.style.top    = "0px";
      if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right  = "0px";
      if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";

      o.minX   = null;
      o.minY   = null;
      o.maxX   = null;
      o.maxY   = null;

      //o.xMapper = fXMapper ? fXMapper : null;
      //o.yMapper = fYMapper ? fYMapper : null;

      o.root.onDragStart   = new Function();
      o.root.onDragEnd   = new Function();
      o.root.onDrag      = new Function();

      this.obj = o;

	  if (x != undefined) this.obj.root.style.left = x+'px';
	  if (y != undefined) this.obj.root.style.top = y+'px';

      this.w = parseInt(this.obj.root.style["width"]);
      this.h = parseInt(this.obj.root.style["height"]);

      this.update();
   },

   isEmpty : function(s) {
      return (!s || s == null || s == "" || s == undefined);
   },

   update : function() {
      this.x = parseInt(this.obj.root.style.left);
      this.y = parseInt(this.obj.root.style.top);
   },

   startDrag : function(e) {
      var o = Drag.obj = this;
      e = Drag.fixE(e);
      Drag.y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
      Drag.x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
      o.root.onDragStart(Drag.x, Drag.y);

      o.lastMouseX   = e.clientX;
      o.lastMouseY   = e.clientY;

      if (o.hmode) {
         if (o.minX != null)   o.minMouseX   = e.clientX - Drag.x + o.minX;
         if (o.maxX != null)   o.maxMouseX   = o.minMouseX + o.maxX - o.minX;
      }
      else {
         if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + Drag.x;
         if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + Drag.x;
      }

      if (o.vmode) {
         if (o.minY != null)   o.minMouseY   = e.clientY - Drag.y + o.minY;
         if (o.maxY != null)   o.maxMouseY   = o.minMouseY + o.maxY - o.minY;
      }
      else {
         if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + Drag.y;
         if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + Drag.y;
      }

      document.onmousemove   = Drag.drag;
      document.onmouseup = Drag.end;

      return false;
   },

   drag : function(e) {
      e = Drag.fixE(e);
      var o = Drag.obj;

      var ey   = e.clientY;
      var ex   = e.clientX;
      Drag.y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
      Drag.x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
      var nx, ny;

      if (o.minX != null) ex = o.hmode ? ceil(ex, o.minMouseX) : floor(ex, o.maxMouseX);
      if (o.maxX != null) ex = o.hmode ? floor(ex, o.maxMouseX) : ceil(ex, o.minMouseX);
      if (o.minY != null) ey = o.vmode ? ceil(ey, o.minMouseY) : floor(ey, o.maxMouseY);
      if (o.maxY != null) ey = o.vmode ? floor(ey, o.maxMouseY) : ceil(ey, o.minMouseY);

      nx = Drag.x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
      ny = Drag.y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));

      if (o.xMapper)      nx = o.xMapper(Drag.y)
      else if (o.yMapper)   ny = o.yMapper(Drag.x)

      Drag.setPosition(nx,ny);

      Drag.obj.lastMouseX   = ex;
      Drag.obj.lastMouseY   = ey;

      Drag.obj.root.onDrag(nx, ny);
      return false;
   },

   write : function(name,text){
      if (ns4){
         document.layers[name].document.write(text);
         document.layers[name].document.close();
      }
      else if (other) document.all[text].innerHTML=text;
      else document.getElementById(name).innerHTML=text;
   },

   trackMouse : function(e) {
      this.mx = (ie5) ? event.clientX + document.body.scrollLeft : e.pageX;
      this.my = (ie5) ? event.clientY + document.body.scrollTop : e.pageY;
   },

   setWidth : function(c) {
      if (c < 100) return false;
      this.changeWidth("root",c);
      this.changeWidth("handle",(c-25));
      this.changeWidth("content",c);
      this.w = c;
   },

   setHeight : function(c) {
      this.changeHeight("root",c);
      this.changeHeight("content",c-25);
      this.h = c;
   },

   changeWidth : function(object, c) {
      if (c <= 0) return false;

      if (ie5) eval("document.all."+object+".style.width = '"+ c +" px';");
      else if (ns4) eval("document.layers."+object+".width") = c + "px";
      else if (w3c) document.getElementById(object).style.width = c + "px";
   },

   changeHeight : function(object, c) {
      if (c <= 0) return false;
      if (ie5) eval("document.all."+object+".style.height = '"+c+" px';");
      else if (ns4) eval("document.layers."+object+".height") = c + "px";
      else if (w3c) document.getElementById(object).style.height = c + "px";
   },

   setSize : function(nw,nh) {
      if (nw != "") this.setWidth(nw);
      if (nw != "") this.setHeight(nh);
   },

   hide : function() {
      this.obj.root.style["visibility"] = "hidden";
      this.visible = false;
   },

   show : function() {
      this.obj.root.style["visibility"] = "visible";
      this.visible = true;
   },

   move : function(x2, y2, k) {

      this.update();
      x1 = this.x;
      y1 = this.y;

      if (x1 == x2 && y1 == y2) return false;

      var dx = Math.round(x2-x1);
      var dy = Math.round(y2-y1);

      if (k == undefined) k = 20;
      else k--;
      
      nx = x1 + ((x2 - x1) / k);
      ny = y1 + ((y2 - y1) / k);

      this.setPosition(nx,ny);

      setTimeout("Drag.move(" + x2 + "," + y2 + "," + k + ")",20);
   },

   windowWidth : function() {
      if (ns6) return window.innerWidth;
      if (ie5) return document.body.offsetWidth;
   },

   windowHeight : function() {
      if (ns6) return window.innerHeight;
      if (ie5) return document.body.offsetHeight;
   },

   setFade : function(c) {
      if (ie5) this.obj.root.style["filter"] = "Alpha(Opacity="+c+");";
      if (ns4 || ns6) this.obj.root.style.MozOpacity = c/100;
   },

   fadeIn : function(c, dc) {
      if (!this.visible) { this.show(); }
      if (c > 100) return;
      if (this.isEmpty(c)) c = 0;
      if (this.isEmpty(dc)) dc = 10;
      this.setFade(c);

      setTimeout("Drag.fadeIn("+(c+dc)+")",20);
   },

   slideFromLeft : function(nx, ny, k) {
      if (this.isEmpty(nx)) { nx = this.x; }
      if (this.isEmpty(ny)) { ny = this.y; }

      if (this.isEmpty(k)) k = 20;
      this.setPosition(-this.w,ny,k);
      this.move(nx,ny);
   },

   slideFromRight : function(nx, ny, k) {
      if (this.isEmpty(nx)) { nx = this.x; }
      if (this.isEmpty(ny)) { ny = this.y; }

      if (this.isEmpty(k)) k = 20;
      this.setPosition(this.windowWidth()+this.w,ny,k);
      this.move(nx,ny);
   },

   slideFromTop : function(nx, ny, k) {
      if (this.isEmpty(nx)) { nx = this.x; }
      if (this.isEmpty(ny)) { ny = this.y; }

      if (this.isEmpty(k)) k = 20;
      this.setPosition(nx,-this.h,k);
      this.move(nx,ny);
   },

   slideFromBottom : function(nx, ny, k) {
      if (this.isEmpty(nx)) { nx = this.x; }
      if (this.isEmpty(ny)) { ny = this.y; }

      if (this.isEmpty(k)) k = 20;
      this.setPosition(nx,this.windowHeight()+this.h,k);
      this.move(nx,ny);
   },

   pause : function(numberMillis) {
        var now = new Date();
        var exitTime = now.getTime() + numberMillis;
        while (true) {
            now = new Date();
            if (now.getTime() > exitTime) return;
        }
    },

   setX : function(c) {
      if (this.isEmpty(c)) return;

      o = this.obj;
      this.obj.root.style[o.hmode ? "left" : "right"] = c + "px";
      return;
   },

   setY : function(c) {
      if (this.isEmpty(c)) return;

      o = this.obj;
      this.obj.root.style[o.vmode ? "top" : "bottom"] = c + "px";
      return;
   },

   setPosition : function(nx, ny) {
      if (!this.visible) this.show();
      if (this.isEmpty(nx) || this.isEmpty(ny)) return;

      this.setX(nx);
      this.setY(ny);
   },

   end : function() {
      document.onmousemove = Drag.trackMouse;
      document.onmouseup   = null;
      Drag.obj.root.onDragEnd( parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]), 
                               parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"]));
      //Drag.obj = null;
      Drag.update();
      
   },

   fixE : function(e) {
      if (typeof e == 'undefined') e = window.event;
      if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
      if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
      return e;
   },

   getCookie : function (key) {
      var search = key + "=";
      var returnvalue="";
      if (d.cookie.length > 0){
         offset = d.cookie.indexOf(search);
         if (offset != -1){
            offset += search.length;
            end = d.cookie.indexOf(";",offset);
            if (end == -1) end = d.cookie.length;
         returnvalue = unescape(d.cookie.substring(offset,end));
         }
      }
      return returnvalue;
   }

};

/* Cookies */

var domain = getDomain();

function getDomain() {
   thisURL = location.href;
   if (thisURL.match(/^http:\/\//)) {
      thisURL = thisURL.replace(/^http:\/\//,"");
      thisURL = thisURL.replace(/^www\./,"");
      thisURL = thisURL.replace(/\/.*?$/,"");
      thisURL = "."+thisURL;
      return thisURL;
   }
}

function setCookie(key, value, minutes) {
   minuteDefault = 525600;
   if (minutes == undefined) minutes = minuteDefault;

   expire = new Date();
   now = expire.getTime();
   offset = Math.floor(now + (minutes * 60 * 1000))
   expire.setTime(offset);
   expireString = expire.toGMTString();

   cookieString = key+"="+value+"; ";
   cookieString += "expires="+expireString+"; ";

   if (domain != null) cookieString += "domain="+domain+"; ";

   document.cookie = cookieString;
}

function getCookie(key) {
   cookieList = document.cookie;
   cookieArray = cookieList.split(/;/)
   for (i=0;i<cookieArray.length;i++) {
      temp = cookieArray[i].split(/=/);
      if (temp[0] == key) return temp[1];
   }
   return null;
}