﻿function DivScroll(divID, scrollTime, dirID, esqID)
{
	// Propriedades
	var frameTime = 33

	var Div
	var Ul
	var Nav =
	{
		left: undefined,
		right: undefined
	}

	var Li =
	{
		n: 0,
		size: 0,
		current: 1,
		update: 0,
		target: 1,
		move: 0
	}
	var ScrollTime

	var Interval

	var Animation =
	{
		ratio: undefined,
		target: undefined,
		current: 0,
		n: undefined,
		nAtual: 0
	}

	// Métodos
	this.MoveLeft = function()
	{
		if (Interval != undefined)
			return

		Li.move = Math.min(Li.current - 1, Li.update)
		if (Li.move <= 0)
			return

		Li.target = Li.current - Li.move
		scrollTime = ScrollTime / Li.update * Li.move
		Animation.n = parseInt(scrollTime / frameTime)
		Animation.ratio = Li.move * Li.size
		Animation.target = Animation.current + Animation.ratio
		Animation.ratio /= Math.max(Animation.n, 1)
		Animation.nAtual = 0;

		Interval = window.setInterval('document.getElementById(\'' + Div.id + '\').DivScroll.Update()', frameTime)
	}

	this.MoveRight = function()
	{
		if (Interval != undefined)
			return

		Li.move = Math.min(Li.n - (Li.current + Li.update - 1), Li.update)
		if (Li.move <= 0)
			return

		Li.target = Li.current + Li.move
		scrollTime = ScrollTime / Li.update * Li.move
		Animation.n = parseInt(scrollTime / frameTime)
		Animation.ratio = -Li.move * Li.size
		Animation.target = Animation.current + Animation.ratio
		Animation.ratio /= Math.max(parseInt(scrollTime / frameTime), 1)
		Animation.nAtual = 0;

		Interval = window.setInterval('document.getElementById(\'' + Div.id + '\').DivScroll.Update()', frameTime)
	}

	this.Update = function()
	{
		Animation.nAtual++
		Animation.current += Animation.ratio
		Ul.style.left = Animation.current + 'px'

		if (Animation.nAtual >= Animation.n)
		{
			this.Finalizar()
		}
	}

	this.Finalizar = function()
	{
		window.clearInterval(Interval)
		Interval = undefined

		Ul.style.left = Animation.target + 'px'
		Animation.current = Animation.target
		Li.current = Li.target
		
		this.UpdateClass()
	}

	this.UpdateClass = function()
	{
		Nav.right.className = (Li.current == (Li.n - Li.update + 1)) ? 'off' : 'on'
		Nav.left.className = (Li.current == 1) ? 'off' : 'on'
	}

	//Inicialização
	if (typeof (divID) == "string")
		Div = document.getElementById(divID)
	else
		Div = divID

	Div.DivScroll = this

	if (typeof (dirID) == "string")
		Nav.right = document.getElementById(dirID)
	else
		Nav.right = dirID

	if (typeof (esqID) == "string")
		Nav.left = document.getElementById(esqID)
	else
		Nav.left = esqID

	Ul = Div.getElementsByTagName('ul')[0]
	var li = Ul.getElementsByTagName('li')[0]
	
	if(li != null)
	{
		Li.n = 1
		
		while(li.nextSibling != null)
		{
			if(li.nextSibling.tagName != undefined)
			{
				Li.n++
			}
			
			li = li.nextSibling
		}
	}
	
	//Li.n = Div.getElementsByTagName('li').length
	
	if (Li.n > 0)
	{
		Div.style.position = 'relative'
		Div.style.overflow = 'hidden'

		margin = {
					left: Div.getElementsByTagName('li')[0].style.marginLeft.replace('px', ''),
					right: Div.getElementsByTagName('li')[0].style.marginRight.replace('px', '')
				 }
		margin.left = margin.left == '' ? 0 : parseInt(margin.left)
		margin.right = margin.right == '' ? 0 : parseInt(margin.right)
		Li.size = Div.getElementsByTagName('li')[0].clientWidth + margin.left + margin.right
		Li.update = Div.clientWidth / Li.size

		Ul.style.position = 'relative'
		Ul.style.width = (Li.n * Li.size) + 'px'

		ScrollTime = scrollTime

		Nav.right.onclick = document.getElementById(Div.id).DivScroll.MoveRight
		Nav.left.onclick = document.getElementById(Div.id).DivScroll.MoveLeft
	}

	this.UpdateClass()

	Div.DivScroll = this
}
