
$(document).ready(function() {

  $('a.top').click(function(){
    $('html,body').animate({scrollTop:0},'slow');
    return false;
  });

  // Zebra tables: fitxa
  $('#fitxa table tr:nth-child(even)').addClass('alt');
  $('#fitxa table tr td:first-child').addClass('title');


  // Selecció idioma
  $('#idioma > .btn-slide').click(function(){
    $('#panel').slideToggle('medium');
    $(this).toggleClass('active'); return false;
  });
  $('a:not(.btn-slide)').click(function(){
    $('#panel').hide();
    $('#idioma > .btn-slide').removeClass('active');
  });
  $('#idioma').click(function(){
    $('#panel').slideUp('medium');
    $('#idioma > .btn-slide').removeClass('active');
  });
  $('body:not(#idioma)').click(function(){
    $('#panel').slideUp('medium');
    $('#idioma > .btn-slide').removeClass('active');
  });


  // Enviar Formulari
  $('.send_form').click(function(evento) {
    $('#'+$(this).parents("form").attr("id")).submit();
    evento.preventDefault();
  });


  // Validar i enviar formulari
  $('.check_form').click(function(evento) {
    if(check_form()) {
      $('#'+$(this).parents("form").attr("id")).submit();
    } else {
      evento.preventDefault();
    }
  });


  // Netejar formulari
  $('.delete_form').click(function(evento) {
    delete_form();
    evento.preventDefault();
  });


  // Galeries imatges: colorbox!!!
  $("a[rel='colorbox']").colorbox({});
  $("a[rel^='colorbox-gal']").colorbox({});
  $("a[rel='colorbox-html']").colorbox({fixedWidth:"540",fixedHeight:"560", iframe:true});
  $("a[rel='colorbox-html-form']").colorbox({fixedWidth:"540",fixedHeight:"700", iframe:true});
  $("a[rel='colorbox-html-fitxa']").colorbox({fixedWidth:"750",fixedHeight:"700", iframe:true});

  $('.open-gal').click(function(){
    $("a[rel='colorbox-gal']:first").click();
    return false;
  });


  // Tooltip
  $('a.tooltip').qtip({
    position: {
      corner: {
        target: 'topMiddle',
        tooltip: 'bottomMiddle'
      },
      adjust: {
        y:7
      }
    },
    style: {
      name: 'dark',
      background: '#303030',
      padding: '10px 10px',
      width: {
        max: 210,
        min: 0
      },
      tip: true
    }
  });                      


  // Tabs menu
  $('a.tab').click(function () {
    $('#menu_fitxa a.current').removeClass('current');
    $(this).addClass('current');

    $('.tab_content').hide();

    var mostra = $(this).attr('rel');
    $('#'+mostra).show();
    return false;
  });


  // Historial toogle
  $("a.history").click(function () {
      var mostra = $(this).attr('rel');
      $('#'+mostra).toggle();
  });


  // Slider
  $('.anythingSlider').anythingSlider({
    easing:"easeInOutExpo",        // Anything other than "linear" or "swing" requires the easing plugin
    autoPlay:true,                 // This turns off the entire FUNCTIONALY, not just if it starts running or not.
    delay:4500,                    // How long between slide transitions in AutoPlay mode
    startStopped:false,            // If autoPlay is on, this can force it to start stopped
    animationTime:600,             // How long the slide transition takes
    buildNavigation: false         // If true, builds and list of anchor links to link to each slide
  });


  // Innerfade
  $('ul#portfolio').innerfade({
    speed:1000,
    timeout:5000,
    type:'random_start',
    containerheight:'360px'
  });


  // Innerfade
  $('ul#portfolio_order').innerfade({
    speed:1000,
    timeout:5000,         
    containerheight:'360px'
  });


  // Lava menu: x hover, mostrar submenus, moure box
  var style = 'easeOutExpo';

  // Definim posició inicial box
  if ($('#lava li.selected').length) {
    var default_left = Math.round($('#lava li.selected').offset().left - $('#lava').offset().left) + 3;
    var default_width = $('#lava li.selected').width() - 28;
  } else {
    var default_left = '-200px';
    var default_width = '50px';   // Més petit dona errors en IE :(
  }
  $('#box').css({left: default_left});
  $('#box .head').css({width: default_width});
  // Sombra
  $('#box2').css({left: default_left});
  $('#box2 .head2').css({width: default_width});

  $("#menu_header li.level_top").hover(function(){
    // Submenus
    if(this.className.indexOf("selected") == -1)  {
      getCurrent = $(this).parent().children("li.selected:eq(0)");
      if (getCurrent = 1 ) {
        $(this).parent().children("li.selected:eq(0)").children("ul").hide();
      }
    }
    $(this).children("ul:eq(0)").show();

    // Box
    left = Math.round($(this).offset().left - $('#lava').offset().left) + 3;
    width = $(this).width() - 28;
    $('#box .head').stop(false, false).animate({width:width},{duration:500, easing: style});
    $('#box').stop(false, false).animate({left: left},{duration:500, easing: style});

  },function(){
    // Submenus
    if(this.className.indexOf("selected") == -1)  {
      $(this).children("ul:eq(0)").hide();
    }
  });

  // Si sortim del menú posicions inicials
  $('#lava > ul').mouseleave(function () {
    // Definim posicions per box
    $('#box .head').stop(false, false).animate({width: default_width},{duration:500, easing: style});
    $('#box').stop(false, false).animate({left: default_left},{duration:500, easing: style});
    // Mostrem submenu x defecte
    $('#lava li.selected').children("ul:eq(0)").show();
  });

  // Menu categories: ampliar imatge --> footer!!!
  $('.footer_over').hover(function(){
    $('#cat_general').hide();
    var mostra = $(this).attr('rel');
    $('#'+mostra).show();
  },function(){
    var mostra = $(this).attr('rel');
    $('#'+mostra).hide();
    $('#cat_general').show();
  });

  // Menu Aux. -- School of Champions
  $('#menu_competicio a').click(function() {
    if ($(this).filter('.collapsed').length) {
      // Encongim expanded
      $('#menu_competicio a.expanded').addClass('collapsed').removeClass('expanded').find('+ ul').slideToggle('medium');
      // Allarguem collapsed
      $(this).addClass('expanded').removeClass('collapsed').find('+ ul').slideToggle('medium');
    }
  });

});

