slider = null;
timer = null;

(function($) {
    
    $.fn.initSlider = function() {
		slider = $(this);
		
                highliteExternalNavigation(slider.find("li:first"));
		slider.hover(function() {
			timerStop();
		}, function() {
			slider.timerSetup();
		});
		
		slider.timerSetup();
    },
    
	$.fn.slideLeft = function(finishCallback) {
		slider = $(this);
		
		timerStop();
			
			
		firstElem = slider.find("li:first");
		lastElem = slider.find(">li:last-child");

		theClone = lastElem.clone();

		lastElem.remove();
		theClone.css("margin-left", "-"+firstElem.width()+"px");
		slider.prepend(theClone);
                
                
		theClone.animate({
			"margin-left": "0px"
			}, {
			duration: 2000,
			complete: function() {
                                highliteExternalNavigation($(this));
				if (finishCallback)
					finishCallback();
			}
			}
		);
	},

	$.fn.slideRight = function(finishCallback) {
		slider = $(this);
		
		timerStop();
		
		firstElem = slider.find("li:first");
                currentSlide = $(firstElem).next();
		firstElem.animate({
			"margin-left": "-="+firstElem.width()+"px"
			}, {
			duration: 2000,
			complete: function() {
				theClone = firstElem.clone();
				firstElem.remove();
				slider.append(theClone);
				theClone.attr("style", "");
				
				
				highliteExternalNavigation(currentSlide);
				if (finishCallback)
					finishCallback();
			}
			});
	},
	
	$.fn.timerSetup = function() {
		slider = $(this);
		
		timerStop();
		
		timer = setTimeout(function() {
			slider.slideRight(function() {
				slider.timerSetup();
			});
		},3000);	
	},
        
        // Externe Navigationsitems highliten
        highliteExternalNavigation = function(currentSlide) {
            
            // Altes highlite entfernen
            $("div#sliderExternalNavigation div.currentSlide").removeClass('currentSlide');
            // Über die ID des Slides, das passende Element in der Navigation finden
            currentHighlite =  $("div#sliderExternalNavigation").find( '[slide="'+currentSlide.attr('id')+'"]' );
           

            currentHighlite.addClass('currentSlide');
        }
	
	timerStop = function() {
		if (timer != null)
			clearTimeout(timer);	
	}	
    
})(jQuery)


