/// <reference path="../jquery-1.3.2-vsdoc.js"/>

(function($) {
	$.fn.slideshow = function(options) {
		var opts = $.extend({}, $.fn.slideshow.defaults, options);

		return this.each(function() {
			/***** Setup *****/
			var obj = $(this);
			var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
			/***** End Setup *****/

			var currentIndex = 0;
			var totalSlides = $('#background_controller > .trans > a').length;

			AttachEvents();

			var timer = setTimeout(SlideAd, o.delay);

			/***** Private functions *****/
			function AttachEvents()
			{
				$('#background_controller > .trans').click(function () {
					return false;
				});

				$('#background_controller > .trans > a').click(function () {
					ChangeSlide($(this));
					clearTimeout(timer);
					return false;
				});

				$('#background_controller > .trans > a').bind('mouseenter', function() {
					$(this).addClass('over');
				}).bind('mouseleave', function () {
					$(this).removeClass('over');
				});
			}

			function SlideAd()
			{
				currentIndex++;
				if (currentIndex > totalSlides - 1)
					currentIndex = 0;

				var nextSlide = $('#background_controller > .trans > a:eq(' + currentIndex + ')');

				if (ChangeSlide(nextSlide))
					timer = setTimeout(SlideAd, o.delay);
			}

			function ChangeSlide(button)
			{
				$('#background_controller > .trans > a').removeClass('current');
				$(button).addClass('current');

				var link = $(button).attr('href');
				var image = $(button).data('image');
				var imagemap = ($(button).data('imagemap').length > 0) ? '#' + $(button).data('imagemap') : '';

				$(document.createElement('img')).bind('load', function() {
					//$(obj).find('> a > img:').remove();
					var t = $(this);
					$(t).css({
						'display': 'none'
					});

					$(obj).append($(t));
					$(t).fadeIn(500, function() {
						$(obj).find('img:lt(1)').remove();
						if (imagemap.length)
							$(t).attr('usemap', imagemap);
						else
							$(t).removeAttr('usemap');
					});
				}).attr('src', image);

				return true;
			}
			/***** End Private functions *****/
		});
	}
	/***** Options *****/
	$.fn.slideshow.defaults = {
		delay: 7000
	};
	/***** End Options *****/
})(jQuery);
