/*
  banners
*/

function initBanners(){
	// hide for old browsers
	if(!document.getElementById || !document.createElement) return;

	var li = document.getElementById('bannerWrapper').getElementsByTagName('li');

	if (li.length) {
	  for(var i=0; i<li.length; i++){
		  var a = li[i].getElementsByTagName('a');
		  
		  // there are always 2 anchors
		  // first anchor has the link to a document or external url
		  // second anchor has the src of the image
		 a[0].setAttribute('imgSrc', a[1].href);
		  li[i].removeChild(a[1]);
	  }
	  
	  window.anchors = new Array();
	  window.current = 0;
	  window.pause = false;
	  window.loaded = -1;
	  window.loading = true;
	  
	  // set opacity to 0 for all images, except the first image 
	  anchors = document.getElementById('bannerWrapper').getElementsByTagName('a');
	  test = anchors[0];
	   anchors = new Array();
	   anchors[0] = test;
	   

		
	  window.alternateImgs = document.getElementById('bannerWrapper').getElementsByTagName('ul')[0].getElementsByTagName('a');			
	  window.totalImages = window.alternateImgs.length + anchors.length;

	 document.getElementById('bannerWrapper').getElementsByTagName('ul')[0].style.display = 'none';

	anchors[0].style.display = 'block';
	  anchors[0].getElementsByTagName('img')[0].xOpacity = .99;
	  anchors[0].getElementsByTagName('img')[0].onload = function(){
		  window.loaded = 0;
		   // if there are more images availbale fade them in
		  if(window.alternateImgs.length) {
			  
			  addImage(window.alternateImgs[0].href, window.alternateImgs[0].getAttribute('imgSrc'));
		  } else {
			
		  }
		  
		  setTimeout(fadeBanners, 5000);
	  }
	}
}

function fadeBanners(){

	if(window.loading){
		setTimeout(fadeBanners, 3000);
		return;
	}
	
	cOpacity = anchors[current].getElementsByTagName('img')[0].xOpacity;
	nIndex = anchors[current+1]?current+1:0;

	nOpacity = anchors[nIndex].getElementsByTagName('img')[0].xOpacity;
	cOpacity -= .02;
	nOpacity += .02;
	
	anchors[nIndex].style.display = 'block';
	anchors[current].getElementsByTagName('img')[0].xOpacity = cOpacity;
	anchors[nIndex].getElementsByTagName('img')[0].xOpacity = nOpacity;
	
	setOpacity(anchors[current].getElementsByTagName('img')[0]); 
	setOpacity(anchors[nIndex].getElementsByTagName('img')[0]);
	
	if(cOpacity<=0){
		anchors[current].style.display = 'none';
		current = nIndex;

		if(window.alternateImgs.length > window.loaded){
		
			window.loading=true;

			addImage(window.alternateImgs[window.loaded].href, window.alternateImgs[window.loaded].getAttribute('imgSrc'));
		}
		
		setTimeout(fadeBanners,5000);
	}else{
		setTimeout(fadeBanners,50);
	}
	
	function setOpacity(obj){
		if(obj.xOpacity>.99){
			obj.xOpacity = .99;
			return;
		}
		
		obj.style.opacity = obj.xOpacity;
		obj.style.MozOpacity = obj.xOpacity;
		obj.style.filter = 'alpha(opacity=' + (obj.xOpacity*100) + ')';
	}
}
function getChildrenByTagName(myID, tagName) {
	tagName = tagName.toLowerCase();
	var parentElement = document.getElementById(myID);
	var result = new Array();
	for (var i=0; i < parentElement.childNodes.length; i++) {
		if (parentElement.childNodes[i].nodeName.toLowerCase() == tagName) {
			result.push(parentElement.childNodes[i])
		}
	}

	return result;
}
function addImage(href, imgSrc){
	var newAnchor = document.createElement('a');
	newAnchor.href = href;

	
	var newImage = document.createElement('img');
	newImage.src = imgSrc;
	newImage.xOpacity = 0;
	
	newAnchor.appendChild(newImage);
	document.getElementById('bannerWrapper').appendChild(newAnchor);
	newAnchor.style.display =  'none';

	newImage.onload = function(){

		window.loading = false;
		window.anchors = getChildrenByTagName('bannerWrapper', 'a');
		
	}

	window.loaded++;
}
