/* This notice must be untouched at all times!

Copyright (c) 2006 Roman Schmid. All rights reserved.
Created 17. 5. 2006 by Roman Schmid : http://www.banal.ch

This Method allows the smooth scrolling of DOM Nodes (preferably DIVs)

LICENSE: LGPL

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License (LGPL) as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

For more details on the GNU Lesser General Public License,
see http://www.gnu.org/copyleft/lesser.html
*/

var OP = window.opera ? true : false;
var DOM = document.getElementById ? true : false;
var MS = (document.all && !OP) ? true : false;
var NS = (document.layers && !OP) ? true : false;
var SA = navigator.userAgent.indexOf('Safari');

function animObject(refName, elemID, totalFrames, callFunc, moveVertical, arrow1, arrow2)
{
	var arrowObj1;
	var arrowObj2;
	/*
	* Get Element based on the Element ID
	*/
	if(DOM){
		this.elem = document.getElementById(elemID);
		if( arrow1 != null && arrow2 != null ){
			arrowObj1 = document.getElementById(arrow1);
			arrowObj2 = document.getElementById(arrow2);
		}
	} else if (MS){
		this.elem = document.all[elemID];
		if( arrow1 != null && arrow2 != null ){
			arrowObj1 = document.all[arrow1];
			arrowObj2 = document.all[arrow2];
		}
	} else if (NS){
		this.elem = document[elemID];
		if( arrow1 != null && arrow2 != null ){
			arrowObj1 = document[arrow1];
			arrowObj2 = document[arrow2];
		}
	} else {
		return;
	}
	
	// The name of the Object created. Used to set Intervals properly
	this.refName = refName;
	
	// Function that will be Called after every animation (A-B)
	this.callFunc = callFunc;
	
	// Amount of Frames needed to get from A to B
	this.totalFrames = totalFrames;
	
	// Position Table and Position ID
	this.POS_TABLE = null;
	this.POS_ID = null;
	
	if( moveVertical == undefined || moveVertical == null || moveVertical == false ){
		this.moveVertical = false;
	} else {
		this.moveVertical = true;
	}

	/******************************************************************
	* Create the Position Table. All Target Points are stored here
	* Call this before any other Action
	*******************************************************************/
	this.buildPosTable = function(num, distance, start)
	{
		// The Width of the Menu Element
		var eleWidth =  this.elem.offsetWidth;
	
		// The Height of the Element
		var eleHeight = this.elem.offsetHeight;
		
		if(distance == null || !distance){
			distance = this.moveVertical ? Math.round(eleHeight/num) : Math.round(eleWidth/num);
		}
		
		if(num == null || !num){
			num = this.moveVertical ? Math.round(eleHeight/distance) : Math.round(eleWidth/distance);
		}
		
		this.POS_TABLE = new Array(); // Position Table
		for(var i=0; i<num; i++){
			this.POS_TABLE.push(start - (i*distance));
		}
		this.POS_ID = 0; // Set Index to 0
		
		if( (this.moveVertical && eleHeight < distance) || (!this.moveVertical && eleWidth < distance) ){
			arrowObj1.style.display = "none";
			arrowObj2.style.display = "none";
		}
		return;
	}
	
	/******************************************************************
	* Initiates a Animation
	* This function will mainly be called by other Functions
	*******************************************************************/
	this.init = function()
	{
		if(this.POS_TABLE == null)
		{
			alert("Create Position Table first");
		}
		else 
		{
			if(this.moveVertical){
				// Get the current vertical position by reading the "top" Style property 
				this.currentPos = parseInt(this.elem.style.top);
			} else {
				// Get the current horizontal position by reading the "left" Style property 
				// Remove the "px" at the end of the String by parsing it to Int
				this.currentPos = parseInt(this.elem.style.left);
			}
			// Currentframe
			this.frame = 1;
			
			// get the Target Position
			this.targetPos = this.POS_TABLE[this.POS_ID];
			
			// Clear Interval if there is any
			if(this.animInterval){
				window.clearInterval(this.animInterval);
			}
		}
		return;
	}
	
	/******************************************************************
	* Scrolls the Object by dynamically altering the left or top Style Property
	* This function will be called by the Interval
	*******************************************************************/
	this.scroll_object = function() 
	{
		if(this.frame < this.totalFrames){
			var newpos = (this.currentPos+((this.targetPos-this.currentPos)/this.totalFrames*this.frame)) + "px";
			if(this.moveVertical){
				this.elem.style.top = newpos;
			} else {
				this.elem.style.left = newpos;
			}
			this.frame++;
		} else {
			if(this.moveVertical){
				this.elem.style.top = this.targetPos + "px";
			} else {
				this.elem.style.left = this.targetPos + "px";
			}
			window.clearInterval(this.animInterval);
			if(this.callFunc){
				this.callFunc(this.POS_ID);
			}
		}
		return;
	}
	
	/******************************************************************
	* Tells the Object to scroll in a direction (dir).
	* Dir can be either 1 or -1. 
	* The wrap Parameter specifys if the animation Wraps (eg. 0 - 1 = go to last element)
	*******************************************************************/
	this.scroll = function(dir, wrap)
	{
		if(dir != 1){
			dir = -1;
		}
		
		var animate = false;
		
		if(this.POS_ID + dir < 0){
			if(wrap){
				this.POS_ID = this.POS_TABLE.length-1;
				animate = true;
			} else {
				this.POS_ID = 0;
			}
		} else if(this.POS_ID + dir > this.POS_TABLE.length-1){
			if(wrap){
				this.POS_ID = 0;
				animate = true;
			} else {
				this.POS_ID = this.POS_TABLE.length-1;
			}
		} else {
			this.POS_ID = this.POS_ID+dir;
			animate = true;
		}
		
		if(animate){
			this.init();
			this.animInterval = window.setInterval(this.refName + ".scroll_object()",40);
		}
		return;
	}
	
	/******************************************************************
	* Tells the Object to scroll to a ID (related to the POS_TABLE)
	*******************************************************************/
	this.scrollToID = function(id)
	{
		if(id > this.POS_TABLE.length-1){
			this.POS_ID = this.POS_TABLE.length-1;
		} else if(id < 0){
			this.POS_ID = 0;
		} else {
			this.POS_ID = id;
		}
		this.init();
		this.animInterval = window.setInterval(this.refName + ".scroll_object()",40);
		return;
	}
	
	/******************************************************************
	* Directly moves to a Position
	*******************************************************************/
	this.moveToID = function(id)
	{
		if(id > this.POS_TABLE.length-1){
			this.POS_ID = this.POS_TABLE.length-1;
		} else if(id < 0){
			this.POS_ID = 0;
		} else {
			this.POS_ID = id;
		}
		this.targetPos = this.POS_TABLE[this.POS_ID];
		if(this.moveVertical){
			this.elem.style.top = this.targetPos + "px";
		} else {
			this.elem.style.left = this.targetPos + "px";
		}
	}
}