/**
 * @author James Scott
 */
var SponsorRotate = new Class({
	initialize: function(s, l, options) {
		this.setOptions(this.getOptions(), options);

		this.index = 0;
		this.anchor  = $(s);
		this.sponsors = l;
    this.fx      =  new Fx.Morph(this.anchor, {duration: 1500,transition: Fx.Transitions.Bounce.easeOut});
		// the periodical starts here, the * 1000 is because milliseconds required
		this.periodical = this.refresh.periodical(this.options.period * 1000, this);

	},

	getOptions: function(){
		return {
		period: 7.5
		};
	},
	refresh: function() {
		if (this.sponsors.length < 1) {
			return;
		}
		if (this.index == this.sponsors.length) {
			this.index = 0;
		}
		var anchor = this.anchor;
		var sp     = this.sponsors[this.index];

		this.fx.cancel();
		this.fx.start({'background-image': sp.logo, width:['0','210']}).chain(function(){
			anchor.set('href', sp.link);
			anchor.set('title', sp.description);
	});



		this.index++;
	}

});

/**
 * Start the sponsor rotate
 * @param {Object} s
 */
function StartSponsorRotate(s, ctxt) {
 var obj = {
 	link:null,
	description:null,
	log:null
 };
	var	srotate = new SponsorRotate($(s),[],{});

	new Request.JSON({url:"/wp/dte-services/sponsorlist.php?Context="+ctxt,
		                  onComplete: function(list) {
											  srotate.sponsors = list;
                        srotate.refresh();
											}}).get();

  return srotate;
}

/**
 * Start the sponsor rotate
 * @param {Object} s
 */
function StartDirectSponsorRotate(s, list) {

	var	srotate = new SponsorRotate($(s),list, {});
  //srotate.sponsors = list;
	//srotate.refresh();
  return srotate;
}


SponsorRotate.implement(new Options);
