﻿/// <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 = obj.find('.buttons > a').length;

			AttachEvents();

			var timer = setTimeout(SlideAd, o.delay);

			/***** Private functions *****/
			function AttachEvents()
			{
				obj.find('.buttons').click(function() {
					return false;
				});

				obj.find('.buttons > a').click(function() {
					ChangeSlide($(this));
					clearTimeout(timer);
					return false;
				});

				$(obj).find('.buttons > a').bind('mouseenter', function() {
					$(this).addClass('over');
				});

				$(obj).find('.buttons > a').bind('mouseleave', function() {
					$(this).removeClass('over');
				});
			}

			function SlideAd()
			{
				currentIndex++;
				if (currentIndex > totalSlides - 1)
					currentIndex = 0;

				var nextSlide = $(obj).find('> .buttons > a:eq(' + currentIndex + ')');

				if (ChangeSlide(nextSlide))
					timer = setTimeout(SlideAd, o.delay);
			}

			function ChangeSlide(button)
			{
				$(obj).find('> .buttons > a').removeClass('current');
				$(button).addClass('current');

				var link = $(button).attr('href');
				var image = $(button).attr('title');
				var imagemap = ($(button).attr('rel').length > 0) ? '#' + $(button).attr('rel') : '';

				$(obj).find('> a').attr('href', link);

				$(document.createElement('img')).bind('load', function() {
					//$(obj).find('> a > img:').remove();
					var t = $(this);
					$(t).css({
						'position': 'absolute',
						'top': 0,
						'width': 896,
						'height': 265,
						'display': 'none'
					});

					$(obj).find('> a').append($(t));
					$(t).fadeIn(500, function() {
						$(obj).find('> a > 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: 3000
	};
	/***** End Options *****/
})(jQuery);
