var Highlights = new Class({
	
	Implements: Options,
		
	options: {
		'showDescription': false,
		'showPlayPause': false,
		'showPreviousNext': false,
		'showNumbers': false,
		'pauseOnNumberJump': true,
		'autoPlay': true,
		'delay': 10000,
		'transitionTime': 150,
		'loadingText': 'Carregando...'
	},
	
	initialize: function(container, options) {
		// Initialization data
		this.container = $(container);
		this.setOptions(options);
		
		// Variables
		this.images = [];
		this.current = 0;
		this.playing = this.options.autoPlay;
		this.periodical = null;
		this.animating = false;
		this.numberLinks = [];
		// Generate the anchor inside the container
		this.anchor = new Element('a', {'class':'mox_hl_anchor', 'href':'#'});
		this.container.adopt(this.anchor);
		
		// Generate the image div inside the anchor
		this.imgdiv = new Element('div', {'class': 'mox_hl_imgdiv'});
		this.anchor.adopt(this.imgdiv);
		
		// Generate the description paragraph inside the anchor
		if (this.options.showDescription) {
			this.desc = new Element('p', {'class': 'mox_hl_desc'});
			this.anchor.adopt(this.desc);
		}
	},
	
	add: function(image, link, desc) {
		n = this.images.length;
		this.images[n] = {img: image, link: link, desc: desc, loaded: false};
		var img = new Asset.image(image, {alt: desc, onload: this.imgAdded.bind({self: this, n:n})});
	},
	
	imgAdded: function() {
		this.self.images[this.n].loaded = true;
	},
	
	start: function() {
		// Generate the optional interface elements
		if(this.options.showNumbers) this.spawnNumbers();
		
		// Show first image, if present
		if(this.images.length > 0) this.jump(0);

		// Autolplay opt-in
		if(this.options.autoPlay) this.play();
	},
	
	jump: function(n) {
		// Check if `n` is in the images range or if it's not the same as the current image
		if(n < 0 || n == this.images.length) return false;
		
		// Check if the image has not yet been loaded
		if(!this.images[n].loaded) {
			this.jump.delay(500, this, n);
			this.showLoading();
			return false;
		}
		
		// Check if there is an animation going on
		if(this.animating) {
			this.jump.delay(500, this, n);
			return false;
		}
		
		// Set the animating flag to on
		this.animating = true;
		
		// Fade out the image
		this.imgdiv.set('tween', {duration: this.options.transitionTime});
		this.imgdiv.tween('opacity', 1, 0);
		if (this.options.showDescription) {
			this.desc.set('tween', {duration: this.options.transitionTime});
			this.desc.tween('opacity', 1, 0);
		}
		
		// Set the new image
		var i = this.images[n];
		(function() {
			this.imgdiv.innerHTML = "";
			this.imgdiv.setStyle('background-image', 'url("'+i.img+'")');
			if (this.options.showDescription) this.desc.innerHTML = i.desc;
			this.anchor.href = i.link;
			
			// Toggle highlight class for the previous and current numberLink
			if(this.options.showNumbers) {
				if(this.previous != this.current) this.numberLinks[this.previous].toggleClass('current');
				this.numberLinks[n].toggleClass('current');
			}
			
			// Fade in the image
			this.imgdiv.set('tween', {duration: this.options.transitionTime});
			this.imgdiv.tween('opacity', 0, 1);
			if (this.options.showDescription) {
				this.desc.set('tween', {duration: this.options.transitionTime});
				this.desc.tween('opacity', 0, 1);
			}
			
			// Set the animating flag to off
			this.animating = false;
		}).delay(this.options.transitionTime, this);
		
		// Set the new current image and record the last displayed
		this.previous = this.current;
		this.current = n;
		
	},
	
	showLoading: function() {
		this.imgdiv.setStyles({
			'background-position': 'center center'
			// Linha abaixo retirada por Guilherme <guilherme@ocampos.com.br> - problemas de compatibilidade com o IE
			//,'background-image': 'modules/mod_arcohighlights/loading.gif'
		});
	},
	
	play: function() {
		this.playing = true;
		this.periodical = this.next.periodical(this.options.delay, this);
	},
	
	pause: function() {
		this.playing = false;
		$clear(this.periodical);
	},
	
	next: function() {
		if ((this.current+1) == this.images.length) n = 0;
		else n = this.current + 1;
		
		this.jump(n);
	},
	
	previous: function() {
		if (this.current == 0) n = this.images.length - 1;
		else n = this.current - 1;
		
		this.jump(n);
	},
	
	// Show the numbers interface
	spawnNumbers: function() {
		var div = new Element('div', {'class': 'mox_hl_numberlinks'});
		for(i=0; i < this.images.length; i++) {
			var a = new Element('a', {'href': 'javascript:void(0)', 'class': 'mox_hl_numberlink', 'html': i+1});
			this.numberLinks[i] = a;
			a.store('self', this);
			a.store('n', i);
			a.addEvent('click', function() {
				if(this.hasClass('current')) return false;
				this.retrieve('self').pause();
				this.retrieve('self').jump(this.retrieve('n'));
			});
			if(i > 0) div.appendText('|');
			div.adopt(a);
		}
		this.container.adopt(div);
	}
	
});

