var active_flash_item = 0;
var anim_running = true;
var flash_slider_timeout;
var flash_slider_text_timeout;
var flash_slider_buttons_timeout;

var megaDropDownHtml = '';
var megaDropDownSpeed = 600;

var flash_parent_id;

function fillMegaDropDown () {
    
    $('#megaDropDown ul').html(megaDropDownHtml);
    $('#megaDropDown').slideDown(megaDropDownSpeed,"easeOutCubic");
    
}

function findaDocSwitcher ()   {

  $("#findadoc input").keypress( function (event){
    
     if (event.keyCode == '13') { // ENTER - Key pressed
       event.preventDefault;
       var value = $(this).val();
       if (value.match(/^\d{5}([\-]\d{4})?$/)) {
         $(".findAdentist input[name='tx_locator_pi1[city]']").attr("name","tx_locator_pi1[zipcode]");
         $("#findadoc").submit();
       }
       else {
         $("#findadoc").submit();
       }
     } 
    });
  
  return false;
}


function initMegaDropDown () {
    
    $('#topMenu ul li a').click(function(){
        
        $(this).blur();
        
        if ($(this).parent().attr('class').toLowerCase().indexOf('active') == -1) {
            
            $('#megaDropDown').slideUp(megaDropDownSpeed);
            
            $(this).parent().parent().children('li.active').removeClass('active');
            $(this).parent().addClass('active');
            megaDropDownHtml = $(this).parent().children('ul').html();
            window.setTimeout("fillMegaDropDown();", megaDropDownSpeed);

        } else {
            
            $('#megaDropDown').slideToggle(megaDropDownSpeed, "easeInOutCubic" );
            
        }
        
        return false;
    
    });
    
    $('#topMenu ul li ul li a, #topMenu ul li ul li ul li a').unbind('click');
    
}

function clearSearchField()  {
  $('#searchPattern').focus(function () {
    $(this).val('');
  });
}



function initTabs () {
    
    $('.tabMenu a').click(function(){
        
        $(this).blur();
        
        $(this).parent().parent().children('li.active').removeClass('active');
        $('.tabContent').hide();
        
        $(this).parent().addClass('active');
        $('#' + $(this).attr('rel')).show();
        
        return false;
        
    });
    
}



function initAnchorLinks () {
    
    $('.scrollTop').click(function(){
        
        // $(document).scrollTop(0);
      $('html').animate({scrollTop : 0},1000);
        return false;
        
    });
    
}



function resizeBackground () {
    
    $('.subhomeAdultsContentRightBackground').each(function(){
        var imageWidth = $(this).children('img').width();
        var contentHeight = $('.subhomeAdultsContentRightContent').height();
        $(this).children('img').css({height: (contentHeight + 40) + 'px', width: imageWidth + 'px'});
        $(this).css('visibility', 'visible');
    });
    
}


function setTeaserMenuContentHeight () {
    
    $('.teaserMenuMiddle').each(function(){
        
        var availableHeight = $(this).height();
        $(this).children('.teaserMenuContent').css('height', (availableHeight - 2) + 'px');
        
    });
    
}


function initTeaserMenu () {
    
    $('.teaserMenu ul li a').click(function(){
        
        $(this).blur();
        
        $(this).parent().parent().children('li.active').removeClass('active');
        $('.teaserMenuContent').hide();
        
        $(this).parent().addClass('active');
        $('#' + $(this).attr('rel')).show();
        
        setTeaserMenuContentHeight ();
        
        return false;
        
    });
    
}

function onBefore () {
//  $('.moodH1').text("at last...");
  //$('.moodH1').hide();
  //$('.moodH1').fadeIn('4000');
}

function onAfter () {
  
}

function keyVisualSliding( element, transType, easer, duration ) { // jQuery Sliding fuer die Keyvisuals der Startseite
  if(typeof($(element).cycle) == 'function'){

    $(element).cycle({
      fx: transType, // choose your transition type, ex: fade, scrollUp, shuffle, etc...
      easing: easer ,
      speed: duration,
      timeout: 6800,
      delay: -2500,
      after: onAfter,
      before: onBefore
    });
  }
}

function hideNonUsedElements()  { // jQuery entfernt leere Tags
    
    $("li:empty").remove();
    $("#content a:empty").remove(); // damit der Close-Button in der Lightbox nicht entfernt wird
    $("p:empty").remove();
  
}

function flash_slider(){
  $('#flash_1').show();

  loop_flash();
  $('.flash_slide').each(function(){
    $(this).click(function(){
      var dir = 'left';
      if($(this).hasClass('right')){
        dir = 'right';
      }
      slide_flash(dir);
      
    });
  });
}

function loop_flash(){
  if(anim_running){
    slide_flash('right');
  }
}


function slide_flash(dir){
  $('.teaser_home').hide();
  $('.home_change_content').hide();
  clearTimeout(flash_slider_timeout);
  clearTimeout(flash_slider_text_timeout);
  clearTimeout(flash_slider_buttons_timeout);
  

  var flashes_count = $('#'+flash_parent_id).children().length -1;

  if(dir == 'right'){
    active_flash_item = active_flash_item + 1;
  }else{
    active_flash_item = active_flash_item - 1;
  }
  if(active_flash_item > flashes_count){
    active_flash_item = 1;
  }
  if(active_flash_item < 1){
    active_flash_item = flashes_count;
  }

  var new_timeout = 11000;
  var timeout_text = 4000;
  var timeout_buttons = 4000;
  if(typeof(flash_durations) != 'undefined'){
    if(typeof(flash_durations[active_flash_item]) != 'undefined'){
      if( flash_durations[active_flash_item] > 0){
        new_timeout = flash_durations[active_flash_item];
      }
    }
  }
  
  if(typeof(flash_text_durations) != 'undefined'){
    if(typeof(flash_text_durations[active_flash_item]) != 'undefined'){
      if( flash_text_durations[active_flash_item] > 0){
        timeout_text = flash_text_durations[active_flash_item];
      }
    }
  }
  
  if(typeof(flash_buttons_durations) != 'undefined'){
    if(typeof(flash_buttons_durations[active_flash_item]) != 'undefined'){
      if( flash_buttons_durations[active_flash_item] > 0){
        timeout_buttons = flash_buttons_durations[active_flash_item];
      }
    }
  }



  flash_slider_timeout = setTimeout('loop_flash()', new_timeout);
  flash_slider_text_timeout = setTimeout('showText()', timeout_text);
  flash_slider_buttons_timeout = setTimeout('showButtons()', timeout_buttons);


  $('#'+flash_parent_id).children().fadeOut('slow',function(){
    setTimeout('show_next_flash()',300);
  });
  // $('#'+flash_parent_id).children('.home_change_content').show();
      
}
function show_next_flash(){
  $('#'+flash_parent_id).children().hide();
  $('#flash_'+active_flash_item).show();
}

function showText() {
  $('.teaser_home').fadeIn(600);
}

function showButtons() {
  $('.home_change_content').show();
}

function fadeFlash() {
  $('#headerWrapper').fadeOut(600);
}


function hide_empty_links(){
  $('a').each(function(){
    if($(this).html().length < 1){
      $(this).hide();
    }
  });
}

function hide_empty_imgTags(){
  $('img.infotextImage').each(function(){
  var imgSrc = $(this).attr('src');
    if(imgSrc.length < 1){
      $(this).hide();
    }
  });
}

function margin_last_download(){
  $('.downloads :last-child').css('margin-bottom','20px');
}

function accordionF2() {
  $('div.csc-frame-frame2').each(function(){
    $(this).find('div[class*=csc-textpic-text]').hide();
    $(this).css('cursor','pointer');
    $(this).click(function(){
      $('div.csc-frame-frame2').not($(this)).find('div[class*=csc-textpic-text]').hide(600);
      $('div.csc-frame-frame2').not($(this)).removeClass('active');
      $(this).find('div[class*=csc-textpic-text]').slideToggle(400);
      
      if ($(this).hasClass('active')) {
        $(this).removeClass('active');
      } else {
        $(this).addClass('active');
      }
  
    });
  });
}


$(document).ready(function() {

  flash_parent_id = $('.home_change_content').parent().attr('id');

  
  $('.ebene0-1.arrow.act').parent().addClass('active');
  //$('.ebene0-1.act').css('color','#ff0000');
  
  flash_slider();

  hide_empty_imgTags();

  margin_last_download();

  hide_empty_links();
  
  clearSearchField();

    initMegaDropDown ();

    initTabs ();

    initAnchorLinks ();

    window.setTimeout("resizeBackground ();", 50);

    initTeaserMenu ();

    setTeaserMenuContentHeight ();

    keyVisualSliding(".keyVisualImage", "fade","","2400" );

    keyVisualSliding(".moodImageImage", "fade","","2400" );

    findaDocSwitcher();

    hideNonUsedElements();

    accordionF2();
  
  $('.news-latest-container').cycle({ 
      fx:    'scrollLeft', 
      speedIn: 2100,
      speedOut: 900,
      easeIn:  'easeOutBounce', 
      easeOut: 'easeOutQuad', 
      delay:   -2000,
      height: '210px',
      containerResize: 0,
      timeout: 20000,
      fit: 0
   });

});





