// JavaScript Document

var JFDSlide = new Class({
	//implements
	Implements: [Options, Events],
	
	//options
	options: {
		 container:"",
		 items:null,
		 size:0,
		 next:null,
		 prev:null,
		 info:null
	},
	
	index: 0,
	bindedFunctions: {},
	effects: {},
	elements: {},
	
	//initializator
	initialize: function(options) {
		this.setOptions(options);
		this.initBindedFunctions();
		this.initElements();
		this.initEffects();
		this.initEvents();
		this.changeText();
	},
	
	initBindedFunctions: function() {
		this.bindedFunctions = {
			next	: this.next.bind(this),
			prev	: this.prev.bind(this)
		};
	},
	
	initElements: function() {
		this.elements = {
			next		: $(this.options.next),
			prev		: $(this.options.prev),
			container	: $(this.options.container),
			info		: $(this.options.info)
		};
		this.elements.container.setStyle('width', this.options.items.length*this.options.size);
	},
	
	initEvents: function() {
		this.elements.next.addEvent('click', this.bindedFunctions.next);
		this.elements.prev.addEvent('click', this.bindedFunctions.prev);
	},
	
	initEffects: function() {
		this.effects = {
			slide	: new Fx.Tween(this.elements.container, {duration:1000, transition:Fx.Transitions.Expo.easeOut}).set('left', 0)
		};
	},
	
	next: function() {
		this.run(1);
	},
	
	prev: function() {
		this.run(-1);
	},
	
	changeText: function() {
		var address = this.options.items[this.index].getFirst().getNext().title.split('|')[1];
		var title = this.options.items[this.index].getFirst().getNext().getFirst().alt;
		this.elements.info.set('html', title+'<br /><a href="'+address+'" title="'+title+'" class="hyperlink">'+address+'</a>');
		return title;
	},
	
	run: function(int) {
		this.index += int;
		if(this.index>this.options.items.length-1) this.index = 0;
		else if(this.index<0) this.index = this.options.items.length-1;
			
		this.effects.slide.cancel().start('left', -this.index*this.options.size);
		
		var action = "SlidePrev"
		if(int>=0) action = "SlideNext";
		if(pageTracker) pageTracker._trackEvent("Showcase", action, this.changeText());
	}
});
