var ff_slides = {

	show_index : -1,
	slide_index : -1,
	interval : null,

	speed : 1000,
	_playing : false,

	target : null,

	shows : [],
	currentShow : null,

	init : function(target, url) {
		this.target = $(target);
		this.get(url);
	},

	initGui : function() {
		var me = this;
		this.target.find('a').click(function() {
			me.handleGuiAction($(this).attr('class'));
			return false;
		});
		this.target.find('img').load(function() {
			me.imageLoaded();
		});
		this.target.hover(function() {
			$(this).stop();
			$(this).find('ul').fadeIn(500);
		}, function() {
			$(this).find('ul').fadeOut(2000);
		});
	},

	handleGuiAction : function(action) {
		switch (action) {
		case 'ff_prevShow':
			this.prevShow();
			break;
		case 'ff_nextShow':
			this.nextShow();
			break;
		case 'ff_prevSlide':
			this.prevSlide();
			break;
		case 'ff_nextSlide':
			this.nextSlide();
			break;
		case 'ff_play':
			if (this.playing()) {
				this.stop();
			} else {
				this.start();
			}
			break;
		case 'ff_nextSlide':
		default:
			this.followLink();
			break;
		}
	},

	get : function(url) {
		var me = this;
		$.get(url, null, function(data, textStatus) {
			me.setData(data);
			me.showSlide(0, 0);
			me.start();
			me.initGui();
		});
	},

	setData : function(xml) {
		xml = $(xml);
		this.speed = parseInt(xml.attr('speed'), 10) || 2000;
		var result = [];
		$(xml).find('slideshow').each(function() {
			var show = {};
			show['directory'] = $(this).attr('directory');
			show['title'] = $(this).attr('title');
			show['href'] = $(this).attr('href');
			var slides = [];
			$(this).find('slide').each(function() {
				slides.push($(this).attr('src'));
			});
			show['slides'] = slides;
			result.push(show);
		});
		this.shows = result;
	},

	imageLoaded : function() {
		if (this.playing()) {
			this.loadNext();
		}
	},

	start : function() {
		this._playing = true;
		this.target.addClass('playing');
		// this.interval = setInterval( "ff_slides.nextSlide()" , this.speed );
		this.showSlide(0, 0);
	},

	stop : function() {
		this._playing = false;
		this.target.removeClass('playing');
		// clearInterval( this.interval );
		clearTimeout(this.interval);
		this.interval = null;
	},
	
	playing : function() {
		return this._playing; // this.interval !== null;
	},
	
	goSlide : function(diff_show, diff_slide) {
		this.showSlide(this.show_index + diff_show, this.slide_index)
	},
	
	showSlide : function(i_show, i_slide) {
		if (i_show === null || i_show === undefined)
			i_show = this.show_index;
		if (i_slide === null || i_slide === undefined)
			i_slide = 0;
	
		if (i_show < 0) {
			i_show = this.shows.length - 1;
		} else if (i_show >= this.shows.length) {
			i_show = 0;
		}
	
		this.currentShow = this.shows[i_show];
		var slides = this.currentShow.slides;
	
		if (i_slide < 0) {
			i_slide = slides.length - 1;
		} else if (i_slide >= slides.length) {
			i_slide = 0;
		}
	
		if (i_show != this.show_index || i_slide != this.slide_index) {
			this.show_index = i_show;
			this.slide_index = i_slide;
	
			var imgPath = this.currentShow.directory + '/' + slides[i_slide];
			var img = $(this.target).find('img');
			img.attr('src', imgPath);
	
			this.target.find('.ff_title').text(this.currentShow.title);
		}
	
	},
	
	loadNext : function() {
		if (this.playing()) {
			this.interval = setTimeout("ff_slides.nextSlide()", this.speed);
		}
	},
	
	nextSlide : function() {
		this.showSlide(null, this.slide_index + 1);
	},
	
	prevSlide : function() {
		this.showSlide(null, this.slide_index - 1);
	},
	
	nextShow : function() {
		this.showSlide(this.show_index + 1, 0);
	},
	
	prevShow : function() {
		this.showSlide(this.show_index - 1, 0);
	},
	
	currentShow : function() {
		return this.shows[show_index];
	},
	
	followLink : function() {
		document.location.href = this.currentShow.href;
	}
}

ff_slides.init('#ff_homeslides',
		"/medias/sys_master/8450667514383648.xml");