$(document).ready(function() { 
  animateNav();
  newsRoll(6000);
  var timer;
  animateShowcase();
});


/*
*---------------------------------------------------------------
* Main navigation animation
*---------------------------------------------------------------
*/
animateNav = function() {
   $('#main_nav > li > ul').each(function() {
    var tmp1 = (173-$(this).height())/2;
    tmp1 = (tmp1>29)?tmp1:29;
    $(this).css(
    {
      paddingTop: (tmp1 - 29) + 'px'/*,
      paddingBottom: tmp1 + 'px'*/
    });
  });

  $('#main_nav ul ul').each(function() {
    var tmp=0, tmp2=0;
    var parentLi = $(this).parents('li');
    if(parentLi.hasClass('sub1')) 
    {
      tmp = (301-$(this).height())/2;;
      tmp2 = 26;
    }
    else if(parentLi.hasClass('sub2')) 
    {
      tmp = (174-$(this).height())/2;;
      tmp2 = 63;
    }
    else if(parentLi.hasClass('sub3')) 
    {
      tmp = (216-$(this).height())/2;;
      tmp2 = 60;
    }

    $(this).css(
    {
      paddingTop: ((tmp + tmp2) + 'px'),
      paddingBottom: (tmp + 'px')    
    });
  });
   
  $('#main_nav ul').hide();

  var not_ie = jQuery.support.opacity;
  
 $('#main_nav > li').hover(
    function(){
      $(this).children('a').addClass('active');
      var bglevel1 = $(this).children('.level1').stop(false, true);
      if(not_ie)
        bglevel1.fadeIn('fast');
      else
        bglevel1.slideDown('fast');

      $(this).children('ul').stop(false, true).fadeIn('fast');
    },
    function() {
      $(this).children('ul').stop(false, true).fadeOut('fast');
      var bglevel1 = $(this).children('.level1').stop(false, true);
      if(not_ie)
        bglevel1.fadeOut('fast');
      else
        bglevel1.slideUp('fast');      

      $(this).children('a').removeClass('active');
    });
  
  $('#main_nav > li > ul > li').hover(
    function(){
      $(this).children('a').addClass('active');
      var subsubnav = $(this).children('ul');      
      if(subsubnav.length) {

        var bglevel2 = $(this).parent().siblings('.level2').stop(true, true).filter(':hidden');
        if(not_ie)
          bglevel2.fadeIn('fast');
        else
          bglevel2.slideDown('fast');
        subsubnav.stop(false, true).fadeIn('fast');
      } 
      else {
        var bglevel2 = $(this).parent().siblings('.level2').stop(true, true);
        if(not_ie)
          bglevel2.fadeOut('fast');
        else
          bglevel2.slideUp('fast');
      }
    },
    function() {
      $(this).children('a').removeClass('active');
      var subsubnav = $(this).children('ul');      
      if(subsubnav.length) {
        subsubnav.stop(false, true).hide();
      }
    });  
    
  $('#main_nav > li > ul').hover(
    function(){ },
    function() {
        var bglevel2 = $(this).siblings('.level2').stop(true, true);      
        if(not_ie)
          bglevel2.fadeOut('fast');
        else
          bglevel2.slideUp('fast');
    });    

}

/*
*---------------------------------------------------------------
* Home page news roller (argument: timeInterval in ms)
*---------------------------------------------------------------
*/

newsRoll = function(timeInterval) {
  var newsList = $('.home_news ul');
  var listLength = newsList.children('li').length;
  var currentIndex =0;
  var listIndex = 0;
  var topMargin = 0;

 
  var timer = setInterval(
    function(){
      listIndex = currentIndex % (listLength);

      var currentLi = newsList.children('li:eq(' + listIndex + ')');
      if(listIndex!= listLength-1)
        topMargin -= currentLi.outerHeight();
      else
        topMargin = 0;
        
      newsList.animate({marginTop: topMargin+ 'px'}, 300);
 
      currentIndex++;
    }
    , timeInterval);

}

/*
*---------------------------------------------------------------
* Showcase animation
*---------------------------------------------------------------
*/

animateShowcase = function() {
  if($('.showcase').length) {
    $('.showcase li:not(.active)').hide().find('.showcase_content').hide().prev('.nav').css('marginLeft', '580px');
    
    changeShowcase();
    
    $('.showcase li .next').click(function(){
      clearTimeout(timer);
      changeShowcase();
    });
    
    $('.showcase li .prev').click(function(){
      clearTimeout(timer);
      var prevLi = $('.showcase li.active').removeClass('active');
      for(i=0;i<2;i++) {
        prevLi = prevLi.prev('li');
        prevLi = ((prevLi.length)?prevLi:$('.showcase li:last-child')); 
      }  
      prevLi.addClass('active');
      changeShowcase();
    });
}
}

changeShowcase = function(){
  var timeInterval = $('.showcase ul').attr('class')*1000;
  var showcase = $('.showcase');
  var activeLi = $('.showcase li.active');
  if(showcase.hasClass('initial'))
    showcase.removeClass('initial');
  else {
    $('.showcase li.last_active').stop().fadeOut('fast', function() {
        $(this).find('.nav').stop().css('marginLeft', '580px');
        $(this).find('.showcase_content').stop().hide();
      }).removeClass('last_active');
    activeLi.fadeIn('fast', function() {
        $(this).find('.nav').animate({marginLeft: '0'}, 500); $(this).find('.showcase_content').fadeIn(1000); 
      });
  }
  activeLi.removeClass('active').addClass('last_active');
  var nextLi = activeLi.next('li');
  ((nextLi.length)?nextLi:$('.showcase li:first-child')).addClass('active');

  if(!activeLi.hasClass('type4'))  
    timer = setTimeout('changeShowcase()', timeInterval);
}

