var glow;

$(document).ready(function()
{
	$('#date_start, #date_end').date_input();

	$('#type_search1').click(function()
	{
		$('#country1, #country2, #country3').show();
		$('#city1, #city2, #city3, #city4').hide();
	});

	$('#type_search2').click(function()
	{
		$('#city1, #city2, #city3, #city4').show();
		$('#country1, #country2, #country3').hide();
	});

	$('#depart').change(function()
	{
		$('#country_sel, #city_sel').html('');

		$.getJSON('cities.php?getCountries&depart='+$(this).val(), function(data)
		{
			var options = '';

			for(var i in data)
			{
				options += '<option value="'+data[i].id+'">'+data[i].name+'</option>';
			}

			$('#country_all, #country_all2').html(options);
			$('#city_all').html('');
		});
	});

	$('#country_all').click(function()
	{
		glow = 1;
		var depart = $('#depart').val();

		$.getJSON('cities.php?getCities&country='+$(this).val()+'&depart='+depart, function(data)
		{
			var options = '';

			for(var i in data)
			{
				options += '<option value="'+data[i].id+'">'+data[i].name+'</option>';
			}

			$('#city_all').html(options);
		});
	});

	$('#city_all').change(function()
	{
		clearInterval(glow);

		glow = setInterval('$.pulseitBut("add_city")', 500);
	});

	$('#country_all2').change(function()
	{
		clearInterval(glow);

		glow = setInterval('$.pulseitBut("add_country")', 500);
	});

	$.pulseitBut = function(but)
	{
		var elem = $('#' + but + ' div div');

		if(elem.hasClass('but_glow'))
		{
			elem.removeClass('but_glow');
		}
		else
		{
			elem.addClass('but_glow');
		}
	};

	$('#add_country').click(function()
	{
		var elem = $('#country_all2 option:selected');
		if (elem.val())
		{
			var option = '<option value="'+elem.val()+'">'+elem.html()+'</option>';

			$('#country_sel').append(option);

			clearInterval(glow);
			$(this).removeClass('but_glow');
		}
	});

	$('#add_city').click(function()
	{
		var elem = $('#city_all option:selected');

		if (elem.val())
		{
			var option = '<option value="'+elem.val()+'">'+elem.html()+'</option>';

			$('#city_sel').append(option);

			clearInterval(glow);
			$(this).removeClass('but_glow');
		}
	});

	$('#del_country').click(function()
	{
		$('#country_sel option:selected').remove();
	});

	$('#del_city').click(function()
	{
		$('#city_sel option:selected').remove();
	});

	$('#clear_country').click(function()
	{
		$('#country_sel').html('');
	});

	$('#clear_city').click(function()
	{
		$('#city_sel').html('');
	});

	$('#searchBut').click(function()
	{
		if ($('#type_search1').prop('checked'))
		{
			$('#country_sel option').each(function()
			{
				$(this).prop('selected', true);
			});
		}
		else
		{
			$('#city_sel option').each(function()
			{
				$(this).prop('selected', true);
			});
		}
	});
});

function createPagesLine(total, sel)
{
	var pages = '';

	for (p = 1; p <= total; p++)
	{
		if (p == sel) pages += '<a href="" class="page current">' + p + '</a> ';
		else pages += '<a href="" class="page">' + p + '</a> ';
	}

	return pages;
};
