//- 2009-09-29 - JDS

var rotator = new Class({
	containerEl				: null,

	foreImg					: null,
	foreLink				: null,
	backImg					: null,
	backLink				: null,

	jsonData				: null,

	currentRotations 		: 0,
	currentPromoIdx			: 0,

	Implements: Options,

	options: {
		startOnInit: true,
		startRandom: false,
		hasPagination: true,
		rotationLimit : 240,
		duration: 1000,
		linger: 3000
	} // options
	,
    initialize: function(containerEl,jsonUrl,options){
		this.setOptions(options);
		this.containerEl = containerEl;
		
		this.result = new Request.JSON({
			url: jsonUrl,
			method: 'get',
			onSuccess: function(jsonObj) {
				this.jsonData = jsonObj;
				this.reset();
			}.bind(this) // onComplete
			,
			onFailure: function(xhr) {
				alert(
					"There was difficulty in retrieving the rotator data.\n"+
					"transport.readyState = "+xhr.readyState+" / transport.status "+xhr.status
				);
			}.bind(this)
		});
		this.result.headers.extend({'Accept': '*/*'}); //-- accept ANYTHING - don't have control over server side MIME-types
		this.result.send("json=true");
	} // constructor
	,
	reset: function() {
		this.foreLink = $('topLayer');
		this.foreImg = $('topLayerImg');

		this.backLink = $('bottomLayer');
		this.backImg = $('bottomLayerImg');

		if(this.options.hasPagination) {
			var pn = $('promoNav').empty();
			for(i=0;i<this.jsonData.promoList.length;i++) {
				new Element('a', {
					'class': 'promo'+(i+1),
					'id': 'promo'+(i+1),
					'events': {	'click': (function (x,obj) { return function() { obj.view(x); } })(i,this) }
				}).injectTop(pn);
			} // for
		} // if

		this.currentPromoIdx = (this.options.startRandom) ? $random(0, this.jsonData.promoList.length-1) : 0;
		new Asset.image(this.jsonData.promoList[this.currentPromoIdx].image, {
			onload: function() {
				this.step();
				if(this.options.startOnInit) { this.start(); }
			}.bind(this)
		});
	} // method..reset
	,
	present: function() {
		if(this.options.hasPagination) {
			$$('#promoNav a').removeClass('on');
			$('promo'+(this.currentPromoIdx+1)).addClass('on');
		} // if
		this.foreImg.src = this.jsonData.promoList[this.currentPromoIdx].image;
		if(this.jsonData.promoList[this.currentPromoIdx].destination==null) {
			this.foreLink.removeProperty('href');
		} else {
			switch(this.jsonData.promoList[this.currentPromoIdx].type) {
				case 'blank'		: this.foreLink.href = "javascript:var newwin = window.open('"+this.jsonData.promoList[this.currentPromoIdx].destination+"', '_blank'); newwin.focus();"; break;
				case 'link'			: this.foreLink.href = this.jsonData.promoList[this.currentPromoIdx].destination; break;
				case 'javascript'	: this.foreLink.href = this.jsonData.promoList[this.currentPromoIdx].destination; break;
			} // switch..case
		} // if..else
	} // method..present
	,
	step: function() {
		var exitEffect = this.jsonData.promoList[this.currentPromoIdx].exitEffect || "crossFade";
		switch(exitEffect) {
			case "crossFade" :
				this.currentPromoIdx = (this.currentPromoIdx+1) % this.jsonData.promoList.length;
				this.backImg.src = this.foreImg.src;
				var tmp = function() {
					this.backImg.setStyle('opacity',1);
					this.foreImg.setStyle('opacity',0);
					var fImg = new Asset.image(this.jsonData.promoList[this.currentPromoIdx].image, {
						onload: function() {
							this.present();
							var tmp = function() {
								this.foreImg.set('tween', { duration: this.jsonData.promoList[this.currentPromoIdx].duration || this.options.duration });
								this.foreImg.tween('opacity', [0,1]);
							}.delay(20,this); //-- give DOM a moment to update
						}.bind(this)
					});
				}.delay(20,this); //-- give DOM a moment to update
				break;
			case "fade" :
				this.backImg.setStyle('opacity',0);
				this.foreImg.set('tween', {
					duration: this.jsonData.promoList[this.currentPromoIdx].duration || this.options.duration,
					onComplete: function() {
						this.currentPromoIdx = (this.currentPromoIdx+1) % this.jsonData.promoList.length;
						var fImg = new Asset.image(this.jsonData.promoList[this.currentPromoIdx].image, {
							onload: function() {
								this.present();
								var tmp = function() {
									this.foreImg.set('tween', { duration: this.jsonData.promoList[this.currentPromoIdx].duration || this.options.duration });
									this.foreImg.tween('opacity', [0,1]);
								}.delay(20,this); //-- give DOM a moment to update
							}.bind(this)
						});
					}.bind(this)
				});
				this.foreImg.tween('opacity', [1,0]);
				break;
		} // switch
		this.currentRotations += (this.currentPromoIdx==0) ? 1 : 0;
		if(this.currentRotations > this.options.rotationLimit) {
			this.currentRotations = 0;
		} // if
	} // method..step
	,
	start: function() {
		this.currentPromoIdx = 0;
		this.currentRotations = 0;
		var linger = this.jsonData.promoList[this.currentPromoIdx].linger || this.options.linger;
		var duration = this.jsonData.promoList[this.currentPromoIdx].duration || this.options.duration;
		this.timer = this.step.periodical(linger+duration+100,this);
	} // method..start
	,
	view: function(idx) {
		$clear(this.timer);
		this.foreImg.get('tween').cancel();
		this.foreImg.setStyle('opacity',1);
		this.currentPromoIdx = idx;
		this.present();
	} // method..view

}); // class..rotator
