$(document).ready(function(){
	if( $( 'body#page_ar_results' ).length ) { // Ergebnisliste
		$('#earliestDepart').datepicker({
			dateFormat: 'dd.mm.yy',
			regional: 'de',
			changeMonth: true,
			changeYear: true,
			minDate: '+5D',
			maxDate: '01.12.' + ((new Date).getFullYear() + 1),
			onSelect: function(dateText, inst){
				if (dateText != '') {
					$('#latestDepart').datepicker('enable');
					$('#latestDepart').datepicker('option', 'minDate', dateText);
				}
				else {
					$('#latestDepart').datepicker('disable');
				}
			},
			showOn: 'both',
			buttonImage: '/fileadmin/ang/template/img/calendar.gif',
			buttonImageOnly: true,
			buttonText: 'Anreisedatum wählen'
		
		});
		$('#latestDepart').datepicker({
			dateFormat: 'dd.mm.yy',
			regional: 'de',
			changeMonth: true,
			changeYear: true,
			minDate: '+5D',
			maxDate: '01.12.' + ((new Date).getFullYear() + 1),
			disabled: true,
			showOn: 'both',
			buttonImage: '/fileadmin/ang/template/img/calendar.gif',
			buttonImageOnly: true,
			buttonText: 'Abreisedatum wählen'
		});
		
		if ($('#earliestDepart').val() == '') {
			$('#latestDepart').datepicker('disable');
		}
		else {
			$('#latestDepart').datepicker('option', 'minDate', $('#earliestDepart').val());
		}
		
		// Höhe des Search-Div-Containers (#left) autom. anpassen
		$('#left').height( $('#left').height() + 30 + $('#verticalNav').height() );
		
		// etracker Events
		$('#country_code').change( function(){
			try{ ET_Event.eventStart('Suche',  $('#country_code option:selected').text() , 'Klick auf "Land"', '' );}catch(e){}
		});
		$('#area_code').change( function(){
			try{ ET_Event.eventStart('Suche',  $('#area_code option:selected').text() , 'Klick auf "Gebiet"', '' );}catch(e){}
		});
		$('#region_name').change( function(){
			try{ ET_Event.eventStart('Suche',  $('#region_name option:selected').text() , 'Klick auf "Region"', '' );}catch(e){}
		});
		$('#fishingSort').change( function(){
			try{ ET_Event.eventStart('Suche',  $('#fishingSort option:selected').text() , 'Klick auf "Angelart"', '' );}catch(e){}
		});
		$('#saltwater').change( function(){
			try{ ET_Event.eventStart('Suche',  $('#saltwater').text() , 'Angelart: ' + $('#saltwater').next().html(), '' );}catch(e){}
		});
		$('#freshwater').change( function(){
			try{ ET_Event.eventStart('Suche',  $('#freshwater').text() , 'Angelart: ' + $('#freshwater').next().html(), '' );}catch(e){}
		});
		$('#fishes').change( function(){
			try{ ET_Event.eventStart('Suche',  $('#fishes option:selected').text() , 'Klick auf "Fische"', '' );}catch(e){}
		});
		$('#codfish').change( function(){
			try{ ET_Event.eventStart('Suche',  $('#codfish').val() , 'Fische: ' + $('#codfish').next().html(), '' );}catch(e){}
		});
		$('#ling').change( function(){
			try{ ET_Event.eventStart('Suche',  $('#ling').val() , 'Fische: ' + $('#ling').next().html(), '' );}catch(e){}
		});
		$('#halibut').change( function(){
			try{ ET_Event.eventStart('Suche',  $('#halibut').val() , 'Fische: ' + $('#halibut').next().html(), '' );}catch(e){}
		});
		$('#pike').change( function(){
			try{ ET_Event.eventStart('Suche',  $('#pike').val() , 'Fische: ' + $('#pike').next().html(), '' );}catch(e){}
		});
		$('#trout').change( function(){
			try{ ET_Event.eventStart('Suche',  $('#trout').val() , 'Fische: ' + $('#trout').next().html(), '' );}catch(e){}
		});
		$('#thunfisch').change( function(){
			try{ ET_Event.eventStart('Suche',  $('#thunfisch').val() , 'Fische: ' + $('#thunfisch').next().html(), '' );}catch(e){}
		});
		
		$('#boat1').change( function(){
			try{ ET_Event.eventStart('Suche',  $('#boat1').val() , 'Bootstyp', '' );}catch(e){}
		});
		$('#boat2').change( function(){
			try{ ET_Event.eventStart('Suche',  $('#boat2').val() , 'Bootstyp', '' );}catch(e){}
		});
		$('#boat3').change( function(){
			try{ ET_Event.eventStart('Suche',  $('#boat3').val() , 'Bootstyp', '' );}catch(e){}
		});
		$('#objectNumber').change( function(){
			try{ ET_Event.eventStart('Suche',  $('#objectNumber').val() , 'Stichwortsuche', '' );}catch(e){}
		});
	}
});

function getCountries()
{
	$.getJSON('/scripts/ar/ajax/ar_update_countries.php', function(data) {
		$('#country_code').html(data);
	});
}


function updateCountries(data,status)
{
	alert(data);
}

 function updateAreas(){
 	$('#area_code').empty();
	$('#area_code').append('<option value="">Gebiet</option>');
 	$('#region_name').empty();
	$('#region_name').append('<option value="">Region</option>');
 	updateSelectboxes();
 }
 
 function updateSelectboxes(){
 	var country_code = $('#country_code').val();
 	var area_code = $('#area_code').val();
	
 	var data = 'country_code=' + country_code + '&area_code=' + area_code;
 	
 	$.getJSON('/scripts/ar/ajax/ar_update_searchparams.php', data, handleData)
 }

 function handleData(data, status){	
	$('#area_code').empty();
	$('#area_code').append(data.area_codes);
	
	$('#region_name').empty();
	$('#region_name').append(data.region_names);
 }
 
 function changeSorting(order_link, order_code){
 	$('#order_code').val(order_code);
 	if (order_link.attr('class').search(/active/) != -1) {
		if (order_link.attr('class').search(/down/) != -1) {
			order_link.attr('class', 'active up');
			$('#sorting_order').val('ASC');
		}
		else {
			order_link.attr('class', 'active down');
			$('#sorting_order').val('DESC');
		}
	}
	else {
		$('.sortables a').each(function(){
			$(this).attr('class', 'up');
		});
		order_link.attr('class', 'active up');
		$('#sorting_order').val('ASC');
	}
	
	$('#search_submit').val(1);
	$('#search_form').submit();
 }
 
 function scroll(new_limit){
 	$('#result_idx').val(new_limit);
	$('#search_submit').val(1);
 	$('#search_form').submit();
 }
 
 function clearForm(){
 	$('#result_idx').val(0);
	$('#sorting_order').val('ASC');
	$('#order_code').val('prio');
 }

 $(document).ready(function() {
	$("#country_code, #area_code, #region_name, #fishingSort, #fishes, #objectNumber, #earliestDepart, #latestDepart, #numberPeople, #bedrooms, #cottage, #holidayCamp, #festival, #saltwater, #freshwater, #codfish, #ling, #halibut, #pike, #trout, #thunfisch, #boat1, #boat2, #boat3").keyup(function(e) {
		if(e.keyCode == 13) {
			$("input[name='search_submit']").click();
		}
	});
	
	// SEO-Text einklappen
	seoTextExtended = false;
	switchSeoText();
	$('div.seo_text').removeClass('minified');
	$('div.seo_text_result a.open_seo_text').html( 'Tipp lesen' ); // Link-Text ändern
 });
 
 function switchSeoText()
 {
 	if( $('div.seo_text_result div.text').length == 0 ) {
 		return false;
 	}
 	
 	if( seoTextExtended == true ) {
	 	$('div.seo_text_result div.text').html( longText ); // Langer Text anzeigen
	 	$('div.seo_text_result a.open_seo_text').html( 'Tipp schließen' ); // Link-Text ändern
	 	seoTextExtended = false;
 	} else {
	 	$('div.seo_text_result div.text').html( shortText ); // Langer Text anzeigen
		$('div.seo_text_result a.open_seo_text').html( 'Tipp lesen' ); // Link-Text ändern
		seoTextExtended = true;
 	}
 }
