/**
 * @date 2010-08-23@23:22:39
 */

var jQ = jQuery;
var SLIDE_DURATION = 5000;
var BASE_PATH = "http://ravinetdarc.basis5.de/";

function playSlideShow() {
	var $active = $('.thumbs .selected');
	var $next = ($active.next().length > 0) ? $active.next().children("a") : $('.thumbs li:first > a');
	$next.trigger('remote_click');
}
function start_timer() {
	window.autoplay = window.setInterval("playSlideShow()", SLIDE_DURATION);
}
function restartSlideShow() {
	window.restartInterval = window.setTimeout("start_timer()", 30 * 1000);
}
(function ($) {
	$.SlideShow = function (el, options) {

		var base = this;

		$el = $(el);
		el = el;

		var slideShowPlay = false,
		SLIDE_AMOUNT = null,
		$currentSlide = null,
		prevSlide = null,
		thumbAmount = null,
		captionFlag = false;
	
		$el.data("SlideShow", base);
		set_currentSlide = function (str) {
			$currentSlide = str;
		};
		get_currentSlide = function () {
			return $currentSlide;
		};
		set_prevSlide = function (str) {
			prevSlide = str;
		};
		get_prevSlide = function () {
			return prevSlide;
		};
		set_thumbAmount = function (n) {
			thumbAmount = n;
		};
		get_thumbAmount = function () {
			return thumbAmount;
		};
		set_captionFlag = function (b) {
			captionFlag = b;
			toogleCaption();
		};
		get_captionFlag = function () {
			return captionFlag;
		};
		toogleCaption = function () {
			if (get_captionFlag() === true )
				$('#caption').stop().show();
			else {
				$('#caption').stop().hide();
			}
		};
	
		init = function () {

			options = $.extend({},$.SlideShow.defaultOptions, options);

			$thumbs = $('.thumb');

			$('#caption').hide().bind({
				mouseenter: function () {
					$(this).stop().show();
				},
				mouseleave: function () {
					$(this).stop().show();
					set_captionFlag(true);
				}

			});

			$('#slideshow').css({width:"400px"}).html('<ul></ul>').bind({
				mouseenter : function() {
					set_captionFlag(true);
				},
				mouseleave : function () {
					set_captionFlag(false);
				}
			});

			thumbAmount = ($thumbs.length);

			$thumbs.each(function(){

				$(this).bind({
					click : function () {
						set_captionFlag(false);
						if ( $(this) != get_currentSlide() ) {

							var img_src = $(this).attr('href');
							var target_url = $(this).children('img').attr('longdesc');
							var caption = $(this).parent().children('.caption').html();

							$('.selected').stop().fadeTo('fast',0.5).removeClass('selected');
							$(this).parent().addClass('selected').fadeTo('fast', 1.0);

							if (get_prevSlide() !== null){
								get_prevSlide().fadeTo('fast', 0.5);
							}
							load_image(img_src, target_url, caption);

							set_prevSlide(get_currentSlide());
							set_currentSlide($(this).parent());

							if (typeof(window.autoplay) === "number") {
								clearInterval(window.autoplay);
								clearTimeout(window.restartInterval);
								restartSlideShow();
							}
							return false;

						}
						else {
							return false;
						}
					},
					mouseenter:    function () {
						$(this).parent().stop().fadeTo('fast', 1.0);
					},
					mouseleave :    function (){
						if ($(this).parent().hasClass('selected') === false) {
							$(this).parent().stop().fadeTo('fast', 0.5);
						}
					},
					remote_click : function(){

						if ($(this) != get_currentSlide() ) {

							var img_src = $(this).attr('href');
							var target_url = $(this).children('img').attr('longdesc');
							var caption = $(this).parent().children('.caption').html();

							$('.selected').stop().fadeTo('fast',0.5).removeClass('selected');
							$(this).parent().addClass('selected').fadeTo('fast', 1.0);
			    
							if (get_prevSlide() !== null){
								get_prevSlide().fadeTo('fast', 0.5);
							}
							load_image(img_src, target_url, caption);

							set_prevSlide(get_currentSlide());
							set_currentSlide($(this).parent());

							return false;
						}
						else {
							return false;
						}
					}
				}).parent().fadeTo('fast', 0.5);
		

			});

		};

		load_image = function (image_src, target_url, caption) {
	
			var img = new Image();
			$(img).load(function(){

				$('#slideshow ul li').attr('id', 'old');

				$('#slideshow ul').prepend('<li class="new" style="display:none"></li>');
				$('.new').html(img).removeClass('new').fadeIn(1000);
				$('#old').fadeOut(1000, function() {
					$(this).remove();
				});
				$('#caption').html(caption);

			})
			.attr('src', BASE_PATH + image_src)
			.bind({
				click : function () {
					if (typeof(target_url) === "undefined" || target_url.length <= 3 ) {
						return false;
					} else {
						window.open(target_url, "_blank");
					}
				},
				mouseenter : function() {
					set_captionFlag(true);
					if (typeof(target_url) === "undefined" || target_url.length <= 3 ) {
						$(img).css({
							'cursor': 'default'
						});
						return false;
					} else {
						$(img).css({'cursor': 'pointer'});
					}
				},
				mouseleave : function () {
					set_captionFlag(false);
				}
			
			});

		};

		// Run initializer
		init();
	};

	$.SlideShow.defaultOptions = {};

	$.fn.slideShow = function(options){
		return this.each(function(){
			(new $.SlideShow(this, options));
		});
	};
})(jQuery);
