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 promo image 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.present();
				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;
		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
	} // method..present
	,
	step: function() {
		this.currentPromoIdx = (this.currentPromoIdx+1) % this.jsonData.promoList.length;
		this.backImg.src = this.foreImg.src;
		var tmp = function() {
			this.foreImg.setStyle('opacity',0);
			new Asset.image(this.jsonData.promoList[this.currentPromoIdx].image, {
				onload: function() {
					this.present();
					var tmp = function() {
						this.foreImg.set('tween', { 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

		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;
		this.timer = this.step.periodical(this.options.linger+this.options.duration,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

