var jps_auto_noTimeout;
function initJPS() {
	jps_auto_noTimeout = new jps_automobile();
	
	jps_auto_noTimeout.timeout = 30000;
	jps_auto_noTimeout.clientErrorFunc = clientError;
}

function clientError(e) {
	if (e.code == 3000) {
		alert('The automobile you are attempting to add already exists.');
	} else if (e.code == 1003) {
		alert('Lost connection with the server. The requested action was not completed. Please try again.');
	} else {
		alert(e);
	}
}

function VehicleSelector(make, model, year, category, indicator) {
	this.makeSelect = make;
	this.modelSelect = model;
	this.yearSelect = year;
	this.categoryList = category;
	this.indicator = indicator
	this.init();
}

VehicleSelector.prototype = {
	init : function () {
		var sel = this;
		addEvent(this.makeSelect, 'change', function () {
			sel.setMake(sel.makeSelect.options[sel.makeSelect.selectedIndex].value);
		});
		addEvent(this.modelSelect, 'change', function () {
			sel.setModel(sel.modelSelect.options[sel.modelSelect.selectedIndex].innerHTML, sel.modelSelect.options[sel.modelSelect.selectedIndex].value);
		});
		addEvent(this.yearSelect, 'change', function () {
			sel.setYear(sel.yearSelect.options[sel.yearSelect.selectedIndex].value);
		});
	},
	setIndicator : function (value) {
		this.indicator.innerHTML = '';
		this.indicator.appendChild(document.createTextNode(value));
		this.indicator.innerHTML = this.indicator.innerHTML + '&nbsp;';
	},
	loadYears : function (yearVal, makeVal, modelVal) {
		this.yearSelect.innerHTML = '';
		
		var option = document.createElement('option');
		option.value = '';
		option.innerHTML = 'Pick Year';
		this.yearSelect.appendChild(option);
		this.yearSelect.selectedIndex = 0;
		this.yearSelect.values = new Array();
		
		var years = jps_auto_noTimeout.getyears();
		
		var count = 0;
		for (var year in years) {
			if (typeof(years[year]) == 'function') continue;
			count++;
			var option = document.createElement('option');
			option.value = year;
			option.appendChild(document.createTextNode(year));
			this.yearSelect.appendChild(option);
			this.yearSelect.values[year] = count;
		}
		this.setIndicator('loading makes...');
		var sel = this;
		
		if (makeVal) {
			this.yearSelect.selectedIndex = this.yearSelect.values[yearVal];
			this.year = yearVal;
		} else {
			yearVal = '';
		}
		setTimeout(function () { sel.loadMakes(yearVal, makeVal, modelVal) }, 10);
	},
	loadMakes : function (year, makeVal, modelVal) {
		this.makeSelect.innerHTML = '';
		
		var option = document.createElement('option');
		option.value = '';
		option.innerHTML = 'Pick Make';
		this.makeSelect.appendChild(option);
		this.makeSelect.selectedIndex = 0;
		this.makeSelect.values = new Array();
		
		if (year != '') {
			var makes = jps_auto_noTimeout.getmakes(year);
			
			var count = 0;
			for (var make in makes) {
				if (typeof(makes[make]) == 'function') continue;
				count++;
				var option = document.createElement('option');
				option.value = make;
				option.appendChild(document.createTextNode(make));
				this.makeSelect.appendChild(option);
				this.makeSelect.values[make] = count;
			}
		}
		this.setIndicator('loading models...');
		var sel = this;
		
		if (makeVal) {
			this.makeSelect.selectedIndex = this.makeSelect.values[makeVal];
			this.make = makeVal;
		} else {
			makeVal = '';
		}
		setTimeout(function () { sel.loadModels(year, makeVal, modelVal) }, 10);
	},
	loadModels : function (year, make, modelVal) {
		this.setIndicator('loading models...');
		this.modelSelect.innerHTML = '';
		
		var option = document.createElement('option');
		option.value = '';
		option.innerHTML = 'Pick Model';
		this.modelSelect.appendChild(option);
		this.modelSelect.selectedIndex = 0;
		this.modelSelect.values = new Array();
		
		if (make != '') {
			var models = jps_auto_noTimeout.getmodels(year, make);
			
			var count = 0;
			for (var model in models) {
				if (typeof(models[model]) == 'function') continue;
				count++;
				var option = document.createElement('option');
				option.value = models[model];
				option.appendChild(document.createTextNode(model));
				this.modelSelect.appendChild(option);
				this.modelSelect.values[model] = count;
			}
		}
		
		this.setIndicator('loading categories...');
		var sel = this;
		
		if (modelVal) {
			this.modelSelect.selectedIndex = this.modelSelect.values[modelVal];
			this.model = modelVal;
			this.autoID = sel.modelSelect.options[sel.modelSelect.selectedIndex].value;
			setTimeout(function () { sel.loadCategories(sel.modelSelect.options[sel.modelSelect.selectedIndex].value) }, 10);
		} else {
			setTimeout(function () { sel.loadCategories(0) }, 10);
		}
	},
	loadCategories : function (autoID) {
		this.setIndicator('');
		if (autoID) {
			window.location = '/vehicles/' + autoID;
		}
	},
	addCategoryNode : function (category, div) {
		var ul = document.createElement('ul');
		var li = document.createElement('li');
		var strong = document.createElement('strong');
		var childUL = document.createElement('ul');
		
		addClass(ul, 'fltl');
		strong.appendChild(document.createTextNode(category.name));
		li.appendChild(strong);
		li.appendChild(childUL);
		ul.appendChild(li);
		div.appendChild(ul);
		
		for (var key in category.children) {
			if (typeof(category.children[key]) == 'funciton') continue;
			
			this.addSubcategoryNode(category.children[key], childUL);
		}
	},
	addSubcategoryNode : function (category, ul) {
		var li = document.createElement('li');
		var a = document.createElement('a');
		
		a.setAttribute('href', '/vehicles/' + this.autoID + '/' + category.category_id);
		a.appendChild(document.createTextNode(category.name));
		li.appendChild(a);
		ul.appendChild(li);
	},
	setMake : function (make) {
		this.make = make;
		this.model = '';
		this.autoID = 0;
		
		this.setIndicator('loading models...');
		var sel = this;
		setTimeout(function () { sel.loadModels(sel.year, make) }, 10);
	},
	setModel : function (model, id) {
		this.model = model;
		this.autoID = id;
		
		this.setIndicator('loading categories...');
		var sel = this;
		setTimeout(function () { sel.loadCategories(id) }, 10);
	},
	setYear : function (year) {
		this.year = year;
		this.make = '';
		this.model = '';
		this.autoID = 0;
		
		this.setIndicator('loading makes...');
		var sel = this;
		setTimeout(function () { sel.loadMakes(year) }, 10);
	}
}