var currentSection = "imgdiv0-pane";
var currentIndex = 0;
var scrollArea =  "img_scroller";
var offset = "imgdiv0-pane";
var hrefTag = "-href";
var playing = false;
var sectionNumber = 0;


function ScrollSection(link){
	if(playing) {stop();}
	ScrollSection_(link);
}

function ScrollSection_(link){
	currentIndex = link;	
	link = 'imgdiv'+link+'-pane';
	if (currentSection == link) {
		sectionHref = currentSection.split("-")[0] + hrefTag;
		document.getElementById(sectionHref).className = "active";		
		return;
	}
	lastSection = currentSection;
	currentSection = link;
	sectionHref = currentSection.split("-")[0] + hrefTag;
	document.getElementById(sectionHref).className = "active";
  	if (lastSection) {
		lastHref = lastSection.split("-")[0] + hrefTag;
		document.getElementById(lastHref).className = "inactive";
	}
	theScroll = document.getElementById(scrollArea);
	position = findElementPos(document.getElementById(link));
	if (offset != "") {
		offsetPos = findElementPos(document.getElementById(offset));
		position = position - offsetPos;
	}
	scrollStart(theScroll, theScroll.scrollLeft, position);
}

var scrollanim = {time:0, begin:0, change:0.0, duration:0.0, element:null, timer:null};

function scrollStart(elem, start, end){
	if (scrollanim.timer != null) {
		clearInterval(scrollanim.timer);
		scrollanim.timer = null;
	}
	scrollanim.time = 0;
	scrollanim.begin = start;
	scrollanim.change = end - start;
	scrollanim.duration = 25;
	scrollanim.element = elem;
	scrollanim.timer = setInterval("scrollAnim();", 15);
}

function scrollAnim(){
	if (scrollanim.time > scrollanim.duration) {
		clearInterval(scrollanim.timer);
		scrollanim.timer = null;
	}
	else {
		move = sineInOut(scrollanim.time, scrollanim.begin, scrollanim.change, scrollanim.duration);
		scrollanim.element.scrollLeft = move;
		scrollanim.time++;
	}
}

function sineInOut(t, b, c, d){
	return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
}

function findElementPos(elemFind){
	var elemX = 0;
	do {
		elemX += elemFind.offsetLeft;
	} while ( elemFind = elemFind.offsetParent )
	return elemX;
}

function slideShow() {
	if(playing) {stop();}
	else {play()}
}	

function play() {
	playing = true;
	if(++currentIndex == sectionNumber){
		currentIndex = 0;
	}
	ScrollSection_(currentIndex);
	timer = setTimeout("play();", 5000);
	document.getElementById("play_button").src = "img/slideshow/stop.jpg";		
}

function stop(){
	clearInterval(timer);
	playing = false;
	document.getElementById("play_button").src = "img/slideshow/play.jpg";		
}

window.onload = function () {
	//for transitional effect
	setTimeout("slideShow();", 5000);
	sectionNumber = document.getElementById('img_toolbar').getElementsByTagName('A').length;
}