$(document).ready(function()
{
	$('#menu > li > ul').each(function() {
		w = $(this).parent().width();
		$(this).width(w);
	});
	
	if ($('#symphony').length > 0) {
		symphony();
		
		$('#symphony input[name="journeyType"], #symphony input[name="pricerPreference"]').change(function() {
			symphony();
		});
		
		$('#symphony .submit').click(function() { $('#symphony form').submit(); });
	}
	
	$('#special-offers div#c').cycle({
	    fx:     'fade', 
	    speed:  500, 
	    timeout: 0, 
	    next:   '#next', 
	    prev:   '#prev'
	});
	
	$('#mailinglist, a[name="mailing"]').click(function() { $('#overlay').fadeIn(); });
	$('#mailinglist').click(function() { floater($('a', this).attr('rel')); });
	$('a[name="close"]').click(function() { $('#overlay').fadeOut(); });
	
	$('#check-my-trip').click(function() { window.location = $('a', this).attr('href'); });
	//$('#accomodation').click(function() { window.location = $('a', this).attr('href'); });
	//$('#eu').click(function() { window.location = $('a', this).attr('href'); });
	
	$('#page div.preview:last').addClass('bb').prev().addClass('bb');
	
	$('ul#menu li').hover(function() { $(this).addClass('s').find('ul:first').show(); }, function() { $(this).removeClass('s').find('ul:first').hide(); });
	
	$.datepicker.setDefaults($.datepicker.regional['sk']);
	
	$('.datepicker').datepicker({
		regional: 'sk',
		dateFormat: 'dd.mm.yy',
		showOn: 'both',
		buttonText: '',
		minDate: 1
	});
	
	$('.combo').combobox();
	
	$('#airports a.l').live('click', function() {
		$('#airports h3 span').fadeOut();
		elm = $(this);
		$('#airports').find('a._c, a._cc, ul.c, ul.cc, ul.cca').remove();
		$.getJSON('ajax/ajax.php' + $(this).attr('href'), function(data) {
			$(elm).after(data.items);
			$('#airports ul.c').find('img').batchImageLoad({
				loadingCompleteCallback: function() { $('#airports ul.c').slideDown(100); }
			});
		});
		return false;
	});
	
	$('#airports a.c').live('click', function() {
		$('#airports h3 span.cc').fadeOut();
		elm = $(this);
		if ($(elm).hasClass('on')) {
			$('#airports a._c').click();
		} else {
			$(elm).addClass('on');
			$('a', $(elm).parent().siblings()).removeClass('on').parent().hide();
			$('#airports h3 span.c').fadeIn();
			$('#airports a._c').fadeIn(100);
			$('#airports ul.cc').remove();
			$.getJSON('ajax/ajax.php' + $(this).attr('href'), function(data) { $(elm).parent().parent().parent().append(data.items); $('#airports ul.cc').slideDown(100); });
		}
		return false;
	});
	
	$('#airports a.cc').live('click', function() {
		$('#airports h3 span.cc').fadeIn();
		elm = $(this);
		if ($(elm).hasClass('on')) {
			$('#airports a._cc').click();
		} else {
			$(elm).addClass('on');
			$('a', $(elm).parent().siblings()).removeClass('on').parent().hide();
			$('#airports h3 span.cc, #airports a._cc').fadeIn(100);
			$('#airports ul.cca').remove();
			$.getJSON('ajax/ajax.php' + $(this).attr('href'), function(data) { $(elm).parent().parent().parent().append(data.items); $('#airports ul.cca').slideDown(100); });
		}
		return false;
	});
	
	$('#airports a.cca').live('click', function() {
		$('#symphony input[name="' + $('#airports input[name="field"]').val() + '"]').val($(this).attr('rev'));
		$('#symphony input[name="_' + $('#airports input[name="field"]').val() + '"]').val($(this).attr('rel'));
		destroyFloater();
		return false;
	});
	
	$('#airports a._c').live('click', function() { // cancel country
		$('#airports h3 span').fadeOut();
		$(this).next().nextAll().remove();
		$(this).fadeOut(50, function() { $('a', $(this).next()).removeClass('on').parent().show(); });
		return false;
	});
	
	$('#airports a._cc').live('click', function() { // cancel city
		$('#airports h3 span.cc').fadeOut();
		$(this).next().nextAll().remove();
		$(this).fadeOut(50, function() { $('a', $(this).next()).removeClass('on').parent().show(); });
		return false;
	});
	
	$('#airports a.fa').live('click', function() {
		val = ($('#symphony input[name="airlines"]').val() == '')
			? $(this).attr('rev')
			: $('#symphony input[name="airlines"]').val() + ', ' + $(this).attr('rev');
		$('#symphony input[name="airlines"]').val(val);
		destroyFloater();
		return false;
	});
	
	$('#symphony-wrapper-trigger').click(function() {
		$(this).remove();
		$('#symphony-wrapper').slideDown();
		$.scrollTo('#symphony-wrapper', 100);
		return false;
	});
});

function symphony() {
	if ($('#symphony input[value="OW"]').is(':checked')) { $('#symphony .ow').hide(); } else { $('#symphony .ow').show(); }
	if ($('#symphony input[value="OJ"]').is(':checked')) { $('#symphony .oj').show(); } else { $('#symphony .oj').hide(); }
	if ($('#symphony input[value="AVB"]').is(':checked')) { $('#symphony .avb').css('display', 'inline-block'); } else { $('#symphony .avb').css('display', 'none'); }

	ac($('#dep0'));
	ac($('#dep1'));
	ac($('#arr0'));
	ac($('#arr1'));
}

function ac(elm) {
	$(elm).autocomplete({
		source: '/ajax/search-airport.php',
		minLength: 2,
		select: function(event, ui) {
			$(elm).val(ui.item.label);
			$('input[name="' + $(elm).attr('id') + '"]').val(ui.item.value);
			return false;
		},
		focus: function(event, ui){
			$(elm).val(ui.item.label);
			$('input[name="' + $(elm).attr('id') + '"]').val(ui.item.value);
			return false;
        },
		open: function(event, ui){
            $('.ui-autocomplete').css('top', $(elm).offset().top + $(elm).outerHeight() + 2).css('z-index', 100);
        }
	}).data( "autocomplete" )._renderItem = function( ul, item ) {
		return $( "<li></li>" )
		.data( "item.autocomplete", item )
		.append( '<a><img src="/data/flags/' + item.icon + '"/> ' + item.label + '</a>')
		.appendTo( ul );
	};
}

function floater(query) {
	var arrPageSizes = ___getPageSize();
	var arrPageScroll = ___getPageScroll();
	
	$.getJSON('ajax/ajax.php' + query, function(data) {
		$('.floater').fadeOut(200, function() { $(this).remove(); });
		$('body').prepend(data.floater);
		$('.floater').find('img').batchImageLoad({
			loadingCompleteCallback: function() {
				_top = arrPageScroll[1] + (arrPageSizes[3] - $('.floater').height()) / 2;
				//_top = arrPageScroll[1] + arrPageSizes[3] / 6;
				_left = (arrPageSizes[2] - $('.floater').width()) / 2;
				$('.floater').fadeIn(200).css({top:_top, left:_left});
			}
		});
		$('.floater form').ajaxForm({
			dataType:  'json',
			beforeSubmit: function() {
				$('.floater .message').fadeOut().remove();
			},
			success: function(responseText, statusText, x, form) {
				$.each(responseText, function(elmID, html) {
					$('#' + elmID).html(html);
				});
				if (responseText.ok) $('.floater').delay(1000).fadeOut();
			}
		});
	});
}

function destroyFloater() {
	$('.floater').fadeOut(function() { $(this).remove(); });
}
