var index = 0;
var selectedindex = 0;
var tabTop = new Array();
var tab1 = 'tabtop_01';
var tab2 = 'tabtop_02';
var tab3 = 'tabtop_03';
var leftArrow = 'tabtopLeft';
var rightArrow = 'tabtopRight';

function tabStartUp(instanceID, communityID) {
	if(tabTop.length == 0) {
		hideTab(tab1);
		hideTab(tab2);
		hideTab(tab3);
		checkArrows();
		return;
	}
	index = -1;
	for(i = 0; i < tabTop.length; i++) {
		if(tabTop[i]['instanceID'] == instanceID && tabTop[i]['communityID'] == communityID) {
			index = i;
			break;
		}
	}
	selectedindex = index;
	if(index < 3) {
	    index = 0;
	}
	else if(index >= tabTop.length-3) {
	    index = tabTop.length-3;
	}
	setTabs(index);
}

function addTab(title, href, instanceID, communityID) {
	var length = tabTop.length;
	tabTop[length] = new Array();
	tabTop[length]['title'] = title;
	tabTop[length]['href'] = href;
	tabTop[length]['instanceID']  = instanceID;
	tabTop[length]['communityID'] = communityID;
}

function tabMoveLeft() {
	if(index > 0) {
		index = index - 1;
	}
	setTabs(index);
}

function tabMoveRight() {	
	if(index < (tabTop.length - 3)) {
		index = index + 1;
	}
	setTabs(index);
}

function setTabs(idx) {	
	if((index+2) < tabTop.length) {
		setTab(tab3, index+2);
    	showTab(tab3);
	} else {
		hideTab(tab3);
	}

	if((index+1) < tabTop.length) {
		setTab(tab2, index+1);
    	showTab(tab2);
	} else {
		hideTab(tab2);
	}

	if(index <= tabTop.length) {
		setTab(tab1, index);
        showTab(tab1);
	} else {
		hideTab(tab1);
	}
	checkArrows();
}

function showTab(id) {
	var tab = document.getElementById(id+'P');
	tab.className = '';
}

function hideTab(id) {	
	var tab = document.getElementById(id+'P');
	tab.className = 'hide';
}

function setTab(id, idx) {			
	var tab = document.getElementById(id);
	tab.innerHTML = '&nbsp;&nbsp;&nbsp;'+tabTop[idx]['title'];
	tab.href = tabTop[idx]['href'];
	if(idx == selectedindex) { tab.className = 'tabwit'; } else { tab.className = ''; }
}

function checkArrows() {
	if(index == 0) {
	    hideArrow(leftArrow);
	} else {
	    showArrow(leftArrow);
	}
	
	if((index+3) >= tabTop.length) {
	    hideArrow(rightArrow);
	} else {
	    showArrow(rightArrow);
	}
}

function showArrow(id) {
	var arrow = document.getElementById(id);
	arrow.className = '';
}

function hideArrow(id) {
	var arrow = document.getElementById(id);
	arrow.className = 'hide';
}