if (typeof Tn == 'undefined') {
    Tn = {};
} 
Tn = $.extend(Tn, {__q: 'Search...', __nl: 'Email...', config: {xebase: 'USD', currency: { code: 'USD', currency: 1},  xe: { USD: {code: 'USD', currency: 1}}},
datepicker: {
 defaults: {defaultDate: '+2', dateFormat: 'dd-mm-yy', gotoCurrent: true, numberOfMonths: 2,
            minDate: +2,
            maxDate: '+4M',
            onSelect: function(selectedDate) {
             /*  var option = this.id.match(/check-in\d+/i) ? "minDate" : "maxDate";
			    var option = "minDate";
                var instance = $(this).data("datepicker");
                var date = $.datepicker.parseDate(instance.settings.dateFormat || $.datepicker._defaults.dateFormat, selectedDate, instance.settings);
                Tn.crsDates.not(this).datepicker("option", option, date);
             */
            }
            }
}});
Tn.Utils = {};
Tn.Utils.xeUsd2xx = function (val) {
   return (Math.round(parseFloat(val)/(Tn.xe.currency)));
};

jQuery(document).ready(function(){
     Tn.lang = $('body:first').attr('lang')||'en';
     Tn.__q =  $("#q").attr('holder');
     Tn.__nl =  $("#nl-email").attr('holder');
	 
     $.ajaxSetup({"error":function(XMLHttpRequest,textStatus, errorThrown) {   
        alert(textStatus);
        alert(errorThrown);
        alert(XMLHttpRequest.responseText);
      }});
    var hoy = new Date();
      $('#date').html( hoy.getDate()+'/'+(hoy.getMonth()+1 )+'/'+ hoy.getFullYear());
	  
     $('#main-menu li').hover(function() {
        $(this).addClass('menuHover');
      }, function() {
        $(this).removeClass('menuHover');
      });
     $("#main-menu li ul").bgiframe({ src: "BLOCKED SCRIPT'&lt;html&gt;&lt;/html&gt;';" });
     $('a.ht-photo, a.car-photo, #view-more-photos, a.tr-photo').lightBox({});
    
     $("#nl-form").submit(function() {
        var pattern=/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
        var email = $('#nl-email').val(); 
        if (pattern.test(email)) {
         $('#newsletter').activity({outside: true, align: 'right', segments: 10, steps: 3, width:2, space: 0, length: 3, color: '#000', speed: 1.5});
            $('#nl-status').removeClass('error');
            /*$.ajax({
              type: "GET",
              url:  $("#nl-form").attr('action'),
              data: $("#nl-form").serialize(),
              beforeSend: function() {
              $('#nl-status').text(Tn.i18n[Tn.lang]['status'][2]).slideDown('slow');
              },
              error: function() {
              $('#nl-status').text(Tn.i18n[Tn.lang]['status'][1]).slideDown('slow');
              },
              success: function(data) { 
              $('#newsletter').activity(false);
              $('#nl-status').html(data).slideDown('slow');
              },
              complete: function() {
              
              setTimeout(function() {
              $('#nl-status').slideUp('slow');
              }, 8000);
              }
           });*/
		   return true;
        } else {
          $('#nl-status').text(Tn.i18n[Tn.lang]['status'][3]).slideDown('slow');
          $('#nl-status').addClass('error'); 
        }
        return false;
        });
// XE


  $("#xe-currency").change(function() {
       $.getJSON( 
         $("#xe-form").attr('action')+'?'+$("#xe-form").serialize(),
         function(data) {
           $.extend(Tn.config,data);
          // window.location = window.location;
         });
                
      });
	  

  $("#sel-flight-type").change(function() {
     if( $(this).val()== 1){ $("#check-out1-flight, #check-out-flight").hide();} else { $("#check-out1-flight, #check-out-flight").show();}
                
      });
           
		   
		   
   $("#crs-flight-from").change(function() {
   $('#crs-flight-to').attr("disabled","disabled");
       $.getJSON( 
         $("#crs-flight").attr('api_action')+'?'+$("#crs-flight").serialize()+ '&r=' + Math.ceil(Math.random() * 1000),
         function(json) {
            $('#crs-flight-to').html(json.arrival_airports_options_for_select_html);
			$('#crs-flight-to').removeAttr("disabled");
         });
                
      });
	  
  $("#q").focus(function() {
         if ($(this).val() == Tn.__q) $(this).val( '');
      }).blur(function() {
         if ($(this).val() == '') $(this).val(Tn.__q);
       });
       
    $("#nl-email").focus(function() {
         if ($(this).val() == Tn.__nl) $(this).val( '');
      }).blur(function() {
         if ($(this).val() == '') $(this).val(Tn.__nl);
       });

    // Preload Background image
        var bg_img = new Image();
        $(bg_img).load(function () {
             $('body:first').css({'background-image': 'url("'+$(this).attr('src')+'")'});
        }).error(function () {
            $('body:first').css({'background-color': '#eeeeee'});
        }).attr('src', '/images/bg/page.jpg');

    // CRS
    Tn.crsTabs();
   $.datepicker.setDefaults(Tn.datepicker.defaults);

        Tn.crsDates = $(".check-in, .check-out").datepicker($.datepicker.regional[Tn.lang]);
   $('.check-in').datepicker('option', {defaultDate: '+2',  dateFormat: 'dd-mm-yy',
            minDate: +2, maxDate: '+4M'});
   $('.check-out').datepicker('option', {defaultDate: '+5',  dateFormat: 'dd-mm-yy',
            minDate: +5,
            maxDate: '+4M'});
$(".crs-price").hide();
$("#crs-hotel-price-range").slider({
                        range: true,
                        min: 0,
                        max: 5000,
                        values: [$("#crs-hotel-min-price").val(), $("#crs-hotel-max-price").val()],
                        slide: function(event, ui) {
                                $("#crs-hotel-min-price").val( ui.values[0]);
                                $("#crs-hotel-max-price").val( ui.values[1]);
                                $('#show-hotel-price-range').html('('+ui.values[0]+','+ui.values[1]+')');
                        }
                });

$("#crs-car-price-range").slider({
                        range: true,
                        min: 0,
                        max: 5000,
                        values: [$("#crs-car-min-price").val(), $("#crs-car-max-price").val()],
                        slide: function(event, ui) {
                                $("#crs-car-min-price").val( ui.values[0]);
                                $("#crs-car-max-price").val( ui.values[1]);
                                $('#show-car-price-range').html('('+ui.values[0]+','+ui.values[1]+')');
                        }
                });

$("#btn-crs-hotel").click(function(e){
  $("#crs-hotel").submit();
}); 

$("#btn-crs-car").click(function(e){
  $("#crs-car").submit();
});         
Tn.loader.init();
$('.sel-crs-prod').change(function(){
$('.crs-prod').hide();
var id = $(this).attr('id').substring(4, 20); 
if($(this).is(':checked')) { $('#'+id).show();}

});

$('#show-hotel-price-range').html('('+$("#crs-hotel-min-price").val()+','+$("#crs-hotel-max-price").val()+')');
 $('#show-car-price-range').html('('+$("#crs-car-min-price").val()+','+$("#crs-car-max-price").val()+')');
   });


Tn.i18n = {
    en: {
        status: ['Loading...', 'Error!!!, Connection Problem. Please tried again', 'Sending...', 'Incorrect email format, please check it', 'Updating prices']
},
    es: {
        status: ['Cargando...', 'Ha ocurrido un error de conexión, por favor inténtelo nuevamente', 'Enviando...','Formato de correo no válido, por favor, corríjalo', 'Actualizando precios']
},
it: {
        status: ['Caricando', 'C\'e\' stato un errore di connessione, per favore intenti di nuovo', 'Inviando...','Indirizzo di posta elettronica non valido, per favore correggerlo', 'Caricando prezzo']
}
};
Tn.crsTabs = function(){
$('.crs-prod').hide();
$(".sel-crs-prod").each(function(k,_this){
var id = $(_this).attr('id').substring(4, 20); 
if($(_this).is(':checked')) { $('#'+id).show();}
});
};
var started = false;
$.extend(Tn, {
  loader: {
     update : function (text) {
     var obj = $("div#loadAnim");
      obj.html(text);
    },
    init : function (options) {
        var defaults = {  
            updateTime: 400,  // milliseconds
            message: "Loading..."  
        };  
        var options = $.extend(defaults, options);  
        if (started) {
            return;
        }
        $("body").append('<div id="loadAnim"></div>');
        var obj = $("div#loadAnim");
        obj.css("display", "none");
        obj.css("color", "#ffffff");
        obj.css("background-color", "#ff3333");
        obj.css("position", "absolute");
        obj.css("padding", "4px 4px 4px 30px");
        obj.css("left", "49%");
        obj.css("top", "0");
        obj.css("z-index", "9999");
        Tn.loader.update( options.message);
        obj.css("display", "none");
        obj.ajaxStart(function() {
         Tn.loader.start(); 
        }).ajaxStop(function() {
         Tn.loader.stop(); 
        });
  
    },
       start : function () {
        if (started) {
            return;
        }
      
        $("div#loadAnim").show();
        started = true;
        $("div#loadAnim").activity({outside: false, align: 'left', segments: 10, steps: 3, width:2, space: 0, length: 3, color: '#ffffff', speed: 1.5});
    },
    stop : function (options) {
        if (!started) {
            return;
        }
        started = false;
        $("div#loadAnim").hide();
        $("div#loadAnim").activity(false);
    }

}});

