var steps = 0;
var oneCount = 0;  //only run animation cycle once per execution
var running = false;
var thresholdHeight = 0; //threshold to start the dough layer animation

function animate(newContentURI) {
  thresholdHeight = parseInt(document.getElementById("animatedLayer").style.top) + 80; //"80" is the height of the header; make sure to sync up!!!
  if (running == false) {
    var width = 0;
    var animationStyle = getStyleObject("animatedLayer");

		running = true;
		oneCount = 0;
		steps = 0;
		moveLoop(1,"animatedLayer", 15, 575,newContentURI);
	}
}

function moveLoop(upOrDown, element, distance, animatedElementSize,newContentURI) {
       //round number of animation steps down to the nearest integer 
       var maxSteps = Math.floor(animatedElementSize / distance);
	if (steps < maxSteps) {
	    move(upOrDown,distance,element);
      //var fadeIncrement = (1.5/maxSteps) * upOrDown;
      //fade(fadeIncrement);

	    setTimeout("moveLoop(" + upOrDown + ",'" + element + "'," + distance + "," + animatedElementSize + ",'" + newContentURI + "')",12);
            steps += 1;
	} else if (oneCount < 1) {
	    steps = 0;
            oneCount = 1;
      loadContentIntoSection(newContentURI,"content");
      checkTextBoxOverflow();
 	    moveLoop(upOrDown * -1, element,distance,animatedElementSize,newContentURI);
	} else {
	    running = false;
	}
} 

function move(upOrDown, distance, element) {
  var animatedElementStyle = getStyleObject(element);
  var doughSectionStyle = getStyleObject("doughSection");
  
  var topOffset = parseInt(animatedElementStyle.top) + (distance * upOrDown);
  var newHeight = parseInt(doughSectionStyle.height) + (distance * upOrDown);
  var thresholdPassed = (topOffset >= (parseInt(thresholdHeight) + 30));
  if (document.layers)
  {
    animatedElementStyle.top = topOffset;
    if (thresholdPassed) {
      doughSectionStyle.height = newHeight;
    } else {
      doughSectionStyle.height = "0";
    }
  }
  else 
  {
    animatedElementStyle.top = topOffset + "px";  
    if (thresholdPassed) {
      doughSectionStyle.height = newHeight + "px";
    } else {
      doughSectionStyle.height = "0px";
    }
  }
 // document.getElementById("debug").innerHTML += "<br>new height:" + topOffset + ":" + (parseInt(thresholdHeight) + 30);
}

function fade(increment) {
  var the_style2 = getStyleObject("content");
  if (the_style2.opacity != null && the_style2.opacity != "") {
    the_style2.opacity = the_style2.opacity - increment;
  } else {
    the_style2.opacity = 1 - increment;
  }

}

  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;
    }
}

function imageSwap(element, imageLocation) {
	element.src = imageLocation;
	return false;
}

function getComputedStyleHeight(element) {
  if (document.defaultView) {
    return document.defaultView.getComputedStyle(element, '').getPropertyValue("height");
  } else if (element.currentStyle) {
    return element.currentStyle.height;
  }
}
var scrollTimeout;
function scrollDiv(elementId, direction){
  var div = document.getElementById(elementId);
  var beforeBoundary = false;
  if (direction > 0) {
    var height = parseInt(getComputedStyleHeight(div));
    beforeBoundary = (div.scrollTop<(div.scrollHeight-height));
  } else {
    beforeBoundary = (div.scrollTop>0);
  }
 
  if (beforeBoundary) {
     div.scrollTop += direction * 5; //scroll 1 pixel up
     var functionDeclaration = 'scrollDiv("' + elementId + '",'+ direction +')';
     scrollTimeout = setTimeout(functionDeclaration, 10);
  } else {
    clearTimeout(scrollTimeout);
  }


}

