// Vehicle home javascript
var all_features;
var cur_feature;


window.addEvent('domready', function() {
	
	//for getNext and getPrevious
	all_features = $('vehicle_feature_menus').getElement('div.features_menu_pos').getElements('h3');
	
	//if some of the required variables are missing to perform the ajax request, return false
	if(!curVehicle || !currentPage || !currentFeature) {
		return;
	}
	
	//if we can't find the menu, don't bother
	var features_menu = $('vehicle_features').getElement('div.feature_menu').getElement('ul');
	if(!features_menu) {
		return;
	}
	
	//generate the list of files to include based off of the nav li ids
	features_menu.getElements('li').each(function(listitem) {
		//grab the id
		var fid = listitem.getProperty('id');
		var feature_link = listitem.getElement('a');
		
		//if this id matches currentFeature, highlight it as selected and return since it doesn't need to be ajaxed
		if(fid == currentFeature) {
			
			cur_feature = fid;
			feature_link.addEvent('click', function(e) {
				e = new Event(e);
				showFeature(fid);
				e.stop();
				return false;
			});
			showFeature(fid);
			return;
		}
		//check for a language
		if(langId === null)
			var url = '/nav/'+curVehicle+'/'+currentPage+'/'+fid+'_component.html';
		else
			var url = '/nav/'+langId+'/'+curVehicle+'/'+currentPage+'/'+fid+'_component.html';
		
		
		//create an empty element that will store the ajax request temporarily
		var div_temp = new Element('div').setStyle('display','none');
		
		
		var myAjax = new Ajax(url, {
				method: 'get',
				update: div_temp,
				evalScripts:true,
				onComplete: function() {
					//inject the element into the feature container
					div_temp.getElement('li').injectInside($('vehicle_features').getElement('ul.feature_content'));
					//now that the new content is loaded, change the menu to access the content via javascript without page reload
					feature_link.addEvent('click', function(e) {
						e = new Event(e);
						showFeature(fid);
						e.stop();
						return false;
					});
				}
			}
		).request();
		
		
		
		
	});
	
	return;

});
function showFeature(id){
	
	
	
	if($('vehicle_features').getElement('ul.feature_content').getElement('li[id='+cur_feature+']')) {
		$('vehicle_features').getElement('ul.feature_content').getElement('li[id='+cur_feature+']').setStyle('display', 'none');
		$('vehicle_features').getElement('div.feature_menu').getElement('ul').getElement("a[id="+cur_feature+"_link]").removeClass("selected_feature_link");
		$('vehicle_features').getElement('div.feature_menu').getElement('ul').getElement("a[id="+cur_feature+"_link]").addClass("off");
	}
	if($('vehicle_features').getElement('ul.feature_content').getElement('li[id='+id+']')) {
		$('vehicle_features').getElement('ul.feature_content').getElement('li[id='+id+']').setStyle('display', 'block');
		$('vehicle_features').getElement('div.feature_menu').getElement('ul').getElement("a[id="+id+"_link']").addClass("selected_feature_link");
		$('vehicle_features').getElement('div.feature_menu').getElement('ul').getElement("a[id="+id+"_link']").removeClass("off");
		
		(fID!= '')?curTarget = fID:curTarget = id;
		fID = id;
		fEmptyCurrent();
		
		if($(id+'_video')){showVideo(id);}
		if($(id+'_flash')){
			eval(id+'_buildSWF()');
		}
		cur_feature = id;
	}
	if($('vehicle_features').getElement('ul.feature_content').getElement('li[id='+id+']').hasClass('large')){
		fFlipStyles('large');
	}else{
		fFlipStyles('');
	}
	
}
function getNext(){
	var feature_position = findFeatureMatch();
	if(feature_position + 1 < all_features.length) {
		window.location = all_features[feature_position+1].getNext().getElement('li a').getProperty('href');
	} else {
		window.location = all_features[0].getNext().getElement('li a').getProperty('href');
	}
}
function getPrevious() {
	var feature_position = findFeatureMatch();
	if (feature_position - 1 >= 0) {
		window.location = all_features[feature_position - 1].getNext().getElement('li a').getProperty('href');
	} else {
		window.location = all_features[all_features.length - 1].getNext().getElement('li a').getProperty('href');
	}
}
function findFeatureMatch(){
	var feature = 0;
	all_features.each(function(el, i) {
		if(el.getProperty('id') == currentPage){
			feature = i;
		}
	});
	return feature;
}

function fFlipStyles(sVal){
	if(sVal == 'large'){
		$('main_content').addClass('large');
	}else{
		$('main_content').removeClass('large');
	}
	
}
