$(document).ready(function() {
  
  $("#contact-phone-company").live('click',function(){
    $("#contact-phone-form").show();
  });
  
  $("#close-pop").live('click',function(){
    $("#contact-phone-form").hide();
  });

  $("#contact-phone-forms").submit(function(){

    var name = $("#calls_title").val(),
        contact = $("#calls_phone").val(),
        comment = $("#calls_comment").val();
    if(name && contact && comment){
      $.ajax({
        type: "POST",
        url: "/tours/phone",
        data: "name=" + name + "&contact=" + contact + "&comment=" + comment,
        success: function(data){
          $("#contact-phone-forms").fadeOut('fast');
          $("#contact-phone-form h3").fadeIn('slow');
        }
      });
      
    } else { alert("Нужно заполнить все поля формы!"); }
    return false;
  });

  
  $("#order-form").submit(function(){

    var tour = $("#zakaz_tour_id").val(),
        name = $("#zakaz_name").val(),
        contact = $("#zakaz_contact").val(),
        comment = $("#zakaz_comment").val();
    if(tour && name && contact && comment){
      $.ajax({
        type: "POST",
        url: "/tours/zakaz",
        data: "tour=" + tour + "&name=" + name + "&contact=" + contact + "&comment=" + comment,
        success: function(data){
          $("#order-form").fadeOut('fast');
          $(".tour-order-form h3").fadeIn('slow');
        }
      });
      
    } else { alert("Нужно заполнить все поля формы заказа!"); }
    return false;
  });
  
  $("#plupperButton").click( function() { plupper.start('galar@plupper.com'); });
  
  $("#form-tour-filter").change(function(){
    
    var prices = $("#slider-range").slider("values");
    
    
    var data_country = "&" + $(this).serialize(),
        special = $("#special").attr('checked');
    
    var rest = [];
    $(".rest-type:checked").each(function(index){
      rest.push($(this).val());
    });

    var arr = data_country.split("&countries=");
    arr = arr.slice(1);
    
    $.ajax({
      type: "POST",
      url: "/tours/result",
      data: 'countries=' + arr + "&special=" + special + "&rest=" + rest + "&prices=" + prices,
      success: function(data){
        $("#tours-panel").fadeOut('500');
        setTimeout(function(){
          $("#tours-panel").html(data);
          $("#tours-panel").fadeIn('1000');
        },500);
      }
    });
  });
  
  
  
  $("#content-topics-tabs ul li span:first").addClass('current-select');
  
  $("#content-topics-nodes .content-topics-node").hide();
  $("#content-topics-nodes ." + $("#content-topics-tabs ul li:first span").attr('id')).show();
    
  $("#content-topics-tabs ul li span").live('click',function(){
    
    $("#content-topics-tabs ul li span").removeClass('current-select');
    $(this).addClass('current-select');
    
    $("#content-topics-nodes .content-topics-node").hide();
    $("#content-topics-nodes ." + $(this).attr('id')).show();
  });
  
  
  $('#countries-set a').live('click',function(){
    var country = $(this).attr('slug'),
        method = $("#content-proposition ul li span.special").attr('method');
    
    $.ajax({
      type: "POST",
      url: "/tours/filter",
      data: "country=" + country + "&method=" + method,
      success: function(data){
        $("#content-proposition-tours table").html(data);
      }
    });

    
    $("#countries-set a").removeClass('current-select');
    $(this).addClass('current-select');
    return false;
  });
  
  $("#content-proposition ul li span").live('click',function(){

    var country = $("#countries-set a.current-select").attr('slug'),
        method = $(this).attr('method');
    
    $.ajax({
      type: "POST",
      url: "/tours/filter",
      data: "country=" + country + "&method=" + method,
      success: function(data){
        $("#content-proposition-tours table").html(data);
      }
    });
    
    $("#content-proposition ul li span").removeClass('special');
    $(this).addClass('special');
    
  });
});
