var animationDuration = 500, // trvani animace (ms)
    slideInterval = 5000, // inverval pro zmenu snimku (ms)
    slideButtonWidth,
    slideOpenWidth,
    animating = false,
    animatingToggles = false,
    activeReached = false,
    timeout,
    timeoutFunction = function(){
      var slides = $('#slider .slide'),
          active = slides.filter('.active'),
          next = active.next('.slide');

      if (!next.length)
        next = slides.eq(0);

      next.click()
    };

$(document).ready(function(){

  var firstButton = $('#slider .slide').eq(0),
      first = $('#slider .slide-act').eq(0);
  slideButtonWidth = parseInt(firstButton.width()) + parseInt(firstButton.css('paddingLeft')) + parseInt(firstButton.css('paddingRight'));
  slideOpenWidth = parseInt(first.width()) + parseInt(first.css('paddingLeft')) + parseInt(first.css('paddingRight'));
  
  $('#sliderWrapper').css('left', slideButtonWidth);
  $('#slider .slide').each(function(i){
    if (activeReached)
      $(this).css('left', slideButtonWidth * i + slideOpenWidth);
    else
      $(this).css('left', slideButtonWidth * i);

    if (i == 0)
      activeReached = true;
  }).eq(0).addClass('active');
  
  $('#slider .slide').click(function(e){
    e.preventDefault();
    if (animating || $(this).hasClass('active'))
      return false;
      
    animating = true;
    var allSlide = $(this).siblings('.slide').andSelf(),
        newActiveIndex = allSlide.index(this);
        
    allSlide.filter('.active').removeClass('active');
    $(this).addClass('active');

    activeReached = false;
    allSlide.each(function(i){
      if (activeReached)
        $(this).animate({left: slideButtonWidth * i + slideOpenWidth}, {queue: false, duration: animationDuration});
      else
        $(this).animate({left: slideButtonWidth * i}, {queue: false, duration: animationDuration});

      if (i == newActiveIndex)
        activeReached = true;
    });
    
    $('#sliderWrapper').animate({
      left: (newActiveIndex + 1) * slideButtonWidth - newActiveIndex * slideOpenWidth
    }, {
      queue: false,
      duration: animationDuration,
      complete: function(){
                  animating = false;
      }});
    clearTimeout(timeout);   
    timeout = setTimeout(timeoutFunction, slideInterval);
  });
  
  timeout = setTimeout(timeoutFunction, slideInterval);
  
  var imgActive = 'static/images/ws-www2_06.png',
      imgInactive = 'static/images/ws-www_06.png';
  $('#switchable').find('.math, .pr-text, .more').click(function(e){
    e.preventDefault();
    if (animatingToggles)
      return;
      

      
    var newActive = $(this).parent(),
        active = newActive.siblings().andSelf().filter('.active');
        
    if (newActive.is(active))
      return;

    animatingToggles = true;  

    active.removeClass('active')
          .find('.math img').attr({src: imgInactive, alt: '+'}).end()
          .find('.more').show(500).end()
          .next('.detail-pr').slideUp(500);
    newActive.addClass('active')
             .find('.math img').attr({src: imgActive, alt: '-'}).end()
             .find('.more').hide(500).end()
             .next('.detail-pr').slideDown(500, function(){ animatingToggles = false; });
      
  }).eq(0).click();
});

$(document).ready(function() {

    
    // tipy
    $("input.tip").hover(function(e) {
       $("#tip_"+$(this).attr("name"))
                    .css("left", e.pageX-5)
                    .css("top", e.pageY+15)
                    .show();
    }, function() {
        $("#tip_"+$(this).attr("name")).hide();
    });
    
    $("a.tip").hover(function(e) {
       $("#"+$(this).attr("rel"))
                    .css("left", e.pageX-5)
                    .css("top", e.pageY+15)
                    .show();
    }, function() {
        $("#"+$(this).attr("rel")).hide();
    });
    
});
