/**
 * Funktionsbibliothek für FUF - Portal
 * @author Stefan Bogda
 */


// Funktionen
        function resize(){
                winwidth = parseInt($('body').innerWidth());
                winheight = parseInt($('body').innerHeight());
        }


     // Teaser Init
        function initTeaser(){
            //console.log('Teaser Inititalisiert')
        }
     // Teaser Init
        function initButton(){
            var buttonFirst = $('ul#top li a:first');
            buttonFirst.animate({backgroundColor:'#C1D8E5', backgroundPosition:'50% 0px'},700);

        }
	
     // Start Teaser
        function fcTeaser(){
	   $('#teaser .teaser-wrapper:first').show();
            var i = $('#teaser .teaser-wrapper').length;
            if(i > 1){ // wenn mehr als ein Bild vorhanden ist
            slideInterval = window.setInterval(function(){fcSlideAuto()},6000);
           }
        }
       function fcSlideAuto(){
            var tActive = $('#teaser .teaser-wrapper:visible').attr('id').split('-')[1];
            if(($('#teaser .teaser-wrapper').length) == tActive){
			var tNext = 1;}
		else{
			var tNext = parseInt(tActive)+1;}
		fcAni(tActive,tNext);
	}
        function fcAni(tActive,tNext){
                var teaserActive =  $('#teaser-'+tActive + ' .teaser-text');
                var teaserNext   =  $('#teaser-'+tNext + ' .teaser-text');

                var wActive = 800;
                var wNext   = wActive;

                teaserActive.animate({left:'-'+wActive},700);
                teaserNext.css({'left':'-'+wNext+ 'px'}).delay(1000).animate({left:0},700);

                var a = $('#teaser-'+tActive).css({'z-index':'5'});
                var b = $('#teaser-'+tNext + ' .teaser-images > *').hide();
                var c = $('#teaser-'+tNext).css({'z-index':'10'}).show();

                $.when(a,b,c).then(function(){
                    var buttonNr = tNext - 1;
                    var buttonBefore = $('ul#top li a');
                    var buttonReset = buttonBefore.animate({backgroundColor:'#ccc', backgroundPosition:'50% -20px'},700);
                    $.when(buttonReset).then(function(){
                        if((tNext)<=4){
                            var button = $('ul#top li a:eq('+buttonNr+')');
                            button.animate({backgroundColor:'#C1D8E5', backgroundPosition:'50% 0px'},700);}
                        })
                  fcImageChange(tNext, tActive);
                  
                });
        }
     // END Teaser
        function fcImageChange(tNext, tActive){       
            var teaserImg = $('#teaser-'+tNext+' .teaser-images > *').length; // Anzahl der Bilder
            for(i=0;i<teaserImg+1;i++){
                var speed = GetRandom(1500,3000);
                $('#teaser-'+tNext+' .teaser-images img:eq('+i+')').fadeIn(speed);}
            $('#teaser-'+tActive).fadeOut(1000);

            }

        function GetRandom( min, max ) {
            if( min > max ) {
                    return( -1 );
            }
            if( min == max ) {
                    return( min );
            }

            return( min + parseInt( Math.random() * ( max-min+1 ) ) );
        }

     // Start Navigation Hinweis
        function showInactiveMsg(posX, posY, text){
            $('#main-wrapper').fadeTo(400,'0.3');
            $('#hinweis').empty().append('<p>'+text.split(' ')[0]+'</p>')
            $('#hinweis').animate({left: posX + 'px', top: posY + 'px'},400).fadeIn(400);
        }

        function hideInactiveMsg(){
            $('#main-wrapper').fadeTo(300,'1.0');
            $('#hinweis').fadeOut(200);
        }

     // END Navigation Hinweis
     function language(){
         $('#language ul').toggle();
         if($('#language ul').is(':visible')){
            $('#language a').addClass('active');}
        else{
            $('#language a').removeClass('active');}
     }
// Portrait Funktionen

       function changeDetail(e){
                var name = e.split('-');
                var type = name[0];
                var toShow = name[1];
                var target = $('#'+type);

                // Linkbehandlung
                target.removeClass('all');
                $('#'+type+' .c-nav a').removeClass('active');
                $('#'+type+' .c-nav a[name='+e+']').addClass('active');
                // richtigen Content anzeigen
                var minHeight = $('#'+type+' .headline').innerHeight() + $('#'+type+' .c-nav').innerHeight()+40;

               if(toShow=='showall'){ // Zeige alle
                   var a1 = $('#'+type+' .'+type+'-detail').fadeOut(300);
                   $.when(a1).then(function(){
                       if(type == 'jobs'){
                           var readMore = $('#jobs-content .read-more');
                           readMore.hide();};
                       var showDetail = $('.'+type+'-detail');
                       var showCount  = showDetail.length;
                       var showHeight = 0;
                       for(i=0;i<showCount;i++){
                           var showHeight = showHeight + showDetail.eq(i).innerHeight() + 40;}
                       if(showHeight<minHeight){
                           var showHeight = minHeight;}
                       target.addClass('all');
                       var a2 = $('#'+type).delay(200).animate({height:showHeight+'px'},400);
                       $.when(a2).then(function(){
                            showDetail.fadeIn(300);});
                   });
               }
               else{
                   var a1 = $('#'+type+' .'+type+'-detail').fadeOut(300);
                   $.when(a1).then(function(){
                       if(type == 'jobs'){
                           var readMore = $('#jobs-content #'+e+' .read-more');
                           readMore.show();
                           var height = $('#'+e).innerHeight() + $('#'+type+' h2:first').innerHeight() + 60;}
                       else{
                           var height = $('#'+e).innerHeight() + $('#'+type+' h2:first').innerHeight() + 60;}
                       if(height<minHeight) {var height=minHeight;}
                       var a2 = $('#'+type).delay(200).animate({height:height+'px'},400);
                       $.when(a2).then(function(){
                            $('#'+e).fadeIn(300);});
                   })      
               }
      }

       
       function sizetheportrait(){
           $('#portrait .c-nav a:first').trigger('click');
       }

       function buildnewsnav(){
       }

        


      // IE FIX für die Navigation unter dem Teaser
           function settop(){
                $('ul#top li:first').css({'margin-left':'0px'});
                $('ul#top li:last').css({'margin-right':'0px'});
       }

  $(document).ready(function()
  {
        

        // Die Navigation für den IE erzwingen
        // Pulsieren der Buttons auf der Startseite
        // Hover
        
        
        // Animation des Teasers
        var a = initTeaser();
        var b = initButton();
        var c = fcTeaser();
        
        $.when(c).then(function(){settop()})

        // Schulungscontainer ausblenden
        $('.schulung-read-more').hide();
 

        
  });
