(function ($) {

	var SWITCH_TIME = 10000; // Time between automatic rotations	
	
	var pageX = -1;
	var pageY = -1;
	var isPluginInitialized = false;
	
	var getRotator = function(element) {
		return $(element).closest('._jsRotatorMarker');
	};
	
	var getRotatorState = function(rotator) {
		var config = $(rotator).data('rotationConfig');
		var links = $('.'+config.linkClass,$(rotator));
		var elements = $(rotator).data('rotationElements');
		var currentIndex = $(rotator).data('rotationIndex');
		var element = elements[currentIndex];
		return {
			config: config,
			links: links,
			elements: elements,
			currentIndex: currentIndex,
			currentElement: elements[currentIndex],
			currentLink: links[currentIndex]
		};
	};
	
	var rotateRight = function(rotator) {
		var state = getRotatorState(rotator);
		var nextIndex = state.currentIndex+1;
		if (nextIndex >= state.elements.size()) {
			nextIndex = 0;
		}
		rotate(rotator,nextIndex);
	};

	var rotateLeft = function(rotator) {
		var state = getRotatorState(rotator);
		var nextIndex = state.currentIndex-1;
		if (nextIndex < 0) {
			nextIndex = state.elements.size()-1;
		}
		rotate(rotator,nextIndex);
	};	
	
    var areCoordinatesInside = function(x,y,element) {
        var top = $(element).offset().top;
        var left = $(element).offset().left;
        var width = $(element).width();
        var height = $(element).height();
        return x > left && x < (left + width) &&
               y > top && y < (top + height);
    }; 	
	
	$.rotateAll = function() {
		$('._jsRotatorMarker').each(function() {
			if (!areCoordinatesInside(pageX,pageY,this) && !$(this).is('.paused')) {
				var nextIndex = getNextNaturalIndex($(this));
				rotate($(this),nextIndex);
			}
		});
		setTimeout('$.rotateAll()',SWITCH_TIME);
	}
	
	var getNextNaturalIndex = function(rotator) {
		var currentIndex = $(rotator).data('rotationIndex');
		var elements = $(rotator).data('rotationElements');
		if (currentIndex+1 >= elements.size()) {
			return 0;
		}
		return currentIndex+1;
	}
	
	var rotate = function(rotator,index) {
		var config = $(rotator).data('rotationConfig');
		var links = $('.'+config.linkClass,$(rotator));
		var elements = $(rotator).data('rotationElements');
		var currentIndex = $(rotator).data('rotationIndex');
		var element = elements[currentIndex];
		var width = elements.first().width();
		
		if (index == currentIndex) {
			return;
		}
		
		if (index > currentIndex) {
			// Rotate right
			var nextIndex = index+1;
			if (nextIndex >= elements.size()) {
				nextIndex = 0;
			}
			
		}
		if (index < currentIndex) {
			// Rotate left
			var nextIndex = index-1;
			if (nextIndex < 0) {
				nextIndex = elements.size()-1;
			}
		}
		var left = -(width*index);
		$(rotator).data('rotationIndex',index);
		links.eq(currentIndex).removeClass(config.activeLinkClass);
		if (config.onLinkDeactivation != null) {
			config.onLinkDeactivation(links.eq(currentIndex));
		}
		if (config.onElementDeactivation != null) {
			config.onElementDeactivation(elements.eq(currentIndex),currentIndex);
		}
		links.eq(index).addClass(config.activeLinkClass);
		if (config.onLinkActivation != null) {
			config.onLinkActivation(links.eq(index));
		}
		if (config.onElementActivation != null) {
			config.onElementActivation(elements.eq(index),index);
		}		
		links.hide().show();
		$(rotator).hide().show();		
		$("._jsRotatorContainer",rotator).stop().animate({
			'left': left
		},1000,function() {
			
			
		});
	};
	
	//var prepareRotate = function
	
	$.fn.rotator = function(options) {
		   if ($(this).size() <= 0) return;
	       var config = jQuery.extend({
	            // Default options:
	            'viewportClass': 'rotatorViewport',
	            'elementClass': 'rotatorElement',
	            'linkClass': 'rotatorLink',
	            'activeElementClass': 'rotatorElementActive',
	            'activeLinkClass': 'rotatorLinkActive',
	            'rotateLeftButtonClass': 'rotateLeft',
	            'rotateRightButtonClass': 'rotateRight',
	            'displayTime': 1000,
	            'autoStart': true,
	            'direction': 'horizontal',
	            'onLinkActivation': null,
	            'onLinkDeactivation': null,
	            'onElementActivation': null,
	            'onInitializationFinished': null
	        }).extend(options);
	        $(this).addClass('_jsRotatorMarker');
	        $(this).data('rotationConfig',config);
	        
	        var links = $('.'+config.linkClass,this);
	        var elements = $('.'+config.elementClass,this);

	        if (links.size() != elements.size() && links.size() > 0) {
	        	// If link size and element size do not conform
	        	return;
	        }

	        var rotator = $(this);
	        $('.'+config.rotateLeftButtonClass,$(this)).click(function(e) {
	        	rotateLeft(getRotator($(this)));
	        	return false;
	        });
	        $('.'+config.rotateRightButtonClass,$(this)).click(function(e) {
	        	rotateRight(getRotator($(this)));
	        	return false;
	        });
	        
	        var selectedIndex = 0;
	        var currIndex = 0;
	        elements.each(function() {
	        	if ($(this).hasClass(config.activeElementClass)) {
	        		selectedIndex = currIndex;
	        	}
	        	currIndex++;
	        });
	        
	        currIndex = 0;
	        var rotator = this;
	        links.each(function() {
	        	var i = currIndex;
	        	$(this).click(function() {
	        		// bei Hero-Teaser nicht
	        		if($(this).closest('.heroTeaser').length <= 0) {
		        		rotate(rotator,i);
		        		return false;
	        		}
	        	});
	        currIndex++;
	        });
       
	        $(this).data('rotationLinks',links);
	        $(this).data('rotationElements',elements);
	        $(this).data('rotationIndex',selectedIndex);
       
	        var width = $(elements).first().width();
	        elements.wrapAll('<div class="_jsRotatorContainer" style="position: relative;width:' + 700*elements.size() +'px; left: 0"/>');
	        var activeElement = $('.'+config.elementClass,this).first();
	        if (!isPluginInitialized) {
	        	setTimeout('$.rotateAll()',4000);
	        }
			if (config.onInitializationFinished != null) {
				config.onInitializationFinished(rotator,rotate);
			}
	        isPluginInitialized = true;
	};

	$(document).mousemove(function(e) {
		pageX = e.pageX;
		pageY = e.pageY;
	});
	
})(jQuery);
