window.onload = rotateAd;
var current_ad_li = 0;
var li_arr;

if(document.all){
	document.execCommand('BackgroundImageCache', false, true);
	
}

function rotateAd(){
	li_arr = document.getElementById('sidebar-ads').getElementsByTagName('li');
	setTimeout("opacity(1, 0, 1500)", 5000);
}

function opacity(opacStart, opacEnd, millisec) {
	//speed for each frame
	var speed = Math.round(millisec / 100);
	var timer = 0;
	//determine the direction for the blending, if start and end are the same nothing happens
	if ( opacStart > opacEnd ) {
		for(i = opacStart*100; i >= opacEnd; i--) {
			setTimeout("setElementOpacity("+ i +", "+ opacStart +", "+ opacEnd +")", (timer * speed));
			timer++;
		}
	} else if(opacStart < opacEnd) {
		for(i = opacStart*100; i <= opacEnd*100; i++) {
			setTimeout("setElementOpacity("+ i +", "+ opacStart +", "+ opacEnd +")", (timer * speed));
			timer++;
		}
	}
}

function setElementOpacity( nOpacity, opacStart, opacEnd ){
	var obj = li_arr[current_ad_li].style;
	nOpacity = nOpacity/100;
	if (document.all){
		obj.filter = "alpha(opacity="+ nOpacity*100 +")";
	} else {
		obj.opacity = nOpacity;
	}
	if ( nOpacity == opacEnd ){
		if ( opacEnd == 0 ){
			obj.display = 'none';
			if (document.all){
				obj.filter += "alpha(opacity=100)";
			} else {
				obj.opacity = 1;
			}
			current_ad_li = ( current_ad_li < li_arr.length-1 ) ? current_ad_li+1 : 0;
			li_arr[current_ad_li].style.display = 'block';
			if (document.all){
				li_arr[current_ad_li].style.filter = "alpha(opacity="+ nOpacity*100 +")";
			} else {
				li_arr[current_ad_li].style.opacity = 0;
			}
			
			opacity(0, 1, 1500);
		} else {
			setTimeout("opacity(1, 0, 1500)", 5000);
		}
	}
}