var cAvailability = function() {

	this.init = function(){

		$('.throbber').hide();

		this.default_address = "Město, Ulice, čp.";
		this.input_address = $("#form_address input:first");
		this.input_address.unbind('click');
		this.input_address.bind('click', function(){
			if ($(this).val() == Availability.default_address) {
				$(this).val('');
			}
		});
		if (this.input_address.val() == '') this.input_address.val(this.default_address);

		this.createAddress();

		$('#availability select').bind('change', function(){
			$('.throbber').show();
			$(this).parent().parent().next().remove();
			$(this).parent().parent().next().remove();
			$(this).parent().parent().next().remove();
			if ($(this).attr('id') == 'select_mesto' && $(this).val() == '') {
				window.location.replace(window.location.pathname);
				return false;
			}
			Availability.createAddress();
			Availability.submit();
		});

		$('#availability input').bind('click', function(){
			$('.throbber').show();
			if ($(this).attr('name') == 'reset') {
				$('#availability select').parent().parent().remove();
				window.location.replace(window.location.pathname);
				return false;
			}
			Availability.submit();
			return false;
		});

	};

	this.createAddress = function () {
		this.address = '';
		$('#availability select').each(function(){
			if ($(this).attr('id') != 'select_cast' && $(this).val() != '') {
				if ($(this).attr('id') == 'select_mesto') {
					tmp = this.options[this.selectedIndex].text.split(' - ');
					Availability.address += tmp[0] + ', ';
				} else if ($(this).attr('id') != 'select_cp') {
					if ($(this).val() != 'aaa_empty') Availability.address += $(this).val() + ', ';
				} else {
					Availability.address += this.options[this.selectedIndex].text + ', ';
				}
			}
		});
		if (this.address != '') {
			this.address = (this.address).substr(0, this.address.length - 2);
			this.input_address.val(this.address);
		}
	};

	this.submit = function(){
		var send_data=$('#availability').serialize()+"&js_is=true";
		$.post($('#availability').attr('action'),send_data, function(data){
			result = $(data).find('#availability').html();
			if (!result) {
				window.location.href="/dostupne-sluzby.html";
				$('#content').html($(data).find('#content').html());
				$('#main_box').attr('class', 'page_dostupne-sluzby');
			} else {
				$('#availability').html(result);
				$('.show_availability_info').html($(data).find('.show_availability_info').html());
			}
			Availability.init();
		}, 'html');
	};

};

var Availability = new cAvailability();

$(function(){
	Availability.init();
});

$(document).ready(function() {
	$('#homeFormHide').hide();
});
