
window.addEvent('domready', function(){
	// The visibility thing hides the elements until after the page is drawn and makes it not so clunky on load
	$('sContainer').setStyle('visibility','visible');
	$('tabholder').setStyle('visibility','visible');
	$$('select').addEvent('change', FormChanges);
	
	$('vSearchbutton').addEvent('mouseover', BtnHover);
	$('vSearchbutton').addEvent('mouseout', BtnHover);
	$('qsMake').setProperty('value', '-1');
	
	$('vSearch').setStyle('height','auto');
	var mySlide = new Fx.Slide('vSearch').hide();  //starts the panel in closed state  

    $('toggleSearch').addEvent('click', function(e){
		e = new Event(e);
		 e.preventDefault(); 
		mySlide.toggle();
		e.stop();
	});

    $('closeSearch').addEvent('click', function(e){
		e = new Event(e);
		mySlide.slideOut();
		e.stop();
	});
	
	$('vSearchbutton').addEvent('click',  function(e){
		e = new Event(e);
		mySlide.toggle();
		var search = DetermineSearchCriteria();
		window.location = escape("/products/lookup/" + search.join("/") + "/");
	});

});
				
var BtnHover = function(e) {
	var btn = $('vSearchbutton');
	switch (e.type) {
		case 'mouseover':
			btn.setStyles({
				"cursor": "pointer",
			//	"background-color": "#228B22",
				"border-top-color": "#228B22",
				"border-right-color":"#228B22",
				"border-bottom-color": "#186318",
				"border-left-color": "#186318"
			});
			break;
		case 'mouseout':
			btn.setStyles({
				"cursor": "auto",
			//	"background-color": "#006400",
				"border-top-color": "#186318",
				"border-right-color":"#186318",
				"border-bottom-color": "#228B22",
				"border-left-color": "#228B22"
				
			});
			break;
	}


};

var FormChanges = function(event){

	// Toggle value of hidden inputs to indicate the search parameters
	$$('select[value!=-1]').each(function(el){
		$('inc' + el.name.toString()).value = 'true';
	});
	
	EnableSearchButtonTest();
	
	switch (this.id) {
		case "qsMake":
			LoadModels(this.value);
		break;
		case "qsModel":
			LoadYears(this.value);
		break;
		case "qsYear":
			LoadFromYearInput(this.value);
		break;
		case "qsCat" :
			LoadSubCategories();
		break;
		case "qsSubcatdet":
			LoadSubCategoryDetail();
		break;
		case "qsReset":
			$$('select').each(function(e){
				e.setProperty('value', '-1');
			});
			$$('input[type=hidden]').each(function(e){
				e.setProperty('value', 'false');
			});
		break;
		
	}

};

var LoadModels = function(make){
	$('qsModel').empty();
		$('qsModel').adopt(new Element('option', {
		value: "-1",
		text: "Choose a Model"
	}));
	var results = new Request.JSON({
		url:"http://" + window.location.hostname + "/products/quickselect/qs_process.php",
		onComplete: function(JSONText){
			var i = 0;
			for (i = 0; i < JSONText.length; i++) {
				$('qsModel').adopt(new Element('option', {
					value: JSONText[i].model,//value: JSONText[i].id,
					text: JSONText[i].model
				}));
			}
		}
	}).get({
		"search": "models",
		"make":  make 
	});
};

var LoadYears = function(modelID){
	$('qsYear').empty();
	$('qsYear').adopt(new Element('option', {
		value: "-1",
		text: "Choose a year"
	}));
	var results = new Request.JSON({
		url:"http://" + window.location.hostname + "/products/quickselect/qs_process.php",
		onComplete: function(JSONText){
			var i = 0;
			for (i = 0; i < JSONText.length; i++) {
				$('qsYear').adopt(new Element('option', {
					value: JSONText[i].YearOffered,
					text: JSONText[i].YearOffered
				}));
			}
		}
	}).get({
		"search": "years",
		"modelid": modelID
	});

};

var LoadFromYearInput = function(year){
	
	if ($('qsYear').value != '-1') {
	
	}

}

var EnableSearchButtonTest = function(){

	if ($('qsMake').value != '-1' &&
	$('qsModel').value != '-1' &&
	$('qsYear').value != '-1') {
		$('vSearchbutton').removeProperty('disabled');
		$('vSearchbutton').setStyles({
			"background-color": "#006400",
			"color": "#ccc"
		});
	}
	else {
	$('vSearchbutton').setProperty('disabled',"true");
		$('vSearchbutton').setStyles({
			"background-color": "#003200",
			"color": "#9a9a9a"
		});
	
	}



}

var LoadCategories = function(){
	$('qsCat').empty();
	$('qsCat').adopt(new Element('option', {
						value: "-1",
						text: ""
					}));
	var results = new Request.JSON({
		url:"http://" + window.location.hostname + "/products/quickselect/qs_process.php",
		onComplete: function(JSONText){
			var i = 0;
			for (i = 0; i < JSONText.length; i++) {
				$('qsCat').adopt(new Element('option', {
					value: JSONText[i].category,
					text: JSONText[i].category
				}));
			}
		}
	}).get({
		"search": "category",
		"modelid": $('qsModel').value,
		"year" : $('qsYear').value
	});

};

var LoadSubCategories = function(){
	$('qsSubcat').empty();
	$('qsSubcat').adopt(new Element('option', {
						value: "-1",
						text: ""
					}));
	var results = new Request.JSON({
		url:"http://" + window.location.hostname + "/products/quickselect/qs_process.php",
		onComplete: function(JSONText){
			var i = 0;
			for (i = 0; i < JSONText.length; i++) {
				var subCatSelect = $('qsSubcat').adopt(new Element('option', {
					value: JSONText[i].subcategory,
					text: JSONText[i].subcategory
				}));
				// If only one subcategory is returned, go ahead and select it.
				if (JSONText.length == 1) {
					subCatSelect.getElement('option[value!=-1]').set('selected','true');
					$('qsSubcatdet').fireEvent('change');
				}
			}
		}
	}).get({
		"search": "subcategory",
		"modelid": $('qsModel').value,
		"year" : $('qsYear').value,
		"category" : $('qsCat').value
	});

};

var LoadSubCategoryDetail = function(){

	$('qsSubcatdet').empty();
	$('qsSubcatdet').adopt(new Element('option', {
						value: "-1",
						text: ""
					}));
	var results = new Request.JSON({
		url:"http://" + window.location.hostname + "/products/quickselect/qs_process.php",
		onComplete: function(JSONText){
			var i = 0;
			for (i = 0; i < JSONText.length; i++) {
				var subCatSelectDetail = $('qsSubcatdet').adopt(new Element('option', {
					value: JSONText[i].subcategorydetail,
					text: JSONText[i].subcategorydetail
				}));
				// If only one subcategory is returned, go ahead and select it.
				if (JSONText.length == 1) {
					subCatSelectDetail.getElement('option[value!=-1]').set('selected','true');
				}
			}
		}
	}).get({
		"search": "subcategorydetail",
		"modelid": $('qsModel').value,
		"year" : $('qsYear').value,
		"category" : $('qsCat').value,
		"subcategory" : $('qsSubcat').value
	});

};

var DetermineSearchCriteria = function(){
	var searches = new Array();
	
	// 'AND' together the names of the selects so we know what to search on
	var selTotal = 0x0;
	$$('input[id^=inc]').each(function(el){
		if (el.value == "true") {
			selTotal = selTotal + parseInt(el.name,16);
		}
	});
	// First item in array is the hex value of all needed searches
	searches.push(selTotal.toString(16).toUpperCase());
	
	// now get the select values IN ORDER of:
	//		make, model, year, subcategory, subcategorydetail
	$$('select[value!=-1]').each(function(el){
		switch (el.name) {
			case 'Make' :
				searches.push(el.value);
			break;
			case 'Model' :
				searches.push(el.value);
			break;
			case 'Year' :
				searches.push(el.value);
			break;
			case 'Subcategory' :
				searches.push(el.value);
			break;
			case 'SubcategoryDetail' :
				searches.push(el.value);
			break;
		}
	});
	
	return searches;
	
}
/**
 * if (el.value != "-1") {
			$('inc' + el.name.toString()).value = 'true';
		}
 */