MakeModelModelTypeDWR.getMinimumBuildYear({callback:populateMinimumBuildYear});
var minimumBuildYear;

function populateMakesWithCounts(makes) {
	if (makes && populateMakesWithCountsHelper(makes)) {
		makeChanged();
	}
}

function populateAuctionMakesWithCounts(makes) {
	if (makes && populateMakesWithCountsHelper(makes)) {
		auctionMakeChanged();
	}
}

function populateMakesWithCountsHelper(makes) {
	jQuery("#makes")
	.append(jQuery("<option></option>")
			.attr("value", "")
			.text(selectMakeMsg));
	var selectedId = jQuery("input#make").attr("value");
	for (var i = 0; i < makes.length; i++){
		if (makes[i].id == selectedId) {
			jQuery("#makes").append(jQuery("<option></option>")
					.attr("value", makes[i].id)
					.attr("selected", "selected")
					.text(makes[i].name + ((makes[i].count > 0)? " ("+makes[i].count+")" : "")));
		} else
			jQuery("#makes").append(jQuery("<option></option>")
					.attr("value", makes[i].id)
					.text(makes[i].name + ((makes[i].count > 0)? " ("+makes[i].count+")" : "")));
	}
	
	return selectedId;
}

function makeChanged(){
	var makeId = jQuery("#makes option:selected").attr("value");
	jQuery("input#make").attr("value", makeId);
	clearCombos(["#models"]);
	//MakeModelModelTypeDWR.getModelsForMake(makeId, {callback : populateModelYears});
	MakeModelModelTypeDWR.getDistinctModelNamesForMakeWithCounts(makeId, {callback: populateModelNames});
}

function auctionMakeChanged(){
	var makeId = jQuery("#makes option:selected").attr("value");
	jQuery("input#make").attr("value", makeId);
	clearCombos(["#models"]);
	MakeModelModelTypeDWR.getDistinctAuctionModelNamesForMakeWithCounts(makeId, {callback: populateModelNames});
}

var models;
function populateModelNames(models){
	if (models) {
			jQuery("#models")
				.find('option')
				.remove()
				.end()
				.append(jQuery("<option></option>")
				.attr("value", "")
				.text(selectModelMsg));
			var selectedName = jQuery("input#modelName").attr("value");
		for (var i = 0; i < models.length; i++){
			if (selectedName && models[i].name.toLowerCase() == selectedName.toLowerCase()) {
				jQuery("#models").append(jQuery("<option></option>")
						.attr("value", models[i].name)
						.attr("selected", "selected")
						.text(models[i].name + ((models[i].count > 0)? " ("+models[i].count+")" : "")));
			} else
				jQuery("#models").append(jQuery("<option></option>")
						.attr("value", models[i].name)
						.text(models[i].name + ((models[i].count > 0)? " ("+models[i].count+")" : "")));
		}
		if (selectedName)
			modelChanged();
	}
}

function modelChanged() {
	var modelId = jQuery("#models option:selected").attr("value");
	jQuery("input#modelName").attr("value", modelId);
	
	var modelName = jQuery("#models option:selected").attr("value");
	if (models) {
		if (modelName != "") {
			sameModels = new Array();
			for ( var i = 0; i < models.length; i++) {
				if (models[i].name.toLowerCase() == modelName.toLowerCase())
					sameModels.push(models[i]);
			}
			var now = new Date();
			var minYear = now.getFullYear();
			for ( var i = 0; i < sameModels.length; i++) {
				if (!sameModels[i].modelYear) {
					minYear = minimumBuildYear;
				} else if (sameModels[i].modelYear < minYear) {
					minYear = sameModels[i].modelYear;
				}	
			}
		} else
			populateModelYears(models);
	}

}

function showXY(xy) {
	var array = xy.split("&");
	jQuery("#mapSearchX").attr("value", array[0]);
	jQuery("#mapSearchY").attr("value", array[1]);
	jQuery("#mapSearchR").attr("value", array[2]);
} 

function mapSearchClose() {
	jQuery("#mapSearch").fadeOut();
}

/* make search counter functions */
function increaseMakeModelCounterOnFormSubmit(triggerId) {
	jQuery('#' + triggerId).submit(function(){
		var modelName = jQuery('#models').val();
		var makeId = parseInt(jQuery('#makes').val()) ;
		if(modelName != null) {
			jQuery.getJSON(increaseModelUrl, {'makeId':makeId, 'modelName':modelName});
		} else if(!isNaN(makeId) && makeId > 0) {
			jQuery.getJSON(increaseMakeUrl, {'makeId':makeId});
		} else {
			;// Can't do statistics
		} 
	});
}

function increaseMakeCounterOnMakeLinkClick(className) {
	jQuery('.' + className).click(function(){
		var makeId = this.id;
		if(!isNaN(makeId) && makeId > 0)
			jQuery.getJSON(increaseMakeUrl, {'makeId':makeId});
	});
}

/* model search counter functions */
function increaseModelCounterOnModelLinkClick(className) {
	jQuery('.' + className).click(function(){
		var makeId = this.id.split(":")[0];
		var modelName = this.id.split(":")[1];
		if(modelName != null)
			jQuery.getJSON(increaseModelUrl, {'makeId':makeId, 'modelName':modelName});
	});
}

/* shrinking facets */ 
function shrinkFacetList() {
	//init links
	jQuery("li.morefacets > a").click(function() {
		var field = jQuery(this).attr("id").split("-")[1];
		jQuery("ul#facets-" + field + " > li:not(.morefacets, .lessfacets) > a").parent().show();
		jQuery(this).hide();
		jQuery("a#less-" + field).show().parent().show();
	});
	
	jQuery("li.lessfacets > a").click(function() {
		var field = jQuery(this).attr("id").split("-")[1];		
		var facets = jQuery("ul#facets-" + field + " > li:not(.morefacets, .lessfacets) > a").parent();
		facets.each(function(index){
			if (index > 11) {
				jQuery(this).hide();
			}
		});		
		jQuery(this).hide();		
		if (facets.size() > 12) {
			jQuery("a#more-" + field).show();
		}
	});
	
	jQuery("ul.extendable").each(function() {
		var field = jQuery(this).attr("id").split("-")[1];
		// var facets = jQuery("ul#facets-" + field + " > li:not(.morefacets, .lessfacets) > a:not(.active)").parent();
		var facets = jQuery("ul#facets-" + field + " > li:not(.morefacets, .lessfacets) > a").parent();
		facets.each(function(index){
			if (index > 11) {
				jQuery(this).hide();
			}
		});
		if (facets.size() > 12) {
			jQuery("a#more-" + field).parent().show();
		}
		//jQuery("a#less-" + field).parent().hide();
	});
}

function appendOptionToSelect(selectId, optionId, optionText) {
	jQuery("#" + selectId).append(
			jQuery("<option></option>").attr("value", optionId).attr("selected",
					"selected").text(optionText));
}

function bodyTypePopup(inputElm) {
	this.input = inputElm;
	this.popup = null;
	this.hide = function() {
		jQuery("#bodyTypePopup").hide();
		jQuery(this.input).show();
	};
	this.init = function() {
		var self = this;
		jQuery(this.input).click(
				function() {
					self.display();
				});
	};
	this.display = function() {
		var self = this;
		
		if (this.popup == null) {
			this.initPopup();
			var offset = jQuery(self.input).offset();
			jQuery(this.popup).offset({top: (offset.top - 20), left: (offset.left - 20)});
		}
		
		jQuery(this.input).hide();
		
		jQuery(this.popup).show();
		jQuery(this.popup).bgiframe();

		if(jQuery('#bodytypeMap')) {
			// Loop through each AREA in the imagemap
			jQuery('#bodytypeMap area').each(function() {
				// Assigning an action to the click event
				jQuery(this).click(function(e) {
					e.preventDefault();
					
					var selected = jQuery(this).attr('id');
					var selectedValue = null;
					
					jQuery(self.input).html();
					jQuery(self.input).find("option").each(function() {if (jQuery(this).text() == selected) {selectedValue = jQuery(this).attr("value");} });
					jQuery(self.input).val(selectedValue);
					
					self.hide();
				});
			
			});
		}
		
		jQuery('#selectNoBodyType').click(function() {
			jQuery(self.input).val('');
			self.hide();
		});
		
		jQuery('#closeSelectBodyTypeLink,#closeSelectBodyTypeX').click(function() {
			self.hide();
		});
	};
	
	this.initPopup = function() {
		this.popup = jQuery('#bodyTypePopup').get(); 
	};
	
	this.init();
	
}

function populateMinimumBuildYear(buildYear){
	minimumBuildYear = buildYear;
}

