// JavaScript Document
// Custom Javascript file written for the scrolling in the toolbar and video page
// Utilizes Spry and MUST be used in conjunction with SpryEffects.js
// 12/30/08 - Needs a more OOP update, could give more property control to the code doing the calling

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Behavioral variable definitions/////////////////////////////////////////////////////////////////////////////////////
var scrollHorizontally = false;	//sets vertical or horizontal scrolling direction
var dRate = 30,						//Default scroll speed in milliseconds
	rate;							//Globally defined instantaneous speed value
var dInc = 10, 						//Default scroll increment in pixels
	inc;							//Globally defined instantaneous increment value
var scrollDirection = "down";		//sets initial scroll direction to down (vs. up, left, or right)

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Movement variable definitions///////////////////////////////////////////////////////////////////////////////////////
var intervalID,						//id of the timer controlling frame rate
	curPos,							//current position of the inner div (the one we will be moving)
	counter,
	distance,
	innerDivLength, 				//Length of the inner div in pixels (height or width)
	outerDivLength,					//Length of the outer div in pixels
	innerDiv;						//pointer to the inner div element

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Main public function to scroll the div//////////////////////////////////////////////////////////////////////////////
JBSMScroll = function(outerDivID, direction, increment, speed, dis)
{
	
	inc = increment ? increment : dInc;
	rate = speed ? speed : dRate;
	counter = 0;
	distance = dis;
	
	//Use Spry to get pointers to the outer and inner div elements
	var outerDiv = Spry.Effect.getElement(outerDivID);
	innerDiv = Spry.Effect.Utils.getFirstChildElement(outerDiv);
	
	//Sets the current scroll direction
	scrollDirection = direction;
	if(scrollDirection == 'left' || scrollDirection == 'right')
		scrollHorizontally = true;
	else
		scrollHorizontally = false;
	
	// IE 7 does not clip static positioned elements -> make element position relative
	if(/MSIE 7.0/.test(navigator.userAgent) && /Windows NT/.test(navigator.userAgent))
		Spry.Effect.makePositioned(outerDiv);
	
	Spry.Effect.makeClipping(outerDiv);
	
	// for IE 6 on win: check if position is static or fixed -> not supported and would cause trouble
	if(/MSIE 6.0/.test(navigator.userAgent) && /Windows NT/.test(navigator.userAgent))
	{
		var pos = Spry.Effect.getStyleProp(element, 'position');
		if(pos && (pos == 'static' || pos == 'fixed'))
		{
			Spry.Effect.setStyleProp(element, 'position', 'relative');
			Spry.Effect.setStyleProp(element, 'top', '');
			Spry.Effect.setStyleProp(element, 'left', '');
		}
	}
	
	if(innerDiv) //if we've successfully aquired our pointer to the element that will be moved...
	{
		//Spry funcions to set up innerDiv's position and clipping styles properly 
		Spry.Effect.makePositioned(innerDiv);
		Spry.Effect.makeClipping(innerDiv);
		
		//Get the dimenions of both divs as Rects
		var outerDivRect = Spry.Effect.getDimensionsRegardlessOfDisplayState(outerDiv);
		var innerDivRect = Spry.Effect.getDimensionsRegardlessOfDisplayState(innerDiv);
		
		//Set the inner div's dimension (opposite to the one we're moving) to JBSMpletely fill the outer div's
		if(!scrollHorizontally)
			Spry.Effect.setStyleProp(innerDiv, 'width', outerDivRect.width + 'px');
		else 
			Spry.Effect.setStyleProp(innerDiv, 'height', outerDivRect.height + 'px');
	}	
	
	//Get the max dimension in the direction we're traveling
	if(scrollHorizontally) {
		curPos = parseInt(Spry.Effect.getStyleProp(innerDiv, "left"));
		innerDivLength = innerDivRect.width;
		outerDivLength = outerDivRect.width;
	}
	else {
		curPos = parseInt(Spry.Effect.getStyleProp(innerDiv, "top"));
		innerDivLength = innerDivRect.height;
		outerDivLength = outerDivRect.height;
	}
	
	//Call our Move function to step the inner div inc pixels every speed milliseconds
	intervalID = setInterval('JBSMScroll.Move();',rate);
};

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Incremental Move function///////////////////////////////////////////////////////////////////////////////////////////
JBSMScroll.Move = function()
{	
	var side = scrollHorizontally ? "left" : "top";	//Side holds the position style that we will move inc pixels every step
	
	if(distance) {
		if(counter >= distance) {
			JBSMScroll.Stop();
			return;
		}
		else counter += inc;
	}
	
	//Check what direction we're moving in and make sure the we don't go too far
	if((scrollDirection == "down" || scrollDirection == "right") && curPos+innerDivLength > outerDivLength) 
		curPos -= inc; //Move the div up or left
	else if((scrollDirection == "up" || scrollDirection == "left") && curPos < 0)
		curPos += inc; //Move the div down or right
	else
		return;
	
	//Move the div via a change of style call through Spry setStyleProp
	Spry.Effect.setStyleProp(innerDiv,side,curPos + 'px');
};	

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Main public function to stop the scroll/////////////////////////////////////////////////////////////////////////////
JBSMScroll.Stop = function()
{
	clearInterval(intervalID); //Clears the timer initiated and pointed to by intervalID
};
