var iRotationInterval=5000;
var iRotationSpeed=50;
var iRotationAnimationInterval=0;
var iFaderAnimationInterval;
var bSwitch=true;
var currentAD1=0;
var currentAD2=0;
preloadImages();
function preloadImages(){
	if(document.images){
		for(i=0;i<imagearray.length;i++){
			var loader=new Image();
			loader.src=imagearray[i];
		}
	}
	if(adCount>1){
		currentAD2=1;
		iRotationAnimationInterval=setInterval("rotateAd()",iRotationInterval);
	}
	if(adCount>0){
		document.getElementById("ad1").style.backgroundImage="url('" + imagearray[currentAD1] + "')";
		document.getElementById("ad1").onclick=function(){
			window.open(urlarray[currentAD1]);
		}
	}
}
function rotateAd(){
	try{
		var AD1=document.getElementById("ad1");
		var AD2=document.getElementById("ad2");
		if(bSwitch){
			AD2.style.backgroundImage='url("' +imagearray[currentAD2] + '")';
			//alert('url("' +imagearray[currentAD2] + '")');
			iFaderAnimationInterval=setInterval(function(){fader(1/iRotationSpeed);},1);
		}else{
			AD1.style.backgroundImage='url("' +imagearray[currentAD1] + '")';
			iFaderAnimationInterval=setInterval(function(){fader(-1/iRotationSpeed);},1);
		}
	}catch(err){
	}
}
function toggleLinks(elm,lnk,switcher){
	if(switcher){
		elm.onclick=function(){
			window.open(lnk);
		}
	}else{
		elm.onclick=function(){
		}
	}
}
function fader(val){
	try{
	var AD1=document.getElementById("ad1");
		var AD2=document.getElementById("ad2");
	if(bSwitch){
		//Enable AD2
		if(parseFloat(AD2.style.opacity)>=1){
			AD2.style.opacity='1';
			//Enable Link for AD2, Disable it for AD1
			toggleLinks(AD2,urlarray[currentAD2],true);
			toggleLinks(AD1,urlarray[currentAD1],false);
			AD1.style.width='0px';
			bSwitch=false;
			clearInterval(iFaderAnimationInterval);
			currentAD1=(currentAD2+1) % adCount;
			currentAD2=(currentAD1+1) % adCount;
		}else{
			//Disable both links
			AD1.style.width='960px';
			AD2.style.width='960px';
			toggleLinks(AD2,urlarray[currentAD2],false);
			toggleLinks(AD1,urlarray[currentAD1],false);
			//Increase AD2 opacity
			AD2.style.opacity=roundNumber(parseFloat(AD2.style.opacity)+val,2);
		}
	}else{
		//Disable AD2
		if(parseFloat(AD2.style.opacity)<=0){
			AD2.style.opacity='0';
			//Enable link for AD1, Disable it for AD2
			toggleLinks(AD2,urlarray[currentAD2],false);
			toggleLinks(AD1,urlarray[currentAD1],true);
			AD2.style.width='0px';
			bSwitch=true;
			clearInterval(iFaderAnimationInterval);
			currentAD1=(currentAD2+1) % adCount;
			currentAD2=(currentAD1+1) % adCount;
		}else{
			//Disable both links
			AD2.style.width='960px';
			AD1.style.width='960px';
			toggleLinks(AD2,urlarray[currentAD2],false);
			toggleLinks(AD1,urlarray[currentAD1],false);
			//Decrease AD2 Opacity
			AD2.style.opacity=roundNumber(parseFloat(AD2.style.opacity)+val,2);
			//alert(AD2.style.opacity);
		}
	}
	}catch(err){
		clearInterval(iFaderAnimationInterval);
	}
}
function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}
