var siteHeight = 595;
var boxPadding = 15;
var interval;

function debug(str) {
	if(!str) {
		return (location.search == '?debug=true');
	} else {
		if(location.search == '?debug=true') {
			alert(str);		
		}
	}
}

function init(){
   // Standorte opaque setzen
   //$('img.inactive').fadeOut(5000);
   $('img.inactive').css('opacity',0.5);
   $('img.inactive').hover(function () { 
      $(this).css('opacity',1);
   },
   function () {
      $(this).css('opacity',0.5);
   });
	
	if($('#container').length > 0) $('#box p').addClass('breit');
	// Containerhöhen anpassen
	if($('.slideshow').length > 1) initSlideshow();
	else $('#slideshow .slideshow').css('position','static');	
	$('#slideshow').length > 0 ? slideshowHeight = $('#slideshow').outerHeight() : slideshowHeight =  0;
	$('#container').length > 0 ? containerHeight = $('#container').outerHeight() : containerHeight = 0;	

}

function startJScrollPane() {

	$('#box').height(siteHeight - slideshowHeight - containerHeight - boxPadding); 	
	$('#box').jScrollPane();
	
	if($('.jScrollPaneTrack').length > 0){
		$('#box').width($('#box').width() + 15);
	    $('.jScrollPaneTrack').height($('.jScrollPaneTrack').height() - 10);
	}else{
		$('#box').height($('#box').height() - boxPadding); 	
	}

}

function initKarriere() {
	$('.tx-karriere-pi1 h1').click(function() {
    $(this).next().toggle('slide');
    $(this).toggleClass('active');
    Cufon.now();
		return false;
	}).next().hide();
	$('.tx-karriere-pi1 h2').click(function() {
		$(this).next().toggle('slide');
		$(this).toggleClass('active');
		return false;
	}).next().hide();	
}

function initSlideshow(){				 	
	height = $('#slideshow .slideshow:gt(0)').height();
	$('#slideshow .slideshow:gt(0)').hide();
	$('#slideshow').height(height);
	
	$('#slideshow').prepend('<div id="slidenavi"></div>');
	$('#slideshow .slideshow').each(function(i){
		$('#slidenavi').append('<div>'+(i+1)+'</div>');			 
	});

	// Listener
	$('#slidenavi div').eq(0).addClass('active');
	$('#slideshow .slideshow').eq(0).addClass('showing');		
	
	$('#slidenavi div').click(function(){ 
		index = $(this).html()-1;
		clearInterval(interval);
		interval = undefined;
		if(index != getActiveIndex()) fadeImages(index); 
	});
	
	$('#slidenavi').fadeIn(2000);
	automaticSlideshow();
}

function getActiveIndex(){
	return $('.slideshow').index($('.showing'));
}

function getNextIndex(){
	if(getActiveIndex() < $('#slideshow .slideshow').length - 1){
		return getActiveIndex() + 1;
	}
	else return 0;
}

function fadeImages(i){
	if(i==undefined) i= getNextIndex();
	$('#slideshow .slideshow').eq(i).show();
	$('#slideshow .showing').fadeOut(500,function(){											  
		$('#slideshow .slideshow').removeClass('showing').eq(i).addClass('showing');
		if(interval==undefined) automaticSlideshow();
	});
	$('#slidenavi div').removeClass('active').eq(i).addClass('active');
}

function automaticSlideshow(){
	interval = setInterval("fadeImages()",7000);
}
 
$(init);