var currentSection = "new1-pane";    // The default loaded section on the page
var tabTag = "-tab";
var paneTag = "-pane";
// Rotation code
var BANNER_COUNT = 0;
var currentBanner = 1;
var startTimer = null;
var BANNER_ROTATION_DELAY = 5000;
var MANUAL = 0;
var AUTO = 1;
var k = 1;
var pointer =2;    

window.onload = startBannerRotation;

function scrollbann(){
		var deptnum = document.getElementById("deptnum").value;      
		setTimeout("startBannerRotation()",1000);
}


function bannerSlide (index){  
	var state; 
	//pointer = index+1 ;
	var divid = "new"+index+"-pane" ; 
    ScrollSection(divid, 'scroller', 'new1-pane', index, AUTO);
	currentBanner = index;
	if (index == BANNER_COUNT) index = 0;
	index++;
	startTimer = setTimeout( "bannerSlide(" + index + ")", BANNER_ROTATION_DELAY );
}

function startBannerRotation () {  
	bannerCount = document.getElementById("deptnum").value;   
	BANNER_COUNT = bannerCount;  
	startTimer = setTimeout( "bannerSlide(2)", BANNER_ROTATION_DELAY );
}

function ScrollSection(link, scrollArea, offset, targetBanner, mode){    //alert(link+"##"+scrollArea+"##"+offset+"##"+targetBanner+"##"+mode);      
    pointer = targetBanner ;
	if (mode == MANUAL) {
		if (startTimer != null) {
			//clearTimeout(startTimer);
			currentBanner = targetBanner;
		}
	}             
	if (currentSection == link) {
		return;
	}        
	lastSection = currentSection;
	currentSection = link;    
    sectionTab = currentSection.split("-")[0] + tabTag;    
    document.getElementById(sectionTab).className = "active";     
    if (lastSection) {
	    lastTab = lastSection.split("-")[0] + tabTag;
	    document.getElementById(lastTab).className = "inactive";
	}                
	theScroll = document.getElementById(scrollArea);   
	position = findElementPos(document.getElementById(link));   //alert(position); 
	if (offset != "") {
		offsetPos = findElementPos(document.getElementById(offset));
		position[0] = position[0] - offsetPos[0];   
	}   
	scrollStart(theScroll, theScroll.scrollLeft, position[0], "horiz");   
}




var scrollanim = {time:0, begin:0, change:0.0, duration:0.0, element:null, timer:null};
function scrollStart(elem, start, end, direction){       
	//console.log("scrollStart from "+start+" to "+end+" in direction "+direction);
	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;
	
	if (direction == "horiz") {
		scrollanim.timer = setInterval("scrollHorizAnim();", 15);
	}
	else {
		scrollanim.timer = setInterval("scrollVertAnim();", 15);
	}
}
function scrollHorizAnim(){   
	if (scrollanim.time > scrollanim.duration) {
		clearInterval(scrollanim.timer);
		scrollanim.timer = null;    
		//bannerSlide(pointer);
	}
	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;
	var elemY = 0;
	do {      //alert(elemFind+"##"+elemFind.offsetLeft+"###"+elemFind.offsetTop+"###"+elemFind.offsetParent);
		elemX += elemFind.offsetLeft;
		elemY += elemFind.offsetTop;
	} while ( elemFind == elemFind.offsetParent )
 
	return Array(elemX, elemY);
}

function continueBannerRotation (index) {
	if (index == BANNER_COUNT) {
		index = 1;
	} else {
		index++;
	}
	startTimer = setTimeout( "bannerSlide(" + index + ")", BANNER_ROTATION_DELAY );
}

function pauseBannerRotation() {
	if (startTimer != null) {
		// Stop the timer
		clearTimeout(startTimer);
	}
}
















/*function actuallyHide() {   
	var popDownload = document.getElementById('dpop');
	if (dpopTimer != "")
	{
		dpopTimer = "";
		moveStart(popDownload, parseInt(popDownload.style.left), parseInt(popDownload.style.left), parseInt(popDownload.style.top), parseInt(popDownload.style.top) - 10, 15);		
		fadeElementSetup('dpop', 100, 0, 13, 1);
	}
}

// Move is also synchronous. One at a time, please.
var moveanim = {time:0, beginX:0, changeX:0.0, beginY:0, changeY:0, duration:0.0, element:null, timer:null};

function moveStart(elem, startX, endX, startY, endY, duration){    alert(8);
	if (moveanim.timer != null) {
		clearInterval(moveanim.timer);
		moveanim.timer = null;
	}
	moveanim.time = 0;
	moveanim.beginX = startX;
	moveanim.changeX = endX - startX;
	moveanim.beginY = startY;
	moveanim.changeY = endY - startY;
	moveanim.duration = duration;
	moveanim.element = elem;

	moveanim.timer = setInterval("moveAnimDo();", 15);
}

*/

/*function moveAnimDo(){    alert(9)
	if (moveanim.time > moveanim.duration) {
		clearInterval(moveanim.timer);
		moveanim.timer = null;
	}
	else {
		moveX = cubicOut(moveanim.time, moveanim.beginX, moveanim.changeX, moveanim.duration);
		moveY = cubicOut(moveanim.time, moveanim.beginY, moveanim.changeY, moveanim.duration);
		moveanim.element.style.left = moveX + "px";
		moveanim.element.style.top = moveY + "px";
		moveanim.time++;
	}
}
*/


//console.log("Initialized");


/*function continueBannerRotation (index) {
	if (index == BANNER_COUNT) {
		index = 1;
	} else {
		index++;
	}
	startTimer = setTimeout( "bannerSlide(" + index + ")", BANNER_ROTATION_DELAY );
}

function pauseBannerRotation() {
	if (startTimer != null) {
		// Stop the timer
		clearTimeout(startTimer);
	}
}
*/
/*function bannerSlide (index){
	var state;          alert(2);
    switch(index) {
		case 1:
			ScrollSection('new-pane', 'scroller', 'new-pane', index, AUTO);
			break;
		case 2:
			ScrollSection('sites-pane', 'scroller', 'new-pane', index, AUTO);
			break;
		case 3:
			ScrollSection('files-pane', 'scroller', 'new-pane', index, AUTO);
			break;
		case 4:
			ScrollSection('editor-pane', 'scroller', 'new-pane', index, AUTO);
			break;
		default:
			break;
	}
	currentBanner = index;
	if (index == BANNER_COUNT) index = 0;
	index++;
	startTimer = setTimeout( "bannerSlide(" + index + ")", BANNER_ROTATION_DELAY );
}
*/




// Scroll the page using the arrows

/*function ScrollArrow(direction, toolbar, scrollArea, offset) {      alert(4);
 
	toolbarElem = document.getElementById(toolbar);
	toolbarNames = new Array();
	// Find all the <div> elements in the toolbar, and extract their id's into an array.
	if (toolbarElem.hasChildNodes())
	{
		var children = toolbarElem.childNodes;
		for (var i = 0; i < children.length; i++) 
		{
			if (toolbarElem.childNodes[i].tagName == "DIV") {
				toolbarNames.push(toolbarElem.childNodes[i].id.split("-")[0]);
			}
		}
	}
	// Now iterate through our array of tab names, find matches, and determine where to go.
	for (var i = 0; i < toolbarNames.length; i++) {
		if (toolbarNames[i] == currentSection.split("-")[0]) {
			if (direction == "left") {
				if (i - 1 < 0) {
					gotoTab = toolbarNames[toolbarNames.length - 1];
				} else {
					gotoTab = toolbarNames[i - 1];
				}
			} else {
				if ((i + 1) > (toolbarNames.length - 1)) {
					gotoTab = toolbarNames[0];
				} else {
					gotoTab = toolbarNames[i + 1];
				}
			}
		}
	}
	// Go to the section name!
	ScrollSection(gotoTab+paneTag, scrollArea, offset);
}

function startBannerRotation (bannerCount) {
	// Reset default banner count
	BANNER_COUNT = bannerCount;   alert(1);
	startTimer = setTimeout( "bannerSlide(2)", BANNER_ROTATION_DELAY );
}
*/