/* 
	This is the filtering of certain category types and such. This isn't used directly, but
	indirectly via some other functions in this file
*/
function newTypeSelection(type)
{
	// array of our three types so we can loop through it
	var types = new Array('typeLink_all', 'typeLink_direct', 'typeLink_universal');
	// using prototype's "each" functionality
	types.each(function(item) 
	{
		if (type.indexOf(item) == 0)
		{
			// if this is our type, then remove the typeOff class and add the typeOn class
			$(type).removeClassName('typeOff');
			$(type).addClassName('typeOn');
		}
		else
		{
			// this is one of the other two, so take off typeOn and put on typeOff
			$(item).removeClassName('typeOn');
			$(item).addClassName('typeOff');
		}
		
	});
}

/* 
	Toggles view of all category types
*/
function toggleAll()
{
	newTypeSelection('typeLink_all');
	// using prototypes $$() utility
 	var directs = $$('.direct');
	// go through all the elements with .direct as the classname and show them all
	for (var i = 0; i < directs.length ; i++)
	{
		directs[i].show();
	}
	// again, using prototypes $$() utility
	var universals = $$('.universal');
	// go through universals and show them as well
	for (var i = 0; i < universals.length ; i++)
	{
		// .show() is another prototype thing that takes off display:none for an elemen't style
		universals[i].show(); 
	}
}


/* 
	Toggles view of all direct category types
*/
function toggleDirect()
{
	newTypeSelection('typeLink_direct');
	// using prototypes $$() utility
	var universals = $$('.universal');
	// fade out all the unviersals
	for (var i = 0; i < universals.length ; i++)
	{
		universals[i].fade({ duration: 0.5}); // fade is from the effects part of scriptaculous
	}

	var directs = $$('.direct');
	// turn on the directs
	for (var i = 0; i < directs.length ; i++)
	{
		directs[i].show();
	}
}

/* 
	Toggles view of all universal category types
*/
function toggleUniversal()
{	
	newTypeSelection('typeLink_universal');
	
	var directs = $$('.direct');
	// fade out the directs
	for (var i = 0; i < directs.length ; i++)
	{
		directs[i].fade({ duration: 0.5});
	}	
	// make sure the universals are shown
	var universals = $$('.universal');

	for (var i = 0; i < universals.length ; i++)
	{
		universals[i].show();
	}
}

// this one removes the default search text when a person focuses on it
var defaultSearchText = '';
function onFocusSearch(input)
{
	if (defaultSearchText == '')
	{
		defaultSearchText = input.value;
		
		if (input.value == defaultSearchText)
		{
			input.value = '';
		}
	}
	else if (input.value == defaultSearchText)
	{
		input.value= '';
	}
}

/*	
	changes back to the default search text after somebody leaves focus of the input box without changing it
	or leaving it blank
*/
function onBlurSearch(input)
{
	if ((input.value != defaultSearchText) && (input.value == ''))
	{
		input.value = defaultSearchText;
	}
}

function validateSearch(searchForm)
{
	var queryInput = $('queryInput');
	
	if (defaultSearchText == '' || queryInput.value == defaultSearchText || queryInput.value == '')
	{
		alert("Please enter a part number and/or brand.");
		queryInput.focus();
		return false;
	}
	else
	{
		return true;
	}
}



/* 
	START OF YMM JS
*/

// so that we can use year in two functions - getMakes() and getModels() (getModels can't
// easily return year itself) so we'll save it when we do getMakes()
var gloablYearID;

function resetModel()
{
	var modelSelect = $('model-selection');
	modelSelect.disabled = true;	
	modelSelect.innerHTML = '';
	modelSelect[0] = new Option('Model', '', false, false);
}

function resetMake()
{
	var makeSelect = $('make-selection');
	makeSelect.disabled = true;	
	makeSelect.innerHTML = '';
	makeSelect[0] = new Option('Model', '', false, false);
}

// function that onchange calls to get the makes and fill them into the select
function getMakes(year)
{
	resetMake();
	resetModel();
	
	gloablYearID = year;
	
	if (!year || !(year > 0))
	{
		resetModel();
		resetMake();
		return;
	}

	// get the make select thing element (needs prototype to work)
	var makeSelect = $('make-selection');
	
	// present a loading thing and take off the disabled thing
	makeSelect.innerHTML = '<option>loading</option>';
	
	// the ajax request (needs prototype to work)	
	// the ajax request (needs prototype to work)
	var arr = $('base_url').value.split('/');
	var sizeOfArr = arr.size();
	
	if (sizeOfArr == '1' || sizeOfArr == '2'){
		var consoleUrl = '/';
	}
	else {
		var consoleUrl = '/MARKETPLACE/';
	}
	
	consoleUrl = consoleUrl + 'lib/consoles/ymmConsole.php';	
						
	new Ajax.Request(consoleUrl,
	{
		method:'get',
		parameters: 'action=getMakes&year='+year,
		onSuccess: function(response)
		{
			// reponse text comes in as json so eval it and set it to json
			var json = response.responseText.evalJSON(true);
			// take off the loading thing
			makeSelect.disabled = false;
			makeSelect.innerHTML = '';

			// make the first option a choose ___
			makeSelect[0] = new Option('choose make', '', false, false);
			
			// go through the json object and fill in the select options
			for (i = 0; i <= json.length; i++)
			{
					makeSelect[i+1] = new Option(json[i].make, json[i].make_id, false, false);
			}						
		},
		// in case there's a failure		
		onFailure: function() 
		{
			makeSelect.innerHTML = '<option>error</option>';
		}
	});
}

// function that onchange calls to get the models and fill them into the select
function getModels(makeID)
{
	if (!(makeID > 0))
	{
		resetModel();
		return;
	}

	// get the model select thing element (needs prototype to work)
	var modelSelect = $('model-selection');
	var modelSelectNoTrim = $('model-selection-noTrim');
	
	// present a loading thing and take off the disabled thing
	modelSelect.innerHTML = '<option>loading</option>';
	
	// the ajax request (needs prototype to work)
	var arr = $('base_url').value.split('/');
	var sizeOfArr = arr.size();
	
	if (sizeOfArr == '1' || sizeOfArr == '2'){
		var consoleUrl = '/';
	}
	else {
		var consoleUrl = '/MARKETPLACE/';
	}
	
	consoleUrl = consoleUrl + 'lib/consoles/ymmConsole.php';	
					
	new Ajax.Request(consoleUrl,
	{
		method:'get',
		parameters: 'action=getModels&makeID='+makeID+'&year='+gloablYearID,
		onSuccess: function(response)
		{
			// reponse text comes in as json so eval it and set it to json		
			var responseJSON = response.responseText.evalJSON(true);
			// take off the loading thing
			modelSelect.disabled = false;
			modelSelect.innerHTML = '';
			modelSelectNoTrim.innerHTML = '';
			
			// make the first option a choose ___
			modelSelect[0] = new Option('choose model', '', false, false);
			modelSelectNoTrim[0] = new Option('choose model', '', false, false);
			
			for (i = 0; i <= responseJSON.length; i++)
			{	
				// go through the json object and fill in the select options
				modelSelect[i+1] = new Option(responseJSON[i].model, responseJSON[i].year_model_id, false, false);
				modelSelectNoTrim[i+1] = new Option(responseJSON[i].modelNoTrim, responseJSON[i].year_model_id, false, false);
			}						
		},
		// in case there's a failure		
		onFailure: function() 
		{
			modelSelect.innerHTML = '<option>error</option>';
		}
	});
}

// submits the YMM form and does the YMM event tracking stuff...
function submitYMM()
{
	var modelSelection = $('model-selection');
	var ymid = modelSelection.value;
	
	if (ymid > 0)
	{
		var ymmForm = $('ymm');
		
		var year = $('year-selection');
		var make = $('make-selection');
		var model = $('model-selection');
		var modelNoTrim = $('model-selection-noTrim');	
		
		// get the make selected (the text, not the value)
		for (var i = 0; i < make.options.length; i++){
			if (make.options[i].selected == true){
				//alert(" and value: "+make.value+' and text: '+make.options[i].text+"\n\n");
				var makeText = make.options[i].text;
				break
			}
		}
		
		for (var i = 0; i < model.options.length; i++){
			if (model.options[i].selected == true){
				//alert(" and value: "+model.value+' and text: '+model.options[i].text+"\n\n");
				//alert("no TRIM!: "+modelNoTrim.options[i].text);
				var modelText = modelNoTrim.options[i].text;
				break
			}
		}
				
		if (!undefined === window.variablename)
		{
			try {		
				pageTracker._trackPageview('/YMM/year/'+year.value);
				pageTracker._trackPageview('/YMM/make/'+makeText);
				pageTracker._trackPageview('/YMM/make model/'+makeText+' '+modelText);
				pageTracker._trackPageview('/YMM/year make model/'+year.value+' '+makeText+' '+modelText);
		
			}catch(err){}

		}
		
		if (!undefined === window.variablename)
		{
			try {
				secondPageTracker._trackPageview('/YMM/year/'+year.value);
				secondPageTracker._trackPageview('/YMM/make/'+makeText);
				secondPageTracker._trackPageview('/YMM/make model/'+makeText+' '+modelText);
				secondPageTracker._trackPageview('/YMM/year make model/'+year.value+' '+makeText+' '+modelText);
			}catch(err){}		
		}

		ymmForm.submit();
	}
	else
	{
		return;
	}
}

// this is on the shopping guide landing page with the big red button
function turnButtonOn()
{
	// turn on the start button
	$('disabledButton').hide();
	$('enabledButton').show();
	// on the button do submitYMM();
}

/* 
	END OF YMM JS
*/


/* for search results page */

var catListDisplay = false;
var brandListDisplay = false;
var BrandObj;
var CategoryObj;

function toggleCategoryList()
{
	if (CategoryObj == undefined || CategoryObj.state == 'finished' )
	{
		if (catListDisplay == false)
		{
			CategoryObj = Effect.BlindDown('categoryList', {duration: 0.4});
			catListDisplay = true;
			$('categoryArrowDown').hide();
			Effect.Appear('categoryArrowUp');
		}
		else
		{
			CategoryObj = Effect.BlindUp('categoryList', {duration: 0.4});
			catListDisplay = false;
			$('categoryArrowUp').hide();
			Effect.Appear('categoryArrowDown');
		}
	}
}

function toggleBrandList()
{
	if (BrandObj == undefined || BrandObj.state == 'finished' )
	{
		if (brandListDisplay == false)
		{
			BrandObj = Effect.BlindDown('brandList', {duration: 0.4});
			brandListDisplay = true;
			$('brandArrowDown').hide();
			Effect.Appear('brandArrowUp');
		}
		else
		{
			BrandObj = Effect.BlindUp('brandList', {duration: 0.4});
			brandListDisplay = false;
			$('brandArrowUp').hide();
			Effect.Appear('brandArrowDown');
		}
	}	
}

/* end of search result page stuff */