var the_timeout;
var the_coords;

function getCoords(divToMoveTo)
{
	
    var the_style = getStyleObject("videosSet");
      if (the_style)
      {
		var curLeft = the_style.left;
        curXPos = parseInt(curLeft.replace(/px/,""));
        var newXPos;
        var curXPos; 
        newXPos = curXPos;
        switch(divToMoveTo){
            case 'down':	
				for (i=0;i<scrollerPoints.length;i++)
				{
					if(curXPos > parseInt(scrollerPoints[i])){
						newXPos = scrollerPoints[i];
						setDot(i);
						break;
					}
				}
				break;
			case 'up':
				for (i=scrollerPoints.length-1;i>=0;i--)
				{
					if(curXPos < parseInt(scrollerPoints[i])){
						newXPos = scrollerPoints[i];
						setDot(i);
						break;
					}
				}
				break;
			default:
				try
				{
					var a = parseInt(divToMoveTo);
					var b = parseFloat(divToMoveTo);
					if(a==b){
						newXPos = scrollerPoints[divToMoveTo];
						setDot(divToMoveTo);
						break;
					}
				}catch(err){
					
				}
				break;
          }
          
          if(curXPos != newXPos)
          {
			the_coords = new Array(curXPos + ":0",newXPos + ":0"); 
			getAnchors(0);
          }
             
       }     
}

function scrollObject(divName,divToMoveTo,arrPoints, stepSize)
{
	
	var arrPointsArray = String(arrPoints).split(",");
	
    var the_style = getStyleObject(divName);
      if (the_style)
      {
		var dots_style = getStyleObject("scrollDots");
		var curDotsWidth = 0;
		if (dots_style)
		{
			curDotsWidth = parseInt(dots_style.width.replace(/px/,"")) + 17;
		}
	
		var curLeft = the_style.left;
		var curTop = the_style.top;
        curXPos = parseInt(curLeft.replace(/px/,""));
		curYPos = parseInt(curTop.replace(/px/,""));
        var newXPos = curXPos;
		var newYPos = curYPos;
		
		var scrollCookieName;
		if(document.URL.match("nutrient") != null){
		   scrollCookieName = 'currentNutrientScroll';
		}
		if(document.URL.match("product") != null){
		   scrollCookieName = 'currentProductScroll';
		}

        switch(divToMoveTo){
            case 'down':	
				document.getElementById("scrollWindow").style["width"] = "36px";
				for (i=arrPointsArray.length-1;i>=0;i--)
				{
					if(curXPos > parseInt(arrPointsArray[i])){
						newXPos = arrPointsArray[i];
						setCookie(scrollCookieName,i,1);
						setArrows(i);
						break;
					}
				}
				break;
			case 'downreverse':	
				for (i=0;i<arrPointsArray.length;i++)
				{
					if(curXPos > parseInt(arrPointsArray[i])){
						newXPos = arrPointsArray[i];
						setCookie(scrollCookieName,i,1);
						setArrows(i);
						break;
					}
				}
				break;
			case 'up':
				for (i=1;i<=arrPointsArray.length;i++)
				{
					if(curXPos < parseInt(arrPointsArray[i])){
						newXPos = arrPointsArray[i];
						setCookie(scrollCookieName,i,1);
						setArrows(i);
						break;
					}
				}
				break;
			case 'upreverse':
				for (i=arrPointsArray.length-1;i>=0;i--)
				{
					if(curXPos < parseInt(arrPointsArray[i])){
						newXPos = arrPointsArray[i];
						setCookie(scrollCookieName,i,1);
						setArrows(i);
						break;
					}
				}
				break;
			default:
				try
				{
					var a = parseInt(divToMoveTo);
					var b = parseFloat(divToMoveTo);
					if(a==b){
						newXPos = arrPointsArray[divToMoveTo];
						setCookie(scrollCookieName,divToMoveTo,1);
						setArrows(divToMoveTo);
						break;
					}
				}catch(err){
					
				}
				break;
          }
          
		  
		  
          if(curXPos != newXPos)
          {
			the_coords = new Array(curXPos + ":" + curYPos,newXPos + ":" + newYPos); 
			getAnchors(divName,0, stepSize);
          }
		  
		  if(curDotsWidth > 0){
			if((curDotsWidth - newXPos) < 36){
				document.getElementById("scrollWindow").style["width"] = "" + (curDotsWidth - newXPos) + "px";
			}else{
				document.getElementById("scrollWindow").style["width"] = "36px";
			}
		  }
             
       }     
}

function isInt(myNum) {
         // get the modulus: if it's 0, then it's an integer
         var myMod = myNum % 1;

         if (myMod == 0) {
                 return true;
         } else {
                 return false;
         }
}

function getAnchors(divName,array_position,stepSize)
{
  var first_anchor = the_coords[array_position];
  var second_anchor = the_coords[array_position+1];

  array_position++;
  if (array_position == the_coords.length-1)
  {
    array_position = 0;
  }

  moveDiv(divName,array_position, first_anchor, second_anchor, 0, 0, stepSize);

}

function moveDiv(divName,array_position, anchor_one, anchor_two, 
                       horizontal_step_size, vertical_step_size, stepSize)
{
  var the_style = getStyleObject(divName);
  if (the_style)
  {

    // get the first anchor
    //    
    var first_points = anchor_one.split(":");
    var first_left = parseInt(first_points[0]);
    var first_top = parseInt(first_points[1]);

    // get the second anchor
    //
    var second_points = anchor_two.split(":");
    var second_left = parseInt(second_points[0]);
    var second_top = parseInt(second_points[1]);

    // if we don't know the step sizes to move the DIV, figure them out
    //
    if ((horizontal_step_size == 0) && (vertical_step_size == 0))
    {
      horizontal_step_size = 
        getStepSize(anchor_one, anchor_two, 0, stepSize);

      vertical_step_size = 
        getStepSize(anchor_one, anchor_two, 1, stepSize);
   }

    // figure out the new coordinates
    //
    var new_left = first_left + horizontal_step_size;
    var new_top = first_top + vertical_step_size;

    // if we're at the end of the segment, set the coordinates to
    // move the DIV to the end
    //
    if (atEndOfPath(horizontal_step_size, second_left, new_left) 
       ||(atEndOfPath(vertical_step_size, second_top, new_top)))
    {
      new_left = second_left;
      new_top = second_top;
    }

    // add the px or don't, depending on the browser
    if (!document.layers) 
    {
      new_left = new_left + "px";
      new_top = new_top + "px";
    }

    // now actually move the DIV
    //
    the_style.left = new_left;
    the_style.top = new_top;
    
    // if we're at the end of the segment, get new anchors
    // otherwise, call moveDiv() again with a new starting point
    // along the segment
    //
    if ((parseInt(new_left) == parseInt(second_left)) && 
            (parseInt(new_top) == parseInt(second_top)))
    {
      //getAnchors(array_position);
    } 
    else 
    {
      var new_anchor_one = new_left + ":" + new_top;

      var timeout_string = "moveDiv('" +
		divName + "'," + 
        array_position + ", '" + new_anchor_one + "', '" +
        anchor_two + "', " + horizontal_step_size + "," + 
        vertical_step_size + "," + stepSize + ");";
	//alert(timeout_string);
      the_timeout = setTimeout(timeout_string, 20);
    }
  }
}

// function getStepSize()
//  this figures out how much to move the DIV each time
//
function getStepSize(anchor_one, anchor_two, position, stepSize)
{
  var first_points = anchor_one.split(":");
  var first_number = parseInt(first_points[position]);

  var second_points  = anchor_two.split(":");
  var second_number = parseInt(second_points[position]);

  var step_size = Math.round((second_number - first_number)/10);
  if(step_size < 0){ step_size = -parseInt(stepSize)};
  if(step_size > 0){ step_size = parseInt(stepSize)};
  return step_size;
}

// function atEndOfPath()
//   if the DIV is about to be moved past the end point of
//   the segment, this will return true.  Otherwise, it will
//   return false.
//
function atEndOfPath(the_step_size, second_number, new_number)
{
    var the_end = false;

    if (((the_step_size > 0) && (new_number > second_number)) ||
        ((the_step_size < 0) && (new_number < second_number)))
   {
     the_end = true;
   }

   return the_end;
}


function getStyleObject(objectId) {
    // cross-browser function to get an object's style object given its
    if(document.getElementById && document.getElementById(objectId)) {
	// W3C DOM
	return document.getElementById(objectId).style;
    } else if (document.all && document.all(objectId)) {
	// MSIE 4 DOM
	return document.all(objectId).style;
    } else if (document.layers && document.layers[objectId]) {
	// NN 4 DOM.. note: this won't find nested layers
	return document.layers[objectId];
    } else {
	return false;
    }
} // getStyleObject


