// Author: Marc Anthony M. Danting
// Email: surfingtoniO@hotmail.com
// Website: http://surfingtonio.com

Array.extend({
	getDistinctRandom:function(c){
		if($type(c)!='array')c=[c];
		c=this.filter(function(a,b){return!c.contains(a)});
		return(c.length==0)?false:c[$random(0,c.length-1)]
	}
});

var formats=['leaderboard','banner','half banner','button','skyscrapper','wide skyscraper','small rectangle','vertical banner','small square','square','medium rectangle','large rectangle'];
var sizes=[{width:728,height:90},{width:468,height:60},{width:234,height:60},{width:125,height:125},{width:120,height:600},{width:160,height:600},{width:180,height:150},{width:120,height:240},{width:200,height:200},{width:250,height:250},{width:300,height:250},{width:336,height:280}];
var ads=sizes.associate(formats);

var AdsenseRotator=new Class({
	options:{
		speed:7000,
		autostart:true,
		clip:false,
		randomize:false,
		direction:'vertical',
		format:'leaderboard',
		fadePreviousAd:false,
		transition:Fx.Transitions.Pow.easeOut,
		transitionSpeed:750
	},
	initialize:function(c,d){
		this.setOptions(d);
		this.speed=this.options.speed;
		this.randomize=this.options.randomize;
		this.direction=this.options.direction;
		this.autostart=this.options.autostart;
		this.clip=this.options.clip;
		this.format=this.options.format;
		this.fadePreviousAd=this.options.fadePreviousAd;
		this.transition=this.options.transition;
		this.transitionSpeed=this.options.transitionSpeed;
		this.timer=null;
		this.effects=[];
		this.fade=[];
		if(this.el=$(c)){
			this.el.setStyles({
				'position':'relative',
				'width':ads[this.format].width,
				'height':ads[this.format].height,
				'overflow':this.clip?'hidden':'visible',
				'opacity':0
			});
			this.ads=$$('#'+c+' div');
			this.ads.each(function(b,i){
				this.fade[i]=new Fx.Styles(b,{
					duration:this.transitionSpeed/2,
					transition:Fx.Transitions.linear
				});
				this.effects[i]=new Fx.Styles(b,{
					duration:this.transitionSpeed,
					transition:this.transition,
					onComplete:(function(a){
						this.ads[this.previousAd].setStyles({'opacity':0})
					}).bind(this)
				});
				b.setStyles({
					'position':'absolute',
					'left':'0px',
					'margin-left':this.direction=='horizontal'?this.el.offsetWidth+'px':'0px','margin-top':this.direction=='vertical'?this.el.offsetHeight+'px':'0px',
					'width':ads[this.format].width,
					'height':ads[this.format].height,
					'display':'none',
					'opacity':0
				})
			},this);
			this.currentAd=this.randomize?this.ads.indexOf(this.ads.getDistinctRandom()):0;
			this.ads[this.currentAd].setStyles({
				'margin-left':'0px',
				'margin-top':'0px',
				'display':'block',
				'opacity':1
			});
			this.el.setStyle('opacity',1);
			this.el.addEvent('click',(function(a){
				this.fireEvent('onAdClick',[a,this.currentAd])
			}).bind(this));
			if(this.ads.length>1&&this.autostart)this.start()
		}
	},
	_getRandomIndex:function(){
		index=$random(0,this.ads.length-1);
		while(this.currentAd==index){
			index=$random(0,this.ads.length-1)
		}
		return index
	},
	_rotateAds:function(){
		this.el.setStyles({'overflow':this.clip?'hidden':'visible'});
		this.ads[this.currentAd].setStyle('zIndex',999);
		if(this.fadePreviousAd)this.fade[this.currentAd].start({'opacity':0});
		this.previousAd=this.currentAd;
		this.currentAd=this.randomize?this.ads.indexOf(this.ads.getDistinctRandom(this.ads[this.currentAd])):this.currentAd==this.ads.length-1?0:this.currentAd+1;
		this.ads[this.currentAd].setStyles({
			'margin-left':(this.direction=='horizontal'?this.el.offsetWidth+'px':'0px'),
			'margin-top':(this.direction=='vertical'?this.el.offsetHeight+'px':'0px'),
			'display':'block',
			'zIndex':1000,
			'opacity':0
		});
		this.effects[this.currentAd].start({'margin-left':'0px','margin-top':'0px','opacity':1})
	},
	start:function(a){
		if(this.ads.length>1&&!this.timer){
			this.currentAd=$defined(a)?a:(this.randomize?$random(0,this.ads.length-1):this.currentAd);
			this.currentAd=this.currentAd.limit(0,this.ads.length-1);
			if(this.ads.length>1){this.timer=this._rotateAds.periodical(this.speed,this)}
		}
	},
	pause:function(){if(this.timer)this.timer=$clear(this.timer)},
	resume:function(){if(!this.timer){this._rotateAds();this.start(this.currentAd)}},
	toggle:function(){if(this.timer){this.pause()}else{this.resume()}}
});

AdsenseRotator.implement(new Options,new Events);