prolific.decodeHash = function () {
	var hashSplit = (window.location + '').replace(/^\/+|\/+$/g,"").split('/#/'),
		args,
		i,
		reqsLeft,
		whenReady,
		initAddr,
		$boxes = $('.filter_box'),
		liveTypes = ['year','make','model','cylinders','engine','results'];
	
	function init () {
		hashSplit = (window.location + '').replace(/^\/+|\/+$/g,"").split('/#/');
		args = hashSplit[1].replace(/-/g, ' ').replace(/-or-/g,'/').split('/');
		initAddr = window.location + '';
		reqsLeft = args.length - 1;
	}
		
	function set (which) {
		var arg = which > 2? args[which].replace('-cylinder','').replace('-liter', ''): args[which];
		return getSelect(which).val(arg);	
	}
	
	function getSelect (which) {
		return $boxes.eq(which).find('select');
	}
	
	function addOption (select, val) {
		getSelect(select).append('<option value="' + val + '">' + val + '</option>');
	}
	
	function checkHash () {
		if (window.location + '' !== initAddr) {
			parseArgs();
			initAddr = window.location + '';
		}
	}
	
	function parseArgs () {
		function cleanArgs () {
			return args.slice(0, i).join('+').replace(/ /g, '_').replace(/\//g,'_or_').split('+');
		}
	
		if (hashSplit.length > 1) {
			init();
			set(0);
			$boxes.slice(1).hide();
			$('#results').empty();
			if (args.length > 1) {
				for (i = 1; i < args.length; i += 1) {
					(function (i) {
						getSelect(i).empty();
						$.get('/catalytic-converter/' + cleanArgs().join('/') + '/', function (response) {
							var $this = $(response).find('#content ul'),
								$select = getSelect(i);
							reqsLeft -= 1;
							$boxes.eq(i).show();
							$select.append('<option value="-1">Select your ' + liveTypes[i] + '...</option>');
							$this.find('li a').each(function () {
								addOption(i, $(this).text().replace(' cylinder','').replace(' liter', ''));
							});
							set(i);
							if (i === args.length - 1) {
								whenReady = window.setInterval(function () {
									if (reqsLeft === 0) {
										getSelect(i).change();
										window.clearInterval(whenReady);
									}
								}, 100);
							}
						});
					}(i));
				}
			} else {
				getSelect(0).change();
			}
		}
	}
	
	parseArgs();
	window.setInterval(checkHash, 250);
	$boxes.find('select').change(function () {
		var $parent = $(this).parent(),
			$filters = $boxes.slice(0, $parent.prevAll().length + 1).find('select'),
			aug = {
				cylinders: '-cylinder',
				engine: '-liter'
			},
			url = 'http://www.globalconverters.com/#/';
		$('#results').empty();
		$filters.each(function (i) {
			if ($(this).val() == -1) return;
			var id = $(this).attr('id').replace('liveFilter_', '');
			url += $(this).val().replace(/ /g, '_').replace(/\//g,'_or_') + (aug[id]? aug[id]: '') + '/';
		});
		location.href = url;
		initAddr = url;
		init();
	});

};

$(function () {
	prolific.decodeHash();
	$('a.go_catalog').click(function () {
		var location = window.location + '';
		if (location.indexOf('#') > -1) {
			window.location = location.replace('#', 'catalytic-converter');
		} else {
			window.location = '/catalytic-converter/';
		}
	});
	
});
