var counter;

var Move = {

   delay : 10,

   previousX : null,
   previousY : null,

   movements : new Array(),

   box : null,
   coast : true,

   initX : null,
   initY : null,

   realX : null,
   realY : null,

   steps : 5,
   stepsLeft : 0,

   // Is the layer moving right now?
   isMoving : false,

   init : function(name) {
	  Move.reset();
      Move.box = document.getElementById(name);
      Move.find();
   },

   setSpeed : function(x, y) {
      if (Move.previousX != null && Move.previousY != null) {
         Gravity.xSpeed = x - Move.previousX;
         Gravity.ySpeed = y - Move.previousY;
      }

      Move.previousX = x;
      Move.previousY = y;
   },

   inBounds : function(x, y) {
      var topLeftX = parseInt(Move.box.style.left);
      var topLeftY = parseInt(Move.box.style.top);

      var topRightX = parseInt(Move.box.style.left) + parseInt(Move.box.style.width);
      var bottomLeftY = parseInt(Move.box.style.top) + parseInt(Move.box.style.height);

      // Figure out if we are inside the bounds of the box
        
      return (
         (x >= topLeftX && x <= topRightX) && 
         (y >= topLeftY && y <= bottomLeftY)
      );
   },

   getCenter : function() {
      var topLeftX = parseInt(Move.box.style.left);
      var topLeftY = parseInt(Move.box.style.top);

      var topRightX = parseInt(Move.box.style.left) + parseInt(Move.box.style.width);
      var bottomLeftY = parseInt(Move.box.style.top) + parseInt(Move.box.style.height);

      var centerX = parseInt((topLeftX + topRightX) / 2);
      var centerY = parseInt((topLeftY + bottomLeftY) / 2);

      var result = new Object();
      result.x = centerX;
      result.y = centerY;

      return result;
   },

   // Adjusts to place the box in the center of your coords
   getOffset : function() {

      var center = Move.getCenter();
      var topLeftX = parseInt(Move.box.style.left);
      var topLeftY = parseInt(Move.box.style.top);

      var cornerX = parseInt(center.x - topLeftX);
      var cornerY = parseInt(center.y - topLeftY);

      var result = new Object();
      result.x = cornerX;
      result.y = cornerY;

      return result;
   },

   set : function(x, y) {
      // Update the layer properties
      Move.realX = x;
      Move.realY = y;

      Move.box.style.left = Math.round(x) + "px";
      Move.box.style.top = Math.round(y) + "px";
   },

   height : function() {
      return parseInt(Move.box.style.height);
   },

   width : function() {
      return parseInt(Move.box.style.width);
   },

   end : function(x, y) {
      //Move.realX = null;
      //Move.realY = null;

      if (typeof Move.onMoveEnd == "function") {
         Move.onMoveEnd();
      }
   },

   clear : function() {
      Move.onMoveEnd = null;

      document.onmousedown = null;
      document.onmouseup = null;
      document.onmousemove = Cursor.getCursor;

      //Gravity.yAcceleration = 0;
      //Gravity.xAcceleration = 0;
   },

   jump : function(x, y) {
      Move.set(x, y);
      setTimeout("Move.end();", Move.delay);
   },

   find : function() {
      // If there are no realX and realY values yet,
      // read those off the DHTML layer.
      if (Move.realX == null) {
         Move.realX = parseInt(Move.box.style.left);
         Move.initX = Move.realX;
      }
      if (Move.realY == null) {
         Move.realY = parseInt(Move.box.style.top);
         Move.initY = Move.realY;
      }
   },

   setRelativeCoordinates : function(x, y) {
      return Move.setCoordinates(x + Move.screenLeft(), y + Move.screenTop());
   },

   /**
      Set our box to coordinates x and y
   */
   setCoordinates : function(x, y) {

      if (Move.box == null) return false;
      Move.isMoving = true;

      Move.find();

      // Calculate distance to travel
      if (Move.stepsLeft <= 0) Move.stepsLeft = Move.steps;

      var distanceX = (x - Move.realX) / Move.stepsLeft;
      var distanceY = (y - Move.realY) / Move.stepsLeft;

      if (Move.coast == false) {
         Move.stepsLeft--;
      }

      Move.set(Move.realX + distanceX, Move.realY + distanceY);

      // If we still need to keep going...
      if (Math.round(Move.realX) != x || Math.round(Move.realY) != y) {
         Move.setSpeed(Move.realX, Move.realY);
         setTimeout("Move.setCoordinates(" + x + "," + y + ");", Move.delay);
      }
      // We are done.
      else {
         Move.end(x, y);
      }
   },

   getVerticalScroll : function() {
      if (window.pageYOffset) return parseInt(window.pageYOffset);
      return document.body.scrollTop;
   },

   getHorizontalScroll : function() {
      if (window.pageXOffset) return parseInt(window.pageXOffset);
      return document.body.scrollLeft;
   },

   screenTop : function() {
      return Move.getVerticalScroll();
   },

   screenBottom : function() {
      return Move.getVerticalScroll() + Move.browserHeight();
   },

   screenLeft : function() {
      return Move.getHorizontalScroll();
   },

   screenRight : function() {
      return Move.getHorizontalScroll() + Move.browserWidth()-50;
   },

   browserHeight : function() {
      if(typeof(window.innerWidth) == 'number') {
         //Non-IE
         return window.innerHeight - 2;
      }
      else if(document.documentElement && (document.documentElement.clientHeight)) {
         //IE 6+ in 'standards compliant mode'
         return document.documentElement.clientHeight;
      }
      else if(document.body && (document.body.clientHeight)) {
         //IE 4 compatible
         return document.body.clientHeight;
      }
   },

   browserWidth : function() {
      if(typeof(window.innerWidth) == 'number') {
         //Non-IE
         return window.innerWidth - 2;
      }
      else if(document.documentElement && (document.documentElement.clientWidth)) {
         //IE 6+ in 'standards compliant mode'
         return document.documentElement.clientWidth;
      }
      else if(document.body && (document.body.clientWidth)) {
         //IE 4 compatible
         return document.body.clientWidth;
      }
   },

   reset : function() {
	   Magnet.disable();
   }

}







	var Cursor = {
	   x : null,
	   y : null,
	
	   lastX : null,
	   lastY : null,
	
	   archive : function() {
		   Cursor.lastX = Cursor.x;
		   Cursor.lastY = Cursor.y;
	   },
	
	   getCursor : function(e) {
		  var e = e?e:event;
	
		  if (e != undefined && e.pageX && e.pageY) {
			   Cursor.archive();
			 Cursor.x = parseInt(e.pageX);
			 Cursor.y = parseInt(e.pageY);
		  }
		  else if (event != undefined && event.clientX && event.clientY) {
			 Cursor.archive();
			 Cursor.x = parseInt(event.clientX + document.body.scrollLeft);
			 Cursor.y = parseInt(event.clientY + document.body.scrollTop);
		  }
	   }
	};

function chechmouse(e){
	
	
   // Update the cursor
   Cursor.getCursor(e);

   // Calculate the distance from the top of the inner browser window
   var distanceFromTop = Cursor.y - Move.screenTop();

   // If the current Y coordinate is less than (at a higher position)
   // than the previous coordinate, the cursor is moving up
   if (Cursor.y < Cursor.lastY && distanceFromTop <= 10) {
      document.onmousemove = null;
	  setPosition();
   }
}


document.onmousemove = chechmouse;



function getViewportSize()
{
  var size = [0, 0];

  if (typeof window.innerWidth != 'undefined')
  {
    size = [
        window.innerWidth,
        window.innerHeight
    ];
  }
  else if (typeof document.documentElement != 'undefined'
      && typeof document.documentElement.clientWidth != 'undefined'
      && document.documentElement.clientWidth != 0)
  {
    size = [
        document.documentElement.clientWidth,
        document.documentElement.clientHeight
    ];
  }
  else
  {
    size = [
        document.getElementsByTagName('body')[0].clientWidth,
        document.getElementsByTagName('body')[0].clientHeight
    ];
  }

  return size;
}

function setPosition(){

this.scrollY = 0; this.scrollX = 0;
if(document.documentElement && document.documentElement.scrollLeft) this.scrollX = document.documentElement.scrollLeft;
if(document.documentElement && document.documentElement.scrollTop) this.scrollY = document.documentElement.scrollTop;
if(document.body && document.body.scrollLeft) this.scrollX = document.body.scrollLeft;
if(document.body && document.body.scrollTop) this.scrollY = document.body.scrollTop;
if(window.pageXOffset) this.scrollX = window.pageXOffset;
if(window.pageYOffset) this.scrollY = window.pageYOffset;
if(window.scrollX) this.scrollX = window.scrollX;
if(window.scrollY) this.scrollY = window.scrollY;

x = getViewportSize();

if (typeof image_width != 'undefined') {
	dialogWidth=image_width;
	dialogHeight=image_height;
}

else {
	dialogWidth=2;
	dialogHeight=2;
}
leftOffset = -5000;
topOffset = -5000;

if (topOffset == -5000) {
	dialogTop = ((x[1]/2) + this.scrollY) - (dialogHeight/2);
	}

	else {
	dialogTop = topOffset + this.scrollY;
	}

if (leftOffset == -5000) {
	dialogLeft = ((x[0] + this.scrollX)/2) - (dialogWidth/2);	
	}
	else {
		dialogLeft = leftOffset;
	}


if(document.getElementById('__blanket')) {
var bl = document.getElementById('__blanket');
bl.style.top = this.scrollY + "px";
}

if(document.getElementById('div_f')) {
var d = document.getElementById('div_f');
d.style.top = dialogTop + "px";
d.style.left = dialogLeft + "px";
doshow(1);
}


}
