jQuery().ready(function(){ 

	jQuery.fn.exists = function(){return jQuery(this).length>0;};

	/*
	 setFrameHeight = function($tableObj){
	 	//if (arguments.length < 1) $tableObj = $slideTableFrameObj;
	 	tableHeight = $tableObj.find('.slideItem').height()-2+'px';
		//alert(tableHeight);
	 	$tableObj.children('.left, .right').css('height', tableHeight);
	
	 }
	 */
	  jQuery.fn.setFrameHeight = function(){
	  	//if (arguments.length < 1) $tableObj = $slideTableFrameObj;
	  	tableHeight = $(this).find('.slideItem').height()-2+'px';
	 	//alert(tableHeight);
	  	$(this).children('.left, .right').css('height', tableHeight);
	  	//alert('jetzt');
	 
	  }
	  
	  
	jQuery.fn.centerHorizontal = function($relativeObj){
	
	
		if(jQuery.browser.msie && jQuery.browser.version < 9){
		
			relationWidth = $relativeObj !== undefined?$relativeObj.width():$(window).width();
			positionFromLeft = (relationWidth-$(this).width())/2;
			//positionFromLeft = positionFromLeft>0?positionFromLeft:'300px';
			positionFromLeft = positionFromLeft>0?'400px':'400px';
			$(this).css('left',positionFromLeft);			
			} else {
				
			relationWidth = $relativeObj !== undefined?$relativeObj.width():$(window).width();
			positionFromLeft = (relationWidth-$(this).width())/2;
			positionFromLeft = positionFromLeft>0?positionFromLeft:0;
			$(this).css('left',positionFromLeft);
			
			}
	}
	  
	  	  
	$.fn.jumpToValue = function(){
		// get your select element and listen for a change event on it
		$(this).change(function() {
		  // set the window's location property to the value of the option the user has selected
		  if($(this).val()!='#'){ window.location = $(this).val(); } 

		});
	}
	

	
	
	// Scroll to Top     
	jQuery.fn.scrollToTop=function(){
		var ToTop=jQuery(this); // Variable initialisieren      
		jQuery(this).removeAttr("href");
		
			jQuery(window).scroll(function(){ // Windows Scroll-Event         
			if(jQuery(window).scrollTop()=="0"){ 
				jQuery(ToTop).fadeOut("slow") // ToTop-Link ausblenden wenn die Scroll-Position 0 ist 
			} else{ 
				jQuery(ToTop).fadeIn("slow") // ToTop-Link einblenden wenn die Scroll-Position nicht 0 ist
			}
		});                
		      
		jQuery(this).click(function(){ // Klick des ToTop-Links
			jQuery("html, body").animate({scrollTop:0}, {duration:'slow', easing: 'easeOutQuad'});          
		})      
	}
		
	jQuery(document).ready(function(){      
		jQuery(function() {
			jQuery("#toTop").scrollToTop(); // Zuweisen von scrollToTop          
		});                      
	});
	
		  
 

	/********* Setupblock **********/
	
	bannerIntervall		= '7000';	//changeintervall for banner
	tableSlideSpeed		= 400; 		//table sliding speed 
	tableSlideDistance	= 528;		//table slide distance
	
	stageFadingSpeed	= 600; 		//
	
	stageFadingTime		= 7000;		//
	
	$leftSlideTableButton	= $('<a class="left" href="#">left</a>');
	$rightSlideTableButton	= $('<a class="right" href="#">right</a><div style="clear: both;"/>');		
		
		
	/********* Setupblock **********/

	$sliderArrowLeft = $('<img class="prev backward" src="/tl_files/setup/img/nav_arrow_left.png" />');
	$sliderArrowRight = $('<img class="next forward" src="/tl_files/setup/img/nav_arrow_right.png" />');
	
	
	$sliderArrowLeft.prependTo('.front #Banner .inside');
	$sliderArrowRight.appendTo('.front #Banner .inside');
	
	
$('.nav_top').scrollToTop();

$('.produktnav .select').jumpToValue();
	
if($.cookie){
	$.cookie("csvListingFilterTable_filters", null); // Reset Table Sort Cookies
	$.cookie("overlayTable_filters", null);
	}
	
	$('.front #Banner .ce_text').each(function(i){
		blockHeight = $(this).outerHeight();
		var gesammtHoehe = 0;
		$(this).children('*').each(function (){
			gesammtHoehe = gesammtHoehe + $(this).outerHeight();
		})
		$(this).children('*:last:visible').each(function (){
			blockContentHeight = (blockHeight-($(this).position().top + $(this).outerHeight())+5)/2;
			$(this).closest('.ce_text').children('*:first').css('margin-top', blockContentHeight+'px');			
		})	
	});	
	
	
	
	
		
	$('#left .produktnav').addClass('produktnav-active');
		
			
			changeStageTeaser = function($navObj,$navObjs){
			
				buttonIndex = $navObjs.index($navObj); 
				$newActiveNode = $items.filter(':eq('+buttonIndex+')');
				$items.filter(':visible').css('z-index', '100');
				$items.filter(':eq('+buttonIndex+')').css('z-index', '90');
				//$items.filter(':visible').fadeOut(400);
				$items.filter(':visible').hide();
				//$items.filter(':eq('+buttonIndex+')').stop().fadeIn(0);
				$items.filter(':eq('+buttonIndex+')').stop().css('opacity', 1).css('display', 'block');
			}
			
						
			changeSlideTeaser = function($items, ItemIndex ,newActiveIndex, duration){	
			
				
				if($items.filter(':eq('+ItemIndex+')').css('opacity')== 0 || $items.filter(':eq('+ItemIndex+')').css('opacity')== 1 ){
				
					$items.stop();
					$items.filter(':eq('+newActiveIndex+')').fadeIn(duration);
					$items.not(':eq('+newActiveIndex+')').fadeOut(duration);				
				}							
				//console.log($items.not(':eq('+newActiveIndex+')').length);				
				//$item.fadeIn;
			}
			
						
			//aktivate Stage Functionality
			//$.makeSlider = $.makeSlider || {};
			
			
			$.fn.makeSlider = function(singleSlideItem, duration, prevNavButton, nextNavButton){
			
					if($(this).exists()){$(this).each(function(){	
				
					singleSlideItem = singleSlideItem || '.item';				
					$singleSlideItems = $(this).find(singleSlideItem);
					
					// set default Class for Navigation Items
					prevNavButton = prevNavButton || '.prev';
					nextNavButton = nextNavButton || '.next';
					
					// get jQuery Objects					
					$prevNavButton = $(this).find(prevNavButton);
					$nextNavButton = $(this).find(nextNavButton);
					
					//default Fade Duration
					duration = duration || 300;
													
					$singleSlideItems.hide();
					$singleSlideItems.filter(':first').show();
					
										
					// define Click Handler
					$prevNavButton.click(function(){						
						itemIndex = $singleSlideItems.index($singleSlideItems.filter(':visible'));	
						newItemIndex = (itemIndex-1)<0?$singleSlideItems.length-1:(itemIndex-1);	
						changeSlideTeaser($singleSlideItems, itemIndex, newItemIndex, stageFadingSpeed);
					})
					
					$nextNavButton.click(function(){						
						itemIndex = $singleSlideItems.index($singleSlideItems.filter(':visible'));	
						newItemIndex = (itemIndex+1)>=$singleSlideItems.length?0:(itemIndex+1);		
						changeSlideTeaser($singleSlideItems, itemIndex, newItemIndex, stageFadingSpeed);
						
					})
										
							
					x = 1;		
					fadeTimer = function(){		
						fadeTimeFunction = setTimeout(function(){
							itemIndex = $singleSlideItems.index($singleSlideItems.filter(':visible'));						
							newItemIndex = (itemIndex+1)>=$singleSlideItems.length?0:(itemIndex+1);						
							changeSlideTeaser($singleSlideItems, itemIndex, newItemIndex, stageFadingSpeed);
							
							//console.log('Fade-Count: '+x);
						
						 	fadeTimer();
						}, 
						stageFadingTime
					)};
					fadeTimer();	
					
									
					$prevNavButton.mouseover(function(){		
						clearTimeout(fadeTimeFunction);
					});
					
					$nextNavButton.mouseover(function(){		
						clearTimeout(fadeTimeFunction);
					});	
					
					
				})}
			}
		
		
		
		
		$('#Banner').makeSlider('.ce_text', stageFadingSpeed);
			
	if(jQuery.browser.msie && jQuery.browser.version < 7){
				
				} else {
	

	var $overlayObj; 
	
	$('.slideTable').each(function(){	
	
		//$slideTableFrameObj = $(this);
	
		$(this).find('tr').click(function(){
			$(this).siblings('tr').removeClass('active');
			$(this).addClass('active');
		})
	 	
				
		$(this).find('tr').live('click', function(){
			$(this).siblings('tr').removeClass('active');
			$(this).addClass('active');
		})
	
	
		$(this).addClass('slideTableActive');
		

		$slideFrame = $(this).find('.slideItem');
		$slideTable = $(this).find('table');
		
		$slideFrame.before($leftSlideTableButton);
		$slideFrame.after($rightSlideTableButton);
		
		$(this).find('tr').click(function(){
			$(this).siblings('tr').removeClass('active');
			$(this).addClass('active');
		})
		//alert($slideTable.height()-2);
		

		
		$(this).children('.left, .right').click(function(){
		
			tablePosition = $slideTable.css('left').replace(/px/,"");
			
			if($(this).attr('class') == "right"){
				slideDistance = ($slideTable.width()-$slideFrame.width()+parseInt($slideTable.css('left').replace(/px/,"")) >= tableSlideDistance) ? tableSlideDistance : $slideTable.width()-$slideFrame.width()-2+parseInt($slideTable.css('left').replace(/px/,""));
			} else {
				slideDistance = (tableSlideDistance*(-1) >= tablePosition) ? tableSlideDistance*(-1) : parseInt(tablePosition) + 1;
			};
			

			$slideTable.animate({ 
				left: parseInt($slideTable.css('left').replace(/px/,""))-slideDistance +'px'
			}, tableSlideSpeed );
			
			$(this).blur();
			return false;
		}) 
		//$(this).find('.filters select').css('border', '4px solid red');
		
			
		$(this).setFrameHeight();
		
		//$('.slideTable').setFrameHeight();
	})
	
		
	if($('#csvListingFilterClean').exists()){
		
					
		/**
     * Table sorting stuff
     *
		 * Ref: http://www.datatables.net/plug-ins/sorting -> Natural sorting
		 */

		jQuery.fn.dataTableExt.oSort['string-asc'] = function(a,b){
			return naturalSort(a,b) * -1;
			};

    jQuery.fn.dataTableExt.oSort['string-desc'] = function(a,b){
			return naturalSort(a,b);
			};

		/**
		 * Initialize the data table
		 */
		$('#csvListingFilterTable').dataTable({
			//"bSearch": false,
			"bPaginate": false,
			"bLengthChange": false,
			"bFilter": false,
			"bSort": true,
			"bInfo": false,
			"bAutoWidth": false
			});
		
	}
	
	$("#csvListingFilterTable").tableFilter({
			clearFiltersControls: [$('#csvListingFilterClean')],
			filteredRows: function(){
				$('.mod_article .slideTable').setFrameHeight();					}
		});

	
		$('.mod_article .slideTable').setFrameHeight();	

	
	
	
	
$(".slide-table-button[rel]").each(function(){
	
		$overlayButtonObj = $(this);
			
		$(this).click(function(){		
					
			// Get Data Table and Wrap Overlay Container
			$overlayTable = $(this).closest('.slideTable');
			
			if($('#table1').exists()){
				$('#table1').append($overlayTable);
			} else {
				$overlayTable.wrap('<div class="simple_overlay" id="table1"></div>');
				$overlayObj = $overlayTable.closest('.simple_overlay').prependTo('body');
			}
																	
			$overlayObj.find('.filterButton').attr('id','csvListingFilterCleanOverlay')	
		
			
			$loadedOverlay = $(this).overlay({
				expose: { 
				    color: '#333', 
				    loadSpeed: 200, 
				    opacity: 0.9,	
				    maskId: 'exposeMask'			  
				},				
				onLoad: function(){	
									
					tableSize = this.getOverlay().find('#csvListingFilterTable').width();
					this.getOverlay().width(tableSize);
					$('#exposeMask').css('min-width', tableSize+60);
							
						
					// Center Table Buttons	
					tblNavPaddingLeft = (($('#table1').width() - 46 - ($('#csvListingFilterCleanOverlay').width())*2)/2)+'px'									
					//this.getOverlay().find('.csvListingFilterReset').css('padding-left', tblNavPaddingLeft );
					
					
					$newOverlay = this;
					$overlayButton = this.getOverlay().find('.overlay-button');
					
					printButtonCaption = $overlayButton.attr('lang') == "de" ? "drucken": "print" ;
								
					
					
					// Set Print Button in Table Navigation
					$printButton = this.getOverlay().find('#printButton').exists()?this.getOverlay().find('#printButton'):$('<a id="printButton" class="slide-table-button" href="#">'+printButtonCaption+'</a>');
					
					// Set Close Button
					scaleDownCaption = $overlayButton.attr('lang') == "de" ? "Tabelle verkleinern": "scale down table" ;								
					
					$closeButton = this.getOverlay().find('#closeButton').exists()?this.getOverlay().find('#printButton'):$('<a id="closeButton" class="slide-table-button" href="#">'+scaleDownCaption+'</a>').click(function(){
							$newOverlay.close();
							return false;
						
						});
					
					$printButton.unbind().click(function(){			
						$('body').addClass('big-table-print');
						window.print();					
						return false					
					})
					
					this.getOverlay().find('#csvListingFilterCleanOverlay').after($closeButton,$printButton);


							
				},
				onClose: function(){
					$('body').removeClass('big-table-print');
					$overlayContent = this.getOverlay().find('.slideTable');
					$overlayContent.find('#printButton').remove();
					$overlayContent.appendTo('#main > .inside .mod_article');
					
					//$('.csvListingFilterReset').css('padding-left', '108px')
					
					
					scaleUpCaption = $overlayButton.attr('lang') == "de" ? "Tabelle vergr&ouml;&szlig;ern": "scale up" ;
					
					$overlayTable.setFrameHeight();
					
					$closeButton.remove();
					
		
				},
				
				api: true
			})
			$loadedOverlay.load();			
			
			//}			
			
			return false;
		})
		

	})
		
	
	}	
	

});
