window.addEvent('domready', function() {
	
	/*var initMultiBox = new multiBox({
		mbClass:'.mb', 
		container: $(document.body),
		descClassName: 'multiBoxDesc', 
		useOverlay: true,
		maxSize: {w:600, h:400}
	});*/
	
	var initMultiBox = new multiBox({
		mbClass: '.mb',//class you need to add links that you want to trigger multiBox with (remember and update CSS files)
		container: $(document.body),//where to inject multiBox
		descClassName: 'multiBoxDesc',//the class name of the description divs
		path: './assets/js/',//path to mp3 and flv players
		useOverlay: true,//use a semi-transparent background. default: false;
		maxSize: {w:600, h:400},//max dimensions (width,height) - set to null to disable resizing
		addDownload: false,//do you want the files to be downloadable?
		//pathToDownloadScript: './Scripts/ForceDownload.asp',//if above is true, specify path to download script (classicASP and ASP.NET versions included)
		addRollover: false,//add rollover fade to each multibox link
		addOverlayIcon: false,//adds overlay icons to images within multibox links
		addChain: false,//cycle through all images fading them out then in
		recalcTop: true,//subtract the height of controls panel from top position
		addTips: true//adds MooTools built in 'Tips' class to each element (see: http://mootools.net/docs/Plugins/Tips)
	});

	var homeEffect = new Fx.Tween($('home_footer'));
	var aboutEffect = new Fx.Tween($('about_footer'));
	var towbarEffect = new Fx.Tween($('towbar_footer'));
	var accessoriesEffect = new Fx.Tween($('accessories_footer'));
	var termsEffect = new Fx.Tween($('terms_footer'));
	var contactEffect = new Fx.Tween($('contact_footer'));
	
	new Autocompleter.Request.JSON('part_input', '/makes/parts_find', {'postVar': 'search','overflow': true});
	
	$('part_input').addEvent('focus', function(e) {
		if($('part_input').value == "Part Number (optional)") {
			$('part_input').value = "";
		}
	});
	
if(!$('home_footer').hasClass('selected')){		
	$('_home_button').addEvent('mouseover', function(e) {
		e.stop();
		homeEffect.cancel();
		homeEffect.start('background-color', '#274e84');
	});

	$('_home_button').addEvent('mouseout', function(e) {
		e.stop();
		homeEffect.cancel();
		homeEffect.start('background-color', '#fff');
	});
}
	
if(!$('about_footer').hasClass('selected')){	
	$('_about_button').addEvent('mouseover', function(e) {
		e.stop();
		aboutEffect.cancel();
		aboutEffect.start('background-color', '#274e84');
	});

	$('_about_button').addEvent('mouseout', function(e) {
		e.stop();
		aboutEffect.cancel();
		aboutEffect.start('background-color', '#fff');
	});
}

if(!$('towbar_footer').hasClass('selected')){	
	$('_towbar_button').addEvent('mouseover', function(e) {
		e.stop();
		towbarEffect.cancel();
		towbarEffect.start('background-color', '#274e84');
	});

	$('_towbar_button').addEvent('mouseout', function(e) {
		e.stop();
		towbarEffect.cancel();
		towbarEffect.start('background-color', '#fff');
	});
}
if(!$('accessories_footer').hasClass('selected')){	
	$('_accessories_button').addEvent('mouseover', function(e) {
		e.stop();
		accessoriesEffect.cancel();
		accessoriesEffect.start('background-color', '#274e84');
	});

	$('_accessories_button').addEvent('mouseout', function(e) {
		e.stop();
		accessoriesEffect.cancel();
		accessoriesEffect.start('background-color', '#fff');
	});
}

if(!$('terms_footer').hasClass('selected')){	
	$('_terms_button').addEvent('mouseover', function(e) {
		e.stop();
		termsEffect.cancel();
		termsEffect.start('background-color', '#274e84');
	});

	$('_terms_button').addEvent('mouseout', function(e) {
		e.stop();
		termsEffect.cancel();
		termsEffect.start('background-color', '#fff');
	});
}

if(!$('contact_footer').hasClass('selected')){	
	$('_contact_button').addEvent('mouseover', function(e) {
		e.stop();
		contactEffect.cancel();
		contactEffect.start('background-color', '#274e84');
	});

	$('_contact_button').addEvent('mouseout', function(e) {
		e.stop();
		contactEffect.cancel();
		contactEffect.start('background-color', '#fff');
	});
}	




	$('make_select').addEvent('change',function(e) {
		//alert('make changed to ' + $('make_select').value);
		if($('make_select').value > 0) {
			$('model_select').options[0].text = 'Loading, Please wait....';
			document.getElementById("selectmodel_select").childNodes[0].nodeValue = $('model_select').options[0].text;
			$('model_select').options[0].selected = true;

			
			$('model_select').options.length = 2;
			e.stop();
			var request = new Request.JSON({
				url: '/makes/json/' + $('make_select').value, //+ 'data.json',
				onComplete: function(jsonObj) {
					addModels(jsonObj.models);
					$('model_select').options[0].text = 'Models';
					document.getElementById("selectmodel_select").childNodes[0].nodeValue = $('model_select').options[0].text;
				}
			}).send();

		}
	});
	
	var addModels = function(models){
		var index = 2;
		models.each(function(model) {
			$('model_select')[index] = new Option(model.name, model.id);
			//alert('adding ' + model.name);
			index++;
		});
	};
	
	
	$('search_form').addEvent('submit',function(e) {
		//alert('submitting');
		if($('part_input').value == "Part Number (optional)")
			$('part_input').value = '';
			
		$('part_input').value = escape($('part_input').value);
		
		if(parseInt($('make_select').value) < 1 && parseInt($('model_select').value) < 1 && parseInt($('part_input').value) < 1)
		{
			alert('You need to select at least a vehicle make.');
		} else {
			 window.location = "/towbar/search/make/"+$('make_select').value+"/model/"+$('model_select').value+"/part/"+$('part_input').value;
		}
		
		
		
		return false;
		
	});
	
	
});

