$(document).ready(function()
{
  /*** DATE TIME PICKER ***/
  $('input.date').datepicker({
    yearRange: 'c-90:c',
    altFormat: 'yy-mm-dd',
    dateFormat: 'dd.mm.yy'
  });

  /*** COLORBOX ***/
  $('a.colorbox_img').colorbox({
    opacity: 0.85,
    slideshow: true,
    slideshowAuto: false,
    slideshowStart: 'Spustiť prezentáciu',
    slideshowStop: 'Zastaviť prezentáciu',
    rel: 'example',
    title: function() { return '<a href="' + this.rel + '" target="_blank" class="org_link">Stiahnuť obrázok</a>'; }
  });

  /*** LTModal ***/
  $('a.lt_modal').click(function()
  {
    if($('#lt_modal').length==0)
    {
      $('<div id="lt_modal"><iframe src="' + this.href + '" class="iframe"></iframe></div>').appendTo('body');
      $('#lt_modal').LTModal({ maskColor: '#fff', maskOpacity: 0.2, onClose: function() { $('#lt_modal').remove(); } });
    }

    return false;
  });

  /*** LTTabs ***/
  $('div.lt_tabs').LTTabs({ speed: 'slow' });

  /*** VIEWPORT ***/
  $('a.open_viewport,a.close_viewport').click(function()
  {
    var trigger = $(this);
    var box = $('#' + trigger.attr('rel'));

    if(trigger.hasClass('r')) { var o_height = 398; var c_heigth = 82; }
    else { var o_height = 438; var c_heigth = 125; }

    if(trigger.hasClass('close_viewport')) box.animate({ height: c_heigth }, 'slow', function() { trigger.hide(); trigger.siblings('.open_viewport').css('display', 'block'); box.parent().removeClass('open'); });
    else box.animate({ height: o_height }, 'slow', function() { trigger.hide(); trigger.siblings('.close_viewport').css('display', 'block'); }).parent().addClass('open');
  });

  /*** PLACEHOLDERS ***/
  $('input.placeholder').focus(function()
  {
    var trigger = $(this);
    if(!trigger.hasClass('off')) trigger.addClass('off').val('');
  });

  /*** HOME - CURRENT TIME ***/
  $('#home_clock').each(function() { setInterval(function() { $.post('/actions/get_time.php', {}, function(result) { $(this).text(result); }); }, 60000); });

  /*** HOME FLIGHT MAP HIGHTLIGHT ***/
  $('#flights_map_highlight').hover(
    function() { $('#box_arrivals,#box_departures').find('.flightradar').addClass('hightlight'); },
    function() { $('#box_arrivals,#box_departures').find('.flightradar').removeClass('hightlight');
  });

  /*** STATUS ***/
  $('div.status_success,div.status_warning,div.status_failed').each(function() { $(this).fadeIn(1500).delay(15000).fadeOut(1500); });

  /*** HOME WARNING ***/
  $('#home_warning').liScroll({travelocity: 0.05});

  /*** HOME TEXT SCROLLER ***/
  $('#home_text_scroll').children().each(function()
  {
    var trigger = $(this);

    if(trigger.hasClass('row_a')) var o = { velocity: 60, direction: 'horizontal', startfrom: 'right' };
    else if(trigger.hasClass('row_b')) var o = { velocity: 50, direction: 'horizontal', startfrom: 'left' };
    else if(trigger.hasClass('row_c')) var o = { velocity: 40, direction: 'horizontal', startfrom: 'right' };
    else if(trigger.hasClass('row_d')) var o = { velocity: 65, direction: 'horizontal', startfrom: 'left' };

    trigger.SetScroller(o);
  });

  $('#viewport_arrivals,#viewport_departures').each(function()
  {
    var trigger = this;
    setInterval(function() { RefreshHomeFlights(trigger); }, 60000);
  });

  /*** FORM VALIDATION ***/
  $('form.validation').submit(function()
  {
    var ret = true;

    $(this).find(':input').each(function()
    {
      $(this).removeClass('invalid');

      if(this.value=='' && this.className.indexOf('required')!=-1)
      {
        if(ret)
        {
          alert('Prosím zadajte hodnotu v poli "'+this.title+'"');
          this.focus();
          ret = false;
        }

        $(this).addClass('invalid');
      }
    });

    return ret;
  });

  /** FLIGHTS ***/
  $('a.show_flight').hover(function(e)
  {
    var trigger = $(this);
    var tooltip = $('#' + this.rel);

    var pos_left = trigger.offset().left;
		var pos_top = trigger.offset().top;

		pos_left += trigger.outerWidth() + 5;
    pos_top -= tooltip.outerHeight() - 2;

		if(/iPad/i.test(navigator.userAgent)) pos_top -= $(window).scrollTop();

		pos_left -= tooltip.outerWidth() + trigger.outerWidth();
		pos_top += (tooltip.outerHeight() + trigger.outerHeight()) / 2;

    tooltip.css({'left': pos_left, 'top': pos_top}).stop(true, true).fadeIn('fast');
  },
  function()
  {
    var popup = $('#' + this.rel).stop(true, true).hide();
  });

  $('input.ac_destination').autocomplete({
    source: '/actions/autocomlete.php?action=destinations',
    minLength: 2
  });
});

function RefreshHomeFlights(elem)
{
  var trigger = $(elem);
  var id = trigger.attr('id');
  var lang = trigger.attr('lang');
  var url = '/actions/ajax_flights.php?lang=' + lang;

  if(id=='viewport_departures') url += '&action=get_departures';
  else url += '&action=get_arrivals';

  $.post(url, function(data) { trigger.empty().append(data); });
}

