function init_country_contacts_select() {
  $('#contactPays').show();
  $('.country-contacts li').hide();
  $('#contactPays select').change(function() {
    country_code =$('#contactPays select option:selected').attr('rel');
    $('.country-contacts li').hide();
    $('.country-contacts li.' + country_code).show();
  });
}

function init_news_select() {
    $('#affNews select').change(function() {
            window.location=$('#affNews select option:selected').attr('rel');
        }
    );
}

jQuery.fn.equalizeHeight = function(selector) {
  var max = 0;
  this.find(selector).each(function(){
    height = $(this).height();
    if (height > max){ max = height;}
  });
  this.find(selector).height(max);
};

function eventsHideallDay() {
    $('article.programme-results').hide();
}

function eventsShowAllDay() {
    $('article.programme-results').show();
}

function eventsTidyEmpty() {
    $('.programme-results').each(function(index) {
        if ($(this).find('tbody tr:visible').length === 0) {
            $(this).hide();
        }
    });
}

function buttonClose(){
  $('#fancybox-close').html('Fermer');
}

function tabs_detail_catalogue(){
  //$('#fancybox-close').html('Fermer');
  //alert("toto");
  // MENU SEARCH ENGINE
  //When page loads...
  $(".tab-content").hide(); //Hide all content
  $("#nav-modal li.active").show(); //Activate first tab
  var activeTab = $("#nav-modal li.active").find("a").attr("href"); //Find the href attribute value to identify the active tab + content
  //alert(activeTab);
  $(activeTab).show();
  
  //On Click Event
  $("#nav-modal li").click(function() {

	  $("#nav-modal li").removeClass("active"); //Remove any "active" class
	  $(this).addClass("active"); //Add "active" class to selected tab
	  $(".tab-content").hide(); //Hide all tab content

	  var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
	  $(activeTab).fadeIn(); //Fade in the active ID content
	  //alert(activeTab);
	  return false;
  });
}


function init_catalogue_pays_select() {
    $('#affAlpha select').change(function() {
            window.location=$('#affAlpha select option:selected').attr('rel');
        }
    );
    $('#affPays select').change(function() {
            window.location=$('#affPays select option:selected').attr('rel');
        }
    );
}

$(document).ready(function() {
  
  
  
  
  init_country_contacts_select();
  init_news_select();
  
  $(".stands").equalizeHeight(".item_light-box");
  
  //var $stands1 = $(".full").find(".stands:first");
  //var $stands2 = $(".full").find(".stands:first").next('.stands');
  
  $('.stands1').children('.item_light-box').children('.lightbox-plugin').attr('rel','group1');
  $('.stands2').children('.item_light-box').children('.lightbox-plugin').attr('rel','group2');
  $('.stands3').children('.item_light-box').children('.lightbox-plugin').attr('rel','group3');
  $('.stands4').children('.item_light-box').children('.lightbox-plugin').attr('rel','group4');
  
  //$stands2.children('.item_light-box').children('.lightbox-plugin').attr('rel','group2');
  
  $("a.lightbox-plugin").fancybox({
    'titlePosition' : 'over',
    'titleFormat' : function(title) {
      return '<span id="fancybox-title-over">' + title + '</span>';
    } 
  });
  
  
  
  // rollover pagination tablesorter
  $('#search-submit').hover(
    function() {
      this.src = this.src.replace( /\.gif$/, '_on.gif' );
    },
    function() {
      this.src = this.src.replace( /_on\.gif$/, '.gif' );
    }
  ); 
	
	
  $('table.tableau tr:nth-child(even)').addClass('pair');
  
  $('#gie dd').hide();

  $('#gie dt').toggle(function(){
     $(this).addClass("active");
     $(this).next().show();
  },function(){
     $(this).removeClass("active");
     $(this).next().hide();
  });
  
  
  $(".travel-contents li:odd").addClass('odd');
  
  
  
 $("#monvinexpo-search .splitted-buttons").buttonset();
  // $(".select-buttons").buttonset(); 
  
  // checkboxes image replacement
  $(".select-buttons input").change(function(){  
      if($(this).is(":checked")){  
          $(this).parent().addClass("selected");  
      }else{  
          $(this).parent().removeClass("selected");  
      }  
  }); 
  
  $('.programme-results tbody tr:nth-child(even)').addClass('pair');
  
  $("td.details a").fancybox({
    onComplete: tabs_detail_catalogue
  });
  


  //  filtre par jour
  $('.splitted-buttons input').click(function() {
        var val = $(this).val();
        
        if (val === 'all') {
            eventsShowAllDay();
            eventsTidyEmpty();
        }
        else {
            eventsHideallDay();
            $('article.' + val).show();
        }

      }
  );
/*
    $('li.close a').click(function() {
        $(this).fancybox.close();
     });
*/
    $('#tastingshortcut').click(function() {
        var val = $(this).attr('checked');
        eventsShowAllDay();
        if (val) {
           $('#monvinexpo-search select').attr('disabled', 'disabled');
           var val = 'Tastings by Vinexpo';
           $('.programme-results table span.tag').each(function(index) {
               if ($(this).text() !== val && val !== 'all') {
                   $(this).parent().parent().hide();
               }
               else {
                   $(this).parent().parent().show();
               }
           });
        }
        else {
           $('#monvinexpo-search select').removeAttr('disabled');
           $('.programme-results table span.tag').each(function(index) {
               $(this).parent().parent().show();
           });
        }
        eventsTidyEmpty();
    }
    );

   // filtre par 
    $('#monvinexpo-search select').change(function() {
            eventsShowAllDay();
            $('.splitted-buttons label').removeClass('ui-state-active');
            $('.splitted-buttons label:first').addClass('ui-state-active');
            var val =$('#monvinexpo-search select option:selected').val();
            $('.programme-results table span.tag').each(function(index) {
                if ($(this).text() !== val && val !== 'all') {
                    $(this).parent().parent().hide();
                }
                else {
                    $(this).parent().parent().show();
                }
            });
            eventsTidyEmpty();
        }
    );

    
    
    // MENU SEARCH ENGINE
    //When page loads...
    $(".tab_content").hide(); //Hide all content
    $("ul.tabs li.active").show(); //Activate first tab
    var activeTab = $("ul.tabs li.active").find("a").attr("href"); //Find the href attribute value to identify the active tab + content
    $(activeTab).show();
    
    //On Click Event
    $("ul.tabs li").click(function() {

		  $("ul.tabs li").removeClass("active"); //Remove any "active" class
		  $(this).addClass("active"); //Add "active" class to selected tab
		  $(".tab_content").hide(); //Hide all tab content

		  var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
		  $(activeTab).fadeIn(); //Fade in the active ID content
		  return false;
    });
    
    
    $('.select-holder .delete').click(function() {
        var parent = $(this).parent().prev();
        $('select',parent).attr('name', 'rub');
        $('select',parent).attr('disabled', '');
        $(this).parent().nextAll('div').remove();
        $(this).parent().remove();
        return false;
    });
    

  
    //  init_catalogue_pays_select();

    $('#produits-form').submit(function() {
        var val = $('.select-holder option:selected', $('#produits-form')).last().val();
        return val !== 'all';
    });

    $('#eventtip').dialog({hide: 'fade', buttons: []}).dialog( "widget" ).find( ".ui-dialog-titlebar-close" ).hide();

    setTimeout(function() {
            $('#eventtip').dialog("close");
        },
        1000
    );

    $('#guide-form').submit(function() {
        if($('#guide-form input[type=checkbox]:checked').length == 0) {
            alert($('#guide-form-alert').text());
            return false;
        }
    });

});


