var Scroll = {};

Scroll.Box = function(config){
  var boxScroll = document.getElementById(config.id);
  if (boxScroll == null)
    return {start:function(){},stop:function(){}};
  var container = document.createElement("div");
  container.style.width = config.width + "px";
  container.style.height = config.height + "px";
  container.style.overflow = "hidden";
  container.style.position = "relative";
  var boxScroll = document.getElementById(config.id);
  boxScroll.style.overflow = "visible";
  boxScroll.style.position = "absolute";
  boxScroll.style.top = "0px";
  boxScroll.style.left = "0px";
  boxScroll.style.width = boxScroll.offsetWidth; 
  boxScroll.style.height = boxScroll.offsetHeight;
  var outHeight = config.height > boxScroll.offsetHeight ? config.height : boxScroll.offsetHeight;
  var outWidth = config.width > boxScroll.offsetWidth ? config.width : boxScroll.offsetWidth;  
  var _timeout;
  
  boxScroll.parentNode.appendChild(container);
  boxScroll.parentNode.removeChild(boxScroll);
  container.appendChild(boxScroll);
  config.timeout = config.timeout == null ? 100 : config.timeout;
  config.shift = config.shift == null ? 1 : config.shift;  
      
  var position = 0;
  var moveObject = false;
  var limite = 0;

  function moveLeft(){
    boxScroll.style.left = position + "px";
    position -= config.shift;
    if (position < limite)
      position = config.width;     
  }
  function moveRight(){
    boxScroll.style.left = position + "px";
    position += config.shift; 
    if (position > limite)
      position = config.width * -1;     
  }
  function moveBottom(){
    boxScroll.style.top = position + "px";
    position += config.shift;  
    if (position > limite)
      position = config.height * -1;
  }
  function moveTop(){
    boxScroll.style.top = position + "px";
    position -= config.shift;
    if (position < limite)
      position = config.height;
  }
       
  var moveFunction = null;  
  switch(config.direction){
    case "left":
      moveFunction = moveLeft;
      limite = outWidth * -1;
      break;
    case "right":
      moveFunction = moveRight;
      limite = outWidth;
      break;    
    case "bottom":
      moveFunction = moveBottom;
      limite = outHeight;
      break;
    default:    
      moveFunction = moveTop;
      limite = outHeight * -1;
  }
  var timeout = config.timeout;
  var count = 0;
  function move(){
    if (moveObject){
      moveFunction();
      _timeout = setTimeout(move,timeout);
    }
  }
  
  function mouseOver(){
    clearTimeout(_timeout);  
    moveObject = false;
  }

  function mouseOut(){
    if (moveObject)
      return;  
    moveObject = true;
    move();
  }
  
  if (config.stopOnOver){
    container.onmouseover = mouseOver;
    container.onmouseout = mouseOut;    
  }
  
  return {
    start : function(){
      if (moveObject)
        return;
      moveObject = true;    
      move();
    },
    stop : function(){  
      moveObject = false;      
    }    
  }
};
