/* main.js, Copyright 23.12.09, Weber eBusiness Services GmbH, Jaroslav Herber */
/* Version 1.0.1 */


jQuery.noConflict();

jQuery(document).ready(function()
{

  // Accordion plugin
  jQuery('.accordion').accordion({
    autoHeight:false,
    header: 'h3'
    //animated: 'bounceslide'
  });
  
  // If News-Box is present
  if(document.getElementById('news_tab_content')){
    loadHomeNewsAnimatedArrowHandler();
    
    //loadHomeNewsTagHeightHandler();
    // Version 2 is for visiblity:hidden instead of display:none
    loadHomeNewsTagHeightHandler_v2();
    
    if(document.getElementById('news_tabs')) {
      loadHomeNewsHandler();
    }
  }
  loadSmallTeaserHeightHandler();
  loadNavHandler();
  loadHomeWeatherHandler();

});

// Handler ------------------------------------------------------------------- 

function loadHomeNewsHandler()
{
  jQuery('.tab_bg:first').show(0);

  var news_tabs = document.getElementById('news_tabs').getElementsByTagName('li');
  
  ClassNames = new Array();
  for(i = 0; i < news_tabs.length; i++) {
    // Remove all links from Tab-Buttons if JS is available
    var link_construction = 'javascript:void(0)';
    news_tabs[i].getElementsByTagName('a')[0].setAttribute('href', link_construction);
    ClassNames[i] = news_tabs[i].className;
  }

  jQuery('#news_tabs li').click(
    function() {
      for(i = 0; i < news_tabs.length; i++){
        if(jQuery('#news_tab_content .tab_content:eq('+i+')').hasClass(jQuery(this).attr('class'))) {
          if(!jQuery(this).hasClass('active')){
            // Abort last animation, if new animation starts
            jQuery('#news_tabs li').stop(true, true);
            jQuery('#news_tab_content .tab_content').stop(true, true);
            
            // Hide the background of non-active Tab-Buttons
            jQuery('.tab_bg').fadeOut(500);
            
            jQuery('#news_tabs li').removeClass('active');
            
            jQuery('#news_tab_content .active').css({
              'position' : 'absolute',
              'z-index' : '1'
            }).fadeOut('slow',
              function() {
                // IE7 Bugfix: fadeOut doesn't set "display: none" to IE7
                jQuery(this).removeClass('active').css('display', 'none');
              }
            );

            jQuery('#news_tab_content .tab_content:eq('+i+')').css({
              'display' : 'none',
              'position' : 'relative',
              'visibility' : 'visible',
              'z-index' : '10'
            }).fadeIn('slow').addClass('active');
            
            jQuery(this).show('fast').addClass('active');
            jQuery('#news_tabs li').filter('li:not(.active)').children('a').children('span');
            jQuery(this).children('a').removeClass('hover').children('span');

            // Background of the active Tab-Button
            jQuery(this).children('.tab_bg').stop(true, true).fadeIn(300);
            
          }
        }
      }
  });
  
  /* change color on tab hover */
  jQuery('#news_tabs ul li').hover(
    function() {
      if(!jQuery(this).hasClass('active')) {
        jQuery(this).stop(true, true).children('a').addClass('hover');
        //jQuery(this).children('a').children('span').stop(true, true).css('color', '#c42e00');
        jQuery(this).children('a').children('span').stop(true, true);
      }
    },
    function(){
      if(!jQuery(this).hasClass('active')) {
        //jQuery(this).children('a').removeClass('hover').children('span').stop(true, true).css('color', '#4c4c4c');
        jQuery(this).children('a').removeClass('hover').children('span').stop(true, true);
      }
    }
  );
  
  /* Hover-effect of the news fields */
  /*
  jQuery('.news-latest-item').hover(
    function(){
      jQuery(this).addClass('hover');
    },
    function(){
      jQuery(this).removeClass('hover');
    }
  );
  */
  /* Link behaviour on click */
  jQuery('.news-latest-item').click(
    function(){
      //window.location.href = jQuery(this).children('.news-latest-item-border').children('h3:first').children('a').attr('href');
    }
  );
}

function loadHomeNewsAnimatedArrowHandler()
{
  // Slide the arrow to position 1
  jQuery('#news_tabs li:eq(0)').click(
    function(){
      if(!jQuery(this).hasClass('active')){
        jQuery('.tab_arrow').stop(true, true).animate({left: "6.25em", color: "blue"}, {duration: 600, easing: 'easeOutBack'});
      }
    }
  );
  
  // Slide the arrow to position 2
  jQuery('#news_tabs li:eq(1)').click(
    function(){
      if(!jQuery(this).hasClass('active')){
        jQuery('.tab_arrow').stop(true, true).animate({left: "20.9em"}, {duration: 600, easing: 'easeOutBack'});
      }
    }
  );
  
  // Slide the arrow to position 3
  jQuery('#news_tabs li:eq(2)').click(
    function(){
      if(!jQuery(this).hasClass('active')){
        jQuery('.tab_arrow').stop(true, true).animate({left: "33.55em"}, {duration: 600, easing: 'easeOutBack'});
      }
    }
  );
}

function loadHomeNewsTagHeightHandler()
{  
  jQuery('.tx-calendar-pi1 .news-latest-item:first').addClass('first');
  /* Set start-height of the news-content div */
  var highest_news_tab_height = jQuery('#news_tab_content .tab_content:first .news-latest-items').height();
  
      // Show all boxes to calculate the highest box
        jQuery('#news_tab_content .tab_content').filter(':not(.active)').css('display', 'block');
        
        for(var i = 0; i < jQuery('#news_tab_content .tab_content').length; i++) {
          if(highest_news_tab_height < jQuery('#news_tab_content .tab_content:eq('+i+') .news-latest-items').height())
            var highest_news_tab_height = jQuery('#news_tab_content .tab_content:eq('+i+') .news-latest-items').height();
        }
        
      // hide all not visible boxes
        jQuery('#news_tab_content .tab_content').filter(':not(.active)').css('display', 'none');

  // IE6 uses high instead of min-height
  if(window.navigator.userAgent.indexOf("MSIE 6") > -1){
    jQuery('#news_tab_content .tab_content .news-latest-items').css('height', highest_news_tab_height);
    
    /* If other news-content boxes are higher, set new height */
    jQuery('#news_tabs li').click(
      function() {
        if(highest_news_tab_height < jQuery('#news_tab_content .tab_content:visible .news-latest-items').height()) {
          highest_news_tab_height = jQuery('#news_tab_content .tab_content:visible .news-latest-items').height();
          jQuery('#news_tab_content .tab_content .news-latest-items').css('height', highest_news_tab_height);
        }
    });
  } else {
    jQuery('#news_tab_content .tab_content .news-latest-items').css('min-height', highest_news_tab_height);
    
    /* If other news-content boxes are higher, set new height */
    jQuery('#news_tabs li').click(
      function() {
        if(highest_news_tab_height < jQuery('#news_tab_content .tab_content:visible .news-latest-items').height()) {
          highest_news_tab_height = jQuery('#news_tab_content .tab_content:visible .news-latest-items').height();
          jQuery('#news_tab_content .tab_content .news-latest-items').css('min-height', highest_news_tab_height);
        }
    });
  }
}

function loadHomeNewsTagHeightHandler_v2()
{
  jQuery('.tx-calendar-pi1 .news-latest-item:first').addClass('first');
  /* Set start-height of the news-content div */
  var highest_news_tab_height = jQuery('#news_tab_content .tab_content:first').height();
        
        for(var i = 0; i < jQuery('#news_tab_content .tab_content').length; i++) {
          if(highest_news_tab_height < jQuery('#news_tab_content .tab_content:eq('+i+')').height())
            var highest_news_tab_height = jQuery('#news_tab_content .tab_content:eq('+i+')').height();
        }

  // IE6 uses high instead of min-height
  if(window.navigator.userAgent.indexOf("MSIE 6") > -1){
    jQuery('#news_tab_content').css('height', highest_news_tab_height);

    jQuery('#news_tabs li').click(
      setNewHeight('height', highest_news_tab_height)
    );
  } else {
    jQuery('#news_tab_content').css('min-height', highest_news_tab_height);
    
    /* If other news-content boxes are higher, set new height */
    jQuery('#news_tabs li').click(
      setNewHeight('min-height', highest_news_tab_height)
    );
  }
}

function loadSmallTeaserHeightHandler() {
  var teaser_number = jQuery('.small_teaser_container').length;
  var teaser_height = 209;
  
  setTeaserHeight(teaser_height, teaser_number);
  
  jQuery('html').click(
    function() {
      setTeaserHeight(teaser_height, teaser_number);
    }
  );
}

function loadHomeWeatherHandler()
{
  var jahr, monat, tag, stunden, minuten;
  var AktuellesDatum = new Date();
  jahr = AktuellesDatum.getYear()-2000;
  monat = AktuellesDatum.getMonth ()+1;
  tag = AktuellesDatum.getDate();
  stunden = AktuellesDatum.getHours();
  minuten = AktuellesDatum.getMinutes();
  
  if(jahr < 10){
    if(jahr < 0) {jahr += 1900;}
  } 
  jQuery('.tx-wesweatheralert-info #date').text(tag+"."+monat+"."+jahr+", ");
  
  /*
  jQuery('.tx-wesweatheralert-info').hover(
    function(){
      jQuery('.weather_info .extended').slideDown(500);
      jQuery('.weather_details').slideUp(500);
      jQuery(this).addClass('extended_weather');
    },
    function(){
      jQuery('.weather_info .extended').stop(true, true).slideUp(500);
      jQuery('.weather_details').stop(true, true).slideDown(500);
      jQuery(this).removeClass('extended_weather');
  });
  */
  
  jQuery('.tx-wesweatheralert-info .details_button').click(
    function(){
      if(window.navigator.userAgent.indexOf("MSIE 6") > -1) {
        jQuery('.tx-wesweatheralert-info .extended').show();
      } else {
        jQuery('.tx-wesweatheralert-info div.extended').fadeIn(800,
          function() {
            jQuery(this).css('filter', 'alpha(opacity=100)');
          }
        );
      }
      //jQuery('.weather_details').hide();
      jQuery('.tx-wesweatheralert-info .weather_info').addClass('extended_weather');
    }
  );

  jQuery('.tx-wesweatheralert-info .close').click(
    function(){
      jQuery('.tx-wesweatheralert-info .extended').stop(true, true).hide();
      //jQuery('.weather_details').stop(true, true).fadeIn(800);
      jQuery('.tx-wesweatheralert-info .weather_info').removeClass('extended_weather');
    }
  );
}


function loadNavHandler()
{
  jQuery('#mainnav li.level1').hover(
    function(){
      jQuery(this).addClass('hover');
    },
    function(){
      jQuery(this).removeClass('hover');
    }
  );
}

// Set min-width to the footer-images in ie6
function ie6_footer_width() {
  if(window.navigator.userAgent.indexOf("MSIE 6") > -1){
    var x;
    x = document.body.clientWidth;
    if(x <= 970)
      jQuery('#bottom_image_layers').css('width', '970px');
    else
      jQuery('#bottom_image_layers').css('width', '100%');
  }
} 

window.onresize = ie6_footer_width;

// Help Functions
function setNewHeight(heightType, highest_news_tab_height)
{
  if(highest_news_tab_height < jQuery('#news_tab_content').height()) {
    highest_news_tab_height = jQuery('#news_tab_content').height();
    jQuery('#news_tab_content').css(heightType, highest_news_tab_height);
  }
}

function setTeaserHeight(teaser_height, teaser_number) {
  if(teaser_number >= 1) {
    for(i = 0; i < teaser_number; i++) {
      if(teaser_height < jQuery('.small_teaser_container:eq('+i+')').height()) {
        teaser_height = jQuery('.small_teaser_container:eq('+i+')').height();
      }
      if(window.navigator.userAgent.indexOf("MSIE 6") > -1){
        jQuery('.small_teaser').css('height', teaser_height);
      } else {
        jQuery('.small_teaser').css('min-height', teaser_height);
      }
    }
  }
}
