var urlop =  {
	init: function(){
		this.manageInputLabel();
		this.search_province();
		this.lightbox();
		this.place();
		this.advs();
		this.panel_atuts();
		this.add();
	},
	add: function() {
		$('input:checkbox').bind('change', function(){
			$('#invoice_send').toggle();
		});
		$('input:checked').each( function(){
			$('#invoice_send').show();
		});
		$('.photos input').change(function(e){
			var next = $('.photos div.hidden')[0];
			$(next).removeClass('hidden');
		});
		$('.categories select').change(function(e){
			var next = $('.categories div.hidden')[0];
			$(next).removeClass('hidden');
		});
		
		if (typeof price != 'undefined'){
			$('#id_period').parent().append('<span></span>');
			$('#id_period').change(urlop.change_price);
			this.change_price();
		}
	},
	change_price: function() {
		var netto = $('#id_period').val() * price;
		var brutto = netto + (netto *0.22);
		$('>span', $('#id_period').parent()).text(netto + ' zł netto (' + brutto + ' zł brutto)');
	},
	search_province: function() {
		$('#FAS-wojewodzctwo').change(function(){
			$.post('/miasta/', {province: $(this).attr('value')}, function(data){
				$('#FAS-miejscowosc').html(data);
			});
		});
	},
	panel_atuts: function() {
		$('.atuts input').change(function(){
			var elements = $('.atuts input');
			var empty =  0;
			for (i=0; i<elements.length; i++)
			{
				if (elements[i].value == "")
					empty++;
			}
			if (empty < 2)
				$('.atuts ol').append('<li><input name="atuts" /></li>');
			
			urlop.panel_atuts();
		});
	},
	advs: function() {
		$('#id_province_1')
			.bind('change', function(){
				$.post('/miasta/', {province: $(this).attr('value')}, function(data){
					$('.select_city_1 select').html(data);
					$($('.select_city_1')[0]).show();
					$('.region2').show();
				});
			})
			.val('');
		$('.select_city_1').bind('change', function(){
			if ($(this).attr('value') != '0') {
				$($('.select_city_1:hidden')[0]).show();
			}
		});
		$('#id_province_2')
			.bind('change', function(){
				$.post('/miasta/', {province: $(this).attr('value')}, function(data){
					$('.select_city_2 select').html(data);
					$($('.select_city_2')[0]).show();
				});
			})
		.val('');
		$('.select_city_2').bind('change', function(){
			if ($(this).attr('value') != '0') {
				$($('.select_city_2:hidden')[0]).show();
			}
		});
	},
	manageInputLabel: function() {
		$( '#formAccommodationSearch, #formNewsletter input, #FAS-miejscowosc, #FAS-obiekt' ).bind( 'focus blur', function(){
			window.status+='-';
			with( $( this ) ){
				
				if( attr( 'value' ) == parent().find('label').text() ){
					attr( 'value', '' );
				} else {
					if( $.trim( attr( 'value' ) ) == '' ) {
						attr( 'value', parent().find('label').text() );
					}
				}
			}
		});
	},
	lightbox: function(){
		$('#facility ul.photos a').lightBox({
			imageLoading: '/media/gfx/lightbox-ico-loading.gif',
			imageBtnClose: '/media/gfx/lightbox-btn-close.gif',
			imageBtnPrev: '/media/gfx/lightbox-btn-prev.gif',
			imageBtnNext: '/media/gfx/lightbox-btn-next.gif',
			imageBlank: '/media/images/lightbox-blank.gif'
		});
	},
	place: function() {
		$('#formQuestionLink').click(function(e){
			e.preventDefault();
			var that = $(this);
			that.hide();
			$('.formRecommend').fadeOut('fast');
			$('#formOpinion').fadeOut('fast');
			$('.formQuestion:not(.forms)').fadeIn('slow');
		});
		
		$('#formRecommendLink').click(function(e){
			e.preventDefault();
			var that = $(this);
			$('#formQuestionLink').show();
			$('.formQuestion').fadeOut('fast');
			$('.formRecommend').fadeIn('slow');
		});
		
		$('#formOpinionLink').click(function(e){
			e.preventDefault();
			var that = $(this);
			$('#formQuestionLink').show();
			$('.formQuestion').fadeOut('fast');
			$('.formRecommend').fadeOut('fast');
			$('#formOpinion').fadeIn('slow');
		});
		
		$('#printFacility').click(function(e){
			e.preventDefault();
			window.print();
		});
	}
};

$(document).ready(function(){
	urlop.init();
});