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,
		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');

		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
		this.currentPromoIdx = 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() {
		$$('#promoNav a').removeClass('on');
		$('promo'+(this.currentPromoIdx+1)).addClass('on');
		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;
		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)
		});

		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

