var lastCartoonLink 	= 0;
var currentCartoonLink 	= 0;
var lastRecLink			= 0;
var currentRecLink		= 0;
var is_ie				= (window.navigator.userAgent.toLowerCase().indexOf("msie") != -1) && !window.opera;
var cartoonStep			= 0;
var recStep 			= 0;
var currentSection		= "home-pane"; // The default loaded section on the page
var paneTag				= "-pane";
var tabTag				= "-tab";
var anchorTag			= "-anchor";


function setOpacity(elem, percent)
{
	if ( is_ie )
	{
		elem.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + percent + ")";
	}
	else
	{	  
		elem.style.opacity = percent / 100;
	}
}
	
	
function crossFadeCartoon()
{	
	cartoonStep += 10;
		 	
	setOpacity(document.getElementById( "cartoons" ).childNodes[ lastCartoonLink ], 100 - cartoonStep );
	setOpacity(document.getElementById( "cartoons" ).childNodes[ currentCartoonLink ], cartoonStep );
		  
	if (cartoonStep < 100)
	{
		setTimeout("crossFadeCartoon()", 100);
	}
	else
	{
		setTimeout("nextCartoon()", 10000);
		cartoonStep = 0;
	}
}


function crossFade()
{	
	recStep += 10;
		 	
	setOpacity(document.getElementById( "sidebarContent" ).childNodes[ lastRecLink ], 100 - recStep );
	setOpacity(document.getElementById( "sidebarContent" ).childNodes[ currentRecLink ], recStep );
		  
	if (recStep < 100)
	{
		setTimeout("crossFade()", 100);
	}
	else
	{
		setTimeout("nextRec()", 7500);
		recStep = 0;
	}
}
	
	
function nextCartoon()
{
	lastCartoonLink = currentCartoonLink;
		  
	currentCartoonLink++;
	currentCartoonLink %= document.getElementById( "cartoons" ).childNodes.length;
			
	setTimeout("crossFadeCartoon()", 100);
}


function nextRec()
{
	lastRecLink = currentRecLink;
		  
	currentRecLink++;
	currentRecLink %= document.getElementById( "sidebarContent" ).childNodes.length;
			
	setTimeout("crossFade()", 100);
}

	   

function ScrollSection(link, scrollArea, offset)
{	
	if (currentSection == link)
	{
		return;
	}
	
	lastSection		= currentSection;
	currentSection	= link;

    currentAnchor = currentSection.split("-")[0] + anchorTag;
    document.getElementById( currentAnchor ).className = "main-menubar-selected";
    
    if ( lastSection )
    {
	    lastAnchor = lastSection.split("-")[0] + anchorTag;
	    document.getElementById( lastAnchor ).className = "main-menubar-unselected";
	}
	
	theScroll = document.getElementById(scrollArea);
	position = findElementPos(document.getElementById(link));
	
	if (offset != "") {
		offsetPos = findElementPos(document.getElementById(offset));
		position[0] = position[0] - offsetPos[0];
	}

	scrollStart(theScroll, theScroll.scrollLeft, position[0] );
}


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 = 20;
	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 findElementPos(elemFind)
{
	var elemX = 0;
	var elemY = 0;
	do {
		elemX += elemFind.offsetLeft;
		elemY += elemFind.offsetTop;
	} while ( elemFind = elemFind.offsetParent )

	return Array(elemX, elemY);
}


function sineInOut(t, b, c, d)
{
	return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
}
