// JavaScript Document

/*******************************************************************************
 Inovesys Tecnologia © 2007 
 Scroller.js
 This file handles the JavaScript for i9sys Scroller object.  
 *******************************************************************************/


// Creates the namespace
var i9sys;
if(!i9sys) {
	i9sys = {};
}
if(!i9sys.Web) {
	i9sys.Web = {};
}
if(!i9sys.Web.Controls) {
	i9sys.Web.Controls = {};
}

//
// Constructor for Scroller
//
i9sys.Web.Controls.Scroller = function(name, direction, content, area)
{
	// Scroll: Setup Scrolling Stuff
	this.varName = name; // Name of the variable created to be called later (this is not elegant but...)
	this.scrollDirection = direction;
	this.scrollArea = document.getElementById(area);

	this.contentElements = new Array();
	this.contentLength = -1;
	this.contentSelected=-1;

	// Find all the <div> elements in the content, and extract their id's into an array.
	contentElem = document.getElementById(content);
	if (contentElem.hasChildNodes())
	{
		var children = contentElem.childNodes;
		for (var i = 0; i < children.length; i++) 
		{
			if (contentElem.childNodes[i].tagName == "DIV")	{
				this.contentElements.push(contentElem.childNodes[i]);
			}
		}
	}
	this.contentLength = this.contentElements.length;
	if(this.contentLength > 0) {
		this.contentSelected = 0;
	}


// Scroll the page manually to the position of element "link", passed to us.
this.ScrollSection = function(link)
{
	// Store the last section, and update the current section
	if (this.contentSelected == link) {
		return;
	}
	this.contentSelected = link;
	
	// Change the section highlight.
	// Extract the root section name, and use that to change the background image to 'top', revealing the alt. state

	// Get the element we want to scroll, get the position of the element to scroll to
	position = this.findElementPos(this.contentElements[link]);

	// Get the position of the offset div -- the div at the far left.
	// This is the amount we compensate for when scrolling
	if (this.contentLength > 0) {
		offsetPos = this.findElementPos(this.contentElements[0]);
		position[0] = position[0] - offsetPos[0];
		position[1] = position[1] - offsetPos[1];
	}

	if (this.scrollDirection == "horiz") {
	this.scrollStart(this.scrollArea, this.scrollArea.scrollLeft, position[0]);
	} else {
	this.scrollStart(this.scrollArea, this.scrollArea.scrollTop, position[1]);
	}
}

// Scroll the page using the arrows
this.ScrollArrow = function(direction)
{
	// Determine where to go.
	if (direction == "left") {
		if (this.contentSelected - 1 < 0) {
			gotoContent = this.contentLength - 1;
		} else {
			gotoContent = this.contentSelected - 1;
		}
	} else {
		if ((this.contentSelected + 1) > (this.contentLength - 1)) {
			gotoContent = 0;
		} else {
			gotoContent = this.contentSelected + 1;
		}
	}
	
	// Go to the section name!
	this.ScrollSection(gotoContent);
}
// Shorthands to ScrollArrow
this.ScrollLeft = function()
{
	this.ScrollArrow('left');
}

this.ScrollRight = function()
{
	this.ScrollArrow('right');
}

//
// Animated Scroll Functions
//
this.scrollanim = {time:0, begin:0, change:0.0, duration:0.0, element:null, timer:null};

this.scrollStart = function(elem, start, end)
{
	if (this.scrollanim.timer != null) {
		clearInterval(this.scrollanim.timer);
		this.scrollanim.timer = null;
	}
	this.scrollanim.time = 0;
	this.scrollanim.begin = start;
	this.scrollanim.change = end - start;
	this.scrollanim.duration = 25;
	this.scrollanim.element = elem;
	
	if (this.scrollDirection == "horiz") {
		this.scrollanim.timer = setInterval(this.varName + ".scrollHorizAnim();", 15);
	} else {
		this.scrollanim.timer = setInterval(this.varName + ".scrollVertAnim();", 15);
	}
}

this.scrollVertAnim = function()
{
	if (this.scrollanim.time > this.scrollanim.duration) {
		clearInterval(this.scrollanim.timer);
		this.scrollanim.timer = null;
	}
	else {
		move = this.sineInOut(this.scrollanim.time, this.scrollanim.begin, this.scrollanim.change, this.scrollanim.duration);
		this.scrollanim.element.scrollTop = move; 
		this.scrollanim.time++;
	}
}

this.scrollHorizAnim = function()
{
	if (this.scrollanim.time > this.scrollanim.duration) {
		clearInterval(this.scrollanim.timer);
		this.scrollanim.timer = null;
	}
	else {
		move = this.sineInOut(this.scrollanim.time, this.scrollanim.begin, this.scrollanim.change, this.scrollanim.duration);
		this.scrollanim.element.scrollLeft = move;
		this.scrollanim.time++;
	}
}

// Utility: Math functions for animation calucations - From http://www.robertpenner.com/easing/
//
// t = time, b = begin, c = change, d = duration
// time = current frame, begin is fixed, change is basically finish - begin, duration is fixed (frames),
this.sineInOut = function(t, b, c, d)
{
	return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
}

// Utility: Find the Y position of an element on a page. Return Y and X as an array
this.findElementPos = function(elemFind)
{
	var elemX = 0;
	var elemY = 0;
	do {
		elemX += elemFind.offsetLeft;
		elemY += elemFind.offsetTop;
	} while ( elemFind = elemFind.offsetParent )

	return Array(elemX, elemY);
}

}