//define here the divs which store the images;
var treeImgBoxes = new Array();
var d=document;
var fadebox = new Array();
var zInterval = null;
var pause=false;
var secFocus = 5000;


var treeNewsBoxes = new Array();
var newsFocus = false;
var useNews = false;


function focusNews(){
	newsFocus = true;
	var idR = this.id.split("_");
	var curr = d.getElementById("treeImages").getAttribute("current_tif");

	var objCurr = d.getElementById("tri_" + curr);
	objCurr.style.opacity = 0;
	objCurr.setAttribute("xopacity",0);
	objCurr.style.display = 'none';

	d.getElementById("nws_" + curr).className = "item";
	this.className = "focus item";
	
	
	d.getElementById("treeImages").setAttribute("current_tif",idR[1]);
	d.getElementById("tri_" + idR[1]).style.opacity = "1.0";
	d.getElementById("tri_" + idR[1]).setAttribute("xopacity",100);
	d.getElementById("tri_" + idR[1]).style.display = 'block';
	
}



function setTreeLink(objRel){
	var setLink = objRel.getAttribute("rel");
	clObj = d.getElementById(objRel.parentNode.id + "_skin");
	clObj.style.cursor = "default";
	clObj.setAttribute("rel", null);
	if(setLink != null && setLink != 0){
		clObj.style.cursor = "pointer";
		clObj.setAttribute("rel", setLink);
	}
}

function treeImage_init() {
	if(!d.getElementById || !d.createElement) return;
	
	if(d.getElementById("newsCast") != null){
		 treeNewsBoxes = d.getElementById("newsCast").getElementsByTagName("div");
		 useNews = true;
		}

	for(var i = 0;i < treeImgBoxes.length ; i++){
		var bid = treeImgBoxes[i];
	 
		fadebox = d.getElementById(bid).getElementsByTagName("div");
		if(fadebox.length > 0){
			for(var u=1;u<fadebox.length;u++){
				if(treeNewsBoxes[u] && useNews){
					treeNewsBoxes[u].id= "nws_" + u;
					treeNewsBoxes[u].onclick = focusNews;
                    treeNewsBoxes[u].onmouseover = focusNews;
					treeNewsBoxes[u].onmouseout = function(){newsFocus = false;};
				}
				fadebox[u].setAttribute("xOpacity", 0);
				fadebox[u].id = "tri_" + u;
			}
			fadebox[0].style.display = "block";
			fadebox[0].setAttribute("xOpacity", "0.99");
			fadebox[0].parentNode.setAttribute("current_tif","0");
			fadebox[0].id = "tri_" + 0;
			d.getElementById(bid + "_slidetitle_0").style.display = "block";
			so_xfade(fadebox, true);
			
			setTreeLink(fadebox[0]);
			//when a link is added clicking on it will help :)
			d.getElementById(bid + "_skin").onclick = function(){ 
				var newLink = this.getAttribute('rel');
				if(newLink) location.href = newLink;
				}
			if(useNews){
				treeNewsBoxes[0].id= "nws_0";	
				treeNewsBoxes[0].className = "item focus";		
				treeNewsBoxes[0].onclick = focusNews;
                treeNewsBoxes[0].onmouseover = focusNews; 
				treeNewsBoxes[0].onmouseout = function(){newsFocus = false;};
			}
			
		}
		fadebox = new Array();
		}
}

function so_xfade(imgs,timeout) {
	
	if(timeout || newsFocus){
		 setTimeout(function(){so_xfade(imgs);},5000);
		 return true;
	}
	if(newsFocus) return true;
	var prntObj = imgs[0].parentNode;
	var current_tif = prntObj.getAttribute("current_tif");
	
	cOpacity = imgs[current_tif].getAttribute("xOpacity");
	var newNumber = parseFloat(parseInt(current_tif)+1);
	nIndex = imgs[newNumber]?newNumber:0;

	nOpacity = parseFloat(imgs[nIndex].getAttribute("xOpacity"));
	cOpacity = (parseFloat(cOpacity - 0.05)).toFixed(2); 
	nOpacity = (parseFloat(nOpacity + 0.05)).toFixed(2);

	imgs[nIndex].style.display = "block";
	imgs[current_tif].setAttribute("xOpacity", cOpacity);
	imgs[nIndex].setAttribute("xOpacity", nOpacity);
	
	
	setOpacity(imgs[current_tif]); 
	setOpacity(imgs[nIndex]);
	
	if(useNews && cOpacity <= 0.50 && cOpacity > 0.40 && imgs[current_tif].style.display != "none") {
		
		treeNewsBoxes[current_tif].className = "item";
		treeNewsBoxes[nIndex].className = "item focus";
		
	}
	

	if(cOpacity<=0) {
		imgs[current_tif].style.display = "none";
		prntObj.setAttribute("current_tif", nIndex);
		imgs[current_tif].setAttribute("xOpacity", 0);
		setTreeLink(imgs[nIndex]);

		
		var imgTitle = d.getElementById(prntObj.id + "_slidetitle_" + nIndex);
		if(imgTitle != null){
			imgTitle.style.display = "block";
		}
		var imgNxtTitle = d.getElementById(prntObj.id + "_slidetitle_" + current_tif);
		if(imgNxtTitle != null){
			imgNxtTitle.style.display = "none";
		}
		
		
		
		/*
		var setLink = imgs[nIndex].getAttribute("rel");
		clObj = d.getElementById(prntObj.id + "_skin");
		clObj.style.cursor = "default";
		clObj.setAttribute("rel", null);
		if(setLink != null && setLink != 0){
			clObj.style.cursor = "pointer";
			clObj.setAttribute("rel", setLink);
		}
		*/
	
		setTimeout(function() {so_xfade(imgs)}, secFocus);
	} else 
		setTimeout(function() {so_xfade(imgs)},50);
	
}


function setOpacity(obj) {
	var xOpacity = parseFloat(obj.getAttribute("xOpacity"));

	if(xOpacity > 0.99){
		obj.setAttribute("xOpacity", 0.99 );
		return;
	}
	obj.style.opacity = xOpacity;
	obj.style.MozOpacity = xOpacity;

	var objChild = obj.firstChild;
	if(objChild.nodeName == "A"){
		objChild = objChild.firstChild;
	}
	

	objChild.style.filter = 'alpha(opacity=' + Math.round(obj.xOpacity*100) + ')';
	
}
/**********************************************

	
	
	CSS styles die toegevoegd moeten worden
	


#imageContainer {
	height: 158px;
	width: 774px;
	position:relative;
	margin-left:195px;
	_margin-left:0px;
	_position:absolute;
	
}
#imageContainer div{
	display:none;
	position:absolute;
	top:0;
	left:0px;
	:left:0px;
}
**********************************************/
