(function($) {

	 //Definition des FTeaser Plugins
	$.fn.fteaser = function(options) {
			
		var main_opts = $.extend({}, $.fn.fteaser.defaults, options);
		
		//Jedes gefunden Element wird hier durchgegangen
		return this.each(function(){
			this.options = main_opts;
			this.outputDiv = this.id + "_content";
			this.currentItemIndex = 0;
			this.teaserItems = new Array();
			
			//init(this);
			initFTeaser(this);
			
		 });
	 };
	

	 //Defaultwerte (vor der Benutzung dieses Plugins k?nnen diese Defaults ?berschrieben werden)
	 $.fn.fteaser.defaults = {
			 itemSelected : 0,
			 fadeInSpeed  : 250,
			 cycleInterval: 3000,
			 useFadeIn : true,
			 activateCycle : false,
			 navigation : true,
			 navigationType : "numeric",
			 navigationLabel : "Navigation:"		
	};



	function initFTeaser($object){
		
		// no FadeIn for IE 7.0
		if(jQuery.browser.msie && jQuery.browser.version == '7.0') $object.options.useFadeIn = false;
		
		//Alle Teaser Contents ausblenden
		$('#'+$object.id).find('.fteaser_item')
			.each(function(i, item){												
				$object.teaserItems[i] = $(this).html();			
				$(this).hide();
			})
					
			
		//Was passiert wenn man auf einen Teaser klickt?
		$('#'+$object.outputDiv).bind("click", function(){itemClicked($object)});
		
		
		//Navigation erzeugen
		if ($object.options.navigation){
			$object.navigationDiv = $object.id + "_navigation";
			var navigationItems = new Array();

			//in navigation ist das zus?tzlich erzeugte HTML f?r die Navigation
			navigation = '';
			if ($object.options.navigationLabel){
				var navigation = '<li id="nav">' + $object.options.navigationLabel + '</li>';
			}

			show_navigation = false;

			//F?r den Typen numeric wird eine numerische Navigation erstellt			
			if ($object.options.navigationType == "numeric"){
				for(i=0;i<$object.teaserItems.length;i++){
					
					//Link-HTML erzeugen
					var navigationItemId = "navigationitem_" + $object.id + "_" + i;
					//navigation += '<li name="teasernav" id="'+navigationItemId+'"><a href="javascript:void(0);">'+(i+1)+'</a></li>';
					navigation += '<li name="teasernav" id="'+navigationItemId+'"><a href="javascript:void(0);">&nbsp;</a></li>';
					//navigation += '<li name="teasernav" id="'+navigationItemId+'">&nbsp;</li>';
					navigationItems[i] = navigationItemId;
					
					show_navigation = true;
				}
				
				if (show_navigation){
					//Links ins DOM
					$("#"+$object.navigationDiv).html(navigation);
					
					//Links mit Funktion ausstatten
					for(i=0;i<navigationItems.length;i++){
						$("#"+navigationItems[i]).bind("click", function(){
							clearInterval($object.cycleInterval);
							
							//Kleiner Hack um herauszufinden welches Element gemeint ist
							var parts = this.id.split('_');
							showItem($object, parts[parts.length-1]);
						});
						
						if (i == navigationItems.length-1){
							$("#"+navigationItems[i]).addClass("last");
						}
					}
				}
			}
			
			
			//F?r den Typen paging wird ein vor und zur?ck Link eingef?gt			
			if ($object.options.navigationType == "paging"){
				var navigationItemIdPrev = "navigationitem_" + $object.id + "_prev";
				navigation += '<li name="teasernav" id="'+navigationItemIdPrev+'"><a href="javascript:void(0);"><-</a></li>';

				var navigationItemIdNext = "navigationitem_" + $object.id + "_next";
				navigation += '<li name="teasernav" class="last" id="'+navigationItemIdNext+'"><a href="javascript:void(0);">-></a></li>';

				$("#"+$object.navigationDiv).html(navigation);

				$("#"+navigationItemIdPrev).bind("click", function(){
					prevItem($object);
				});
					
				$("#"+navigationItemIdNext).bind("click", function(){
					nextItem($object);
				});

				
			}			
			
		}		
		
		
		//Wenn gew?nscht, wird der automatische Teaser Wechsel gestartet
		if ($object.options.activateCycle == true){
			$object.cycleInterval = window.setInterval(function () {nextItem($object)}, $object.options.cycleInterval);
		}

		
		//Ersten Teaser aufrufen
		showItem($object, $object.options.itemSelected);
		
	};
	
	
	//Funktion die aufgerufen wird, wenn ein Teaser geklickt wird
	function itemClicked($object){
		clearInterval($object.cycleInterval);
		if(!$object.options.navigation) nextItem($object);
	};
	
	
	//N?chstes Item anzeigen 
	function nextItem($object){
		if($object.currentItemIndex >= $object.teaserItems.length-1) showItem($object, 0);
		else showItem($object, $object.currentItemIndex+1);
	};
	
	
	//Vorheriges Item anzeigen
	function prevItem($object){
		if($object.currentItemIndex == 0) showItem($object, $object.teaserItems.length-1);
		else showItem($object, $object.currentItemIndex-1);
	};	
	
	
	//Item anzeigen
	function showItem($object, itemIndex){
        
        
		
		//Interner Index wird auf den aktull ?bergebenen Index ge?ndert
		$object.currentItemIndex = itemIndex; 

		
		//In der Navigation die Klasse active hinzuf?gen / rausnehmen
		$('#'+ $object.id + "_navigation" + ' li[name=teasernav]').each(function(i,item){
			if(i==$object.currentItemIndex) $(item).addClass('active');
			else $(item).removeClass('active');
		});
		
		
		//Div ausschalten und mit neuem Content bef?llen
		$('#'+$object.outputDiv).hide();
		$('#'+$object.outputDiv).html($object.teaserItems[itemIndex]);
		
		
		//Einfaden oder einfach wieder anschalten				
		if($object.options.useFadeIn && $object.options.fadeInSpeed > 0) $('#'+$object.outputDiv).fadeIn($object.options.fadeInSpeed);
		else $('#'+$object.outputDiv).show();
				
	};
	
	
})(jQuery);
