$().ready(function(){
	
	$.datepicker.setDefaults($.datepicker.regional['pl']);
	
	var maxDate = new Date();
	maxDate.setDate(maxDate.getDate() + 90);
		// calendar
	$('input#field-date').datepicker({
		changeMonth: true,
		changeYear: true,
		minDate: new Date(),
		dateFormat: 'dd-mm-yy',
		showOtherMonts: true,
		showAnim: 'slideDown',
		showButtonPanel: true

	});
	
	$('img.country-flag').each(function(){
		if(typeof $(this).attr('src') != 'undefined')
		{
			$(this).show();
		}
	});
	
	$('#field-country-to, #field-country-from').change(function(){
		
		var val = $(this).children('option:selected').val();
		val = val.toLowerCase();
		
		if(val.length > 0)
		$(this).parent().children('img').attr('src', meta.staticUrl+'img/flagi_gif/'+val+'.gif').show();
		else
		$(this).parent().children('img').attr('src', '').hide();
	});
	
	if(typeof searchShow != 'undefined')
	{
		$('div#div-select-bodywork').hide();
		$('a#show-search').parent().attr('class', 'hide-search');
		$('a#show-search').children('span:first-child').text("Ukryj wyszukiwarkę");
		$('a#show-search').children('span.arrow-down').attr('class', 'arrow-up');
		window.location = '#ogloszenia';
	}
	else
	{
		$('div.announcements-search > form').hide();
		$('div#div-select-ann-type').hide();
	}
	
	
	$('a#show-search').click(function(){
		
		var action = $(this).parent().attr('class');
		
		if(action == 'show-search')
		{
			$(this).parent().attr('class', 'hide-search');
			$('div.announcements-search > form').slideDown('slow');
			$(this).children('span:first-child').text("Ukryj wyszukiwarkę");
			$(this).children('span.arrow-down').attr('class', 'arrow-up');
			$('div#div-select-bodywork').hide();
			$('div#div-select-ann-type').show();
		}
		else if(action == 'hide-search')
		{
			$(this).parent().attr('class', 'show-search');
			$('div.announcements-search > form').slideUp('slow');
			$(this).children('span:first-child').text("Pokaż wyszukiwarkę");
			$(this).children('span.arrow-up').attr('class', 'arrow-down');
			
			$('div#div-select-bodywork').show();
			$('div#div-select-ann-type').hide();
		}
		
		return false;
	});
	
	$('div#div-select-ann-type > select#select-ann-type').change(function(){
		
		var selected = $(this).children('option:selected');
		
		if(selected.attr('class') != 'this')
		{
			window.location = selected.val();
		}
		
	});
	
	$('div#div-select-bodywork > select#select-bodywork').change(function(){
		
		var selected = $(this).children('option:selected');
		
		if(selected.val() != uri.params[0])
		{
			window.location = '/'+uri.ctrl+'/'+uri.action+'/'+selected.val();
		}
		
	});
});
