(function( $ ){
	var methods =
		{	
			init: function(timeout) 
			{
				if (timeout == undefined)
				{
					timeout = 5000;
				}
				var $this = $(this);
				
				var slideCount = 0;
				var currentSlide = 1;
				var interval = null;
				
				$('ul.slideshowBody').css('display', 'none');

				//if video is there, show video button
				if ($this.find(".video").is('div'))
				{
					$.fn.afSlideshow.clonedVideo = $(".focus .video").clone();
					//moved from afgruppen-functions
					$(".play a").mouseenter(function() {
						$("img", this).fadeOut("fast");
					});
					$(".play a").mouseleave(function() {
						$("img", this).fadeIn("fast");
					});
					$(".play a").click(function () {
						$(".focus .video").fadeIn("medium");
						$(".focus").addClass("videoactive");
						$(".focus .video").empty().append($.fn.afSlideshow.clonedVideo.clone().children());
						
						$(".videoclose").click(function () {
						$(".focus .video").fadeOut("medium");
						$(".focus").removeClass("videoactive");	
						
						$(".focus .video").empty();
						});
					});
					
				}
				else
				{
					$this.find(".play").css("display", "none");
				}
				
				this.find("ul>li").each(function()
					{	
						slideCount++;
						var slideshow = $(this).parent().parent();
						slideshow.find("div.controls>ul")
									.append('<li class="btn'+slideCount+'"><a href="#"><img src="/images/focusmodule/btn_'+slideCount+'.png" alt="'+slideCount+'"/></a></li>')
									.find("li.btn"+slideCount).click(
											function(event)
											{
												var className = $(this).attr('class').split(' ').slice()[0]; 
												var num = className.substr(3,1);
												$this.afSlideshow('showSlide',num);								
												currentSlide = num;
												
												event.preventDefault();												

												return false;
											});
					}
				)
				$this.afSlideshow('showSlide',1); //select first slide
				
				if(slideCount>1)
				{
					interval = setInterval(function(){
						if (currentSlide>=slideCount)
							currentSlide = 1;							
						else
							currentSlide++							
						$this.afSlideshow('showSlide', currentSlide);	}, timeout);
						$this.find(".overimage").mouseenter(function() {
							clearInterval(interval);
						});
					$this.find(".overimage").mouseleave(function() {
						interval = setInterval(function(){
						if (currentSlide>=slideCount)
							currentSlide = 1;							
						else
							currentSlide++							
						$this.afSlideshow('showSlide', currentSlide);	}, timeout);
					});
				}
				else
				{
					$this.find("div.controls>ul").css('display','none');
				}
			},
			showSlide: function(number)
			{
				var $parent = $(this);	
				if ($parent.find("div.slideshow").css("display")=="block")
				{				
					this.find("ul.slideshowBody li:nth-child("+number+")").each(function()
					{
						var $slide = $(this);
						$parent.find("div.controls>ul li").removeClass('selected');
						$parent.find("div.controls>ul li:nth-child("+number+")").addClass('selected');//mark button as selected
						
						if ($parent.find("div.slideshow").children().size() > 0)
						{
							$parent.find("div.slideshow").fadeOut(400, function()
							{
								$parent.find("div.slideshow").empty().append($slide.find('img').clone()).fadeIn();
							});
						}
						else //first = no animation
						{
							$parent.find("div.slideshow").empty().append($slide.find('img').clone());
						}
						
						if ($parent.find("div.info").children().size() > 0)
						{
							$parent.find("div.info").fadeOut(400, function()
							{
								$parent.find("div.info").empty().append($slide.find('div').children().clone()).fadeIn();
							});
						}
						else
						{
							$parent.find("div.info").empty().append($slide.find('div').children().clone());							
						}
					});				
				}
			}
		};
	
	$.fn.afSlideshow = function(method) {
		// Method calling logic
		if ( methods[method] ) {
		  return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
		} else if ( typeof method === 'object' || ! method ) {
		  return methods.init.apply( this, arguments );
		} else {
		  $.error( 'Method ' +  method + ' does not exist on jQuery.afSlideshow' );
		}    
	};

})( jQuery );

