/**********************************************************************************************************************************
 *
 * Ad Rotator with Fade
 * Version 1.1
 * (C) Kerri Shotts, 2008
 *
 **********************************************************************************************************************************/

//
// This portion by "bitshaker" 
//
function getElementsByClassName(oElm, strTagName, strClassName)
	{
		var arrElements = (strTagName == "*" && document.all)? document.all : oElm.getElementsByTagName(strTagName);
	    var arrReturnElements = new Array();
	    strClassName = strClassName.replace(/\-/g, "\\-");
	    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	    var oElement;
	    for(var i=0; i<arrElements.length; i++){
	        oElement = arrElements[i];
	        if(oRegExp.test(oElement.className)){
	            arrReturnElements.push(oElement);
	        }
	    }
	    return (arrReturnElements)
	}


	//
	// fadeAd takes two elements, n, and p, and a transparency. It then proceeds to fade between the two, until 
	// element p is gone, and n is fully visible.
	//
	fadeAd = function ( n, p, t )
	{
		n.className = "ad trans" + t;
		p.style.zOrder = 50;
		p.style.zIndex = 50;
		n.style.zOrder = 100;
		n.style.zIndex = 100;
		if (t<=0)
		{
			p.style.display="none";
			n.className = "ad";
			window.fading = 0;
		}
		else
		{	window.fading = 1;
			window.setTimeout ( "fadeAd ( document.getElementById ('"+ n.id +"'), document.getElementById ('"+ p.id +"'), " + (t-2) + ")", 50 );
		}
	}
	
	//
	// switchAd changes to the specified ad (selAd). If it is < 1, then the next ad following the current ad
	// is selected. If the ad rotator was set up with a delay, the delay is persisted.
	//			
	switchAd = function ( adObject, selAd ) {
	
		if (window.fading == 1)
		{
			if (window.fastTimer)
			{
				window.clearTimeout( window.fastTimer );
			}
			window.fastTimer = window.setTimeout ( "switchAd(document.getElementById('" + adObject.id + "'), " + selAd + ");" , 1000 );
			return;
		}
	
		var nextAd = selAd;
		var t = adObject;
	    var o = t.firstAd;
	    var p
	    var n
	    var prevAd = t.currentAd;
	    
		if (selAd < 1) 
		{
			nextAd = t.currentAd + 1;
			if (nextAd > t.numberAds)
			{
				nextAd = 1;
			}
		}
		t.currentAd = nextAd;
		for (i=1; i<= t.numberAds; i++)
		{
			if (i==t.currentAd)
			{
			  if (o)
			  {
				o.className = "ad trans9";
				o.style.display="block";
				n = o;
			  }
			}
			else if (i==prevAd)
			{
				p = o;
				p.style.display="block";
			}
			else
			{
				if (o)
				{
					o.style.display="none";
				}
			}
			
			if (o)
			{		
				o = o.nextSibling;
			}
			if (o)
			{
				if (o.className!="ad")
				{
					o = o.nextSibling;
				}
			}
		}
		// fade from previous to new
		if (n && p)
		{
			fadeAd ( n, p, 9 );
		}
		else
		{
			n.className = "ad";
		}
	    
	    
	    o = t.adSelector;
		for (i=1; i<= t.numberAds; i++)
		{
			if (i==t.currentAd)
			{
				if (o)
				{
					o.className = "select";
				}
			}
			else
			{
				if (o)
				{
					o.className = "";
				}
			}
			
			if (o)
			{
				o = o.nextSibling;
			}
			if (o)
			{
				while (o.nodeType!=1 && o.nextSibling)
				{
					o = o.nextSibling;
				}
			}
		}
		
		if (t.previousTimer)
		{
			window.clearTimeout( t.previousTimer );
			if (t.delay>0)
			{
				t.previousTimer = window.setTimeout ( "switchAd(document.getElementById('" + t.id + "'), -1);" , t.delay );
			}
		}
	}

	//
	// initAds
	//
	// delay = ms between ads; 0 for manual advance
	// r = 1 for random, 0 for first (initial ad)
	//
	// assumes the following: outer container has id of "ads", each ad is a DIV with class "ad",
	// the outer container contains a div id "adSelect", and that your first ad is id "ad1".
	//
	function initAds ( delay, r )
	{
		var ads = document.getElementById ( "ads" );
		
		var all_ads = getElementsByClassName( document, "DIV", "ad" );
		
		
		var iAd = 1;
		
		// determine the number of ads
		ads.numberAds = all_ads.length;

		// build our selector area
		var adSel = document.getElementById ("adSelect");
		adSel.innerHTML = "";
		
		for (i=0; i<ads.numberAds; i++)
		{
			adSel.innerHTML = adSel.innerHTML + '<a href="#" ad=' + (i+1) + ' id="adChooser' + (i+1) + '" onclick="switchAd(this.parentNode.parentNode, ' + (i+1) +'); return false;">' + all_ads[i].getAttribute('caption') + '</a>'
		}
		
		
		// we default to the first ad
		ads.currentAd = 1;
		
		// and what is the first ad?
		ads.firstAd = document.getElementById ("ad1");
		
		ads.adObject = document.getElementById ( "ads" );
		
		ads.delay = delay;
		
		ads.adSelector = document.getElementById ( "adChooser1" );
		
		// if we have a delay, we'll start the timer, otherwise, ther user must advance on their own.
		if (delay > 0)
		{
			ads.previousTimer = window.setTimeout ( "switchAd(document.getElementById('" + ads.id + "'), -1);" , delay );
		}
		else
		{
			ads.delay = 0;
		}
		
		// if r = 1 then we go to a RANDOM image :-)

		if (r==1)
		{		
			iAd=Math.floor(Math.random()*(ads.numberAds)) + 1;
		}

		// go to the first ad
		switchAd(document.getElementById( ads.id ), iAd);
		
	}
