/* =======================================================
MAPPING APPLICATION OVERRIDES
@author: bwright@organic.com
@date: 5/6/2009

This file contains the functions necessary to operate on data
for a specific application. 

The core data file included for this (events) application has not been
copied to the mapping_appliction object, because we do not want to 
maintain multiple copies.
=======================================================*/
var isCurrentEventFound = false;
	

/* ----------------------------------------------------------
mapping_application.init (Override)
 ----------------------------------------------------------*/
mapping_application.init = function() {
	$(mapping_application.model.left_column_container).innerHTML = ""+  //Set the left-column HTML content necessary to build out our application
		//"<!-- PRIMARY SELECTOR SPAN -->"+
		"<span id='selectors_spans'>"+
			"<span id='primary_selector_span'></span>"+
			//"<!-- PRIMARY DESCRIPTION SPAN -->"+
			"<br/><span id='primary_selector_description_span'>"+
				"<p id='primary_event_title'></p>"+
				"<p id='primary_event_desc'></p>"+
			"</span>"+
			//"<!-- SECONDARY SELECTOR SPAN -->"+
			"<br/><span id='secondary_selector_span'></span>"+
		"</span>"+
		//"<!-- DIRECTIONS CONTAINER SPAN -->"+
		"<div id='directions_span'>"+
			//"<!-- DIRECTIONS OVERVIEW Container -->"+
			"<div id='directions_overview_container'>"+
			
				"<div class='background highlight'>"+
					"<p id='curListPos' class='background_text'></p>"+
				"</div>"+
				"<div class='content'>"+
					"<p id='directionsdetail1' class='detail'></p>"+
					"<p id='directionsdetail2' class='detail'></p>"+
					"<p id='directionsdetail3' class='detail'></p>"+
					"<p id='directionsdetail4' class='detail'></p>"+
					"<p id='directionsdetail5' class='detail'></p>"+
				"</div>"+
			
			"</div>"+
			//"<!-- DIRECTIONS Container -->"+
			"<div id='directions_container'></div>"+
			//"<!-- DIRECTIONS Commands -->"+
			"<div id='directions_commands_container'>"+
				"<a href='#' id='printdirections' onclick='javascript: mapping_application.controller.printDirections(); return false;'>Print Directions</a>"+
				"<a href='#' id='backtoresults' onclick='mapping_application.controller.turnOffDirections(); false;'>Back to Events</a>"+
			"</div>"+
		"</div>";

	mapping_application.controller.initialize_map(); //Called here in case we want to override some core mapping_application variables (icon, size, etc)
	mapping_application_overrides.controller.dataLoader();
};

/* ----------------------------------------------------------
mapping_application.view.setDirectionsOverview (Override)
 ----------------------------------------------------------*/
mapping_application.view.setDirectionsOverview = function() {
	if ($defined(mapping_data.primary_events[mapping_application.model.current_primary_selection].secondary_events[mapping_application.model.current_secondary_selection]) == true) {
		var secondary_event = mapping_data.primary_events[mapping_application.model.current_primary_selection].secondary_events[mapping_application.model.current_secondary_selection];
		//Set the list number 
		$('curListPos').innerHTML = secondary_event.listPosition; //.listPosition is set dynamically while building the UL/LI list.
		
		//Set the address/phone info (SHOULD BE EASIER TO DETERMINE INFO
		$('directionsdetail1').innerHTML = secondary_event.name;
		if ($defined(directions.getDistance()) == true) { //Set the distance summary
			var distance = directions.getDistance().html.replace(/mi/, 'Miles'); //Edit text
			$('directionsdetail2').innerHTML = distance+' away';
		}
		
		$('directionsdetail3').innerHTML = secondary_event.address1;
		
		if ($defined(secondary_event.city)==true && $defined(secondary_event.state) == true && $defined(secondary_event.zip) == true) {
			$('directionsdetail4').innerHTML = secondary_event.city+", "+secondary_event.state+" "+secondary_event.zip;
		}
		
		if ($defined(secondary_event.phone1) == true) {
			$('directionsdetail5').innerHTML = secondary_event.phone1;
		}
	}
};

/* ----------------------------------------------------------
mapping_application.controller.onMarkerClick (Override)
 ----------------------------------------------------------*/
mapping_application.controller.onMarkerClick = function(a) {
	mapping_application_overrides.controller.onSecondarySelectorChange(a); //Has contingent methods. This is how the marker updates our secondary when clicked, and the secondary sets model variables, and calls methods.
};




/* ----------------------------------------------------------
mapping_application_overrides
 ----------------------------------------------------------*/
mapping_application_overrides = {
	/* ----------------------------------------------------------
	MODEL
	 ----------------------------------------------------------*/
	model: {
		iconImage: '/shared/images/mapping_application/motorsports/marker',
		selectedMarkerLI: []
	},

	/* ----------------------------------------------------------
	VIEW
	 ----------------------------------------------------------*/
	view: {
		buildPrimaryDescription: function(selectedValue) {
			$('primary_event_title').innerHTML = mapping_data.primary_events[selectedValue]['name'];
			$('primary_event_desc').innerHTML = mapping_data.primary_events[selectedValue]['description'];
		},

		doCustomIconImage: function(markerNumber) {
				return mapping_application_overrides.model.iconImage+''+markerNumber+'.png';
		},
		
		doHighlight: function(elID) {
			/* This should accept a greater range of params, like a selector, element, etc */
			var list = $$('#'+elID+' div.background');
			var list2 = $$('#'+elID+' div.content p.name');
			mapping_application_overrides.model.selectedMarkerLI[0] = list[0];
			mapping_application_overrides.model.selectedMarkerLI[1] = list2[0];
                        list[0].addClass('highlight');
                        list2[0].addClass('highlight');
		},
		
		undoHighlight: function() {
			if ($(mapping_application_overrides.model.selectedMarkerLI[0])&&$(mapping_application_overrides.model.selectedMarkerLI[1])) {
				mapping_application_overrides.model.selectedMarkerLI[0].removeClass('highlight');
				mapping_application_overrides.model.selectedMarkerLI[1].removeClass('highlight');
			}
		}
	},
	/* ----------------------------------------------------------
	CONTROLLER
	 ----------------------------------------------------------*/
	controller: {
		
		dataLoader: function() {
			var default_selected_event_id;
			
			
			//Build the primary selector string 
			var primary_select_string = "<select id='primary_selector' onchange='javascript: mapping_application_overrides.controller.onPrimarySelectorChange(this.value);'>";
			var seekingDate = true;
			var lastSearchedEvent = "";
			for (o in mapping_data.primary_events) {
				lastSearchedEvent = o; //We need to keep track of the object we're searching so we can use the last one if no other one is current.
				var selected = "";
				//Check for current date to default to.
				if (seekingDate == true && mapping_application_overrides.controller.isCurrentEvent(mapping_data.primary_events[o]) == true) { //MATCH ON DATES!
					selected = 'selected';
					seekingDate = false;
					default_selected_event_id = mapping_data.primary_events[o]['id'];
				} 	
				primary_select_string += "<option "+selected+" value='"+mapping_data.primary_events[o]['id']+"'>"+mapping_data.primary_events[o]['name']+"</option>";
			}
			
			
			if (seekingDate == true) { //if seekingDate, we have no current event, so use the last available one
				default_selected_event_id = mapping_data.primary_events[o]['id'];
			}
			
			
			primary_select_string += "</select>";
			$('primary_selector_span').innerHTML = primary_select_string;
			
			//LAUNCH SECONDARY
			
			mapping_application_overrides.controller.onPrimarySelectorChange(default_selected_event_id);
			

		},

		/* mapping_application.controller.onPrimarySelectorChange */
		buildSecondarySelector: function(default_selected_event_id) {
			var secondaries = mapping_data.primary_events[default_selected_event_id].secondary_events;
			//Build the secondary selector LI list 
			var secondary_select_string = "<ul>";
			var count = 0;
			var className = 'even';
			var customIcon = '/shared/images/mapping_application/motorsports/marker';
			
			
			for (o in secondaries) {
			
				//UPDATE THE SECONDARY_EVENT_OBJECT with its position
				secondaries[o].listPosition = Number(count+1);				
			
				mapping_application.view.plotPoint(secondaries[o], mapping_application_overrides.view.doCustomIconImage(Number(count + 1))); 
				//	/shared/images/mapping_application/motorsports 
				((count % 2) != 1)? className = 'even' : className = 'odd'; //SET THE CLASS
				secondary_select_string += ""+
					"<li class='"+className+"' id='"+secondaries[o]['id']+"'"+
						"onclick='javscript: mapping_application_overrides.controller.onSecondarySelectorChange(\""+secondaries[o]['id']+"\","+count+",\"marker_nav\") ;'>"+
					"<div class='background'>"+
						"<p class='background_text'>"+Number(count+1)+"</p>"+
					"</div>"+
					"<div class='content'>"+
						"<p class='name'>"+secondaries[o]['name']+"</p>"+
						"<p class='date'>Date: "+secondaries[o]['date']+"</p>";
				if (secondaries[o]['time'] != "") {
					secondary_select_string += "<p class='time'>Time: "+secondaries[o]['time']+"</p>";
				}					
				secondary_select_string += "</div></li>";
				
				
				count += 1;
			}
			
			secondary_select_string += "</ul>";
			$('secondary_selector_span').innerHTML = secondary_select_string;
		},

		/* mapping_application.controller.onPrimarySelectorChange */
		onPrimarySelectorChange: function(selectedValue) {
			mapping_application.model.current_primary_selection = selectedValue;
			mapping_application.view.clearPoints();
			closeOverlay.all();
			mapping_application_overrides.view.buildPrimaryDescription(selectedValue);
			this.buildSecondarySelector(selectedValue);
		},


		/* mapping_application.controller.onSecondarySelectorChange */		
		onSecondarySelectorChange: function(selectedValue,position,location) {
			mapping_application.model.current_secondary_selection = selectedValue;
			mapping_application_overrides.view.undoHighlight();
			mapping_application_overrides.view.doHighlight(selectedValue);
			
			
			if ($defined(position) == true) {
				mapping_application.view.showInfo(position); //Corresponds to the position in the marker array, since the UL/LI position is built in the same order as the marker.
			}
			
			if(location == "marker_nav"){
				linkTrack("map_marker_nav", selectedValue);
			}
		},
		
		isCurrentEvent: function (primary_event) {
			
			if ($defined(primary_event.date) == true) {
				var myDate=new Date();
				var date_components = primary_event.date.split("/");
				myDate.setFullYear(date_components[2],Number(date_components[0]-1),date_components[1]);
				var today = new Date();
				if (today < myDate && isCurrentEventFound == false) { // < = BEFORE, not AFTER. If Today is BEFORE the event date, return true
					isCurrentEventFound = true;
					return true;
				} 
			} 
			return false;
		}

	}
	
}



//INITIALIZE
onload_register('mapping_data.init()'); //Initialize the data
onload_register('mapping_application.init()');
