// Scroll object code
//###########################################################################################################
var MyScroll = {};

MyScroll.to = 0;
MyScroll.current = 0;

MyScroll.go = function (element) {  

  // check if the element exists
  if (element.length == 0) {
    return false;
  }
    
  MyScroll.current = window.pageYOffset;  
  MyScroll.setToPosition(element);
  setTimeout("MyScroll.scroll(MyScroll.to)",1000);
}


MyScroll.setToPosition = function (element) {  
  var position = element.offset();
  MyScroll.to = position.top - 50;
}

MyScroll.scroll = function (ypos) {
  current = window.pageYOffset;
  
  distance = current - ypos;
  
  //set the speed according to the distance we need to go
  speed = MyScroll.setSpeed(distance);
  
  if (distance > 0) {
    while (distance > 0) {
      current = MyScroll.ssMin(current);
      scroll(0,current);
      distance = MyScroll.ssMin(distance);
    }   
  } else {
    while (distance < 0) {
      current = MyScroll.ssAdd(current);
      scroll(0,current);
      distance = MyScroll.ssAdd(distance);
    }
  }   
}

//this controlls the speed at which we are scrolling
MyScroll.ssAdd = function (value) {
  value = value + speed;
  return value;
}

MyScroll.ssMin = function (value) {
  value = value - speed;  
  return value;
}

MyScroll.setSpeed = function (distance) {

  if (distance < 0) {
    distance = distance * -1;
  }

  myspeed = 1;

  //set the speed according to the distance we need to go
  myspeed = distance / 100;
  myspeed = Math.floor(myspeed);
  if (myspeed == 0) {
    myspeed = 1;
  }
  
  return myspeed;
  
}
//###########################################################################################################