
$(document).ready(function() {

  $(document).bind("contextmenu",function(e){
    return false;
  });

  $('a.top').click(function(){
    $.scrollTo('body',800);
    return false;
  });

  $('#fires ul, #ofertes ul').innerfade({
    speed:1000,
    timeout:5000
  });

  $('#language').change(function() {
    location.href= $('#language option:selected').attr('title');
  });

  $('#currency, #manufacturer, #country, .radio_default').change(function() {
    $('#'+$(this).parents("form").attr("id")).submit();
  });

  $('.send_form').click(function() {
    $('#'+$(this).parents("form").attr("id")).submit();
  });

  $('.check_form').click(function(evento) {
    if(check_form()) {
      $('#'+$(this).parents("form").attr("id")).submit();
    } else {
      evento.preventDefault();
    }
  });

  $('.delete_form').click(function(evento) {
    delete_form();
    evento.preventDefault();
  });        

  $("a[rel='colorbox']").colorbox({});
  $("a[rel='colorbox-gal']").colorbox({});
  $("a[rel='colorbox-html']").colorbox({fixedWidth:"542",fixedHeight:"550", iframe:true});
  $("a[rel='colorbox-form']").colorbox({fixedWidth:"559",fixedHeight:"680", iframe:true});
  $("a[rel='colorbox-form-short']").colorbox({fixedWidth:"542",fixedHeight:"385", iframe:true});

  // Obrir galeria
  $('.open-gal').click(function(){
    $("a[rel='colorbox-gal']:first").click();
    return false;
  });

  // Buscador actiu + text default
  var search_aux = $('#search').attr("value");
  if (search_aux == "") search_aux = "Search";

  $('#search').focus(function(){
    if($(this).attr("value") == "Search") $(this).removeClass('blank').attr("value", "");
  });

  $('#search_form').click(function(e){
    e.stopPropagation();
  });

  $(document).click(function(){
    if (search_aux == "Search") {
      if($('#search').attr("value") == "") $('#search').addClass('blank').attr("value", search_aux);
    } else {
      if($('#search').attr("value") == "") $('#search').attr("value", search_aux);
    }
  });

  $('#search_button').click(function() {
    if ($('#search').attr("value") == "Search" || $('#search').attr("value") == "") {
    } else {
      $('#'+$(this).parents("form").attr("id")).submit();
    }
  });

  // 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) + 1;
    var default_width = $('#lava li.selected').width() - 4;
  } else {
    var default_left = '-200px';
    var default_width = '50px';   // Més petit dona errors en IE :(
  }
  $('#box').css({left: default_left, width: default_width});
  // Sombra
  $('#box2').css({left: default_left, width: default_width});

  $("#menu_header li.level_top").hover(function(){
    // Submenus
    if(this.className.indexOf("selected") == -1)  {
      var 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
    var left = Math.round($(this).offset().left - $('#lava').offset().left) + 1;
    var width = $(this).width() - 4;
    $('#box').stop(false, false).animate({left: left, width:width},{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').stop(false, false).animate({left: default_left, width: default_width},{duration:500, easing: style});
    // Mostrem submenu x defecte
    $('#lava li.selected').children("ul:eq(0)").show();
  });      

  $('#menu_categories a').click(function() {
    if ($(this).filter('.collapsed').length) {
      $(this).addClass('expanded').removeClass('collapsed').find('+ ul').slideToggle('medium');
    } else if ($(this).filter('.expanded').length) {
      $(this).addClass('collapsed').removeClass('expanded').find('+ ul').slideToggle('medium');
    }
  });

  // 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
  });

  // 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();
  });

  $('.corner').corner();

  $('.print-fitxa').click(function(){
    $("#fitxa").jqprint();
    return false;
  });

  //Tooltip
  $ ('.cart_tip').each (function (){
    $(this).qtip ({
      content: $('span', this).html(),
      show: { delay: 100 },
      position: { corner: { target: 'topLeft', tooltip: 'bottomMiddle' }, adjust: { x:82, y: 0 } },
      style: {
        padding:0,
        width: 220,
        height: 160,
        color: '#fff',
        background: '#666',
        border: {
          width: 5,
          color: '#666'
          },
        tip: 'bottomMiddle'
      }
    });
  });

});


