
// JQuery No Conflict Mode aktivieren
var $j = jQuery.noConflict();
	
$j(document).ready(function() {
	
	var lockAccordeons = false;

	
	/* SCROLL TO TOP */
	//$j('html, body').animate({scrollTop:0}, 1);
	
	/* ACCORDION */
	$j(".accordion").accordion({ 
		"handle":           "h2",
        "panel":            ".akkordeonInhalt",
        "speed":            300,
        "easing":           "swing",
        "accordion":        true,
        "toggle":           true,
        "activeClassPanel": "open",
        "activeClassLi":    "active",
        "lockedClass":      "locked",
		"loadingClass": 	"loading"
    });

	
	/* FANCYBOX GALLERY */
	$j(".fancylink").fancybox({
		'hideOnContentClick': false,
		'hideOnOverlayClick': false,
	 	'height'			: 520,
		'width'				: 740,
		'autoDimensions'	: false,
		'padding'			: 0,
		'margin'			: 0,
		'overlayOpacity'	: 0.8,
		'overlayColor'		: '#444'
	});
	
	$j(".fancybox").fancybox();

	
	// Make the Accordion Linkable with Anchors 
	var incomingUrl = document.location.toString();
	if (incomingUrl.match('#')) { // the URL contains an anchor
		//click the navigation item corresponding to the anchor
	 	var incomingAnchor = '#' + incomingUrl.split('#')[1];
		if(incomingUrl.split('#')[1] != '') {
			$j('.accordion li' + incomingAnchor + ' a.accordion-opener').click();
	 	} 
	}


	/* When Hash changes and you're still on the same page
	   Needs observeHasChange.js (just loaded, nthn else) */
	jQuery(window).hashchange(function() {

	    if(lockAccordeons == false) {	
	    	var incomingUrl = document.location.toString();    	
			if (incomingUrl.match('#')) { // the URL contains an anchor
				//click the navigation item corresponding to the anchor
		 		var incomingAnchor = '#' + incomingUrl.split('#')[1];
			 	if(incomingUrl.split('#')[1] != '') {
					$j('.accordion li' + incomingAnchor + ' a.accordion-opener').click();
			 	} else if($j('.accordion li#PID3 a.accordion-opener').length != 0) {
			 		$j('.accordion li#PID3 a.accordion-opener').click();
			 	} else if($j('.accordion li#PID4 a.accordion-opener').length != 0) {		 		
			 		$j('.accordion li#PID4 a.accordion-opener').click();
			 	}			 		
			} else {
				if($j('.accordion li#PID3 a.accordion-opener')) {
			 		$j('.accordion li#PID3 a.accordion-opener').click();
			 	} else if($j('.accordion li#PID4 a.accordion-opener')) {
			 		$j('.accordion li#PID4 a.accordion-opener').click();
			 	}				
			}
	    }
	    lockAccordeons = false;
	 }); 
	
	
	// Fade Out the Dropdown after klick in the dropdow
	$j('#navi li ul.dropdown a').click(function() {
		$j('li .dropdown').fadeOut(100);	
	});
	
	// Show Dropdown after it has been faded out by jQuery
	$j('#navi li.PID3').hover(
	function(){
		$j('li.PID3 .dropdown').show();
		},
	function(){	
		$j('li.PID3 .dropdown').fadeOut(100);
	});
	
	
	$j('#navi li.PID4').hover(
	function(){
		$j('li.PID4 .dropdown').show();
		},
	function(){	
		$j('li.PID4 .dropdown').fadeOut(100);
	});
	
	// Prevent Page reload if Main Accordion needs to be opened on active Page
	$j('#seite-3 li.PID3 a#PID3').click(function(e) {
		e.preventDefault();
		$j('.accordion li#PID3 a.accordion-opener').click();
	});
	$j('#seite-4 li.PID4 a#PID4').click(function(e) {
		e.preventDefault();
		$j('.accordion li#PID4 a.accordion-opener').click();
	});
	
	
/*	$j("#seite-2 .dropdown a, #seite-5 .dropdown a, #seite-20 .dropdown a, #seite-21 .dropdown a, #seite-37 .dropdown a, #seite-38 .dropdown a, #seite-4 li.PID3 .dropdown a, #seite-3 li.PID4 .dropdown a").click(function (e) {
	    // e == our event data
	    e.preventDefault();
	    var incomingUrl = $j(this).attr('href').toString();
	 	var incomingAnchor = incomingUrl.substring(0,incomingUrl.length-5);
		// Determine to which Page the Submenu Belongs
		var parentPage = $j(this).parent().parent().siblings('a').attr('href').toString();
		window.location = parentPage + '#PID' + incomingAnchor;
	}); */
	


	/* Verhindert ein Neuladen der Seite und Öffnet das entsprechende Akkordeon bei benutzung des Dropdown für eine Unterseite der aktuellen Seite */	
	$j("#seite-3 li.PID3 .dropdown a, #seite-4 li.PID4 .dropdown a").click(function (e) {
		// e == our event data
	    e.preventDefault();
	 	var myClass = $j(this).attr("class");
	 	$j('ul.accordion li#' + myClass + ' a.accordion-opener').click();
	 	// Link des Untermenüpunktes wird in die Adresszeile eingetragen und scrollen verhindert
	 	var scrollmem = $j('body').scrollTop();
		window.location.hash = myClass;		
		$j('html,body').scrollTop(scrollmem);
		
		lockAccordeons = true; //the on haschange does not open any accordeon if this is true
				
	});

	// Link des Untermenüpunktes wird in die Adresszeile eingetragen und scrollen verhindern
	$j(".accordion-opener").click(function() {
		var myClassLong = $j(this).attr("class").toString();
	 	var myClass = myClassLong.substring(17);		
	 	var scrollmem = $j('body').scrollTop();
		if(myClass != "PID3" && myClass != "PID4") {
			window.location.hash = myClass;		
			$j('html,body').scrollTop(scrollmem);
		} else {
			window.location.hash = "";		
		}
		
		lockAccordeons = true; //the on haschange does not open any accordeon if this is true
		
	});

	
	/* Startseiten Pimping */
	$j('#slideshow, .inhalt').hover(function() {
		$j('.inhalt').stop().animate({bottom:'-5px'},{queue:false,duration:200});
		$j('#teaserHintergrund').stop().animate({bottom:'-10px'},{queue:false,duration:200});
	  }, function() {
		$j('.inhalt').stop().animate({bottom:'-115px'},{queue:false,duration:200});
		$j('#teaserHintergrund').stop().animate({bottom:'-120px'},{queue:false,duration:200});  
	});

$j('#PID3 a.accordion-opener').attr("href", "leistungen.html#");
$j('#navi a#PID3').attr("href", "leistungen.html#");

$j('#PID4 a.accordion-opener').attr("href", "unternehmen.html#");
$j('#navi a#PID4').attr("href", "unternehmen.html#");

});

