/*
	based on mootools 1.2.1
	built by shanghaikid
*/

Selectors.Pseudo.random = function(probability, local){
	return Math.random() < (probability || .8).toFloat();
};

var myFx = new Fx.Scroll(window);

/*链接交换*/
var linkBd = new Class({

	Implements: [Options, Events],

	options:{
		oBdLinks : $$('#bdLinksPool a'), /*所有bd链接*/
		probability : 0.9,
		targetId:'p1',
		speed: 0.5
		
	},

	initialize:function(options){
		this.setOptions(options);	

		this.target = $(this.options.targetId).getElements('a');		
		this.oLinks = $(this.options.targetId).getElements('a').clone(); //目标原始链接
		this.bdLinks = []; // 数量匹配后的bd链接
		this.probability = this.options.probability; //概率
		this.query ="#" + this.options.targetId + " a:random("+this.probability+")";	
		this.first = 0;

		this.makeBdLinks(); //bd链接数量匹配
		this.attachLink(this.target); //绑定链接交互程序
		this.changeLink();
		
	},

	makeBdLinks:function(){
		this.target.each(function(item,index){
			this.bdLinks[index]= this.options.oBdLinks.getRandom();
		},this);
	},

	attachLink:function(target){
		target.each(function(link){
			link.addEvent('click',this.exchangeLinks.bind(this));
		},this)
	},

	exchangeLinks:function(){

	
		if(this.first == 0){
			this.retoreLinks();
			this.first = 1;
		}else if(this.first !== 0 && this.probability>1){
			this.retoreLinks();
			this.probability = this.options.probability;
		}else{
			this.changeLink();
			this.probability = this.probability + this.options.speed;
		}		
		
	},

	changeLink:function(){		
		
		(function(){ 

			$$(this.query).each(function(link,index){ //bd链接换正常链接
				link.setProperty('href', this.bdLinks[index]);
				link.setProperty('target','_blank');
			},this);	
						

		}).delay(10,this);			
						
	},

	retoreLinks:function(){
			

		(function(){ 

			this.target.each(function(link,index){ //正常连接换bd链接
				link.setProperty('href', this.oLinks[index]);
				link.setProperty('target','_blank');
			},this);
			

		}).delay(10,this);
						
	}	

});

