/* Browser-Detect */
   var isIE6;
   var isSafari;
  
   var is_chrome = /chrome/.test( navigator.userAgent.toLowerCase() ); 
   
   if($.browser.msie && $.browser.version == '6.0') isIE6 = true;
   if($.browser.safari && !is_chrome) isSafari = true;
/* Browser-Detect END */
   
   
   
$(document).ready(function(){


/*    $('#outer').scroll(function(){
        $(this)[0].scrollLeft = 0;
    });*/
    document.getElementById('outer').onscroll = function() {
        this.scrollLeft = 0;
    }
   
   setContentHeight();
   
  /* Klapp-Navigation im Header */
  $('ul.header-navi li.uigruppe, ul.header-navi li.service, ul.header-navi li.newsletter').mouseover(function(){
  
  $('#header .header-box').hide();
  $('ul.header-navi li').removeClass('border').removeClass('open');      
  
    $(this).addClass('open');
    
    if ($(this).hasClass('uigruppe')) {
      var box = 'ul.uigruppe-box';
      var list = 'ul.header-navi li.uigruppe';
    }
    else if ($(this).hasClass('service')) {
      var box = 'ul.service-box';
      var list = 'ul.header-navi li.service';
    }
  else if ($(this).hasClass('newsletter')) {
      var box = 'div#header div.newsletter-box';
      var list = 'ul.header-navi li.newsletter';
    
    $(this).addClass('border');       
    $('div#header div.newsletter-box div.boder-abdecker').width($(this).width() + 23);    
    
    }
    
    $(box).css({left: $(this).position().left+2, display: 'block'});  

    $(document).mousemove(function(e){
      if(
          (
          e.clientX < $(list).offset().left &&
          e.clientX < $(box).offset().left
          )
        ||
          (
          e.clientX > ($(list).offset().left + $(list).outerWidth(true))  &&
          e.clientX > ($(box).offset().left + $(box).outerWidth(true))
          )
        ||
          (
          e.clientY > ($(box).offset().top + $(box).outerHeight(true))
          )

      ) {
        $(document).unbind('mousemove');
        $(box).css('display', 'none');
        $(list).removeClass('open');
    $(list).removeClass('border');    

      }
    });
    
  });

  /* Toggle-Boxen */
  $('div.toggle-box div.Bheader').click(function() {
    if ($(this).parents('div.toggle-box').hasClass('Bopen')) {
      var $slide_elem = $(this).next('div.BcontentC');
      if (!$slide_elem.queue().length) {
        $(this).parents('div.toggle-box').removeClass('Bopen').addClass('Bclosed');
        $slide_elem.data('auto_height', $slide_elem.height()).animate({height:1}, 500, 'swing', function() {
          $(this).css({display: 'none'});
      
      setContentHeight();
        });
      }
    } else {
      var $slide_elem = $(this).next('div.BcontentC');
      if (!$slide_elem.queue().length) {          
      
        $(this).parents('div.toggle-box').removeClass('Bclosed').addClass('Bopen');
        $slide_elem.animate({height:$(this).next('div.BcontentC').data('auto_height')}, 500, 'swing', function() {
          $(this).css({display: 'block'});
      
      setContentHeight();
        });
      }
    }
  });
  
  
  /* setzt die Höhe von 'div#contentC div.content' (nur für IE6) */
  
  function setContentHeight(){
    
    if(!isIE6) return;
    
    var contentCpaddingBottom = 0;
    var contentEl = $('div#outer'); 
    if (contentEl.length == 0) return;
    var absHeight = $('div#outer').height();
    var viewportHeight = $(window).height();
    
    var newContentHeight;
    var absPosition = contentEl.offset();                 
      
    if(absHeight < viewportHeight){
                    
      newContentHeight = viewportHeight - absPosition.top - contentCpaddingBottom;            
      
      contentEl.css({height: newContentHeight + 'px'});     
    }else{
      
      newContentHeight = $(document).height() - absPosition.top - contentCpaddingBottom;        
      contentEl.css({height: newContentHeight + 'px'});           
    }       
  }

  /* Hover auf der Dachmarkenseite (nur für IE6) */
  $('.teaser-menu').hover(function(){
    $(this).addClass('hover');
  }, function() {
    $(this).removeClass('hover');
  });
  
  /* Widgets */
  
  /* Widgets an-/austoggeln */
  $('div.Wtogglebutton').click (function(){
    if ($(this).hasClass('Won')) {
      $(this).removeClass('Won').addClass('Woff');
    } else {
      $(this).removeClass('Woff').addClass('Won');
    }
  });  
   
   /* Widgets END */


  /* Tabs */
  $('ul.tabs li.tab').click(function() {
    if (! $(this).hasClass('active')) {
      var idx = $(this).prevAll('li.tab').length;
      $(this).parents('ul.tabs').children('li.tab.active').removeClass('active');
      $(this).addClass('active');

      var $content = $(this).parents('ul.tabs').nextAll('ul.tabs-content:eq(0)');

      $content.children('li').removeClass('tab-current');
      $content.children('li:eq('+idx+')').addClass('tab-current');
    
    hideFooter();
    }
  });
  
  
  /* Fonds im Fokus -- Teaser*/
  
  $('div.content div.fonds-im-fokus-teaser .item').mouseenter(function(){     
    $(this).addClass('active');
   });
   
   $('div.content div.fonds-im-fokus-teaser .item').mouseleave(function(){      
    $(this).removeClass('active');
   }); 
   
  
  /* Footer */  
   
   $('#footer div#footer-right div.buttons .inner').mouseenter(function(){      
    $(this).addClass('active');
   });
   
   $('#footer div#footer-right div.buttons .inner').mouseleave(function(){      
    $(this).removeClass('active');
   });   
        
   $('#footer div#footer-right div.buttons .weiterempfehlen .inner').click(function(){  
      
    $('.footer-boxen .box').hide();   
    
    $('.footer-boxen div.bewerten-sub-box').mouseleave (function(){
    $('.footer-boxen div.bewerten-box').hide();
      });
    
    /* margintop                                    */    
    /* top Positionierung der Boxen ist browserabhängig                 */
    /* Standardwert (var margintop = 18;) gilt für Firefox, Chrome und IE8      */        
    var margintop = 18;
    if(isIE6) margintop = 18 - 17;
    if(isSafari) margintop = 18 + 2;
    
    if ($('#footer').hasClass('footer-three')) {
      positionFooterBox($('.footer-boxen div.weiterempfehlen-box'), $(this), margintop, -90);
    } else {
      positionFooterBox($('.footer-boxen div.weiterempfehlen-box'), $(this), margintop, -12);
    }

    return false; 
   });
   
   $('div.footer-boxen div.weiterempfehlen-box .close').click(function(){     
    $('.footer-boxen .box').hide(); 
    return false;
   });
   
   
   $('div.footer-boxen div.weiterempfehlen-box .senden-button').click(function(){             

  $('div.footer-boxen div.weiterempfehlen-box .page-1').hide();
  $('div.footer-boxen div.weiterempfehlen-box .page-2').show(); 
  if(!isIE6) $('div.footer-boxen div.weiterempfehlen-box .page-2').height($('div.footer-boxen div.weiterempfehlen-box .page-1').height());

  return false;
   });
   
   
   $('#footer div#footer-right div.buttons .bewerten .inner').unbind('mouseenter').mouseenter(function(){         
        
      $('.footer-boxen div.bewerten-sub-box').unbind('mouseleave').mouseleave (function(){
    $('.footer-boxen div.bewerten-box').hide();
      });
          
    $('.footer-boxen div.kommentieren-sub-box').hide(); 
        
    currentRatingID = 'footer';   // Bewerten Box wurde über den Footer aufgerufen
                    // Verwendung: ratingArr[currentRatingID]...
                  
    setStarRatingStatus();    
    
    positionBewertenBox();
 
   });
   
   // Positioniert die Bewerten Box neu in Abhängigkeit vom auslösenden DOM Element (Widget Button oder Footer Button)
   function positionBewertenBox(){
    
    var marginleft = -243;
    
    if(currentRatingID == 'footer'){
       var el = $('#footer div#footer-right div.buttons .bewerten .inner');
     }else{
       var el =  $('div.widget#' + currentRatingID + ' div.widget-footer div.rating-container');
       marginleft = -257;
     }
     
    var margintop = 18;
    if(isIE6) margintop = 18 - 1;
    if(isSafari) margintop = 18 - 1;
    //if(isIE7) margintop = 18;
      
    positionFooterBox($('.footer-boxen div.bewerten-box'), $(el), margintop, marginleft);      
   }
   
   $('.footer-boxen div.bewerten-sub-box').mouseleave (function(){
    $('.footer-boxen .box').hide();
   });            
  
  
   $('div.footer-boxen div.bewerten-box .comment-button').click(function(){  
        
      setCommentPage();
      
        $('div.footer-boxen div.bewerten-sub-box').unbind('mouseleave');
    
        $kommentierenBox = $('div.footer-boxen div.bewerten-box div.kommentieren-sub-box');

      $kommentierenBox.show();
      positionBewertenBox();
      
      return false;
   });

   $('div.footer-boxen div.kommentieren-sub-box .senden-button').click(function(){      
    
  ratingArr[currentRatingID]['commentStatus'] = 'sent';
  
  $('div.footer-boxen div.kommentieren-sub-box .page-1').hide();  
  $('div.footer-boxen div.kommentieren-sub-box .page-2').show();  
  return false;
   });  
   
   $('div.footer-boxen div.bewerten-box div.kommentieren-sub-box .close').click(function(){     
    
    hideCommentPage();
    
    $('.footer-boxen div.bewerten-sub-box').unbind('mouseleave').mouseleave (function(){
    $('.footer-boxen div.bewerten-box').hide();
      });  
   
    return false;
   });
   
   // Versteckt die Kommentar Box
   // Positioniert die Bewerten Box neu
   function hideCommentPage(){     
     $('div.footer-boxen div.kommentieren-sub-box').hide();
     
     positionBewertenBox();
   }
   
   // Setzt die Anzeige der Unterseiten in der Kommantar Box zurück
   function resetCommentPage(){
    $('div.footer-boxen div.bewerten-box div.kommentieren-sub-box .page-1').show(); 
    $('div.footer-boxen div.bewerten-box div.kommentieren-sub-box .page-2').hide();    
    
    $('div.footer-boxen div.bewerten-box div.kommentieren-sub-box div.formular input.text').val('');
    $('div.footer-boxen div.bewerten-box div.kommentieren-sub-box div.formular textarea').val('');    
    
    $('div.footer-boxen div.bewerten-box div.kommentieren-sub-box div.formular select option:first').attr('selected', 'selected');

   }
   
   // Regelt die Anzeige der Unterseiten in der Kommantar Box
   function setCommentPage(){        
     
     if(ratingArr[currentRatingID]['commentStatus'] == "sent"){
      $('div.footer-boxen div.bewerten-box div.kommentieren-sub-box .page-1').hide(); 
      $('div.footer-boxen div.bewerten-box div.kommentieren-sub-box .page-2').show();        
     }else{
      resetCommentPage();      
     }
   }
  
   $('#footer div#footer-right div.buttons .teilen .inner').mouseenter(function(){    
   
    var margintop = 18;
    if(isIE6) margintop = 18 - 18;
    if(isSafari) margintop = 18 - 1;
         
    positionFooterBox($('.footer-boxen div.teilen-box'), $(this), margintop, -12);    
   });
    
  
   $('.footer-boxen div.teilen-box').mouseleave (function(){
    $(this).hide();
   });    
  
   function positionFooterBox($box, $button, topmargin, leftmargin){
  
    $('.footer-boxen').css({ visibility:'visible'});  
    
    var absPosition = $button.offset();
    
    var top = absPosition.top - $box.height() + topmargin;
    var left = absPosition.left + leftmargin;
    
    $box.css({
      left: left,
        top: top
    });

    $box.show();
  }
  
  function hideFooter(){
   $('.footer-boxen .box') .hide();
   $('.footer-boxen div.bewerten-sub-box').mouseleave (function(){
    $('.footer-boxen div.bewerten-box').hide();
      });
  }
  
  /* Footer + Widget::Star Rating */
  
  var currentRatingID; //
  
  var ratedTypeArr = new Array("nicht hilfreich", "weniger hilfreich", "neutral?", "hilfreich", "sehr hilfreich");  // Bewertungsarten, beschreibend
    
   var ratingArr = {
     "footer":{"ratedCountArr":new Array("838", "400", "600", "1000", "2000"), "ratesTotalCount":4838, "ratingLockedItem":null, "ratingStatus":null, "commentStatus":null},
     "widget_1":{"ratedCountArr":new Array("120", "200", "300", "4000", "6000"), "ratesTotalCount":8546, "ratingLockedItem":null, "ratingStatus":null, "commentStatus":null},
     "widget_2":{"ratedCountArr":new Array("320", "400", "100", "7635", "6525"), "ratesTotalCount":6523, "ratingLockedItem":null, "ratingStatus":null, "commentStatus":null}
   };  
  
  /*
  ratedCountArr = Anzahl der Bewertungen, index = 0 -> ratedTypeArr[0] = "nicht hilfreich"
  ratesTotalCount = Gesamtanzahl der Bewertungen
  
  ratingLockedItem = DOM-Element, geklickter Bewertungs-Stern (div.star)
  ratingStatus =  'locked'  => wurde irgendein Stern geklickt
            'sent'    => rating wurde gesendet
  commentStatus = 'sent'    => Kommentar wurde gesendet 
    */   
       
    
   $('.footer-boxen div.bewerten-box .star-rating-container .star').css({ cursor:'pointer' }).unbind('mouseover').mouseover(function(){       
      
    if(ratingArr[currentRatingID]['ratingStatus'] == 'sent') return false;
    
    fillRatingStars(this);        
   });
    
   $('.footer-boxen div.bewerten-box .star-rating-container .star').unbind('click').click(function(){     
      
    if(ratingArr[currentRatingID]['ratingStatus'] == 'sent') return false;
      
    ratingArr[currentRatingID]['ratingLockedItem'] = this;
    ratingArr[currentRatingID]['ratingStatus'] = 'locked';                  
      
    $('.footer-boxen div.bewerten-box .rating-container .rate-button').removeClass("not-clickable");    
      
    return false;
   });         
  
   
   $('.footer-boxen div.bewerten-box .rating-container .rate-button').unbind('click').click(function(){           
        
    if(ratingArr[currentRatingID]['ratingStatus'] != 'locked') return false;
        
    ratingArr[currentRatingID]['ratingStatus'] = 'sent';
        
    starRatingSent();
    return false; 
   });       
  
   
   function starRatingSent(){ 
      var el = $('.footer-boxen div.bewerten-box .rating-container .rate-button');
    $(el).addClass("not-clickable");
    $(el).html('bewertet');
        
    $('.footer-boxen div.bewerten-box .star-rating-container .star').css({ cursor:'default' });
   }
      
   
   $('.footer-boxen div.bewerten-box .star-rating-container').mouseleave (function(){    
   
      if(ratingArr[currentRatingID]['ratingStatus'] == 'locked' || ratingArr[currentRatingID]['ratingStatus'] == 'sent'){
      fillRatingStars(ratingArr[currentRatingID]['ratingLockedItem']);
    }else{
      setStarRatingStarsDefault();
    }
   });  
   
   // Setzt die Anzeige der Sterne zurück
   function setStarRatingStarsDefault(){
    $stars = $('.footer-boxen div.bewerten-box .star-rating-container').find("div.star"); 
    $stars.removeClass("rating"); 
    $('.footer-boxen div.bewerten-box .rated-container .rated-type').hide();
    $('.footer-boxen div.bewerten-box .rated-container .rated-count').html(ratingArr[currentRatingID]['ratesTotalCount'] + ' Stimmen').show();         
   }    
    
   // Bestimmt die Benutzbarkeit der Sterne in der Bewerten Box
   function setStarRatingStatus(){        
    
    if(ratingArr[currentRatingID]['ratingStatus'] == 'locked'){
      fillRatingStars(ratingArr[currentRatingID]['ratingLockedItem']);
      
      $('.footer-boxen div.bewerten-box .star-rating-container .star').css({ cursor:'pointer' }); 
      $('.footer-boxen div.bewerten-box .rating-container .rate-button').removeClass("not-clickable").html('bewerten');
      
    }else if(ratingArr[currentRatingID]['ratingStatus'] == 'sent'){
      fillRatingStars(ratingArr[currentRatingID]['ratingLockedItem']);
      starRatingSent();
      $('.footer-boxen div.bewerten-box .rating-container .rate-button').addClass("not-clickable");
    }else{
      setStarRatingStarsDefault();
      $('.footer-boxen div.bewerten-box .star-rating-container .star').css({ cursor:'pointer' }); 
      $('.footer-boxen div.bewerten-box .rating-container .rate-button').addClass("not-clickable").html('bewerten');
    }
   }
    
   // Zeigt die ausgewählte Bewertung an
   // el = $('.footer-boxen div.bewerten-box .star-rating-container .star') => DOM Element, ausgewählter Stern 
   
   function fillRatingStars(el){  
    
    $stars = $('.footer-boxen div.bewerten-box .star-rating-container').find("div.star");
  
    $stars.removeClass("rating");
    $(el).prevAll().addClass("rating");
    $(el).addClass("rating"); 
  
    $('.footer-boxen div.bewerten-box .rated-container .rated-type').html(ratedTypeArr[$stars.index(el)]).show();
    $('.footer-boxen div.bewerten-box .rated-container .rated-count').html(ratingArr[currentRatingID]['ratedCountArr'][$stars.index(el)] + ' von ' + ratingArr[currentRatingID]['ratesTotalCount'] + ' Stimmen').show();   
   }      
   /* Footer END*/


   /* Widgets::Star-Rating */    
   
   $('div.widget div.widget-footer div.rating-container').mouseenter(function(){    
      
      $('.footer-boxen div.bewerten-sub-box').unbind('mouseleave').mouseleave (function(){
    $('.footer-boxen div.bewerten-box').hide();
      });
        
    $('.footer-boxen div.kommentieren-sub-box').hide(); 
    
    currentRatingID = $(this).parent().parent().attr('id');
    
    setStarRatingStatus();            
    
    positionBewertenBox();
    $('.footer-boxen').css({ visibility:'visible'});  
    
   });
   
  /* Info Popup Layer */
   
   var infoBoxSafeMarginRight = 10;
   var infoBoxSafeMarginBottom = 40;
      
   $('.info-icon').click(function(){          
    positionInfoBox($('.info-boxen div#box-' + $(this).attr('id')), this);
    
    return false;
   });
   
   $('.info-boxen .box .close').click(function(){   
      $('.info-boxen .box').hide();
   });
  
  function positionInfoBox(box, icon){
    $('.info-boxen .box').hide();
    
    $outer = $('#outer');
    
    var absPositionIcon = $(icon).offset();     
    var absPositionOuter = $outer.offset();
    
    var top = absPositionIcon.top - 17 - (isIE6 ? 20 : 0);
    var left = absPositionIcon.left - 17;
    
    if((left + box.width() + infoBoxSafeMarginRight) > (absPositionOuter.left + $outer.width())) left = absPositionOuter.left + $outer.width() - box.width() - infoBoxSafeMarginRight;   
    if((top + box.height() + infoBoxSafeMarginBottom) > $outer.height()) top = $outer.height() - box.height() - infoBoxSafeMarginBottom;   
    
    box.css({
        left: left,
        top: top
    });
        
    box.show();   
  }
  /* Info Popup Layer END*/
  
  
  /* Fondspreisliste */
  
  $('div#contentC div.fondspreisliste-detail div.list-container a.button-extend-search').click(function(){  
      $('div#contentC div.fondspreisliste-detail div.list-container div.list-row.extended-search').show();
    $('div.toggle-box.fondspreisliste-detail div.BcontentC').css({ height:'auto'});
    $(this).hide();
    $('div#contentC div.fondspreisliste-detail div.list-container div.bottom-row .button-minimize-search').show();
  });
  
  $('div#contentC div.fondspreisliste-detail div.list-container a.button-minimize-search').click(function(){  
      $('div#contentC div.fondspreisliste-detail div.list-container div.list-row.extended-search').hide();
    $('div.toggle-box.fondspreisliste-detail div.BcontentC').css({ height:'auto'});
    $(this).hide();
    $('div#contentC div.fondspreisliste-detail div.list-container div.bottom-row .button-extend-search').show();
  });  
  
  /* Fondspreisabo Fondsauswahl */
  
  $('div#contentC div.content a.button-fondsauswahl').click(function(){  
      
    var box = $('div.fondspreisabo-box');
    
    var absPositionBtn = $(this).offset();    
    
    var top = absPositionBtn.top - box.height() + $(this).height()- (isIE6 ? 23 : 2);
    var left = absPositionBtn.left - 23;
    
    box.css({
        left: left,
        top: top
    });
    
    box.css({ visibility:'visible'}); 
    box.show();   
  });  
  
   $('div.fondspreisabo-box div.header div.close').click(function(){      
    $('div.fondspreisabo-box').hide(); 
    return false;
   });

  /* QuickVote */
  $('ul.qv-answers div.qv-radio').click(function() {
    var $qv_answers = $(this).parents('ul.qv-answers');
    $qv_answers.find('div.qv-radio').removeClass('qv-active');
    $qv_answers.find('input').attr('checked', '');
    $(this).prev('input').attr('checked', 'checked');
    $(this).addClass('qv-active');
  });

  $('.tab-quickvote a.senden-button').click(function() {
    $('#qv-form').submit();
    return false;
  });
     
});

window.onload = function() {

  var sliderHeight = 0;
  $('.slider').each (function() {
    sliderHeight += $(this).outerHeight(true);
  });
  if (sliderHeight + 60> $('#contentC').outerHeight()) {
    $('#contentC').css('height', sliderHeight + 60);
  }


  /* Slider */
  $('div.sliderbg').each(function() {
    var top = $(this).position().top;
    var left = $(this).position().left;
    $(this).css({left: left,
      top: top,
      width: $(this).width(),
      height: $(this).height()
    });
  });
  $('div.sliderbg').each(function() {
    $(this).css({position: 'absolute', zIndex:5});
  });

  $('div.slideable h4').hover(
    function() {
      var $slider = $(this).parents('div.sliderbg');
      if (!$slider.hasClass('open')) {
        $slider.animate({left:-8}, 30, 'swing', function(){
        });
      }
    },
    function(){
      var $slider = $(this).parents('div.sliderbg');
      if (!$slider.hasClass('open')) {
        $slider.animate({left: 0}, 300, 'swing',  function () {
        });
      }
    }
  );

  $('div.slideable h4').click(function() {
    var $slider = $(this).parents('div.sliderbg');
    if ($slider.hasClass('open')) {
      $slider.animate({left:0}, function() {
        $slider.removeClass('open');
      });
    } else {
      $('div.sliderbg.open').each(function(){
        $(this).find('h4').trigger('click');
      });
      var out = $slider.outerWidth() - 115;
      $slider.animate({left:-out}, function() {
        $slider.addClass('open');
      });
    }
    return false;
  });

};

