jQuery(function() {
		jQuery("#departure").datepicker({dateFormat: 'dd.mm.yy', minDate: 0});
});

function getCatalogues()
{
	categoryId = jQuery("#categories").val();
	emptyChildElements('category');
	if(categoryId == '') {
		return;
	}
	
	showProgressIcon('on');
	jQuery.getJSON("/index.php?eID=tx_inetia_travel_pi1&a=ca&cid="+categoryId,
		function(data){
		jQuery("#catalogues").children().remove();
			if(data != 'empty') {
				jQuery("#catalogues").append('<option value=""></option>');
				jQuery.each(data.items, function(i,item){
					jQuery("#catalogues").append('<option value="'+item.uid+'">'+item.name+'</option>');
	        	});
          	} else {
          		jQuery("#catalogues").append('<option value="">&nbsp;</option>');
          	}
		});
	showProgressIcon('off');
}

function getCountries()
{
	
	catalogId = jQuery("#catalogues").val();
	emptyChildElements('catalog');
	if(catalogId == '') {
		return;
	}
	
	showProgressIcon('on');
	jQuery.getJSON("/index.php?eID=tx_inetia_travel_pi1&a=co&caid="+catalogId,
        function(data){
          	jQuery("#countries").children().remove();
          	jQuery("#countries").append('<option value=""></option>');
			if(data != 'empty') {
				jQuery.each(data.items, function(i,item){
	            	jQuery("#countries").append('<option value="'+item.uid+'">'+item.name+'</option>');
	          	});
          	} else {
          		jQuery("#countries").append('<option value="">&nbsp;</option>');
          	}
		});
		
	showProgressIcon('off');
}

function getCities()
{
	countryId = jQuery("#countries").val();
	emptyChildElements('country');
	if(countryId == '') {
		return;
	}
	
	showProgressIcon('on');
	jQuery.getJSON("/index.php?eID=tx_inetia_travel_pi1&a=ci&ciid="+countryId,
        function(data){
          	jQuery("#cities").children().remove();
          	jQuery("#cities").append('<option value=""></option>');
			if(data != 'empty') {
				jQuery.each(data.items, function(i,item){
	            	jQuery("#cities").append('<option value="'+item.uid+'">'+item.name+'</option>');
	          	});
          	} else {
          		jQuery("#cities").append('<option value="">&nbsp;</option>');
          	}
		});
		
	showProgressIcon('off');
}

function emptyChildElements(n)
{
	switch(n)
	{
		case 'category':
		  jQuery("#catalogues").children().remove();
		  jQuery("#countries").children().remove();
		  jQuery("#cities").children().remove();
		  break;    
		case 'catalog':
		  jQuery("#countries").children().remove();
		  jQuery("#cities").children().remove();
		  break;
  		case 'country':
		  jQuery("#cities").children().remove();
  		  break;
  		case 'city':
  		
  		  break;
		default:
		  
	}
}

function showProgressIcon(state)
{
	if(state == 'off') {
		jQuery("#progress").css({"visibility" : "hidden",
							"display" : "none"
							});
		return;
	}
	
	jQuery("#progress").css({"visibility" : "visible",
						"display" : "block"
						});
}
