var Promotions = Class.create();

/*
A bit specific for this page only :/
*/

Promotions.prototype = {
	
	elements: [],
	currentKey: 0,
	currentElm: null,
	target: null,
	
	initialize: function (elements) {
		this.elements = elements;
		this.target = document.getElementById("recommend");
		this.build();
		if(this.elements.length > 1) {
			new PeriodicalExecuter(function(pe) {
				this.build();
			}.bind(this), 15);
		}
	},
	
	build: function() {
		var container = document.createElement("div");
		Element.hide(container);
				
		var imageHolder = document.createElement("a");
				
		imageHolder.target =  this.elements[this.currentKey].target;
		imageHolder.className = "imageHolder";
		imageHolder.href = this.elements[this.currentKey].url
		if(this.elements[this.currentKey].target)
			imageHolder.target = this.elements[this.currentKey].target
		container.appendChild(imageHolder);
		
		var title = document.createElement("a");
		title.className = "title";
		title.appendChild(document.createTextNode(this.elements[this.currentKey].title))
		if(this.elements[this.currentKey].target)
			title.target = this.elements[this.currentKey].target
		container.appendChild(title);
		
		var description = document.createElement("a");
		description.innerHTML = this.elements[this.currentKey].description;
		description.className = "description";
		description.href = this.elements[this.currentKey].url;
		if(this.elements[this.currentKey].target)
			description.target = this.elements[this.currentKey].target
		container.appendChild(description);

		var image = new Image();
		image.src = this.elements[this.currentKey].image;
		image.alt = this.elements[this.currentKey].title;
		imageHolder.appendChild(image);
		
		this.target.appendChild(container);
		if(this.currentElm) {
			//todo
			new Effect.Fade(this.currentElm, {
				duration:2,
				afterFinish: function () {
					this.currentElm.parentNode.removeChild(this.currentElm)
					this.showNextElement(container) 
				}.bind(this)
			});
			
		} else {
			new Effect.Appear(container);
			this.currentElm = container;
		}
		
		this.currentKey++;
		if(this.currentKey >= this.elements.length)
			this.currentKey = 0;
	},
	
	showNextElement: function(container) {
		new Effect.Appear(container, { oncomplete: this.currentElm = container }) 
	}
	
}