/************************************************************************************************************
(C) www.dhtmlgoodies.com, September 2005

This is a script from www.dhtmlgoodies.com. You will find this and a lot of other scripts at our website.	

Terms of use:
You are free to use this script as long as the copyright message is kept intact. However, you may not
redistribute, sell or repost it without our permission.

Note:
This script was modified to do horizontal scroll instead of vertical and don't use a scroll bar,
just buttons.

Thank you!

www.dhtmlgoodies.com
Alf Magne Kalleland

************************************************************************************************************/	
var visibleContentWidth = 0;	
var totalContentWidth = false;

var scrollActive = false;
var scrollbuttonActive = false;
var scrollbuttonDirection = false;
var scrollbuttonSpeed = 2; // How fast the content scrolls when you click the scroll buttons(Up and down arrows)
var scrollTimer = 10;	// Also how fast the content scrolls. By decreasing this value, the content will move faster	

var operaBrowser = false;
if(navigator.userAgent.indexOf('Opera')>=0)operaBrowser=1;
	
function scrollDiv_startScroll(e)
{
	if(document.all && !operaBrowser)e = event;
	scrollActive = true;
}

function scrollDiv_stopScroll()
{
	scrollActive = false;
	scrollbuttonActive = false;
}

function cancelEvent()
{
	return false;			
}

function scrolldiv_scrollButton()
{
	if(this.id=='scrolldiv_scrollDown') scrollbuttonDirection = scrollbuttonSpeed;
	else scrollbuttonDirection = scrollbuttonSpeed*-1;
	scrollbuttonActive=true;
	scrolldiv_scrollButtonScroll();
}

function scrolldiv_scrollButtonScroll()
{
	if(!scrollbuttonActive)return;
	
	var topPos = document.getElementById('scrolldiv_content').style.left.replace('px','');
	topPos = topPos / 1 + scrollbuttonDirection;

	if (topPos >= 0 && scrollbuttonDirection > 0){
		topPos=0;
		scrollbuttonActive=false;
		return;
	}
	if ((topPos / 1) <= ((visibleContentWidth - totalContentWidth) / 1) && scrollbuttonDirection < 0) {
		topPos = visibleContentWidth - totalContentWidth;	
		scrollbuttonActive=false;
		return;
	}	
	document.getElementById('scrolldiv_content').style.left = topPos + 'px';
	
	setTimeout('scrolldiv_scrollButtonScroll()',scrollTimer);
}

function scrolldiv_scrollButtonStop()
{
	scrollbuttonActive = false;
}

function scrolldiv_initScroll(tagName, selectedIdx)
{
	var contents = document.getElementById('scrolldiv_content');
	var contentElements = contents.getElementsByTagName(tagName);
	
	var widthToSelectedItem = 0;
	if (contentElements != null) {
		var totalWidth = 0;
		for (var i = 0; i < contentElements.length; i++) {
			if (i == selectedIdx)
				widthToSelectedItem = totalWidth;
			
			totalWidth += contentElements[i].offsetWidth;
		}
		totalContentWidth = totalWidth;
	}
	else {
		totalContentWidth = 0;
	}
	visibleContentWidth = document.getElementById('scrolldiv_parentContainer').offsetWidth;
		
	document.getElementById('scrolldiv_scrollDown').onmousedown = scrolldiv_scrollButton;
	document.getElementById('scrolldiv_scrollUp').onmousedown = scrolldiv_scrollButton;
	document.getElementById('scrolldiv_scrollDown').onmouseup = scrolldiv_scrollButtonStop;
	document.getElementById('scrolldiv_scrollUp').onmouseup = scrolldiv_scrollButtonStop;
	document.getElementById('scrolldiv_scrollUp').onselectstart = cancelEvent;
	document.getElementById('scrolldiv_scrollDown').onselectstart = cancelEvent;

	if (widthToSelectedItem < (visibleContentWidth - 100))
		widthToSelectedItem = 0;
	else if (widthToSelectedItem > totalContentWidth - visibleContentWidth)
		widthToSelectedItem = totalContentWidth - visibleContentWidth;

	document.getElementById('scrolldiv_content').style.left = -widthToSelectedItem + 'px';
}
/*
Setting scroll button speed
*/
function setScrollButtonSpeed(newScrollButtonSpeed)
{
	scrollbuttonSpeed = newScrollButtonSpeed;
}
/*
Setting interval of the scroll
*/
function setScrollTimer(newInterval)
{
	scrollTimer = newInterval;
}