var currentY = 0;//current position
var moveTo = 0;//the y value to move to
var start;
var end;
var currentOpenId = 0;//the index of the id of the currently open section
var direction = "";
var timerId;
var moving = false;

function init(id,linkID) {
	n1 = document.cookie.indexOf('sectionid=');
	n2 = document.cookie.indexOf(';', n1);
	
	if(n2 == -1)
		n2 = document.cookie.length;
		
	if(n1 == -1) {
		currentOpenId = (id==undefined)? 0 : id;	
	} else {
		cookie = document.cookie.substring(n1, n2);
		currentOpenId = parseInt(cookie.substring(cookie.indexOf('=') + 1));
	}
	
	if(currentOpenId > sections.length - 1) {
		currentOpenId = (id==undefined)? 0 : id;
	}
	
	y = 0;
	for(var i = 0; i < sections.length; i++) {		
		moveLayer(sections[i], y);
		
		if(i == currentOpenId) {
			y += length + buttonLength;
		} else {
			y += buttonLength;
		}
	}
	
}

function startMove(id,urldirect) {
	idIndex = idToIndex(id)
	if(idIndex != currentOpenId && !moving) {
		move(idIndex);
	}
}

function move(index) {
	moving = true;

	if(currentOpenId < index) {
		//move all sections	above id up
		start = currentOpenId + 1;
		ypos = getLayerY(sections[start]);
		direction = 'up';
		moveTo = ypos - length;
		end = index;		
	} else if(currentOpenId > index) {
		//move all sections below id down
		start = index + 1;
		ypos = getLayerY(sections[start]);
		end = currentOpenId;
		direction = 'down';
		moveTo = ypos + length;				
	}

	document.cookie = 'sectionid=' + index;
	
	currentOpenId = index;
	doMove();
}

function doMove(){
	if(direction == 'down') {
		ypos += step;
	} else {
		ypos -= step;
	}

	pos = ypos;

	for(var i = start; i <= end; i++) {		
		moveLayer(sections[i], pos);
		pos += buttonLength;
	}

	if ((direction == 'down' && ypos >= moveTo) || (direction <= 'up' && ypos == moveTo)) {
		window.clearTimeout(timerId);
		timerId = 0;
		moving = false;		
	} else {
		timerId = window.setTimeout("doMove();", delay);
	}
}

function moveLayer(id, y) {
	if(document.layers) {
		document[id].top = y;
	}
	if(document.all) {
		document.all[id].style.top = y;
	}
	if(!document.all && document.getElementById) {
		document.getElementById(id).style.top = y + "px";
	}
}

function getLayerY(id) {	
	if(document.layers) {
		y = document[id].top;
	}
	if(document.all) {
		y = document.all[id].style.top;
	}
	if(!document.all && document.getElementById) {
		y = document.getElementById(id).style.top;
	}
	
	if(y.indexOf('px')) {
		y = parseInt(y.substring(0, y.indexOf('px')));
	}
	
	return y;
}

function idToIndex(id) {
	for(var i = 0; i < sections.length; i++) {
		if(sections[i] == id) {
			break;
		}
	}
	
	return i;
}