var directionArr = ['rauf', 'runter', 'links', 'rechts'];
var updateID;
var contentFrame = null;
var activeDirection = -1;
var scrollspeed = 5;
var menuColor = 'black';
var menuColorActive = 'white';
var defaultPicText = 'Communication with a smile';
var defaultImageUrl = 'pics/smile_7e.jpg';
//var defaultImageUrl = 'pics/Elisa_10e.jpg';
var baseUrl = 'http://www.followup.at/';
//var baseUrl = 'http://fup.debian.is1130.com/';

function setButtonVisible(direction, visibility, index){
	document.getElementById(directionArr[direction]).style.visibility=(visibility?'visible':'hidden');
}

function calculatePosition(index) {
	contentFrame[index].content.style.top=0;
	contentFrame[index].content.style.left=0;
	contentFrame[index].content.style.top=contentFrame[index].content.offsetTop;
	document.getElementById('scrollbuttons').style.top=document.body.offsetHeight-70;
	//if (index==1)
	//	alert('1: '+index+', '+ contentFrame[index].content.style.left +', '+ document.getElementById('scrollbuttons').style.left +', '+ (document.body.offsetWidth > contentFrame[index].min));
	if (document.body.offsetWidth > contentFrame[index].min) {
	//	if (index==1) alert('1');
		contentFrame[index].content.style.left=contentFrame[index].content.offsetLeft;
		if (index == 0) {
			document.getElementById('scrollbuttons').style.left=contentFrame[index].left;
		} else {
			if ((document.body.offsetWidth - contentFrame[index].min - 40) > 0)
				document.getElementById('scrollbuttons').style.left = document.body.offsetWidth-contentFrame[index].min - 40 - 5;
			else
				document.getElementById('scrollbuttons').style.left = 5;
			//document.body.offsetWidth-contentFrame[index].left
		}
		//document.getElementById('scrollbuttons').style.left = ((index==1)?document.body.offsetWidth-contentFrame[index].left:contentFrame[index].left);
	} else {
	//	if (index==1) alert('2');
		contentFrame[index].content.style.left=contentFrame[index].content.offsetLeft;
		if (index == 0) {
			document.getElementById('scrollbuttons').style.left=document.body.offsetWidth-contentFrame[index].right;
		} else {
			document.getElementById('scrollbuttons').style.left = 5;
		}
	}
	//if (index==1)
	//	alert('2: '+index+': '+ contentFrame[index].content.style.left +', '+ document.getElementById('scrollbuttons').style.left +', '+ (document.body.offsetWidth > contentFrame[index].min));
}
function updateScrollButtons(value, frameValue, offsetValue, button0, button1, index) {
	if(value>frameValue){
		if(value+offsetValue>frameValue) {
			setButtonVisible(button1,1, index);
		} else {
			setButtonVisible(button1,0, index);
			//alert('1:' + value+', '+frameValue+', '+offsetValue);
			activeDirection=-1;
			clearInterval(updateID);
		}
		if(offsetValue<0) {
			setButtonVisible(button0,1, index);
		} else {
			setButtonVisible(button0,0, index);
			//alert('2:' + value+', '+frameValue+', '+offsetValue);
			activeDirection=-1;
			clearInterval(updateID);
		}
	} else {
		activeDirection=-1;
		setButtonVisible(button0,0, index);
		setButtonVisible(button1,0, index);
	}
}
function checkScrollButtons(index){
	var height=contentFrame[index].content.offsetHeight;
	var frameHeight=document.body.offsetHeight;
	var offsetTop=contentFrame[index].content.offsetTop;
	var width=contentFrame[index].content.offsetWidth;
	var frameWidth=document.body.offsetWidth;
	var offsetLeft=contentFrame[index].content.offsetLeft;
	//alert(width+', '+frameWidth+', '+offsetLeft);
	if (activeDirection<2)
		updateScrollButtons(height, frameHeight, offsetTop, 0, 1, index);
	if ((activeDirection<0)||(activeDirection>1))
		updateScrollButtons(width, frameWidth, offsetLeft, 2, 3, index);
}
function updateCommand(direction, index) {
	if (direction < 2)
		contentFrame[index].content.style.pixelTop+=(direction?-scrollspeed:scrollspeed);
	else
		contentFrame[index].content.style.pixelLeft+=((direction-2)?-scrollspeed:scrollspeed);
	checkScrollButtons(index);
}
function scroll(direction, index){
	activeDirection=direction;
	updateID=setInterval('updateCommand('+direction+','+index+')',5);
}
function stopscroll(){
	clearInterval(updateID);
}

function init(index) {
	initValues(index);
	initScrollbars(index);
	initMenu(null, null, null);
}

function initValues(index) {
	if (contentFrame == null) {
		contentFrame = [new Object(), new Object()];
		contentFrame[0].str = 'content';
		contentFrame[0].left = 435;
		contentFrame[0].right = 50;
		contentFrame[0].min = 480;
		contentFrame[0].content = null;
		contentFrame[1].str = 'menucontent';
		contentFrame[1].left = 50;
		contentFrame[1].right = 50;
		contentFrame[1].min = 180;
		contentFrame[1].content = null;
	}
	if (index == 0)
		top.document.title=document.title;
	contentFrame[index].content=document.getElementById(contentFrame[index].str);
}

function init(index, activeDocName, imageUrl, picText) {
	initValues(index);
	initScrollbars(index);
	initMenu(activeDocName, imageUrl, picText);
}

function initMenu(activeDocName, imageUrl, picText) {
	//alert('activeDocName: ' + activeDocName + " | " + imageUrl + " | " + picText);
	//alert('imageUrl: ' + imageUrl);
	//alert('picText: ' + picText);
	//alert('top.menucontent.document: ' + top.menucontent.document);
	if (document != top.menucontent.document) {
		if (imageUrl != null) {
			//alert('imageUrl: ' + imageUrl);
			top.menucontent.document.images[0].src = baseUrl + imageUrl;
		} else {
			top.menucontent.document.images[0].src = baseUrl + defaultImageUrl;
		}
		var tmpH2 = top.menucontent.document.images[0].height;
		for (var i = 0; i < top.menucontent.document.links.length; ++i) {
			if (top.menucontent.document.links[i].firstChild.nodeValue == activeDocName) {
				//alert(i+': '+ menuColorActive + ', ' + top.menucontent.document.links[i].firstChild.nodeValue);
				top.menucontent.document.links[i].style.color = menuColorActive;
			}
			else {
				//alert(i+': '+ menuColor+ ', ' + top.menucontent.document.links[i].firstChild.nodeValue);
				top.menucontent.document.links[i].style.color = menuColor;
			}
		}
		if (picText != null) {
			//alert('picText: ' + picText);
			top.menucontent.document.getElementById('menupic').firstChild.nodeValue = picText;
		} else {
			//top.menucontent.document.getElementById('menupic').firstChild.nodeValue = picText;
			top.menucontent.document.getElementById('menupic').firstChild.nodeValue = defaultPicText;
		}
		top.menucontent.document.getElementById('menupic').style.color = menuColorActive;
	}
}

function initScrollbars(index) {
	//var activeDocName = 'Seminare';
	//var imageUrl = 'pics/2344_30_6e.jpg';
	//var picText = 'Hallo!';
	//
	//parent.document.getElementsByName(contentFrame[index].str)[0].scrolling='no';
	//document.scrolling='no';
	document.getElementById('scrollbuttons').style.visibility='visible';
	document.getElementById('rauf').style.visibility='visible';
	document.getElementById('runter').style.visibility='visible';
	document.getElementById('links').style.visibility='visible';
	document.getElementById('rechts').style.visibility='visible';
}
