$(function(){
    //Setup onchange for River select (for redrawing outfitters and sections)
    $('#river').change(function(){
        refreshRiver();
    });
	
	$('#client').change(function(){
		refreshRivers();
	});
	
	$('#photo_time_start').before('<em class="description">Between</em> ').after(' <em class="description">and</em> ');
    
	$('#photo_time_start').change(function(){		
		refreshPhotoTimeEnd();
	});
	
	$('#photo_time_end').change(function(){		
		refreshPhotoTimeStart();
	});
	
	$('#search-accordion-wrapper').accordion({
		collapsible:true,
		active:currentAccordionActive
	});
	
	//Init sorting selector
	initSortSelect();
	
    //Do initial setup, in case something was preselected
    refreshRiver();
refreshRivers();
    refreshPhotoTimeEnd();
    refreshPhotoTimeStart();
});

function refreshPhotoTimeEnd()
{
	var currentStart 	= Number($('#photo_time_start').val());
	var currentEnd 		= Number($('#photo_time_end').val());

	$('#photo_time_end').empty();
	
	$.each(photoTimeOptions, function(i, option){			
		if(i > currentStart || currentStart == -1 || i == -1){		
			$('#photo_time_end').append('<option value="' + i + '">' + option + '</option>')
		}
	});
	
	if(currentStart == -1){
		$('#photo_time_end').val(-1);
	} else if (currentEnd == -1){
		$('#photo_time_end').val($('#photo_time_end option:eq(1)').val());
	} else {
		$('#photo_time_end').val(currentEnd);
	}
	
	//Now if current start is set to -1, redraw all on time_start so its full again
	if(currentStart == -1){
		refreshPhotoTimeStart();
	}
}

function refreshPhotoTimeStart()
{
	var currentStart 	= Number($('#photo_time_start').val());
	var currentEnd 		= Number($('#photo_time_end').val());
	
	$('#photo_time_start').empty();
	
	$.each(photoTimeOptions, function(i, option){			
		if(i < currentEnd || currentEnd == -1 || currentEnd == 0 || i == -1){
			$('#photo_time_start').append('<option value="' + i + '">' + option + '</option>')
		}
	});
	
	if(currentEnd == -1){
		$('#photo_time_start').val(-1);
	} else if (currentStart == -1){
		$('#photo_time_start').val($('#photo_time_start option:last').val());
	} else {
		$('#photo_time_start').val(currentStart);
	}
}

function refreshRivers()
{
	var currentSection = $('#river_section').val();
	var currentRiver = $('#river').val();
	
	if ($('#client').val() == -1) {
		//No client picked, set to defaults
		
		$('#river option[value!=-1]').remove();
		
		$.each(rivers, function(i, river){
			$('#river').append('<option value="' + river.id + '">' + river.name + '</option>');
		});
		
		/*$('#river_section option[value!=-1]').remove();
		
		$.each(sections, function(i, section){
			$('#river_section').append('<option value="' + section.id + '">' + section.name + '</option>');
		});*/
	}
	else {
		$('#river option[value!=-1]').remove();
		//alert(clients[$('#client').val()].name);
		$.each(clients[$('#client').val()].rivers, function(i, river){
			//alert(river.name);
			$('#river').append('<option value="' + river.id + '">' + river.name + '</option>');
		});
		
		//Now redraw sections for available rivers
		$('#river_section option[value!=-1]').remove();
		
		$('#river option[value!=-1]').each(function(i){
			$.each(rivers[$(this).val()].sections, function(i, section){
				$('#river_section').append('<option value="' + section.id + '">' + section.name + '</option>');
			});
		});
	}
	
	$('#river').val(currentRiver);
	$('#river_section').val(currentSection);
}

function refreshRiver(){
	var currentSection = $('#river_section').val();
	var currentClient = $('#client').val();
	
    if ($('#river').val() == -1) {
		//No river picked, set to defaults
		
		$('#client option[value!=-1]').remove();
		
		if($('#client').get(0).tagName.toLowerCase() == 'select'){
			$.each(clients, function(i, client){
				$('#client').append('<option value="' + client.id + '">' + client.name + '</option>');
			});
		}
		
		$('#river_section option[value!=-1]').remove();
		
		$.each(sections, function(i, section){
			$('#river_section').append('<option value="' + section.id + '">' + section.name + '</option>');
		});
	}
	else {
		//Redraw clients
		$('#client option[value!=-1]').remove();
		//alert($('#client').get(0).tagName);
		if($('#client').get(0).tagName.toLowerCase() == 'select'){
			$.each(rivers[$('#river').val()].clients, function(i, client){
				$('#client').append('<option value="' + client.id + '">' + client.name + '</option>');
			});
		}
		
		//Redraw sections
		$('#river_section option[value!=-1]').remove();
		
		$.each(rivers[$('#river').val()].sections, function(i, section){
			$('#river_section').append('<option value="' + section.id + '">' + section.name + '</option>');
		});
	}
	
	$('#client').val(currentClient);
	$('#river_section').val(currentSection);
}

function initSortSelect()
{
	//Set the current value
	$('.sort-select').val(window.location.pathname);
	
	$('.sort-select').change(function(){
		window.location = $(this).val();								  
	});
}
