// JavaScript Document


// Start Kategorien Google Maps
function getSelectBox() {
  var catChecked = [];
  $("input:checkbox[ name='tx_csgomapsext_show[cat]']:checked").each(function(){
    catChecked.push('cat' + $(this).val());
  });
  $('.partnerlist').children().fadeOut('fast');
  for (var i=0; i<catChecked.length;i++) {
    $('.'+catChecked[i]).fadeIn('fast');
  }
}

$('input[type=checkbox]').click(function() {
  getSelectBox();
});
// Ende Kategorien Google Maps


var topline = $('#topline');
var subline = $('#subline');


// Accordion
//$(function() {
//  $( ".accordion" ).accordion({
//    active: true,
//    autoHeight: false,
//    collapsible: false,
//    alwaysOpen: true,
//    heightStyle: 'auto'

//  });
//});

// Seiten unterstreichen bei direktem Aufruf und wenn keine Subnav
function setUnderline () {
  $('.topnav > li').each(function(index) {
    if ($(this).hasClass('active') && $('.subnav').length == 0 ) {
      underline ( $(this), topline, 1 );  
    }
  });
  
  $('#subnav > li').each(function(index) {
    if ($(this).hasClass('active') ) {
      underline ( $(this),subline, 1, $(this).height() );
    }
  });
  
  $('#sectionnav > li').each(function(index) {
    if ($(this).hasClass('active') ) {
      underline ( $(this),subline, 1, $(this).height() );
    }
  });
  
}

function underline (object, line, speed, offset ) {
  if (object.length > 0) {
    switch (object.attr('id')) {
      case "sitemap":
        var padding = 6;
        break;
          case "contact":
        var padding = 6;
        break;
      case "search":
        var padding = 6;
        break;
      default:
        var padding = 18;
    }
    line.animate({
        'width': object.children().width(),
        'left': object.children().position().left + padding,
        'top': object.children().position().top + offset,
        'opacity':1
      },{
        duration:speed,
        easing: 'easeInOutQuad',
        queue:false
      })
  } else {

    line.animate({
      'width': 180,
      'left': $('#logo').position().left,
      'opacity':0
    },{
      duration: speed,
      easing: 'easeInOutQuad',
      queue:false
    })
  }  
}

// Bei mainnav MouseOver
function toogleProduct (mydiv) {
  if (mydiv.parent().hasClass('overview') == false) {
      mydiv.parent().children('div').toggleClass('hide');
  }
}

// Mainnav wenn ausserhalb oder auf einen Link geklickt wird
$(document).click(function(event) {
  var target = $(event.target);
  if ( !target.hasClass('dropdown') && target.parents('.dropdown').length == 0 ) {
    showDropDown(null);
  }
  if (target.parents().hasClass('L3') ) {
    showDropDown(null);  
  }
});

   
// Mainnav Slide UP / DOWN

var twiceClick = null;

$('.dropdown > a').click(function(event) {
  if (twiceClick != this.parentNode || $(this).parent().children().is(':hidden') ) {
    showDropDown( this.parentNode );
    event.preventDefault();
  } else {
    showDropDown( null );
    event.preventDefault();
  }  
  twiceClick = this.parentNode;
});

var showDropDown = function(object) {
  $('.dropdown').each(function(i, element) {
    var mainnav = $(element).children('.borderbottom'); //.mainnav
    if(!$(mainnav).is(':hidden') ) {
      $(mainnav).css('z-index', 1000 );
      $(mainnav).stop(true, true).slideUp(500);
    }
  });
  if(object != null ) {
    $(object).children('.borderbottom').css('z-index', 2000 );
    $(object).children('.borderbottom').stop(true, true).slideDown(500);
  }
}
  
    
    
    
jQuery(function($) {
  $(document).ready(function() {
    
    // remove empty li
    //$('ul li').filter(function() {return $(this).text() == '';}).remove();
    $('ul li:empty').remove();

    // START SectionScroll //    
    $('.slide').parent().parent().css( "height", "450" );
    
    
    // START SectionScroll //    
    if ($('.section').length > 0) {

      $('<div id="scroller"><ul><li id="totop">top</li><li id="prev">prev</li><li id="next">next</li><li id="tobottom">bottom</li></ul></div>').appendTo("body"); //append the Next arrow div to the bottom of the document
      
      // Scroller ein und ausblenden
      $(window).scroll(function () {
        if ($(window).scrollTop() > 100) {
          $('#scroller').fadeIn();
        } else {
      $('#scroller').fadeOut();
        }
      });
      
      // Ganz nach oben scrollen
      $('#totop').click(function(e){
        e.preventDefault();
        $.scrollTo($('.outerwrap'), 1000, 'easeInOutQuart');
      });
      
      // Ganz nach unten scrollen
      $('#tobottom').click(function(e){
        e.preventDefault();
        $.scrollTo( $('.content').height(), 1000, 'easeInOutQuart');
      });
      
      // Scrolle zur nächsten section
      $('#next').click(function(e){
        e.preventDefault();
        scrollTop = $(window).scrollTop();
        $('.content > div > .section').each(function(i, div){ // loop through article headings
          divtop = $(div).offset().top; // get article heading top
          if (scrollTop < divtop - 40 - $(this).css('marginTop').replace("px", "") ) { // compare if document is below heading
            newScroll = divtop - 39 - $(this).css('marginTop').replace("px", "");
            $.scrollTo(newScroll, 1000, 'easeInOutQuart'); // scroll to in .8 of a second
            return false; // exit function
          }
        });
      });
      
      // Scrolle zur vorherigen section
      jQuery.fn.reverse = function() {
        return this.pushStack(this.get().reverse(), arguments);
      };
      
      $('#prev').click(function(e){
        e.preventDefault();
        scrollTop = $(window).scrollTop();
        $('.content > div > .section').reverse().each(function(i, div){ // loop through article headings
          divtop = $(div).offset().top; // get article heading top
          if (scrollTop > divtop + 40 ) { // compare if document is below heading
            newScroll = divtop - 39 - $(this).css('marginTop').replace("px", "");
            $.scrollTo(newScroll, 1000, 'easeInOutQuart'); // scroll to in .8 of a second
            return false; // exit function
          }
        });
      });
      
    }
    // ENDE SectionScroll //
    
   
    // Accordion Renewed
    $('.accordion h3').click(function() {
      $(this).next().slideToggle('slow');
      $(this).toggleClass('state-active');
      return false;
    }).next().hide();
    
    $('.accordion h3').mouseover(function() {
      $(this).addClass('state-hover');
    })
      
    $('.accordion h3').mouseleave(function() {
      $(this).removeClass('state-hover');
    })
      
    // Accordion Renewed h5
    $('.accordion h5').click(function() {
      $(this).next().slideToggle('slow');
      $(this).toggleClass('state-active');
      return false;
    }).next().hide();
    
    $('.accordion h5').mouseover(function() {
      $(this).addClass('state-hover');
    })
      
    $('.accordion h5').mouseleave(function() {
      $(this).removeClass('state-hover');
    })  
      
    // Accordion Downloads H3
      
    $('.downloads h3').click(function() {
      $(this).parent().next().slideToggle('slow');
      $(this).toggleClass('state-active');
      return false;
    }).parent().next().hide();
    
    $('.downloads h3').mouseover(function() {
      $(this).addClass('state-hover');
    })
      
    $('.downloads h3').mouseleave(function() {
      $(this).removeClass('state-hover');
    })
     
     // Accordion Downloads H5
      
    $('.downloads h5').click(function() {
      $(this).parent().next().slideToggle('slow');
      $(this).toggleClass('state-active');
      return false;
    }).parent().next().hide();
    
    $('.downloads h5').mouseover(function() {
      $(this).addClass('state-hover');
    })
      
    $('.downloads h5').mouseleave(function() {
      $(this).removeClass('state-hover');
    }) 
      
         
    
    // Gradient bei mouse over ausblenden
    $('div.gradient').mouseenter(function() {
      if ($(this).find('a').length == 0 ) {
      
        if ( $(this).not('hidden') ) {
          $(this).fadeOut('slow');
          $(this).addClass('hidden');
        }
      }
    });
      
    
    $('div.gradient').parent().mouseleave(function() {
      if ( $(this).children().hasClass('hidden') ) {
        $(this).children().fadeIn('slow');
        $(this).children().removeClass('hidden');
      }
    })      
      
      
    // Pseudoklassen für IE umwandeln
    $('.content .csc-menu-1 li:nth-child(odd)').addClass('odd');
    
    // Fancybox
    $('a.grouped_elements').fancybox({
      'titlePosition': 'inside',
      'overlayColor': '#000',
      'overlayOpacity': '0.9',
      'hideOnContentClick': 'true',
      'speedIn': '1000',
      'speedOut': '1000',
      'transitionIn': 'elastic',
      'transitionOut': 'none',
      'overlayShow': true,
      'width': 140,
      'height': 50,
      'autoDimensions': false,
      'autoScale': true
    });
    
    if($("#slideContentWrap div").length > 1) {
      $("#slideContentWrap").innerfade({
        animationtype: 'fade',
        speed: 2000,
        timeout: 6500,
        type: 'sequence',
        containerheight: '280px'
      });
    }  
  
    // slider imagecylce FCE
    $('.fade').each(function(i) {
      $(this).after('<div class="pager'+i+' tx-imagecycle-pi1-pager"></div>').cycle({
        pager:   '.pager' + i,
        fx:      'fade',
        width:   '100%',
        height:  '450px',
        fit:     1,
        timeout: 6500,
        speed:   2000,
        easing:  'easeInOutQuart',
        delay:   0,
        requeueTimeout: 250
      });  
    });
  
    // fullscreen FCE
    $(".slider").AnySlider({
      bullets: true,
      interval: 0,
      showControls: false,
      animation: 'fade',
      speed: 1000,
      responsive:true,
      touch:false,
      delay:0
      //afterChange: function () { 
      //  $.fn.column();
      //}
    });
    
    
    
    // Mouseover bei mainnav
    $('ul.L3 > li > a')
      .mouseover(function() {
        toogleProduct ($(this));
      })
      .mouseleave(function() {
        toogleProduct ($(this));
      })
      
      // Topnav und Subnav Unterstrich bei direktem seitenaufruf
      setUnderline();
  
    // Gelbe Linie beim resize setzen
    $(window).resize(function() {
      setUnderline();
    });
    
    // Topnav Unterstreichen
    $('.topnav > li').not( '#logo')
      .mouseover(function() {
        var active = 0;
        underline( $(this), topline, 200 );
      })
      // Topnav Unterstrich ausblenden wenn kein subnav ansonsten auf active setzen  
      .mouseleave(function() {
    if ( $('.subnav').length != 0 ) {
      var active = 0;
    } else {
      var active = $(this).parent().find('li.active');
    }
        underline(active, topline, 200 );
      })
      
      // Subnav Unterstreichen
      $('#subnav > li')
        .mouseover(function() {
          var active = null;
          underline( $(this), subline, 200, $(this).height() ); 
        })
        .mouseleave(function() {
          var active = $(this).parent().find('li.active');
          underline(active, subline, 200, $(this).height() );
        })
        
        // Sectionnav Unterstreichen
        $('#sectionnav > li')
          .mouseover(function() {
            var active = null;
            underline( $(this), subline, 200, $(this).height() );
          })
          .mouseleave(function() {
            var active = $(this).parent().find('li.active');
            underline(active, subline, 200, $(this).height() );
         })    
         
         // Sticky Subnav
         if ($('#subnavwrap').length > 0){
           $('#subnavwrap').stickUp();
           // Scroll TO Anchor
           $("ul#sectionnav > li > a").click(function(event) {
           event.preventDefault();  // <---- This is the magic stuff
             var ID = $(this).attr('href');
             var DelimiterPos = ID.search('#');
             var ANCHOR = ID.substr(DelimiterPos);
             $('html, body').animate({
               scrollTop: $(ANCHOR).offset().top - $('#subnavwrap').height() - $(ANCHOR).css('marginTop').replace("px", "")
             }, 1000, 'easeInOutQuart');
           });
           
           
           // Add and remove Class on Subnav
           $(window).scroll(function() {
             var windscroll = $(window).scrollTop();
             if ( windscroll > 80 && $('#sectionnav').length != 0 ) {
               $('.section').each(function(i) {
                 if ($(this).position().top <= windscroll + 50) {
                   $('ul#sectionnav li.active').removeClass('active');
                   $('ul#sectionnav li').eq(i).addClass('active');
                   underline($('ul#sectionnav li').eq(i), subline ,200, $('ul#sectionnav li').eq(i).height() );
                 }
               });
             } else {
               $('ul#sectionnav li.active').removeClass('active');
               $('ul#sectionnav li:first').addClass('active');
             }
           }).scroll();   
        }
  });
});