setContainerHeight = 220 ;

jQuery(document).ready ( init );

function init()
{
	var a = $('input:checked');
	var query = '';
	
	for( i=0; i<a.length; i++ ) {
		
		query += '&search_dest[]=' + $('input:checked:eq(' + i + ')').val();
	}

	if( $('#search_text').length > 0 ) {
		$('#search_text').autocomplete('/autocomplete.php?' + query);
	}
	
	// vanoce-silvestr.php
	$("#hotel").change(function () {
	    var tour_id = this.value;
	    loadTerms(tour_id);
	});

	$("#adults_count").change(function () {
	    var count = this.value;
	    var sibs  = $("tr.adults_sibs").size();    // count of adult siblings
	    showPersonNameFields(count, 'adults', sibs);
	});

	$("#children_count").change(function () {
	    var count = this.value;
	    var sibs  = $("tr.children_sibs").size();    // count of children siblings
	    showPersonNameFields(count, 'children', sibs);
	});

	// darkovy-poukaz.php
	$("#gift_value").change(function () {
	    var val   = this.value;
	    var value = $("#own_gift_value");
	    var text  = $("#own_gift_text");

	    if (val < 7)
	    {
		value.css('visibility', 'hidden');
		text.css('visibility', 'hidden');
	    }
	    else
	    {
		value.css('visibility', 'visible');
		text.css('visibility', 'visible');
	    }
	});

	$("#same_addr").click(function () {
	    var is_checked = this.checked;
	    var elements   = $(".deliver_info");

	    if (is_checked)
	    {
		elements.attr('disabled', 'disabled');
		elements.val('')
	    }
	    else
	    {
		elements.attr('disabled', '');
	    }
	});
	
	//ajaxContact();
	ajaxNewsletter();

	$('#foto').innerfade({
			speed: 1500, timeout: 10000, type: 'sequence', containerheight: setContainerHeight + 'px'
		});
}

/***********************
 * vanoce-silvestr.php *
 ***********************
function loadTerms(tour_id)
{
    $.post("vanoce-silvestr-ajax.php",
	    {tour_id: tour_id},
	    function(html){
	    $("#term_pane").html(html);
	    });

    return false;
}

function showPersonNameFields(count, type, sibs)
{
    $.post("inputs_creator.php",
	    {count: count,
	     type : type,
	     sibs : sibs
	    },
	    function(html)
	    {
		if (sibs > 0)
		{
		    if (sibs < count)
		    {
			$("tr." + type + "_sibs:last").after(html);
		    }
		    else
		    {
			for (var i = 0; i < (sibs - count); i++)
			{
			    $("tr." + type + "_sibs:last").remove();
			}
			if (count == 0)
			{
			    $("#" + type + "_field").html(html);
			}
		    }
		}
		else
		{
		    $("#" + type + "_field").html(html);
		}
	    });

    return false;
}


/**********************
 * darkovy-poukaz.php *
 **********************/


function ajaxNewsletter()
{
	$("#newsletter form").submit(function () {	
		
		$.post(
			"/js/newsletter-ajax.php?send=odebírat",
			$("#newsletter form").serialize(),
			function(html){
    			$("#newsletter").html(html);
    			ajaxNewsletter();

    			$(".message").fadeTo( 2000, 0.99, function() {
    				$(this).fadeOut( 2000, function() {
    					$(this).remove();
    				});
    			});
  			});

		return false;
    });
}


function ajaxContact(type)
{
	$("#contactform form").submit(function () {	
		
		$.post(
			"/js/contact-form-ajax.php?send=Odeslat&type=" + type,
			$("#contactform form").serialize(),
			function(html){
				/*$("#contactform").fadeTo( "fast", 0.3, function(){
					$(this).html(html).fadeTo("fast",1);
				});
				
    			ajaxContact();*/
				$("#contactform").html(html);
				ajaxContact();
  			});
  			
		return false;
    });
} 
