$(document).ready(function() {	
	
    $('#fade .csc-textpic-imagecolumn ul, #fade .csc-textpic-imagerow ul li').cycle({
		random: 1,
		fx: 'fade',
		timeout: 6000,
		speed: 3000
	});
	
  
  /*    Layermenü für Internet Explorer 6    */
    if(jQuery.browser.msie && (jQuery.browser.version == '6.0')) {
        $('.menu li').mouseenter(function() {
			$(this).find('.submenu').show();
		});
		$('.menu li').mouseleave(function() {
			$(this).find('.submenu').hide();
		});
    };	
	//Layermenü Klassen für ein bzw. ohne Untermenü hinzufügen 
	$('.menu li ul li:only-child').removeClass('li_sub_unten').addClass('onechild');
	$('#inhalt_links').find('.menu > li ').addClass('no_submenu'); 
	$('#inhalt_links').find('.menu > li > ul.submenu').parent().removeClass('no_submenu'); 
	

	//Klappmenü
	var timeout = 400;
	var timerref = 0;
	var menueeintrag = 0;
	
	function menue_timer() {
		timerref = window.setTimeout(menue_schliessen, timeout);
	}
	function menue_canceltimer() {
		if(timerref) {
			window.clearTimeout(timerref);
			timerref = null;
		}
	}
	function menue_oeffnen() {
		menue_canceltimer();
		menue_schliessen();
		if( $(this).hasClass('start') || $(this).hasClass('no_submenu')) {
		} else {
			menueeintrag = $(this).find('ul').eq(0).css('display', 'block');		
			hintergrund = $(this).css('background', 'url(fileadmin/bilder/bg_li_hover.gif) no-repeat left top');		
			breite = $(this).css('width', '105px');
			farbe = $(this).find('a').css('color', '#8F0011');
		}
	}
	
	function menue_schliessen() {
		if(menueeintrag) {
			menueeintrag.css('display','none');
			if($(menueeintrag).parent().hasClass('aktiv')) {
				hintergrund.css('background', 'url(fileadmin/bilder/bg_li_aktiv.gif) no-repeat left top');
			}else {
			hintergrund.css('background', 'url(fileadmin/bilder/bg_li.gif) no-repeat left top');	
			farbe.css('color', '#FF9842');
			}			
			breite.css('width', '100px');
			
		}
	}	

	$('.menu > li').bind('mouseover', menue_oeffnen);
	$('.menu > li').bind('mouseleave', menue_timer);
	//Ende Klappmenü
	
	
	
	
	
	
	//Verweis-Attribute in der Sitemap hinzufügen
	$("div.csc-sitemap > ul > li > a").addClass('first_sitemap_a');
	$("div.csc-sitemap > ul > li").addClass('first_sitemap_li');
	
	//Attribute für Hintergrund-div hinzufügen
	$("#images .items > div").addClass('wrap_image');
	
	
	//Tabs für Firmenphilosophie
	$("#flowtabs").tabs("#flowpanes > div", { 
     // enable "cross-fading" effect 
        effect: 'fade', 
		fadeInSpeed: "slow"
    });
	
	
	// thumbnail scrollable 
    $("#thumbnails").scrollable({size: 4, clickable: false}).find("img").each(function(index) { 
 
        // thumbnail images trigger the overlay 
        $(this).overlay({ 
 
           // effect: 'apple', 
            target: '#box', 
            expose: {maskId: 'mask'}, 
 
            /* 
                when box is opened, scroll to correct position (in 0 seconds) 
                the "images" variable is defined below 
            */ 
            onLoad: function() { 
                images.seekTo(index, 0); 
            } 
        }); 
    });
	
	// scrollable inside the box. Its API is assigned to the "images" variable 
    var images = $("div#images").scrollable({size: 1, api:true}); 
 
     
    // enable tooltips for the images 
    $("#images img").tooltip({ 
        position: "bottom center", 
        offset: [-75, -30], 
        opacity: 0.8, 
        effect: 'fade', 
 
        // position tooltips relative to the parent scrollable 
        relative: true 
    });
	
	// enable reflections 
    //$("#thumbnails img").reflect({height: 0.5, opacity: 0.6});
	
	
	//Prüft auf Browser-Version und schaltet für ie6 & ie7 das expose aus
	 var version = navigator.appVersion;
	 muster = new RegExp("MSIE 8.0");
	 if(version.search(muster) != -1) {
		version= "8.0";
	 }
	 var agent = navigator.appName;
	 if(agent=="Microsoft Internet Explorer" && version != "8.0") {
	 }else {
		//Expose öffnen, setzen und schließen
		var myexpose = $("#thumbnails .entries > div img").click(function() {
			myexpose = $("div#images").expose({api: true}).load();
		});
		$("#box .close").click(function() {
				//alert("myexpose ist: " + myexpose);
				myexpose.die();
		});
	}
	
	//Sliding Boxes
	 if($('#slider').length !=0) {
		var totalPanels			= $(".scrollContainer").children().size();
			
		var regWidth			= $(".panel").css("width");
		var regImgWidth			= $(".panel img").css("width");
		var regTitleSize		= $(".panel h2").css("font-size");
		var regParSize			= $(".panel p").css("font-size");
		
		if(jQuery.browser.msie && (jQuery.browser.version == '6.0')) {
			var movingDistance	    = 168;
		}else {
			var movingDistance	    = 164;
		}
		
		var curWidth			= 350;
		var curImgWidth			= 326;
		var curTitleSize		= "20px";
		var curParSize			= "15px";


		var $panels				= $('#slider .scrollContainer > div');
		var $container			= $('#slider .scrollContainer');

		$panels.css({'float' : 'left','position' : 'relative'});
	   
		$("#slider").data("currentlyMoving", false);
		
		$("#slider img.left").mouseenter(function(){
			$(this).attr({src: "/fileadmin/bilder/leftarrow_hover.png"});
		});
		$("#slider img.left").mouseleave(function(){
			$(this).attr({src: "/fileadmin/bilder/leftarrow.png"});
		});$("#slider img.right").mouseenter(function(){
			$(this).attr({src: "/fileadmin/bilder/rightarrow_hover.png"});
		});
		$("#slider img.right").mouseleave(function(){
			$(this).attr({src: "/fileadmin/bilder/rightarrow.png"});
		});

		$container
			.css('width', ($panels[0].offsetWidth * $panels.length) + 100 )
			.css('left', "-350px");

		var scroll = $('#slider .scroll').css('overflow', 'hidden');

		function returnToNormal(element) {
			$(element)
				.animate({ width: regWidth })
				.find("img")
				.animate({ width: regImgWidth })
				.end()
				.find("h2")
				.animate({ fontSize: regTitleSize })
				.end()
				.find("p")
				.animate({ fontSize: regParSize });
		};
		
		function growBigger(element) {
			$(element)
				.animate({ width: curWidth })
				.find("img")
				.animate({ width: curImgWidth })
				.end()
				.find("h2")
				.animate({ fontSize: curTitleSize })
				.end()
				.find("p")
				.animate({ fontSize: curParSize });
		}
		
		//direction true = right, false = left
		function change(direction) {
		   
			//if not at the first or last panel
			if((direction && !(curPanel < totalPanels)) || (!direction && (curPanel <= 1))) { return false; }	
			
			//if not currently moving
			if (($("#slider").data("currentlyMoving") == false)) {
				
				$("#slider").data("currentlyMoving", true);
				
				var next         = direction ? curPanel + 1 : curPanel - 1;
				var leftValue    = $(".scrollContainer").css("left");
				var movement	 = direction ? parseFloat(leftValue, 10) - movingDistance : parseFloat(leftValue, 10) + movingDistance;
			
				$(".scrollContainer")
					.stop()
					.animate({
						"left": movement
					}, function() {
						$("#slider").data("currentlyMoving", false);
					});
				
				returnToNormal("#panel_"+curPanel);
				growBigger("#panel_"+next);
				
				curPanel = next;
				
				//remove all previous bound functions
				$("#panel_"+(curPanel+1)).unbind();	
				
				//go forward
				$("#panel_"+(curPanel+1)).click(function(){ change(true); });
				
				//remove all previous bound functions															
				$("#panel_"+(curPanel-1)).unbind();
				
				//go back
				$("#panel_"+(curPanel-1)).click(function(){ change(false); }); 
				
				//remove all previous bound functions
				$("#panel_"+curPanel).unbind();
			}
		}
		
		// Set up "Current" panel and next and prev
		growBigger("#panel_3");	
		var curPanel = 3;
		
		$("#panel_"+(curPanel+1)).click(function(){ change(true); });
		$("#panel_"+(curPanel-1)).click(function(){ change(false); });
		
		//when the left/right arrows are clicked
		$(".right").click(function(){ change(true); });	
		$(".left").click(function(){ change(false); });
		
		$(window).keydown(function(event){
		  switch (event.keyCode) {
				case 13: //enter
					$(".right").click();
					break;
				case 32: //space
					$(".right").click();
					break;
			case 37: //left arrow
					$(".left").click();
					break;
				case 39: //right arrow
					$(".right").click();
					break;
		  }
		});
	}
	
	
});

