if(typeof(console) !== 'object'){ console = { log: function(){} }; }
if(typeof(ecarlist) === 'undefined') ecarlist = {};
ecarlist.drop_down_filters = {
	map_condition: { 'preowned': '10426', 'Preowned': '10426', '10426': 'preowned', 'used': '10426', 'new': '10425', 'New': '10425', '10425': 'new', 'all_conditions': 'All_vehicles', 'All_conditions': 'All_vehicles', '47603':  'Certified Preowned', 'Certified Preowned':  '47603', '-12': 'Demo', 'Demo': '-12', 'demo': '-12', '-10': 'undefined', 	'undefined': '-10', 	'Undefined': '-10', 	'All_vehicles': 'All_vehicles', 'all_vehicles': 'All_vheicles' },
	load: true,
	reset: false,
	check_condition_count: function(){
		$ec.ajax({
        	processDataBoolean: false,
        	url: '/web/ajax',
        	data: 'mode=filters&vehicle_status=website_unsold&stores=' + $ec("#drop_down_filters input[name='store']").val() + '&include_group_inventory=1',
        	type: 'GET',
        	dataType: 'json',
        	success: function(data){
				if(data.condition.length < 2){
					$ec('#condition').hide();
				} 
        	}
       	});

	},
	set_years: '',
	set_makes: '',
	set_models: '',
	set_make_name: '',
	url: '',
	set_conditions: '',
	reset_sec: false,
	reset_makes: false,
	reset_years: '',
	reset_models: '',
	set_url: function(){
		var mode = 'mode=filters&vehicle_status=website_unsold';
		var stores = $ec("#drop_down_filters input[name='store']").val() ? '&stores=' + $ec("#drop_down_filters input[name='store']").val() : '';
		var new_stores = $ec("#drop_down_filters input[name='new_stores']").val() ? '&new_stores=' + $ec("#drop_down_filters input[name='new_stores']").val() : '';
		var used_stores = $ec("#drop_down_filters input[name='used_stores']").val() ? '&used_stores=' + $ec("#drop_down_filters input[name='used_stores']").val() :'';
		var website = '&' +  $ec("#drop_down_filters input[name='website']").val();
		var include_group_inventory = ($ec("#drop_down_filters input[name='include_group_inventory']").val() == '1') ? '&include_group_inventory=1' : '';
		var include_new_make_ids = ($ec("#drop_down_filters input[name='include_new_make_ids']").val() != '' &&  $ec("#drop_down_filters input[name='include_new_make_ids']").val()) ? '&include_new_make_ids=' + $ec("#drop_down_filters input[name='include_new_make_ids']").val() : '';
		var makes;
		var years;
		var models;
		var condition;
		var url; 

		// always use preowned or new when you are on that page unless your on the homepage
		if(($ec("#drop_down_filters input[name='set_condition']").val() && $ec("#drop_down_filters input[name='template']").val() == 'home') || $ec("#drop_down_filters input[name='set_condition']").val() != 'All_vehicles'){
			condition = '&condition_id=' + $ec("#drop_down_filters input[name='set_condition']").val(); 
		}else{
			condition = '';
		}

		if(this.load == true){	
			this.load = false;
			makes ='&makes';
			this.set_makes = $ec("#drop_down_filters input[name='set_make']").val(); 
			if (this.set_makes === '137') {
				this.set_make_name = $ec("#drop_down_filters input[name='set_make_name']").val(); 
			}
			if (this.set_makes === '137') {
				makes = '&make_id=137&make_name=' + this.set_make_name;
			} else if (this.set_makes !== 'All_makes') {
				makes = '&make_ids=' + this.set_makes;
			}
			years = ($ec("#drop_down_filters input[name='set_year']").val() != 'All_years') ? '&year=' + $ec("#drop_down_filters input[name='set_year']").val() : '&years';
			models = ($ec("#drop_down_filters input[name='set_model']").val() != 'All_models') ? '&models=' + $ec("#drop_down_filters input[name='set_model']").val() : '&models';
			this.set_years = $ec("#drop_down_filters input[name='set_year']").val(); 
			this.set_conditions = $ec("#drop_down_filters input[name='set_condition']").val(); 
			this.set_models = $ec("#drop_down_filters input[name='set_model']").val(); 
			url = mode + stores + include_group_inventory + include_new_make_ids + makes + years + models + condition; 
		}else if(ecarlist.drop_down_filters.reset == true){
			ecarlist.drop_down_filters.reset = false;
			this.set_makes = ' ';
			this.set_make_name = ' ';
			this.set_models = ' ';
			this.set_years = ' ';
			this.set_conditions = ' ';//($ec("#drop_down_filters input[name='set_condition']").val() != 'All_vehicles') ?  $ec("#drop_down_filters input[name='set_condition']").val() : '';
			url = mode + stores + include_group_inventory + include_new_make_ids; 
		}else{
			// when dropdowns are clicked on clicked on
			if(this.reset_makes == true && this.set_makes == 'All_makes'){
				makes = '';
				models = '';
				if($ec("#drop_down_filters input[name='template']").val() != 'home'){
					this.set_models = '';
				}
				ecarlist.drop_down_filters.reset_makes = false;
			}else{
				makes ='&makes';				
				var make_id = ($ec("#drop_down_filters #make_id").val() != 'All_makes') ? $ec("#drop_down_filters #make_id").val() : ''; 
				if (make_id === '137') {
					makes = '&make_id=137&make_name=' + $ec("#drop_down_filters #make_id :selected").html().replace(/ \(\d+\)$/,'');
				} else if (make_id !== '') {
					makes = '&make_ids=' + make_id;
				}
				models = ($ec("#drop_down_filters #model").val() != 'All_models' ) ? '&models=' + $ec("#drop_down_filters #model").val() : '&models';
			}
			
			years = ($ec("#drop_down_filters #year").val() != 'All_years') ? '&year=' + $ec("#drop_down_filters #year").val() : '&year';
			condition = ($ec("#drop_down_filters #condition").val() != 'All_vehicles') ? '&condition_id=' + $ec("#drop_down_filters #condition").val() : '&condition';
			url = mode + stores + include_group_inventory + include_new_make_ids + makes + years + models + condition; 
		}
		if($ec("#drop_down_filters input[name='set_condition']").val() == '10425'){
			url += new_stores;
		}else if($ec("#drop_down_filters input[name='set_condition']").val() == '10426'){
			url += used_stores;
		}
		
		return url;
	},
	get_inventory:  function(){
    	$ec.ajax({
        	processDataBoolean: false,
        	url: '/web/ajax',
        	data: this.set_url(),
        	type: 'GET',
        	dataType: 'json',
        	success: function(data){
            	ecarlist.drop_down_filters.fill_drop_downs(data);
        	},
        	beforeSend: function(){
            	$ec('#drop_down_filters #submit').hide();
            	$ec('#drop_down_filters #waiting').show();
       		 },
        	complete: function(){
            	$ec('#drop_down_filters #submit').show();
            	$ec('#drop_down_filters #waiting').hide();
			},
			error: function(){
				$ec('#searchBar').hide();	
			}
    	});
	},
	fill_drop_downs: function(data){
		var makes = $ec('#drop_down_filters #make_id');
		var years = $ec('#drop_down_filters #year');
		var models = $ec('#drop_down_filters #model');
		var conditions = $ec('#drop_down_filters #condition');
		var website = $ec("#drop_down_filters input[name='website']").val();	
		var remove_count = $ec("#drop_down_filters input[name='remove_count']").val() == '1' ? true : false;

			makes.empty();
			makes.append('<option value="reset">Clear All</option>');
			makes.append('<option value="All_makes" selected>All Makes</option>');
			for(var x = 0; x < data.makes.length; x++){
				var selected = ((ecarlist.drop_down_filters.set_make_name == data.makes[x].make_name || (ecarlist.drop_down_filters.set_makes == data.makes[x].make_id && data.makes[x].make_id != '137'))) ? 'selected' : '';
				var makes_link = '<option value="' + data.makes[x].make_id + '" ' + selected  +  ' >' + data.makes[x].make_name;
				makes_link += remove_count ? '' : ' (' + data.makes[x].make_count +  ')'; 
				makes_link += '</option>';
				makes.append(makes_link);
			}	
		
			years.empty();
			years.append('<option value="reset">Clear All</option>');
			years.append('<option value="All_years" selected>All Years</option>');
			for(var x = 0; x < data.years.length; x++){
				var selected = (ecarlist.drop_down_filters.set_years == data.years[x].year) ? 'selected' : '';
				var years_link = '<option value="' + data.years[x].year + '" ' + selected  +  ' >' + data.years[x].year; 
				years_link += remove_count ? '' : ' (' + data.years[x].year_count + ')';
				years_link += '</option>';
				years.append(years_link);
			}
		
			models.empty();
			models.append('<option value="reset">Clear All</option>');
			models.append('<option value="All_models" selected>All Models</option>');
			for(var x = 0; x < data.models.length; x++){
				if(data.models[x].model != ''){
					var selected = ((ecarlist.drop_down_filters.set_models == data.models[x].model)) ? 'selected' : '';
					var models_link = '<option value="' + escape(data.models[x].model) + '" ' + selected  +  '>' + data.models[x].model;
					models_link += remove_count == '1' ? '' : ' (' + data.models[x].model_count  +  ')';
					models_link += "</option>";
					models.append(models_link);
				}
			}
			
			conditions.empty();	
			
			conditions.append('<option value="reset">Clear All</option>');
			conditions.append('<option value="All_vehicles" selected>All Condition</option>');
			
			for(var x = 0; x < data.condition.length; x++){
				if( ecarlist.drop_down_filters.set_conditions == data.condition[x].condition_id ){
					var selected = 'selected';
				}else if((ecarlist.drop_down_filters.set_conditions == '10426' || ecarlist.drop_down_filters.set_conditions == '47603' ) && data.condition.length == 1){
					var selected = 'selected';
				}else{
					var selected = ''	
				}

				//var selected = ( ecarlist.drop_down_filters.set_conditions == data.condition[x].condition_id ) ? 'selected' : '';
				if( data.condition[x].condition_id != null ){
					if( data.condition[x].condition_name == 'Used' || data.condition[x].condition_id == '10426' ){
						var condition_name = 'Preowned';
					} else if( data.condition[x].condition_name == 'New' || data.condition[x].condition_id == '10425' ){
						var condition_name = 'New';
					} else if( data.condition[x].condition_name == 'Certified Preowned' || data.condition[x].condition_id == '47603' ){
						var condition_name = 'Certified Preowned';
					} else if( data.condition[x].condition_name == 'Demo' || data.condition[x].condition_id == '-12' ){
						var condition_name = 'Demo';
					} else	{
						var condition_name = 'undefined';
					}
					conditions.append('<option value="' + data.condition[x].condition_id + '" ' + selected + ' >' + condition_name + /* ' (' + data.condition[x].condition_count  +  ')'  + */ "</option>");
				}
			}
					
			
			var	sold = $ec("#drop_down_filters input[name='sold']").val() ? '?sold=' + $ec("#drop_down_filters input[name='sold']").val() : '';	
			var makes_link = $ec('#make_id' + ' option:selected').html() == 'All Makes' ? 'All_makes' : $ec('#make_id' + ' option:selected').html();
			var link = 'http://' + website  + '/web/inventory/' + years.val()  + '/' + makes_link + '/' + models.val().replace('\/','-_-') + '/All_body_types/' + this.map_condition[conditions.val()] + '/' + sold;
			$ec('#submit').attr('href',link.replace(/\s\(\d*\)\s*/g,''));	
	},
	init: function(){
		this.check_condition_count();
		this.get_inventory();
		$ec('#drop_down_filters select').change(function(){
			if(ecarlist.drop_down_filters.set_makes != 'All_makes' && $ec('#drop_down_filters #model').val() != 'All_models'){
				if(ecarlist.drop_down_filters.set_models == 'All_models') 
				ecarlist.drop_down_filters.reset_makes = true;
			}else{
				ecarlist.drop_down_filters.reset_makes = false;
			}

			ecarlist.drop_down_filters.set_makes = $ec('#drop_down_filters #make_id').val();
			ecarlist.drop_down_filters.set_make_name = $ec('#drop_down_filters #make_id :selected').text().replace(/\s\(\d*\)\s*/g,'');
			ecarlist.drop_down_filters.set_models = unescape($ec('#drop_down_filters #model').val());
			ecarlist.drop_down_filters.set_years = $ec('#drop_down_filters #year').val();
			ecarlist.drop_down_filters.set_conditions = $ec('#drop_down_filters #condition').val();
			if($ec(this).val() == 'reset'){
				ecarlist.drop_down_filters.reset = true;	
			}
			ecarlist.drop_down_filters.get_inventory();	
		});
	}
}

$ec(document).ready(function(){ 
	ecarlist.drop_down_filters.init();
});



