$.widget("ui.flashPlayer", {
	_preload: function () {
		var self = this;

		this.element.addClass('flashPlayer');

		var $slidesElement = $('<div class="fpSlides" />');
		var $thumbsElement = $('<div class="fpThumbs" />');
		this.element.append($slidesElement, $thumbsElement);

		$.each(this.options.thumbs, function(index, value) {
			var $newThumbCont = $('<div id="fpThumbCont' + index +'" class="fpThumbContainer"/>').hover(function() {
				$(this).addClass('hover');
			}, function() {
				$(this).removeClass('hover');
			});

			var $newThumbLoader = $('<div id="fpThumbLoader' + index +'" class="fpThumbLoader"/>');

			var $newThumb = $('<img id="fpThumb' + index +'"/>').attr('src', value).click(function() {
				self.setSlide(index);
			});
			$newThumbCont.append($newThumb, $newThumbLoader);
			$thumbsElement.append($newThumbCont);
		});
	},

	_init: function() {
		this.options.content.length = this.options.thumbs.length = Math.min(7, this.options.content.length, this.options.thumbs.length);
	
		this._preload();
		this.setSlide(0);
	},

	setSlide: function(index) {
		this.element.find('.fpSlide').hide();

		var $slide = $('#fpSlide' + index);
		if($slide.length == 0) {
			this.element.find('.fpSlide').hide();
			this._loadSlide(index);
		}
		else {
			$slide.show();
		}
		
		this.element.find('.fpThumbContainer').removeClass('active');
		this.element.find('#fpThumbCont' + index).addClass('active');
		
		this._queueSlide(index, this.options.duration * 1000);
	},

	_queueSlide: function(currentindex, timeout) {
		var self = this;

		this.element.find('.fpThumbLoader').stop(true, false).css({'width': ''});

		var $nextThumb = this.element.find('#fpThumbCont' + currentindex);

		$nextThumb
			.find('.fpThumbLoader')
			.css({'width': ''})
			.animate({'width': '100%'}, timeout, 'linear', function() {
				$(this).css({'width': ''});
				window.setTimeout(function() {
					self.setSlide((currentindex + 1) % self.options.content.length);
				}, 10);
			});
	},

	_loadSlide: function(index) {
		var $newslide = $('<div id="fpSlide' + index + '" class="fpSlide"/>');
		
		this.element.find('.fpSlides').append($newslide);
		var $currentItem = this.options.content[index];
		if(typeof($currentItem)=='object'&&($currentItem instanceof Array)) {
			var $slidecontent = $('<a></a>').attr('href', $currentItem[0]).attr('target', '_blank').append($('<img/>').attr('src', $currentItem[1]));
			$newslide.append($slidecontent);
		} 
		else if(this.options.content[index].indexOf(".swf")!= -1) {
			$newslide.flash(
			{
				swf: this.options.content[index],
				width: this.options.width,
				height: this.options.height,
				wmode: 'opaque'
			});
			$newslide.addClass('swf');
		}
		else {
			var $slidecontent = $('<img/>').attr('src', this.options.content[index]);
			$newslide.append($slidecontent);
		}

	},

	options: {
		content: [],
		thumbs: [],
		duration: 10,
		width: 780,
		height: 435
	}
});

