jQuery(document).ready(function() {
	jQuery('.sort').find('li:not(:last)').after('<li>|</li>');

	populate_year_high = function() {
		from = jQuery('#f_uc_search_year_low  option:selected').val();
		to   = jQuery('#f_uc_search_year_low  option:last'    ).val();
		sel  = jQuery('#f_uc_search_year_high option:selected').val();
		jQuery('#f_uc_search_year_high').empty();
		for (i=from; i<=to; i++) {
			jQuery('#f_uc_search_year_high').append('<option value="'+ i +'">'+ i +'</option>');
		}
		jQuery('#f_uc_search_year_high option[value='+ sel +']').attr('selected', 'selected');
		jQuery('#years').show();
	}
	
	populate_price_high = function() {
		from = parseInt(jQuery('#f_uc_search_price_low option:selected').val() + 0);
		sel  = jQuery('#f_uc_search_price_high option:selected').val();
		jQuery('#f_uc_search_price_high option:not(:last)').remove()
		jQuery('#f_uc_search_price_low option').each(function() {
			val = parseInt(jQuery(this).val() + 0)
			if (val > from)
				jQuery('#f_uc_search_price_high option:last').before(jQuery(this).clone());
		});
		jQuery('#f_uc_search_price_high option[value='+ sel +']').attr('selected', 'selected');
		if (!jQuery('#f_uc_search_price_high option[value='+ sel +']').length)
			jQuery('#f_uc_search_price_high option:first').attr('selected', 'selected');
		jQuery('#price').show();
	}
	
	populate_mileage_high = function() {
		from = parseInt(jQuery('#f_uc_search_mileage_low option:selected').val() + 0);
		sel  = jQuery('#f_uc_search_mileage_high option:selected').val();
		jQuery('#f_uc_search_mileage_high option:not(:last)').remove()
		jQuery('#f_uc_search_mileage_low option').each(function() {
			val = parseInt(jQuery(this).val() + 0)
			if (val > from)
				jQuery('#f_uc_search_mileage_high option:last').before(jQuery(this).clone());
		});
		jQuery('#f_uc_search_mileage_high option[value='+ sel +']').attr('selected', 'selected');
		if (!jQuery('#f_uc_search_mileage_high option[value='+ sel +']').length)
			jQuery('#f_uc_search_mileage_high option:first').attr('selected', 'selected');
	}
	
	populate_models = function() {
		sel  = jQuery('#f_uc_search_make option:selected').val();

		jQuery('#models').show();
		if (!sel) jQuery('#models').hide()

		t = jQuery('#f_uc_search_type option:selected').val();
		
		jQuery('#f_uc_search_model').empty();

		models_aoa = new Array();
		for (model in uc_models[sel])
			models_aoa.push({ id : model, name : uc_models[sel][model]});
		sorted_models = models_aoa.sort(function(a, b) { return (a['id'] && a['name'] > b['name']) ? 1 : -1});
		
		for (key in sorted_models) {
			jQuery('#f_uc_search_model').append('<option value="'+ sorted_models[key]['id'] +'">'+ sorted_models[key]['name'].replace(/\([0-9]+\)/, '') +'</option>');
		}
		jQuery('#f_uc_search_model option:first').addClass('first');
		
		if (t == 'lto')
			jQuery('#price').hide();
		else
			jQuery('#price').show();
	}
	
	populate_models();
	jQuery('#f_uc_search_model option[value='+ uc_model +']').attr('selected', 'selected');

	populate_year_high();
	jQuery('#f_uc_search_year_low  option[value='+ uc_year_low  +']').attr('selected', 'selected');
	if (!uc_year_low) jQuery('#f_uc_search_year_low option:first').attr('selected', 'selected');
	jQuery('#f_uc_search_year_high option[value='+ uc_year_high +']').attr('selected', 'selected');
	if (!uc_year_high) jQuery('#f_uc_search_year_high option:last').attr('selected', 'selected');

	populate_price_high();
	jQuery('#f_uc_search_price_low  option[value='+ uc_price_low  +']').attr('selected', 'selected');
	if (!uc_price_low) jQuery('#f_uc_search_price_low option:first').attr('selected', 'selected');
	jQuery('#f_uc_search_price_high option[value='+ uc_price_high +']').attr('selected', 'selected');
	if (!uc_price_high) jQuery('#f_uc_search_price_high option:last').attr('selected', 'selected');

	populate_mileage_high();
	jQuery('#f_uc_search_mileage_low  option[value='+ uc_mileage_low  +']').attr('selected', 'selected');
	if (!uc_mileage_low) jQuery('#f_uc_search_mileage_low option:first').attr('selected', 'selected');
	jQuery('#f_uc_search_mileage_high option[value='+ uc_mileage_high +']').attr('selected', 'selected');
	if (!uc_mileage_high) jQuery('#f_uc_search_mileage_high option:last').attr('selected', 'selected');

	jQuery('#f_uc_search_make'       ).change(populate_models);
	jQuery('#f_uc_search_year_low'   ).change(populate_year_high);
	jQuery('#f_uc_search_price_low'  ).change(populate_price_high);
	jQuery('#f_uc_search_mileage_low').change(populate_mileage_high);
	
	jQuery('#f_uc_search_type').change(function() {
		if (jQuery('#f_uc_search_type option:selected').val() == 'lto')
			jQuery('#price').hide();
		else
			jQuery('#price').show();
	});
	
	jQuery('#f_uc_search .submit').click(function() {
		qstype     = jQuery('#f_uc_search_type').val();
		qsmake     = jQuery('#f_uc_search_make').val();
		qsmodel    = jQuery('#f_uc_search_model').val();
		qsyears    = jQuery('#f_uc_search_year_low'   ).val() +'-'+ jQuery('#f_uc_search_year_high'   ).val();
		qsmileage  = jQuery('#f_uc_search_mileage_low').val() +'-'+ jQuery('#f_uc_search_mileage_high').val();
		qsprice    = jQuery('#f_uc_search_price_low'  ).val() +'-'+ jQuery('#f_uc_search_price_high'  ).val();
		qsdistance = jQuery('#f_uc_search_distance').val();
		qspostcode = jQuery('#f_uc_search_postcode').val().replace(/O/gi, '0').replace(/ /g, '').replace(/^(...)(...)/, '$1 $2');
		
		qs = new Array();
		if (qstype)     qs[qs.length] = 'type='+ qstype;
		if (qsmake)     qs[qs.length] = 'make='+ qsmake;
		if (qsmodel)    qs[qs.length] = 'model='+ qsmodel;
		if (qsyears)    qs[qs.length] = 'years='+ qsyears;
		if (qsmileage)  qs[qs.length] = 'mileage='+ qsmileage;
		if (qsprice && qstype != 'lto') qs[qs.length] = 'price='+ qsprice;
		if (qsdistance) qs[qs.length] = 'distance='+ qsdistance;
		if (/^[a-z][0-9][a-z] [0-9][a-z][0-9]$/i.test(qspostcode)) qs[qs.length] = 'postcode='+ qspostcode;
		window.location = '?'+ qs.join('&');
	});
	
	jQuery('#makes li:last').css({marginBottom:'10px'});
});
uc_models = {"acura":{"":"all","118":"RDX","121":"RL","146":"TL","149":"TSX","48":"CSX","587":"RSX","605":"EL","606":"NSX","712":"ZDX","730":"CL","737":"Integra","740":"Vigor","97":"MDX"},"astonmartin":{"":"all","153":"Vantage","814":"DB7"},"audi":{"":"all","10":"A5","11":"A6","111":"Q7","1128":"Cabriolet","114":"R8","12":"A8","1260":"S4","1261":"S6","1262":"S8","1263":"S5","150":"TT","593":"Q5","607":"Allroad","741":"RS4","742":"RS6","8":"A3","9":"A4"},"bentley":{"":"all","1215":"Azure","18":"Arnage","43":"Continental"},"bmw":{"":"all","131":"3 Series","132":"5 Series","133":"6 Series","134":"7 Series","165":"Z4","744":"Z8","786":"8 Series"},"buick":{"":"all","16":"Allure","319":"Rainier","320":"Rendez-vous","58":"Enclave","608":"LeSabre","609":"Park Avenue","610":"Terraza","723":"LaCrosse","746":"Century","749":"Regal","90":"Lucerne"},"cadillac":{"":"all","140":"SRX","141":"STS","162":"XLR","49":"CTS","53":"DTS","611":"DeVille","63":"Escalade","756":"Catera","758":"Eldorado","759":"Fleetwood","761":"Seville"},"chevrolet":{"":"all","106":"Optra","147":"Trailblazer","152":"Uplander","203":"Silverado","204":"Colorado","205":"Avalanche","21":"Aveo","210":"Tahoe","211":"Suburban","323":"Epica","40":"Cobalt","45":"Corvette","546":"Monte Carlo","579":"Traverse","586":"SSR","61":"Equinox","612":"Astro","639":"Camaro","646":"Cruze","656":"Express","768":"Blazer","769":"C/K","771":"Cavalier","777":"El Camino","780":"Lumina","781":"Nova","783":"Tracker","785":"Venture","83":"Impala","833":"HHR","95":"Malibu"},"chrysler":{"":"all","107":"Pacifica","110":"PT Cruiser","128":"Sebring","19":"Aspen","2":"300","208":"Town & Country","46":"Crossfire","787":"Cirrus","788":"Concorde","792":"Intrepid","793":"LeBaron","795":"New Yorker","797":"Neon"},"de_tomaso":{"":"all","1218":"Pantera"},"dodge":{"":"all","104":"Nitro","156":"Viper","20":"Avenger","24":"Charger","243":"Dakota","244":"Durango","245":"RAM 1500","263":"Sprinter","581":"Challenger","614":"SRT-4","75":"Grand Caravan","800":"Caravan","802":"SX","803":"RAM 2500","804":"RAM 3500","808":"Stratus","85":"Journey","865":"Caliber","94":"Magnum"},"eagle":{"":"all","886":"Talon","887":"Vision"},"ferrari":{"":"all","1219":"F360","615":"360","68":"F430","888":"328","889":"348","894":"F355","898":"Mondial","899":"Testarossa"},"ford":{"":"all","101":"Mustang","1140":"Model A","115":"Ranger","1168":"E-450","1169":"Econoline","143":"Taurus","54":"Edge","547":"Five Hundred","548":"Freestar","549":"Freestyle","582":"Transit","583":"GT","588":"Fiesta","604":"Flex","617":"Thunderbird","64":"Escape","65":"Expedition","66":"Explorer","67":"F-150","70":"Focus","71":"Fusion","731":"F-250","732":"F-350","734":"F-450","735":"F-550","736":"F-650","809":"Aerostar","811":"Bronco","900":"Contour","901":"Crown Victoria","902":"E-150","903":"E-250","904":"E-350","905":"Escort","906":"Excursion","909":"Granada","911":"Probe","912":"Shelby GT500","913":"Tempo","914":"Windstar"},"gmc":{"":"all","1265":"Suburban","13":"Acadia","1357":"Sierra","164":"Yukon","206":"Canyon","207":"Sierra","60":"Envoy","618":"Jimmy","619":"Safari","682":"Terrain","938":"Savana","939":"Sonoma"},"honda":{"":"all","105":"Odyssey","109":"Pilot","119":"Ridgeline","125":"S2000","15":"Accord","25":"Civic","47":"CR-V","56":"Element","647":"Insight","69":"Fit","727":"Crosstour","943":"CR-X","946":"Prelude"},"hummer":{"":"all","80":"H2","81":"H3","947":"H1"},"hyundai":{"":"all","126":"Santa Fe","136":"Sonata","14":"Accent","145":"Tiburon","151":"Tucson","218":"Veracruz","22":"Azera","55":"Elantra","59":"Entourage","598":"Genesis"},"infiniti":{"":"all","113":"QX","264":"G","268":"EX","551":"Q45","72":"FX","73":"G","92":"M"},"isuzu":{"":"all","962":"Rodeo","964":"Trooper"},"jaguar":{"":"all","142":"S-Type","160":"XJ","161":"XK","163":"X-Type","276":"XF"},"jeep":{"":"all","108":"Patriot","157":"Wrangler","41":"Commander","42":"Compass","76":"Grand Cherokee","86":"Liberty","970":"Cherokee"},"kia":{"":"all","120":"Rio","122":"Rondo","129":"Sedona","137":"Sorento","138":"Spectra","139":"Sportage","17":"Amanti","279":"Borrego","603":"Soul","686":"Forte","93":"Magentis","975":"Optima","976":"Sephia"},"lamborghini":{"":"all","100":"Murciélago","1220":"Countach","74":"Gallardo","977":"Diablo"},"landrover":{"":"all","116":"Range Rover","577":"Freelander","719":"LR4","87":"LR2","88":"LR3","982":"Defender","983":"Discovery"},"lexus":{"":"all","123":"RX","127":"SC","450":"RX","62":"ES","662":"HS","78":"GS","79":"GX","84":"IS","89":"LS","91":"LX"},"lincoln":{"":"all","103":"Navigator","550":"MKS","569":"Town Car","576":"LS","622":"Aviator","644":"MKT","96":"Mark LT","98":"MKX","985":"Continental","986":"Zephyr","99":"MKZ"},"lotus":{"":"all","57":"Elise","657":"Evora","670":"Exige","987":"Elan","988":"Esprit"},"maserati":{"":"all","112":"Quattroporte","77":"Gran Turismo"},"mazda":{"":"all","1":"Mazda3","1001":"Protégé","1002":"RX-7","102":"MX-5","124":"RX-8","135":"B-Series","148":"Tribute","3":"Mazda5","50":"CX-7","51":"CX-9","574":"MPV","6":"Mazda6","721":"Mazda2","990":"323","991":"626","997":"Millenia","998":"MX-3"},"mercedesbenz":{"":"all","1003":"190-Series","1006":"300-Series","1078":"SLR McLaren","26":"B Class","27":"C Class","28":"CL Class","29":"CLK Class","30":"CLS Class","31":"E Class","32":"G Class","33":"GL Class","34":"M Class","35":"R Class","36":"S Class","37":"SL Class","38":"SLK Class"},"mercury":{"":"all","1014":"Cougar","1015":"Marauder","1020":"Mountaineer","1021":"Mystique","1022":"Sable","1025":"Villager","568":"Grand Marquis"},"mini":{"":"all","44":"Cooper"},"mitsubishi":{"":"all","1028":"Lancer Evolution","1032":"Raider","167":"Eclipse","168":"Endeavor","169":"Galant","170":"Lancer","171":"Outlander","537":"Outlander","566":"Montero"},"nissan":{"":"all","1035":"200SX","1036":"240SX","1038":"300ZX","1040":"Pulsar","1182":"Axxess","1257":"370Z","172":"350Z","173":"Altima","174":"Armada","175":"Frontier","176":"Maxima","177":"Murano","178":"Pathfinder","179":"Quest","180":"Rogue","181":"Sentra","182":"Titan","183":"Versa","184":"Xterra","277":"GT-R","640":"Cube","689":"X-Trail"},"oldsmobile":{"":"all","1042":"Achieva","1043":"Alero","1044":"Aurora","1045":"Bravada","1048":"Cutlass","1051":"Intrigue","1053":"Silhouette","1195":"88","1196":"98"},"plymouth":{"":"all","1055":"Acclaim","1056":"Breeze","1058":"Grand Voyager","1060":"Neon","1061":"Prowler","1064":"Voyager","1192":"Road Runner"},"pontiac":{"":"all","1066":"Bonneville","1067":"Fiero","1068":"Firebird","1069":"Grand AM","1070":"GTO","1072":"Montana","1075":"Sunbird","1076":"Sunfire","1077":"Trans Sport","185":"G5","186":"G6","187":"G8","188":"Grand Prix","190":"Solstice","191":"Torrent","192":"Vibe","193":"Wave","690":"Aztek"},"porsche":{"":"all","1080":"928","1082":"944","194":"911","195":"Boxster","196":"Cayenne","197":"Cayman","658":"Panamera","693":"Carrera"},"rollsroyce":{"":"all","1240":"Silver Seraph","1245":"Silver Spirit","1247":"Silver Spur","198":"Phantom"},"saab":{"":"all","1087":"900","200":"9-3","201":"9-5","202":"9-7x","695":"9-2x"},"saturn":{"":"all","1089":"S-Series","212":"Astra","213":"Aura","214":"Outlook","215":"Sky","216":"VUE","559":"Ion","573":"Relay","628":"L-Series"},"scion":{"":"all","651":"tC","652":"xB","653":"xD"},"smart":{"":"all","217":"Fortwo"},"subaru":{"":"all","1091":"Baja","219":"Forester","220":"Impreza","222":"Legacy","223":"Tribeca","676":"Outback"},"suzuki":{"":"all","1093":"Esteem","1096":"Sidekick","1098":"Vitara","224":"Grand Vitara","225":"Swift+","226":"SX-4","227":"XL-7","555":"Verona","556":"Aerio","716":"Kizashi"},"toyota":{"":"all","1101":"Land Cruiser","1102":"MR2","1103":"Paseo","1104":"Previa","1105":"Supra","1107":"Tercel","228":"Avalon","229":"Camry","230":"Corolla","231":"FJ Cruiser","232":"4Runner","233":"Highlander","234":"Matrix","235":"Prius","236":"RAV4","237":"Sequoia","238":"Sienna","240":"Tacoma","241":"Tundra","242":"Yaris","373":"Venza","625":"Celica","627":"Echo","699":"Solara"},"volkswagen":{"":"all","1108":"Beetle","1109":"Cabriolet","1111":"Corrado","1112":"Eurovan","246":"Eos","248":"Jetta","250":"Passat","251":"Rabbit","252":"Tiguan","253":"Touareg","553":"Phaeton","580":"Routan","626":"Golf","678":"GTI","679":"GLI"},"volvo":{"":"all","1119":"700 Series","1121":"850","1124":"S70","1125":"S90","1126":"V40","254":"C30","255":"C70","256":"S40","257":"V50","258":"S60","259":"S80","260":"V70","261":"XC90","270":"XC70","585":"XC60"}}
