var interval = 6; // interval in seconds
var fadeSpeed = 40; // fade speed, the lower the speed the faster the fade.  40 is normal.
var list; // global list variable cache
var tickerObj; // global tickerObj cache
var hex = 255;

function fadeText(divId)
{
	if(tickerObj)
	{
		if(hex>0)
		{
			hex-=5; // increase color darkness
			tickerObj.style.color="rgb("+hex+","+hex+","+hex+")";
			setTimeout("fadeText('" + divId + "')", fadeSpeed); 
		}
		else
			hex=255; //reset hex value
	}
}

function initialiseList(divId)
{
	tickerObj = document.getElementById(divId);
	if(!tickerObj)
		reportError("Could not find a div element with id \"" + divId + "\"");
	
	list = tickerObj.childNodes;
	if(list.length <= 0)
		reportError("The div element \"" + divId + "\" does not have any children");
		
	for (var i=0; i<list.length; i++)
	{
		var node = list[i];
		if (node.nodeType == 3 && !/\S/.test(node.nodeValue)) 
	        		tickerObj.removeChild(node);

	}
	run(divId, 0);
}
function run(divId, count)
{
	fadeText(divId);
	list[count].style.display = "block";
	if(count > 0)
		list[count-1].style.display = "none";
	else
		list[list.length-1].style.display = "none";
	
	count++;
	if(count == list.length)
		count = 0;
		
	window.setTimeout("run('" + divId + "', " + count+ ")", interval*1000);
}
function reportError(error)
{
	alert("The script could not run because you have errors:\n\n" + error);
	return false;
}

var slideSpeed = 20;	// Higher value = faster
var timer = 1;	// Lower value = faster

var objectIdToSlideDown = false;
var activeId = false;
var overwriteId = false;
var divs;
var innit;
function showHideContent()
{
	if(!overwriteId)
		var numericId = this.id.replace(/[^0-9]/g,'');
	else
	{
		var numericId = overwriteId;
		overwriteId = false;
	}
	
	if(innit)
	{
		slideSpeed = 30;
		innit = false;
	}
	else
	{
		slideSpeed = 20;
	}
	
	
	var answerDiv = document.getElementById('a' + numericId);
	if(!answerDiv.style.display || answerDiv.style.display=='none')
	{
		if(activeId &&  activeId!=numericId){
			objectIdToSlideDown = numericId;
			slideContent(activeId,(slideSpeed*-1));
		}else{
			answerDiv.style.display='block';
			answerDiv.style.visibility = 'visible';
			slideContent(numericId,slideSpeed);
		}
	}
	else
	{
		slideContent(numericId,(slideSpeed*-1));
		activeId = false;
	}	
}

function slideContent(inputId,direction)
{
	var obj =document.getElementById('a' + inputId);
	var contentObj = document.getElementById('ac' + inputId);
	height = obj.clientHeight;
	height = height + direction;
	rerunFunction = true;
	if(height>contentObj.offsetHeight){
		height = contentObj.offsetHeight;
		rerunFunction = false;
	}
	if(height<0){
		height = 0;
		rerunFunction = false;
	}

	obj.style.height = height + 'px';
	var topPos = height - contentObj.offsetHeight;
	if(topPos>0)topPos=0;
	contentObj.style.top = topPos + 'px';
	if(rerunFunction){
		setTimeout('slideContent(' + inputId + ',' + direction + ')',timer);
	}else{
		if(height==0){
			obj.style.display='none'; 
			if(objectIdToSlideDown && objectIdToSlideDown!=inputId){
				document.getElementById('a' + objectIdToSlideDown).style.display='block';
				document.getElementById('a' + objectIdToSlideDown).style.visibility='visible';
				slideContent(objectIdToSlideDown,slideSpeed);				
			}
		}else{
			activeId = inputId;
		}
	}
}



function initShowHideDivs()
{
	divs = document.getElementsByTagName('DIV');
	var divCounter = 1;
	for(var no=0;no<divs.length;no++){
		if(divs[no].className=='portfolioItem'){
			divs[no].onclick = showHideContent;
			divs[no].id = 'q'+divCounter;
			
			var answer = divs[no].nextSibling;
			while(answer && answer.tagName!='DIV'){
				answer = answer.nextSibling;
			}
			
			answer.id = 'a'+divCounter;	
			
			contentDiv = answer.getElementsByTagName('DIV')[0];
			contentDiv.style.top = 0 - contentDiv.offsetHeight + 'px'; 	
			contentDiv.className='portfolioItemContentBody';
			contentDiv.id = 'ac' + divCounter;
			answer.style.display='none';
			divCounter++;
		}		
	}	
	overwriteId = 1;
	innit = true;
	showHideContent();
	fixMeffyIE();
}
window.onload = initShowHideDivs;