/**
 * The flash slideshow plugin for jQuery
 * This plugin allows to rotate the slides with the flash or with the html source when the flash disabled
 *
 * This plugin requires:
 * jQuery, jQuery Tools Tabs, jQuery Tools Falshembed
 *
 * Copyright (c) 2009 Visual Trading Systems, vtsystems.com
 * Alex Shevlyakov
 */


(function($) {

	$.flashSlideshow = {
		version: '1.0',
		defaultConf: {
			effect: "fade",
			fadeOutSpeed: "slow",
			rotate: true,
			autoplay: true,
			interval: 33000,
			clickable: false,
			flashAttr: "flash",
			flashVersion: [10, 0],
			slides: ".slide",
			tabs: null // ".tabs"
		}
	};

	function FlashSlideshow(conf, root) {
		var self = this, $self = $(this);

		var slides  = (typeof(conf.slides) == "string") ? root.find(conf.slides) : $(conf.slides);
		var tabs    = (typeof(conf.tabs)   == "string") ? root.find(conf.tabs)   : $(conf.tabs);
		var flashes = root.find("["+ conf.flashAttr+"]");

		if (!conf.tabs || tabs.length) {
			// we need to create the tabs if they are not defined
			var tabs = $("<div></div>").addClass("tabs").appendTo(root);
			slides.each(function(i) {
				$("<a>" + (i+1) + "</a>").attr("href", "#").addClass("tab").appendTo(tabs);
			});
		}

		tabs.tabs(slides, {
			effect: conf.effect,
			fadeOutSpeed: conf.fadeOutSpeed,
			rotate: conf.rotate
		}).slideshow({
			autoplay:  conf.autoplay,
			interval:  conf.interval,
			clickable: conf.clickable
		});

		$.flashSlideshowAPI = tabs.tabs();

		flashes.each(function() {
			if ($(this).attr(conf.flashAttr)) {
				$(this).flashembed({
					src: $(this).attr(conf.flashAttr),
					wmode: 'transparent',
					version: conf.flashVersion
				});
			}
		});
	}

	// jQuery plugin implementation
	$.fn.flashSlideshow = function(conf) {
		var conf = $.extend($.flashSlideshow.defaultConf, conf);

		return this.each(function(i) {
			var $ss = new FlashSlideshow(conf, $(this));
		});
	};

}) (jQuery);