function getHashParameter(aP){var qS = new String(location.hash.substring(1,location.hash.length));var p = qS.split("&");var val = "";if(aP){for(i=0;i<p.length;i++){if(p[i].split( "=" )[0] == aP){val = p[i].split( "=" )[1];}}return val;}}

var dataLoad = {
	init : "null",
	vehicle : "null",
	index : null,
	accordion : {}
}

function setupIncentivesNavAccordion(cur_page) {
		
	try {
		
		if (window.location.pathname.indexOf('/incentives/programs/') >= 1) {
			$("vehicleAccordionTitle").setHTML('View incentives by vehicle:');
		} else {
			$("vehicleAccordionTitle").setHTML('VEHICLES');
		}
	
		$('lowerOffers').empty();
		$$('li').each(function(el){
		    if(el.getProperty('class').contains('daa_offer') == true){
		        el.remove();
		    }
		});
		
		$('lowerOffers').setHTML("<li><h2>Offers</h2></li>");		
		
		
	} catch(err){}
	
	// Set the global cur_vehicle variable to whatever was passed in from an app
	cur_vehicle = getParameter('family');
	
	// Insert the DMA-based event offers and programs into the navigation
	if ((userData.zipcode || getParameter('zip'))) {
		
		if(getParameter('zip')){
			var zip = getParameter('zip');
		} else {
			var zip = userData.zipcode;
		}
		
		var dmaLookup = new Ajax("/hostd/getlocatedealersbasic.xml?zip=" + zip + "&is" + brand.capitalize() + "=Y", {
			method: 'get',
			onComplete: function(text, xml) {
				// Look up the DAA to allow for finding the current list of event offers
				dma = daaLookup(xml);				
				
				if (dma != undefined) {
					// Grab the list of events in this DAA
					//var jSonRequest = new Json.Remote('/crossbrand/shared/javascripts/incentive/daa_json.js', { // Real URL: '/dma/apps/data/json/daa_'+dma+'.js'
					var jSonRequest = new Json.Remote('/dma/apps/data/json/daa_'+dma+'.js', { // Real URL: '/dma/apps/data/json/daa_'+dma+'.js'
						method: 'get',
						onComplete: function(data) {						
							// Parse the list of events that don't pertain to a vehicle
							
							if (window.location.pathname.indexOf('/incentives/programs/') > -1) {
									
									try {
										$('daa_tagline').remove();
									} catch(err){}
									
									var tagline = data[0].tagline;
									var div = new Element('div' , { "id": "daa_tagline" });
									
									div.clone().injectBefore($("slot_1").getFirst()).setHTML("<h4>"+tagline+"</h4>");								
									
							}							
							
							for (var event in data[0].events) {
								var curEvent = data[0].events[event];								
								
								// Ensure the current iteration is an object and not say an enumerable function
								if (typeof curEvent === 'object') {
									// Events with nav == 1 are to appear in each vehicle menu
									if (curEvent['nav'] == '1') {
										// Build the LI for this event
										var listItem = new Element('li', {
											'class': curEvent.short_name + " daa_offer"
										});
										
										// Build the link for this event
										var link = new Element('a', {
											'href': curEvent.link
										});
										
										// Set the text of this event, and insert it into each vehicle's menu
										link.setText(curEvent.title);
										link.injectInside(listItem);
										
										$$('#vehicleNavAccordions li ul').each(function(elt) {
											// Acquire the currently iterated-to vehicle id
											var curVehicle = elt.getPrevious().getProperty('id');
											curVehicle = curVehicle.substr(0, curVehicle.indexOf('NavAccordion'));
											
											// Clone the list item template and set the LID to the new vehicle/event combination
											var listItemToInsert = listItem.clone();
											listItemToInsert.getElement('a').setProperty('name', '&lid='+curVehicle+'_'+curEvent.short_name+'&lpos=vehicle_nav');
											listItemToInsert.injectInside(elt);
										});
									}
									
									// Events with nav == 2 belong in their own box below the vehicle list
									else if (curEvent['nav'] == '2') {
										// Handle non-vehicle links										
																												
										$('lowerBox').setStyle('display','block');
									
										var listItem = new Element('li', {
											'class': curEvent.short_name + " daa_offer"
										});

										var link = new Element('a', {
											'href': link,
											'name': curEvent.short_name + '&lpos=otherOffers',
											'onClick' : "javascript:linkTrack('otherOffers','"+curEvent.short_name+"')"
											
										});

										// Set the link text and inject it within the vehicle's menu
										var title = curEvent.title.toString().replace('&#44;',',');
										link.setText(title);
										link.injectInside(listItem);
									
										if($('lowerOffers').getFirst() != null){
											listItem.clone().injectAfter($('lowerOffers').getFirst());
										} else {
											listItem.clone().injectAfter($('lowerOffers').getFirst());
										}									
										
									}
								}
							}
							
							// Loop over the vehicles in the DAA list and add any events they have to their list
							for (var vehicle in data[0].vehicles) {
								var curVehicle = data[0].vehicles[vehicle];

								for (var event in curVehicle.events) {
									var curEvent = curVehicle.events[event];
									
									// Ensure the iterated-to item is an events object and then parse it
									if (typeof curEvent === 'object') {										
										
										// Build the list item and it's link
										var listItem = new Element('li', {
											'class': curEvent.short_name + " daa_offer"
										});
										
							
										//var family = $(vehicle + 'NavAccordion').getParent().getPropery('id').toString().split("NavAccordion")[0];										 

										var link = new Element('a', {
											'href': curEvent.link,
											'name': '&lid='+ curEvent.short_name + '&lpos=vehicle_nav',
											'onClick': 'javascript:linkTrack("vehicle_nav","'+ curEvent.short_name +'")'
										});

										// Set the link text and inject it within the vehicle's menu
										var title = curEvent.title.toString().replace('&#44;',',');
										link.setText(title);
										link.injectInside(listItem);
									
										listItem.clone().injectInside($(vehicle + 'NavAccordion').getNext());
										
									}
								}
							}

							// Now that the menus are entirely populated, build the Accordions for them
					
							naviLoad.init(cur_page);
							
														
						}
					}).send();
				} else {		
					
					naviLoad.init(cur_page);
				}
			}
		}).request();
	} else {	

		naviLoad.init(cur_page);
	}
}



var naviLoad = {
	
	titles : [],
	cur_page : "",
	acc : {},
	selIndex : null,
	veh : "",
			
	init: function(cur_page, index){		
		this.titles = $$('#vehicleNavAccordions h3');
		this.cur_page = cur_page.toString();
		this.initAccordion();
		
		if (this.cur_page) {
			var incentiveItem = $$('#vehicleNavAccordions li.' + this.cur_page + ' a');
			incentiveItem.addClass('current');
		}
		
		if(window.location.pathname.indexOf('/incentives/programs/') == -1){
			$$('#vehicleNavAccordions li h3').addEvent('click', function() { 
				var vehicle = this.getProperty('id').toString().split("NavAccordion")[0];
				naviLoad.veh = vehicle;
	
				if(this.veh == ("crossfire" || "pacifica")){
					year = "2008";
				} else {
					year = "2009"
				}
			
				$(this.getProperty('id')).fireEvent(displayHandler.gotoVehicle(vehicle, year)); 
			
				var items = $$('#vehicleNavAccordions li h3');
			
				if(window.ie){
					for(i=0; i < items.length; i++){
						if(items[i].getProperty('id') == this.getProperty('id')){
							naviLoad.acc.display(i);
						}
					}
				}
			
			});
		
			var links = $$('#vehicleNavAccordions li.incentives a');
		
			links.each(function(el, i){
				if (window.location.pathname.indexOf('/incentives/programs/') == -1) {
					var familyRegExp = /family=([_a-zA-Z0-9]+)/;
					familyRegExp.exec(el.getProperty('href'));			
					var family = el.getParent().getParent().getPrevious().getProperty('id').toString().split('NavAccordion')[0];
					el.setProperty('href','javascript:displayHandler.gotoVehicle("'+family+'", "2009");');			
					dealerConnector.selected_vehicle = family;
				
					var items = $$('#vehicleNavAccordions li h3');

					if(window.ie){
						for(i=0; i < items.length; i++){
							if(items[i].getProperty('id') == el.getProperty('id')){
								naviLoad.acc.display(i);
							}
						}
					}				
				}
			});
		}
	},	
	
	ieCheck : function(id){
		if(window.ie){
			naviLoad.init(this.cur_page);			
		}
	},	
	
	initAccordion : function(){		
		this.acc = new Accordion('#vehicleNavAccordions li h3','#vehicleNavAccordions .vehicleNavAccordion', {
			onActive: function(elt) { 
				elt.getParent().addClass('current');
				naviLoad.veh = elt.getProperty('id').toString().split("NavAccordion")[0];
			},
			onBackground: function(elt) { elt.getParent().removeClass('current'); }
			
		});
		
		
		
		this.setActions();		
		
	},
	
	params : function(){
		for ( var index = 0; index < this.titles.length; index++ ) {	
			if ((this.titles[index].getProperty('id') == getParameter('family') + 'NavAccordion') || (this.titles[index].getProperty('id') == getHashParameter('family') + 'NavAccordion')) {										
				return index;
			} 
							
		}
	},
	
	setActions : function(){		
		this.acc.display(this.params());						
	}
	
	
}





/*
function buildAccordions(){
	
	var selected_vehicle = "";
	
	var incentivesNavAccordion = new Accordion('#vehicleNavAccordions li h3','#vehicleNavAccordions .vehicleNavAccordion', {
		display: (function() {
			if(getParameter('family') || getHashParameter('family')){
				var clickableTitles = $$('#vehicleNavAccordions h3');				
				for (var index = 0; index < clickableTitles.length; index++ ) {
					if ((clickableTitles[index]).getProperty('id') == (getParameter('family') + 'NavAccordion') || (clickableTitles[index]).getProperty('id') == (getHashParameter('family') + 'NavAccordion')) {										
						return index;		
					}				
				}			
			} else {
				return -1;
			}
		})(),
		
		onComplete: function() { 
			
			var element= $(this.elements[this.previous]);
			
			linkTrack("vehicle_nav", element.getText());
			console.log(element.getPrevious())
			dataLoad.vehicle = element.getPrevious().getProperty('id').toString().split("NavAccordion")[0];
			
		    if (window.location.pathname.indexOf('/incentives/programs/') == -1) {
				displayHandler.gotoVehicle(dataLoad.vehicle, "2009");
			}
		},
		
		
		// Highlight the new selection
		onActive: function(elt) { 
			elt.getParent().addClass('current');			
		},
		
		// Un-highlight the old selection
		onBackground: function(elt) { elt.getParent().removeClass('current');}
	});	
	

	
	// At pageload highlight the current incentive item

	if (cur_page) {
		var incentiveItem = $$('#vehicleNavAccordions li.' + cur_page + ' a');
		incentiveItem.addClass('current');
	}	
	
	$$('#vehicleNavAccordions li.incentives a').each(function(el, i){
		if (window.location.pathname.indexOf('/incentives/programs/') == -1) {
			var familyRegExp = /family=([_a-zA-Z0-9]+)/;
			familyRegExp.exec(el.getProperty('href'));			
			var family = el.getParent().getParent().getPrevious().getProperty('id').toString().split('NavAccordion')[0];
			el.setProperty('href','javascript:displayHandler.gotoVehicle("'+family+'", "2009");');			
			dealerConnector.selected_vehicle = family;
		}
	});		
	
	
}
*/


onload_register('setupIncentivesNavAccordion(cur_page);');