/**
 * JS pro verejne stranky, utf8 (ěščřžýáíéúů)
 */
$(document).ready(function() {
    // boxy s nabidkou a objednavkou jednotlivych kurzu
    // init_active_box();
    // js-hiding - rozklikavaci zalozky na karte kurzu
    init_js_hiding();
    if(window.location.hash && $('div.js-hiding h3#'+window.location.hash).length) {
      $('div.js-hiding h3#'+window.location.hash).click();
    }
    // slidebox - animace slevovych karet v paticce
    $('.slide-box').each(function() {
        var tts = $(this).attr('title');
        if(!tts) tts = 4;
        $(this).attr('title', '');
        $(this).attr('counter', tts);
        $(this).attr('tts', tts);
        $(this).attr('anim_stopped', '0');
        var items = $(this).find('.item');
        if(items.length > 1) {
          items.hide().css({position:'absolute',top:'0px',left:'0px'}).filter(':first').addClass('active').show();
          var i; var nav_str = '';
          for(i=1;i<=items.length;i++) {
            nav_str += '<span>'+i+'</span>';
          }
          $('<div class="overflow"></div>').appendTo(this).append(items);
          $('<span class="nav">'+nav_str+'</span>').appendTo(this);
          $('<span class="prev">&nbsp;</span>').click(function() { show_prev(this); }).appendTo(this);
          $('<span class="next">&nbsp;</span>').click(function() { show_next(this); }).appendTo(this);
          var slide_box = this;
          $(this).find('.item, span.next, span.prev, span.nav').mouseenter(function() {
              $(slide_box).attr('anim_stopped','1');
          }).mouseleave(function() {
              $(slide_box).attr('anim_stopped','0');
              $(slide_box).attr('counter', tts);
          })
          $(this).find('span.nav span').click(function() { show_item(this); }).filter(':first').addClass('active');
        }
    });
    $('.bubbleicon').mouseenter(function() {
        $(this).find('.bubble').show();
    }).mouseleave(function() {
        $(this).find('.bubble').hide();
    });
    if($('.slide-box').length) {
      setInterval("countdown();", 1000);
    }
    $("a[href^='http://']").not("a[href^='http://www.kite-kurzy.cz'], a[href^='http://kite-kurzy.cz'], a[rel^='shadowbox']").click(function(){
      var reKB = /kiteboarding\.cz/;
      if(this.href == "http://www.kiteboarding.cz/kite-clanky/nejvetsi-kiteboarding-a-snowkiting-bazar-v-cr.php") {
        _gaq.push(['_trackEvent', 'ExtLink', 'Kiteboarding.cz', 'Bazar']);
      } else if(reKB.test(this.href)) {
        _gaq.push(['_trackEvent', 'ExtLink', 'Kiteboarding.cz']);
      }
      var reKT = /kite-tripy\.cz/;
      if(reKT.test(this.href)) {
        _gaq.push(['_trackEvent', 'ExtLink', 'Kite-tripy.cz']);
      }
      return !window.open(this.href);
    });
});
/* funkce pro slide box */
var countdown = function() {
  $('.slide-box').each(function() {
      var counter = $(this).attr('counter');
      var anim_stopped = $(this).attr('anim_stopped');
      var tts = $(this).attr('tts');
      if(counter > 0) {
        counter--;
      } else {
        if(anim_stopped=='0') {
          show_next($(this).find('span.next').get(0));
          counter = tts;
        }
      }
      $(this).attr('counter', counter);
      $(this).attr('anim_stopped', anim_stopped);
  });
}
var show_next = function(elem) {
  var slidebox = $(elem).parent().parent(); 
  var actual = slidebox.find('.item.active').removeClass('active');
  var width = slidebox.width();
  var next = actual.next('.item');
  if(!next.length) {
    next = slidebox.find('.item:first');
  }
  if(!next.length) return;
  actual.animate({left:'-'+width+'px'},1000);
  next.addClass('active').css('left',width+'px').show().animate({left:'0px'},1000);
  slidebox.find('span.nav span').removeClass('active').eq(next.prevAll().length).addClass('active');
}
var show_prev = function(elem) {
  var slidebox = $(elem).parent().parent(); 
  var actual = slidebox.find('.item.active').removeClass('active');
  var width = slidebox.width();
  var prev = actual.prev('.item');
  if(!prev.length) {
    prev = slidebox.find('.item:last');
  }
  if(!prev.length) return;
  actual.animate({left:width+'px'},1000);
  prev.addClass('active').css('left','-'+width+'px').show().animate({left:'0px'},1000);
  slidebox.find('span.nav span').removeClass('active').eq(prev.prevAll().length).addClass('active');
}
var show_item = function(elem) {
  var slidebox = $(elem).parent().parent(); 
  var actual = slidebox.find('.item.active');
  var width = slidebox.width();
  var item = slidebox.find('.item').eq($(elem).text()-1);
  if(!item.length || item.get(0) == actual.get(0)) return;
  actual.removeClass('active').animate({left:'-'+width+'px'},1000);
  item.addClass('active').css('left',width+'px').show().animate({left:'0px'},1000);
  slidebox.find('span.nav span').removeClass('active');
  $(elem).addClass('active');
}
/**
 * funkce inicializuje skryvaci boxy
 */
function init_js_hiding(elem) {
  if(!elem) elem = '';
  $(elem+' div.js-hiding h3').click(function() {
    // zmenit viditelnost aktualniho
    $(this).parent().toggleClass('hidden').find('>*').not('h3').toggle();
    // skryt vse krome aktualniho - vzdy je viditelny pouze jeden kurz
    $('div.js-hiding h3').not(this).parent().not($(this).parents()).addClass('hidden').find('>*').not('h3').hide();
    var kotva = $(this).attr('id');
    if(kotva && $(this).filter(':visible').length) { window.location.hash = kotva; } 
  }).parent().addClass('hidden');
  $(elem+' div.js-hiding>*').not('h3').hide();
}
function openTab(name) { $("h3#"+name).parent().filter('.hidden').find('>h3').click(); }
/**
 * Funkce pro preblikavaci box s nabidkou kurzu
 * /
var kurzy_animation_stopped = false;
var set_active_tab = function(hash) {
  if(!hash || !$("#kurzy-box div.tabbed-body "+hash).length) return true;
  $("#kurzy-box div.tabbed-body>div").hide().filter(hash).fadeIn();
  $("#kurzy-box ul.tabs li").removeClass('active').find("a[href$='"+hash+"']").parent().addClass('active');
  return true;
}
var set_next_tab_active = function() {
  if(!kurzy_animation_stopped) {
    var a = $('#kurzy-box ul.tabs li.active').next().find('a');
    if(!a.length) {
      a = $('#kurzy-box ul.tabs li:first a');
    }
    a.click();
  }
}
var init_active_box = function() {
  if($("#kurzy-box div.tabbed-body>div").length > 1) {
    $("#kurzy-box ul.tabs a").click(function() { return !set_active_tab(this.href.replace(/^[^#]+/,'')); });
    if(window.location.hash && $('#kurzy-box div.tabbed-body '+window.location.hash).length) {
      set_active_tab(window.location.hash);
    } else {
      set_active_tab($('#kurzy-box ul.tabs li:first a').attr('href'));
    }
    var switching = setInterval("set_next_tab_active()", 5000);
    $('#kurzy-box').mouseenter(function() {
      kurzy_animation_stopped = true;
    }).mouseleave(function() {
      kurzy_animation_stopped = false;
    });
  } else {
    $("#kurzy-box ul.tabs").hide();
  }
}
//*/

