// strings really need a trim function
String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
};


/**************************************************************

	Script		: Slider
	Version		: 1.0
	Authors		: Samuel Birch
	Desc		: Slider bar.
	Licence		: Open Source MIT Licence

**************************************************************/

var Slider = new Class({
	options: {
		onChange: Class.empty,
		onComplete: Class.empty,
		onTick: function(pos){
			this.knob.setStyle(this.p, pos);
		},
		mode: 'horizontal',
		steps: 100,
		offset: 0
	},

	initialize: function(el, knob, options){
		this.element = $(el);
		this.knob = $(knob);
		this.setOptions(options);
		this.previousChange = -1;
		this.previousEnd = -1;
		this.step = -1;
		this.element.addEvent('mousedown', this.clickedElement.bindWithEvent(this));
		var mod, offset;
		switch(this.options.mode){
			case 'horizontal':
				this.z = 'x';
				this.p = 'left';
				mod = {'x': 'left', 'y': false};
				offset = 'offsetWidth';
				break;
			case 'vertical':
				this.z = 'y';
				this.p = 'top';
				mod = {'x': false, 'y': 'top'};
				offset = 'offsetHeight';
		}
		this.max = this.element[offset] - this.knob[offset] + (this.options.offset * 2);
		this.half = this.knob[offset]/2;
		this.getPos = this.element['get' + this.p.capitalize()].bind(this.element);
		this.knob.setStyle('position', 'relative').setStyle(this.p, - this.options.offset);
		var lim = {};
		lim[this.z] = [- this.options.offset, this.max - this.options.offset];
		this.drag = new Drag.Base(this.knob, {
			limit: lim,
			modifiers: mod,
			snap: 0,
			onStart: function(){
				this.draggedKnob();
			}.bind(this),
			onDrag: function(){
				this.draggedKnob();
			}.bind(this),
			onComplete: function(){
				this.draggedKnob();
				this.end();
				linkTrack('filter', this.sliderName + '_' + nameCleaner((this.element.getParent().getParent().getElements('li'))[this.step].getText().trim()));
			}.bind(this)
		});
		if (this.options.initialize) this.options.initialize.call(this);
	},

	/*
	Property: set
		The slider will get the step you pass.

	Arguments:
		step - one integer
	*/

	set: function(step){
		this.step = step.limit(0, this.options.steps);
		this.checkStep();
		this.end();
		this.fireEvent('onTick', this.toPosition(this.step));
		return this;
	},
	
	setDefault: function(step){
		if (step > this.options.steps) step = this.options.steps;
		else if (step < 0) step = 0;
		this.step = step;
		this.checkStep();
		this.end();
		this.knob.setStyle(this.p, this.toPosition(this.step)+'px');
		return this;
	},

	scrolledElement: function(event){
		if (event.wheel < 0) this.set(this.step + 1);
		else if (event.wheel > 0) this.set(this.step - 1);
		event.stop();
	},

	clickedElement: function(event){
		var position = event.page[this.z] - this.getPos() - this.half;
		position = position.limit(-this.options.offset, this.max -this.options.offset);
		this.step = this.toStep(position);
		this.checkStep();
		this.end();
		this.set(this.step);
	},

	draggedKnob: function(){
		this.step = this.toStep(this.drag.value.now[this.z]);
		this.checkStep();
		this.setDefault(this.step);
	},

	checkStep: function(){
		if (this.previousChange != this.step){
			this.previousChange = this.step;
			this.fireEvent('onChange', this.step);
		}
	},

	end: function(){
		if (this.previousEnd !== this.step){
			this.previousEnd = this.step;
			this.fireEvent('onComplete', this.step + '');
		}
	},

	toStep: function(position){
		return Math.round((position + this.options.offset) / this.max * this.options.steps);
	},

	toPosition: function(step){
		return this.max * step / this.options.steps;
	}
});

Slider.implement(new Events);
Slider.implement(new Options);

/*************************************************************/




/**************************************************************
vehiclelineup (namespace)
**************************************************************/
var vehiclelineup = {};



/**************************************************************
vehiclelineup.app  (model, view controller, filters, init)
**************************************************************/
vehiclelineup.app = {

	
	/****************************
	//APPLICATION:  MODEL
	****************************/
	model : {
		currentYear: "2010",
		points: [], //Will collect the various points
		params: {}, //A global place to store the incoming parameters
		getPoints: function(point) { //Accepts an index (int/name) into the points array
			return [vehiclelineup.app.model.points[point].min, vehiclelineup.app.model.points[point].max,  vehiclelineup.app.model.points[point].mode];	
		},
		setPoints: function(point,values) {
			if ($defined(point)&&$defined(values)) {
				vehiclelineup.app.model.points[point].min = values[0];
				vehiclelineup.app.model.points[point].max = values[1];
			}
		},
		currentDisplayedModelYear : null,
		setCurrentDisplayedModelYear : function(year) { vehiclelineup.app.model.currentDisplayedModelYear = year; },
		getCurrentDisplayedModelYear : function(year) { return vehiclelineup.app.model.currentDisplayedModelYear; },
		currentYearSelector : true,
		setCurrentYearSelector: function(s) {
			vehiclelineup.app.model.currentYearSelector = s;
			vehiclelineup.app.controller.highlightSelectorEl(s);
		},
		getCurrentYearSelector: function() {
			return vehiclelineup.app.model.currentYearSelector;
		},
		isPointClear: function(point) {
			if (
				(vehiclelineup.app.model.points[point].min == -1) &&
				(vehiclelineup.app.model.points[point].max == -1)
			) {
				return true;
			}
			return false;
		},
		getVehicle: function(id) {
			for (var group in vehiclelineup.app.view.vehicles) {
				for (var vehicle in vehiclelineup.app.view.vehicles[group]) {
					if (vehicle == id)
						return vehiclelineup.app.view.vehicles[group][vehicle];
				}
			}
			
			return false;
		}
	},
	
	
	/****************************
	//APPLICATION:  VIEW
	****************************/
	view : {
		vehicles : [],
		slides: [],
		matchesShown: 0,
		setMatchesShown: function(x) {
			vehiclelineup.app.view.matchesShown = x;
		},
		updateMatchesShown: function() {
			$('matchesShownNumber').setHTML(vehiclelineup.app.view.matchesShown);
		},
		updateVehicles : function() { //Loop through all vehicles to determine whether or not they should be displayed.
			var vehicle = null;
			var view = vehiclelineup.app.view;
			var x = null;
			var vehicleMatchesCount = 0;
			var currentModelYear = 0;
			for (x in view.vehicles) {
				for (vehicle in view.vehicles[x]) {
					if (view.updateVehicle(view.vehicles[x][vehicle])) {
						if (vehiclelineup.app.controller.isArray(vehiclelineup.app.model.params.group)) {
							for (var groupIndex = 0, length = vehiclelineup.app.model.params.group.length; groupIndex < length; groupIndex++) {
								if ($($(vehiclelineup.app.model.params.group[groupIndex]+'Group').getElement('#'+view.vehicles[x][vehicle].id+view.vehicles[x][vehicle].modelYear+'Container'))) {
									vehicleMatchesCount++;
								}
							}
						}
						else if (vehiclelineup.app.model.params.group) {
							if ($($(vehiclelineup.app.model.params.group+'Group').getElement('#'+view.vehicles[x][vehicle].id+view.vehicles[x][vehicle].modelYear+'Container'))) {
								vehicleMatchesCount++;
							}
						}
						else {
							vehicleMatchesCount++;
						}
						currentModelYear = Math.max(currentModelYear,view.vehicles[x][vehicle].modelYear);
					}
				}
			}
			view.setMatchesShown(vehicleMatchesCount);
			view.updateMatchesShown();
			vehiclelineup.app.model.setCurrentDisplayedModelYear(currentModelYear);
		},
		updateVehicle: function(v) {
			var model = vehiclelineup.app.model;
			var view = vehiclelineup.app.view;
			
			if ($defined(v)) {
				if (
					/*
					Ideally, the most restrictive & most used tests will go in order. They can short-circuit early, rather than later. 
					The first one must be isAcceptableModelYear so that the vehicle can be enabled or disabled regardless of whether or not the other criteria is met.
					*/
					view.isAcceptableModelYear(v) && //Will enable()/disable() Vehicle within this method, which is DIFFERENT from turnOn() and turnOff()
					view.isAvailableToApp(v) &&
					((model.isPointClear('price')) || (view.isAcceptablePrice(v))) &&
					((model.isPointClear('mpg')) || (view.isAcceptableMPG(v))) &&
					((model.isPointClear('horsepower')) || (view.isAcceptableHorsepower(v))) &&
					((model.isPointClear('seats')) || (view.isAcceptableSeats(v))) &&
					((model.isPointClear('towing')) || (view.isAcceptableTowing(v)))
				) {
					v.turnOn(); //Colored State
					return true;
				}
				v.turnOff(); //Grayed State
				return false;
			}
		},
		
		/****************************
		//BEGIN FILTER FUNCTION DEFINITIONS
		****************************/
		isAcceptablePrice: function(v) {
			return vehiclelineup.app.view.filterRouter([v.price,v.price], vehiclelineup.app.model.getPoints('price'));
		},
		isAcceptableMPG: function(v) {
			return vehiclelineup.app.view.filterRouter([v.mpg,v.mpg], vehiclelineup.app.model.getPoints('mpg'));
		},
		isAcceptableHorsepower: function(v) {
			return vehiclelineup.app.view.filterRouter([v.horsepower.min,v.horsepower.max], vehiclelineup.app.model.getPoints('horsepower'));
		},
		isAcceptableSeats: function(v) {
			return vehiclelineup.app.view.filterRouter([v.seats.min,v.seats.max], vehiclelineup.app.model.getPoints('seats'));
		},
		isAcceptableTowing: function(v) {
			return vehiclelineup.app.view.filterRouter([v.towingCapacity,v.towingCapacity], vehiclelineup.app.model.getPoints('towing'));
		},
		isAcceptableModelYear: function(v) {
			if (v.isCurrentYear == vehiclelineup.app.model.getCurrentYearSelector()) {
				v.enable(); //If the vehicle matches the current selector state, enable the vehicle
				return true;
			}
			v.disable();
			return false; //else disable the vehicle.
		},
		isAvailableToApp: function(v) {
			var flagName = ( $defined(vehiclelineup.app.flasgTranslationMap[vehiclelineup.app.model.params.app]) ) ? vehiclelineup.app.flasgTranslationMap[vehiclelineup.app.model.params.app] : vehiclelineup.app.model.params.app;
			
			if ( $defined(vehicle_data[v.modelYear][v.id.toString()].flags[flagName]) && vehicle_data[v.modelYear][v.id.toString()].flags[flagName] == false )
			{				
				return false;
			}
			return true;
		},
		
		/* Route to Utility Functions Based on Mode */
		filterRouter: function(vehicleValues, pointValues) {
			if (pointValues[2] == 'at_least') {
				//Min for each
				return vehiclelineup.app.view.isGreaterThanMin(Math.max(vehicleValues[0],vehicleValues[1]),pointValues[0]);
			}
			else if (pointValues[2] == 'at_most') {
				//Max for each
				return vehiclelineup.app.view.isLessThanMax(Math.min(vehicleValues[0],vehicleValues[1]),pointValues[1]);
			}
			else if (pointValues[2] == 'range') {
				//Pass in the min/max for both the vehicle and the slider
				return vehiclelineup.app.view.isWithinRange(vehicleValues[0],vehicleValues[1],pointValues[0],pointValues[1]); 
			}
		},
		
		
		
		/* Utility Functions */
		isGreaterThanMin: function(vehicleValueMin, pointMin) {
			if ( vehicleValueMin >= pointMin ) {
				return true;
			}
			return false;
		},
		
		isLessThanMax: function(vehicleValueMax, pointMax) {
			if (pointMax == -1) { pointMax = vehicleValueMax; } //In case of -1, we need to default to our Vehicle value, since at -1 for max we accept everything.
			if ( vehicleValueMax <= pointMax ) {
				return true;
			}
			return false;
		},
		
		isWithinRange: function(vehicleValueMin, vehicleValueMax, pointMin, pointMax) {
			// If the max slider value is blank and the min slider value isn't, we have an unbounded range
			if (pointMax == -1 && pointMax != pointMin) {
				return (vehicleValueMax >= pointMin || vehicleValueMin >= pointMin);
			}
			
			// If the min slider value is blank and the max slider value isn't, we have an unbounded range
			if (pointMin == -1 && pointMax != pointMin) {
				return ((vehicleValueMax != 0) && (vehicleValueMax <= pointMax || vehicleValueMax <= pointMax));
			}

			// If the max and min slider values are blank, we're at the all values point
			if (pointMax == -1 && pointMax == pointMin) {
				pointMax = vehicleValueMax;
				pointMin = vehicleValueMin;
			}
			
			if ( 
				//Range Tests
				((vehicleValueMax <= pointMax) && (vehicleValueMax >= pointMin)) ||	//v.max fits between slider range
				((vehicleValueMin >= pointMin) && (vehicleValueMin <= pointMax)) ||	//v.min fits between slider range
				((vehicleValueMin <= pointMin) && (vehicleValueMax >= pointMax))	// v.max >= p.max AND v.min <= p.min  //vehicle SPANS slider range
			
			) {
				return true;
			}
			return false;
		}
	},
	
	
	/****************************
	//APPLICATION:  CONTROLLER
	*****************************/
	controller : {
		//Reset Button
		resetButtonEl: 'resetButton',
		fullLineupButtonEl: 'showAllVehicles',
		appTitleEl: 'appTitle',
		appDirectionsEl: 'appDirections',
	
		//currentYear/previousYear selector
		currentYearSelectorEl: 'currentYearSelector',
		previousYearSelectorEl: 'previousYearSelector',
		highlightSelectorEl: function(s) {
			if (s==true && brand_id != 'jeep') {
				$(vehiclelineup.app.controller.currentYearSelectorEl).addClass('ysActive');
				$(vehiclelineup.app.controller.previousYearSelectorEl).removeClass('ysActive');
			} else {
				$(vehiclelineup.app.controller.currentYearSelectorEl).removeClass('ysActive');
				$(vehiclelineup.app.controller.previousYearSelectorEl).addClass('ysActive');
			}
		},
		sliders : [], //Will be set to window['sliders'] as part of initialization
		sliderPointsLength : 0,
		dolightSelectedPoint: function(sliderName, pos) {
			($$('#'+sliderName+'Slider ol li')).each((function(el) {
				if (el.getProperty('value') == pos ) {
					this.highlightSelectedPoint(el);
				}else {
					this.lowlightSelectedPoint(el);
				}
			}).bind(vehiclelineup.app.controller));
		},
		highlightSelectedPoint: function(el) {
			el.addClass('liselected');
			el.removeClass('lideselected');
		},
		lowlightSelectedPoint: function(el) {
			el.removeClass('liselected');
			el.addClass('lideselected');
		},
		doTracking: function(id, state) {
			var toStrip = /Container/; //Must change this in ONCLICK also! 
			var eventVehicle = vehiclelineup.app.model.getVehicle(id.replace(toStrip, ''));
			var lid = (eventVehicle.active) ? 'active_' : 'inactive_';
			
			lid += eventVehicle.id + state;
			
			linkTrack(nameCleaner(eventVehicle.groupID), lid);
		},
		updateMLC: function() {
			var newMLC = window.location.pathname + vehiclelineup.app.model.getCurrentDisplayedModelYear();

			// tack on optional group if we have one
			if (vehiclelineup.app.controller.isArray(vehiclelineup.app.model.params.group)) {
				newMLC += '/' + vehiclelineup.app.model.params.group.join('|');
			}
			else if (vehiclelineup.app.model.params.group) {
				newMLC += '/' + vehiclelineup.app.model.params.group;
			}

			newMLC += ';/' + brand + window.location.pathname + vehiclelineup.app.model.getCurrentDisplayedModelYear();

			// tack on ANOTHER optional group if we have one
			if (vehiclelineup.app.controller.isArray(vehiclelineup.app.model.params.group)) {
				newMLC += '/' + vehiclelineup.app.model.params.group.join('|');
			}
			else if (vehiclelineup.app.model.params.group) {
				newMLC += '/' + vehiclelineup.app.model.params.group;
			}

			// tack on optional application name if we have one
			if (vehiclelineup.app.model.params.app)
				newMLC += '/' + vehiclelineup.app.model.params.app;
			
			// Clear out any filenames that may have shown up from window.location
			newMLC = newMLC.replace(/index.html/g, '');

			resetMLC(newMLC);
		},
		isArray: function(obj) {
			return Object.prototype.toString.call(obj) === "[object Array]";
		}
	},
	
	
	/****************************
	//APPLICATION: advancedFilters open/close
	****************************/
	advancedFilters : {
		controlElement: 'advancedFilterToggler',
		controlledElement: 'advancedFilters',
		filterContainer: 'filterContainer',
		controller: {}, //Init'd to the plugin
		openStatusText: 'Close filters',
		closedStatusText: 'Open filters'
	},
	
	
	/****************************
	//APPLICATION: map
	****************************/
	map : {
		'gaq' : 'get_a_quote', //4
		'satd' : 'testdrive', //6
		'compcomp' : 'compare', //3
		'brochure' : 'brochure', //2 (brochure, not brochure_opts)
		'vehicle_home' : 'true',
		'dealer' : 'true',
		'bmo' : 'true', //1
		'specs' : 'true',
		'inventory' : 'inventory', //5
		'sticker' : 'true',
		'towing' : 'tow', //7
		'incentives' : 'true',
		'priceequip' : 'true'
	},
	
	/****************************
	//APPLICATION: Form menu data flag translation map for applications
	****************************/
	flasgTranslationMap : {
		'priceequip' : 'build',
		'bmo' : 'build'
	},
	

	
	/****************************
	//APPLICATION:  INIT
	****************************/
	init: function () {

		var app = vehiclelineup.app;
		var filters = app.advancedFilters;
		var controller = app.controller;
		var model = app.model;
		var view = app.view;
		var params = model.params;
		controller.sliders = window['sliders'];
		controller.sliderPointsLength = controller.sliders.price.points.length - 1;
		view.vehicles = vehicles;
	
		
		
		/******* BEG PARAMS **********/
		//APPLICATION PARAM CONFIGURATION
		params.app = (getParameter('app')!='') ? getParameter('app'):'vehicle_home' ;
		params.sliderOn = (getParameter('showslider') == 'false') ? false : true;
		params.rolloverOn = (getParameter('showrollover') == 'false') ? false : true;
		params.extra = getParameter('extra');
		params.group = getParameter('group');
		/****************************/
		
		
		//SETUP: Group param handling (figure out how many groups there are)
		if (params.group && params.group.indexOf("|") > 0) {
			var groups_list = params.group.split("|");
			params.group = new Array();
			for (i = 0, j = groups_list.length; i < j; i++){
				params.group[i] = groups_list[i];
			}
		}
		
		
		//SETUP: AdvancedFilters (open/close switch)
		filters.controller = new Fx.Slide(
			filters.controlledElement, 
			{	
				onComplete: function() {
					if (filters.controller.open) {
						$(filters.controlElement).setHTML(filters.closedStatusText).setProperty('name', '&lid=open_filter_box&lpos=filter');
					}
					else {
						$(filters.controlElement).setHTML(filters.openStatusText).setProperty('name', '&lid=close_filter_box&lpos=filter');
						$('advancedFilters').getParent().setStyle('height','auto');
					}
					
					$(vehiclelineup.app.advancedFilters.filterContainer).toggleClass('closed');
				}
			} 
		); // #2
		$(filters.controlElement.toString()).addEvent('click', function(event) { // #3
			filters.controller.toggle();
			new Event(event).stop(); //Kill Event
		});
		
		
		
		/****************************
		//SETUP: SLIDERS/MODEL
		****************************/
		for (slider in controller.sliders) { //For each slider object, build a functional slider...
		
			//SLIDERS: Add the mootools slider plugin capability to each slider in the global sliders object.
			//Each built object will be added as a new property of each respective global slider.
			var sliderObj = new Slider( 
				$$('#'+slider.toString()+'Slider .sliderTrack')[0], 
				$$('#'+slider.toString()+'Slider .sliderKnob')[0], 
				{
					mode: 'vertical',
					steps: controller.sliderPointsLength,
					offset: 0,
					onTick: function(pos){
						this.knob.effect(this.p, {duration: 150, transition: Fx.Transitions.quadOut}).start(pos);
					},
					onChange: function(pos) {
						if ($defined(this.sliderName)&&(pos >= 0)) {
							model.setPoints(this.sliderName,this.getFilterValues(pos)); //ALWAYS SET MIN/MAX Values for the slider
							controller.dolightSelectedPoint(this.sliderName,pos); //Update the slider text elements
							view.updateVehicles();
						}						
					}
				}	
			).set(controller.sliderPointsLength);
			
			// Migrate positioning up to the dynamically created parent so as to mask objects
			$('advancedFilters').getParent().setStyle('position', 'relative');
			
			//Set some properties on the pluginObj
			controller.sliders[slider.toString()].pluginObj = sliderObj; //Plugin Object
			controller.sliders[slider.toString()].pluginObj.sliderName = slider.toString(); //Add the slidername 
			controller.sliders[slider.toString()].pluginObj.sliderType = controller.sliders[slider.toString()].selection_mode; //Add the selection mode
			/* 
				get(Min|Max)FilterValue of the slider
				This function returns the value from within the points array of the sliderObj.
				This function is called by the onchange function of the slider plugin obj in order to set the 
				current point within the model.
			*/
			controller.sliders[slider.toString()].pluginObj.getFilterValues = function(pos) {
				var localMinFilterValue = -1;				
				var localMaxFilterValue = -1;				
				if ($defined(controller.sliders[this.sliderName].points[pos])) {
					if ($defined(controller.sliders[this.sliderName].points[pos].min)) {
						localMinFilterValue =  controller.sliders[this.sliderName].points[pos].min;
					}
					if ($defined(controller.sliders[this.sliderName].points[pos].max)) {
						localMaxFilterValue = controller.sliders[this.sliderName].points[pos].max;
					}
				} 
				return [localMinFilterValue, localMaxFilterValue];
			};
			
			//Set OL/LI elements to be clickable
			var li_counter = 0;
			($$('#'+slider.toString()+'Slider ol li')).each(function(el) {
				el.setProperty('value', li_counter);
				el.addEvent('click', function(e){
					e = new Event(e);
					this.set(Number(el.getProperty('value'))); //Bound to the pluginObject (Returns the POSITION as a value between 0 and sliderPointsLength)
					linkTrack('filter', this.sliderName + '_' + nameCleaner((this.element.getParent().getParent().getElements('li'))[this.step].getText().trim()));
					e.stop();
				}.bind(vehiclelineup.app.controller.sliders[slider.toLowerCase()].pluginObj));
				li_counter ++;
			});
			
			//MODEL : Default all points to -1
			model.points[slider.toString()] = {min: -1, max: -1, mode: controller.sliders[slider.toString()].selection_mode};
			
		}
		

		/****************************
		//SETUP: Vehicle Elements | add control/response elements
		****************************/
		for (x in view.vehicles) {
			for (vehicle in view.vehicles[x]) {
				
				//Define some vehicle properties.
				view.vehicles[x][vehicle].imgContainer = $$('#'+view.vehicles[x][vehicle].id.toLowerCase()+view.vehicles[x][vehicle].modelYear.toString()+'Container img'); //Store the image container in our vehicle object
				view.vehicles[x][vehicle].container = $$('#'+view.vehicles[x][vehicle].id.toLowerCase()+view.vehicles[x][vehicle].modelYear.toString()+'Container');
				
				//UPDATE THE PRICE VALUE (Replace any unwanted characters)
				var badChars = /,/;
				view.vehicles[x][vehicle].price = view.vehicles[x][vehicle].price.replace(badChars, "");
				
				//ADD VEHICLE BEHAVIOR
				view.vehicles[x][vehicle].turnOn 	= function() {
					this.imgContainer.setProperty('src', this.imagePathOn);
					this.active = true;
				};
				view.vehicles[x][vehicle].turnOff 	= function() {
					this.imgContainer.setProperty('src', this.imagePathOff);
					this.active = false;
				};
				view.vehicles[x][vehicle].enable 	= function() { this.container.setStyle('display', ''); };
				view.vehicles[x][vehicle].disable 	= function() { this.container.setStyle('display', 'none'); };
				

				
				
				/***************************************/
				//CREATE ROLLOVER EFFECT & ADD ON-CLICK
				var vehicleIsAppEnabled = view.isAvailableToApp(view.vehicles[x][vehicle]);
				if (params.rolloverOn == true) { //IF RollOvers are on...
				
					if (!vehicleIsAppEnabled) { //IF the vehicle is not enabled, replace the child node of .vechileDetails
						$$('#'+view.vehicles[x][vehicle].id.toLowerCase()+view.vehicles[x][vehicle].modelYear.toString()+'Container .vehicleDetails')[0].empty();
						$$('#'+view.vehicles[x][vehicle].id.toLowerCase()+view.vehicles[x][vehicle].modelYear.toString()+'Container .vehicleDetails')[0].setHTML("<span class='disabledVehicle'>This vehicle cannot be selected at this time. Please contact your dealer for information. <br/><a href='/bridge/index.html?app=cdl'>Find a dealer.</a></span>"); //Include a link to find a dealer. The container is not clickable.
					}
					
					
					//Add rollover state to vehicles
					$(view.vehicles[x][vehicle].id.toLowerCase()+view.vehicles[x][vehicle].modelYear.toString()+'Container').addEvent('mouseenter', function(e){
						e = new Event(e);
						var id = this.getProperty('id');
						$$('#'+id+' .vehicleDetails')[0].setStyle('visibility','visible');
						vehiclelineup.app.controller.doTracking(this.id, '_rollover'); //Tracking
						e.stop();
					}); //THIS = container (challenger2008Container), , so this.id = challenger2008 Container
					
					$(view.vehicles[x][vehicle].id.toLowerCase()+view.vehicles[x][vehicle].modelYear.toString()+'Container').addEvent('mouseleave', function(e){
						e = new Event(e);
						var id = this.getProperty('id');
						$$('#'+id+' .vehicleDetails')[0].setStyle('visibility','hidden');
						e.stop();
					}); //THIS = container (challenger2008Container), so this.id = challenger2008Container
					
				}
				
				//ADD ONCLICK BEHAVIOR (Only add on-click behavior to vehicles available to the APP)
				if (vehicleIsAppEnabled) {
					$(view.vehicles[x][vehicle].id.toLowerCase()+view.vehicles[x][vehicle].modelYear.toString()+'Container').addEvent('click', function(e){
						e = new Event(e);

						vehiclelineup.app.controller.doTracking(this.id+this.modelYear, '_click'); //Tracking						
						
						if (vehiclelineup.app.model.params.app == 'vehicle_home') {
							window.location='/'+language+'/'+this.modelYear+'/'+this.id;
						} else {
							javascript:wrap(vehiclelineup.app.model.params.app, "",this.modelYear+this.id,"");
						} 
						
						e.stop();
					}.bind(view.vehicles[x][vehicle])); //THIS = vehicle object, challenger2008, so this.id = challenger
				}
				/***************************************/
				
			}
		}		

		
		
		
		/****************************
		//SETUP: current/previousYear Selector Button
		****************************/
		if ($defined($(controller.previousYearSelectorEl))) {
			$(controller.currentYearSelectorEl).addEvent('click', function(event) {
				model.setCurrentYearSelector(true);
				view.updateVehicles();
				controller.updateMLC();
				new Event(event).stop(); //Kill Event
			});
			
			$(controller.previousYearSelectorEl).addEvent('click', function(event) {
				if (brand_id == 'jeep' || brand_id == 'chrysler') {
					model.setCurrentYearSelector(true);
				} else {
					model.setCurrentYearSelector(true);
				}
				view.updateVehicles();
				controller.updateMLC();
				new Event(event).stop(); //Kill Event
			});
		}
		

		/****************************
		//SETUP: Slider Reset Button
		****************************/
		$(controller.resetButtonEl).addEvent('click', function(event) {
			for (slider in controller.sliders) {
				controller.sliders[slider.toString()].pluginObj.set(controller.sliderPointsLength);
			}
			new Event(event).stop(); //Kill Event
		});


		/****************************
		//SETUP: Full Lineup Button
		****************************/
		$(controller.fullLineupButtonEl).addEvent('click', function(event) {
			// show all groups
			$$('.vehicleGroup').each(function(elt) {
				elt.setStyle('display', 'block');
			});
			
			this.addClass('hide').setStyle('display', 'none'); // hide the button
			
			// Remove the group param since there's no way to return to the group-only view (important for accurate MLCs)
			vehiclelineup.app.model.params.group = '';
			
			view.updateVehicles();
			
			new Event(event).stop(); //Kill Event
		});


		/****************************
		//SETUP: App specific header
		****************************/
		if (params.app == 'bmo' ||
			params.app == 'priceequip' ||
			params.app == 'brochure')
		{
			var appName = '';
			
			if (params.app == 'bmo' || params.app == 'priceequip')
				appName = 'Build My Own';
			else
				appName = 'Get A Brochure';
			
			// Set text and reveal page title
			$(controller.appTitleEl).setText(appName + ' Vehicle Selector').removeClass('hide');
		}
		
		/****************************
		//SETUP: Compare App specific directions
		****************************/
		if ( params.extra == "vehicleselector" )
		{
			$(controller.appDirectionsEl).setHTML("To explore a " + brand_name + " model, click on a vehicle name or image. To compare a " + brand_name + " vehicle against competitor, use the <a href='javascript:submitNoFormCompare(\"edmunds\")' name='&lpos=content'>" + brand_name + " vs. Competition tool</a>.").removeClass('hide');
		}
		

		/******* BEG PARAMS ***********/
		if (params.sliderOn == false) { //If false, then we toggle out the slider.
			filters.controller.toggle();
		}

	
		
		//SLIDER PARAM CONFIGURATION
		params.year = getParameter('year') ? (getParameter('year') == vehiclelineup.app.model.currentYear) : true;
	
			//set default year for 2011 vehicles to 2010
		//this needs to be removed, once 2011 vehicles are added to lineup		
		if (brand_id == 'ramtrucks') {
			params.year = false;
		} else if (brand_id == 'jeep') {
			params.year = true;
		}else if (brand_id == 'chrysler') {
			params.year = false;
		}
		
		model.setCurrentYearSelector(params.year);
		
		for (slider in controller.sliders) {
			params[slider.toString()] = ((Number(getParameter(slider.toString())) >= 0)||(Number(getParameter(slider.toString())) <= controller.sliderPointsLength)) ? Number(getParameter(slider.toString())) : 0;
			if (params[slider.toString()] > 0) {
				controller.sliders[slider.toString()].pluginObj.set(((params[slider.toString()])-controller.sliderPointsLength)*-1); //Inverse of parameter.
			}
			controller.dolightSelectedPoint(slider.toString(),((params[slider.toString()])-controller.sliderPointsLength)*-1); //Set the styles on the points.
		};
		/****************************/
		
		
		/***************************************/
		//SHOW REQUESTED (OR ALL) GROUPS OF VEHICLES
		if (params.group && $(params.group+'Group')) {
			$(params.group+'Group').setStyle('display', 'block');
			
			// Enable the full lineup button
			$(controller.fullLineupButtonEl).removeClass('hide').setStyle('display', 'block');
		}
		else if (params.group && controller.isArray(params.group)) {
			for (i = 0, j = params.group.length; i < j; i++){
				$(params.group[i]+'Group').setStyle('display', 'block');
			}
			// Enable the full lineup button
			$(controller.fullLineupButtonEl).removeClass('hide').setStyle('display', 'block');
		}
		else {
			$$('.vehicleGroup').each(function(elt) {
				elt.setStyle('display', 'block');
			});
		}
		/***************************************/
		
		view.updateVehicles(); //Set up the proper vehicles.
		controller.updateMLC();
		
	} //END INIT
	
};


onload_register('vehiclelineup.app.init()');
