
$(document).ready(function() { countryList(); });


var countryList = function() {
    $.getJSON("/php/_search_select.php", { action : 'countryList', tourType :$("input[name=tourType]:checked").val(),time : (new Date()).getTime() },function(data){
            $('#tourCountry').empty();
            $('#tourLocality').empty().hide();

            $('#tourCountry').append('<option value="">Kérem válasszon</option>');
            if (data) {
                $.each(data, function(index,itemData) {
                    var newOption = '<option value="'+ itemData.countryID +'">'+ itemData.countryName +'</option>';
                    $('#tourCountry').append(newOption);
        	});
            }
    })
}

var localityList = function() {
    $.getJSON("/php/_search_select.php", { action : 'localityList', tourType :$("input[name=tourType]:checked").val(),tourCountry :$("#tourCountry option:selected").val(),time : (new Date()).getTime() },function(data){
            $('#tourLocality').hide();
            $('#tourLocality').empty();
            if (data) {
                $.each(data, function(index,itemData) {
                    var newOption = '<option value="'+ itemData.localityID +'">'+ itemData.localityName +'</option>';
                    $('#tourLocality').append(newOption);
        	});
            $('#tourLocality').show();
            }
    })
}

var loadList = function() {

    if ($("#tourCountry option:selected").val()) {

	$.post('/php/_search_toru.php?'+(new Date()).getTime(), $("#searchForm").serialize(),function(data){
	if (data) {
        $('#loader').show();
	    var newItem=''
		
		$.each(data, function(num,item) {
		    newItem = '<div class="leiras" id="'+item.id+'">';
		     if (item.photo) { newItem+='<img src="'+item.photo+'" alt="" hspace="5" align="left">'; }
		     newItem+='<strong>Megnevezés:</strong>'+item.title+'<br>';
		     if (item.star) { newItem+='<strong>Csillagok:</strong>'+item.star+'<br>'; }
		     if (item.category) { newItem+='<strong>Kategória:</strong>'+item.category+'<br>'; }
		     newItem+='<strong>Ország:</strong>'+item.country+'<br>\
		    <strong>Országrész:</strong>'+item.locality+'<br>\
		    <strong>Leírás: </strong>'+item.description+'<br>\
		    <a href="'+item.url+'" class="reszletek" target="_blank">Részletek>></a>\
		    </div>';
		    if ($(".leiras:last").attr("id")) { $(".leiras:last").after(newItem); } else { $('.main').append(newItem); }
		});
        $('#loader').fadeOut(2000, "linear");
	}
	}, "json");
    
    }
}


$(function() {
    $('#tourCountry').change(function() { $('#lastID').val(0); localityList(); });
    $('#tourLocality').change(function() { $('#lastID').val(0); });

    $("input[name='tourType']").change(function(){ $('#lastID').val(0); $('#tourCountry option[value=""]').attr("selected","selected"); countryList(); }); 




    $('#searchForm').submit(function() {
    if ($("#tourCountry option:selected").val()) {
	$('.main').empty();
	$('#lastID').val(0);
	loadList();
    }
	return false;
    });

$(window).scroll(function(){
        if  ($(window).scrollTop() == $(document).height() - $(window).height()){
	$('#lastID').val($(".leiras:last").attr("id"));
	loadList();
       }
}); 

});
