﻿
/// <reference path="../../../../../jQuery/1.3.2/jquery-1.3.2-vsdoc.js" />

/**
 *
 * RangeDataDisplay
 *
 */
	
	if (!mrm.global.isNamespaceDefined("mrm.common.display.views")) mrm.global.createNamespace("mrm.common.display.views", "1.0");
	
	
	mrm.common.display.views.DataDisplay = mrm.common.display.SwfExternalDataHost.subClass(
		{
			/*
			 =============================
			 CONSTANTS
			 =============================
			 */
				DISPLAY_MODE_ADVANCED_SEARCH	: "DISPLAY_MODE_ADVANCED_SEARCH",
				DISPLAY_MODE_RANGE_DETAILS		: "DISPLAY_MODE_RANGE_DETAILS",
				
				DISPLAY_TYPE_GRAPH				: "DISPLAY_TYPE_GRAPH",
				DISPLAY_TYPE_GRID				: "DISPLAY_TYPE_GRID",
				
				DISPLAY_W						: 430,
				DISPLAY_H_ADVANCED_SEARCH		: 220,
				DISPLAY_H_RANGE_DETAILS			: 265,
				
				RANGE_DETAILS_ITEM_HEIGHT_FACTOR	: 32,
				RANGE_DETAILS_DISPLAY_HEIGHT_EXTRA	: 110,
				
				JQ_TARGET_RANGE_DETAILS_ITEMS_CONTAINER	: "body > div.main-container > div.range-container > div.range-view-container > div.available-models-container div.model-items-container",
				JQ_TARGET_RANGE_DETAILS_ITEM			: "div.model-item",
				
				
				JQ_TARGET_RANGE_DATA_DISPLAY_STATE_CONTAINER	: "body > div.main-container > div.range-container > div.range-view-container > div.explore-range-container div.display-state-container",
				JQ_TARGET_RANGE_DATA_DISPLAY_STATE				: "div.display-state",
				
				
				SWF_DATA_DISPLAY				: "DataDisplay.swf",
				SWF_DATA_DISPLAY_GRAPH			: "DataDisplayGraph.swf",
				SWF_DATA_DISPLAY_GRID			: "DataDisplayGrid.swf",
				
				SWF_DATA_DISPLAY_BGND_COLOUR_DARK	: "#CCCCCC",
				SWF_DATA_DISPLAY_BGND_COLOUR_LIGHT	: "#EEEEEE",
				
				CLASS_LOADING_ICON_CONTAINER : "AUTO-DISABLE-DISPLAY-LOADING-ICON",
				
				
				DATA_LABEL_SCOPE				: "dataScope",
				DATA_LABEL_CATEGORY				: "dataCategory",
				DATA_LABEL_SELECTION_SUMMARY	: "selectionSummary",
				DATA_LABEL_DATA					: "data",
				DATA_LABEL_DATA_SUMMARY			: "dataSummary",
				DATA_LABEL_ID					: "id",
				DATA_LABEL_SELECTED				: "selected",
				
				
				DATA_VALUE_CATEGORY_PRICE			: "DATA_CATEGORY_PRICE",
				DATA_VALUE_CATEGORY_MILEAGE			: "DATA_CATEGORY_MILEAGE",
				DATA_VALUE_CATEGORY_FUEL_TYPE		: "DATA_CATEGORY_FUEL_TYPE",
				DATA_VALUE_CATEGORY_TRANSMISSION	: "DATA_CATEGORY_TRANSMISSION",
				
				DATA_VALUE_SCOPE_LEVEL_1			: "SCOPE_LEVEL_1",
				DATA_VALUE_SCOPE_LEVEL_2			: "SCOPE_LEVEL_2",
				DATA_VALUE_SCOPE_LEVEL_3			: "SCOPE_LEVEL_3",
				
				DATA_LABEL_SELECTED_MIN				: "selectedMin",
				DATA_LABEL_SELECTED_MAX				: "selectedMax",
				DATA_LABEL_SELECTABLE_MIN			: "selectableMin",
				DATA_LABEL_SELECTABLE_MAX			: "selectableMax",
				
				/* Item selection events */
				UPDATE_EVENT_DATA_LABEL_SCOPE		: "scope",
				UPDATE_EVENT_DATA_LABEL_SELECT		: "select",
				UPDATE_EVENT_DATA_LABEL_LEVEL1ID	: "level1Id",
				UPDATE_EVENT_DATA_LABEL_LEVEL2ID	: "level2Id",
				UPDATE_EVENT_DATA_LABEL_LEVEL3ID	: "level3Id",
				
				/* Selected Value & Selection Summary events */
				UPDATE_EVENT_DATA_LABEL_CATEGORY	: "dataCategory",
				UPDATE_EVENT_DATA_LABEL_VALUE_MIN	: "minValue",
				UPDATE_EVENT_DATA_LABEL_VALUE_MAX	: "maxValue",
			
				UPDATE_EVENT_DATA_LABEL_SELECTABLE_MIN	: "selectableMin",
				UPDATE_EVENT_DATA_LABEL_SELECTABLE_MAX	: "selectableMax",
				UPDATE_EVENT_DATA_LABEL_SELECTED_MIN	: "selectedMin",
				UPDATE_EVENT_DATA_LABEL_SELECTED_MAX	: "selectedMax",
			
			
			
			/*
			 =============================
			 CONSTRUCTOR
			 =============================
			 */
				init : function (displayManagerInstance, classInstanceID, replacementTargetParentID, replacementTargetID, swfInstanceID, swfData, displayMode, displayType, dataCategory, displayTitle, deferSwfCreation)
				{
					this._displayManager = displayManagerInstance;
					
					var height;
					
					if (displayMode == this.DISPLAY_MODE_RANGE_DETAILS)
					{
						// Set to default
						height = this.DISPLAY_H_RANGE_DETAILS;
						
						var _h;
						 
						// Find and count items (if found)
						var items = $(this.JQ_TARGET_RANGE_DETAILS_ITEMS_CONTAINER).find(this.JQ_TARGET_RANGE_DETAILS_ITEM);
						if (items.length > 0)
						{
							_h = (this.RANGE_DETAILS_ITEM_HEIGHT_FACTOR * items.length) + this.RANGE_DETAILS_DISPLAY_HEIGHT_EXTRA;
						}
						
						// Set html display state divs to match height
						var displayStateContainer	= $(this.JQ_TARGET_RANGE_DATA_DISPLAY_STATE_CONTAINER);
						var displayStates			= displayStateContainer.find(this.JQ_TARGET_RANGE_DATA_DISPLAY_STATE);
						
						displayStateContainer.height(_h);
						displayStates.height(_h);
						height = String(_h) + "px";
					}
					else height = this.DISPLAY_H_ADVANCED_SEARCH;
					
					
					var targetFlashVersion		= "9.0.48";
					//var targetFlashVersion		= "100";
					
					var swfAttributes			= {};
					/*swfAttributes.data			= "http://www.lexus.ucl.dev.portfolio-europe.co.uk/assets/flash/RangeDataDisplay.swf";*/
					swfAttributes.data			= "/assets/flash/" + ((displayType == this.DISPLAY_TYPE_GRAPH) ? this.SWF_DATA_DISPLAY_GRAPH : this.SWF_DATA_DISPLAY_GRID);
					swfAttributes.id			= swfInstanceID;
					swfAttributes.name			= swfInstanceID;
					swfAttributes.width			= this.DISPLAY_W;
					swfAttributes.height		= height;
					swfAttributes.pluginspage	= "http://www.macromedia.com/go/getflashplayer";
					
					var swfParameters				= {};
					swfParameters.align				= "top";
					swfParameters.allowfullscreen	= true;
					swfParameters.allowscriptaccess	= "sameDomain";
					swfParameters.base				= "../../";/*"http://www.lexus.ucl.dev.portfolio-europe.co.uk";*/
					swfParameters.bgcolor			= "#ff0000";//((displayMode == this.DISPLAY_MODE_RANGE_DETAILS) ? this.SWF_DATA_DISPLAY_BGND_COLOUR_DARK : this.SWF_DATA_DISPLAY_BGND_COLOUR_LIGHT);
					swfParameters.devicefont		= false;
					swfParameters.loop				= false;
					swfParameters.menu				= true;
					swfParameters.play				= true;
					swfParameters.quality			= "best";
					swfParameters.salign			= "tl";
					swfParameters.scale				= "noscale";
					swfParameters.wmode				= "transparent"; //opaque
					
					this._swfFlashVars = "&displayMode=" + displayMode + "&displayTitle=" + displayTitle + "&dataCategory=" + dataCategory;
					
					this._swfData = swfData;
					
					
					//
					// LOADING STATE ELEMENTS
					//
					
					// Unique flash replace ID for
					this._loadingDataClass			= "loading-data " + replacementTargetParentID + "-loading";
					this._loadingDataFlashReplaceID = replacementTargetID + "-loading-data-swf-replace-id";
					
					// Instance (created empty)
					this._loadingDataSwf	= null;
					this._loadingDataSwfID	= replacementTargetID + "-loading-data-swf";
					
					// Attributes / parameters
					this._loadingDataSwfAttributes				= {};
					this._loadingDataSwfAttributes.data			= "/assets/flash/DataLoadingDisplay.swf";
					this._loadingDataSwfAttributes.id			= this._loadingDataSwfID;
					this._loadingDataSwfAttributes.name			= this._loadingDataSwfID;
					this._loadingDataSwfAttributes.width		= "100px";
					this._loadingDataSwfAttributes.height		= "100px";
					this._loadingDataSwfAttributes.pluginspage	= "http://www.macromedia.com/go/getflashplayer";
					
					this._loadingDataSwfParameters						= {};
					this._loadingDataSwfParameters.align				= "top";
					this._loadingDataSwfParameters.allowfullscreen		= true;
					this._loadingDataSwfParameters.allowscriptaccess	= "sameDomain";
					this._loadingDataSwfParameters.base					= "../../";
					this._loadingDataSwfParameters.bgcolor				= "#0000ff";
					this._loadingDataSwfParameters.devicefont			= false;
					this._loadingDataSwfParameters.loop					= false;
					this._loadingDataSwfParameters.menu					= true;
					this._loadingDataSwfParameters.play					= true;
					this._loadingDataSwfParameters.quality				= "best";
					this._loadingDataSwfParameters.salign				= "tl";
					this._loadingDataSwfParameters.scale				= "noscale";
					this._loadingDataSwfParameters.wmode				= "transparent";
					
					
					var deferSwfSetup = (deferSwfCreation) ? true : false;
					
					// Call superclass constructor
					this._super(classInstanceID, replacementTargetParentID, replacementTargetID, swfInstanceID, targetFlashVersion, swfAttributes, swfParameters, this._swfFlashVars, swfData, deferSwfSetup);
				},
			
			
			
			/*
			 =============================
			 INTERNAL RUN-TIME PROPERTIES
			 =============================
			 */
				_displayManager				: undefined,
				
				_loadingDataClass			: undefined,
				_loadingDataFlashReplaceID	: undefined,
				_loadingDataSwf				: undefined,
				_loadingDataSwfID			: undefined,
				_loadingDataSwfAttributes	: undefined,
				_loadingDataSwfParameters	: undefined,
			
			
			
			/*
			 =============================
			 PUBLIC METHODS
			 =============================
			 */
				setSwfObjectReady : function (b, swfObjectID)
				{
					//alert("setSwfObjectReady : swfObjectID = " + swfObjectID);
					this._super(b, swfObjectID);
					if (b && this.isSwfObjectInstanceDefined()) this.getSwfObject().triggerDataLoad();
				},
				
				
				// Create swf object if not already created
				createDisplay : function ()
				{
					if (!this.isSwfObjectInstanceDefined())
					{
						this._setupSwfObject();
					}
				},
				
			
				
				
				
				
				/**
				 * Broadcasts update from swf instance upto Global JS displayManager 
				 *
				 * PLEASE NOTE : At this time only itemSelection events are broadcast by flash instances, therefore no need to have outbound event type filtering (e.g. to filter itemSelection events from valueSelection events). 
				 *
				 *	- Pass update event up to Global JS displayManager 'mrm.runtime.global.displayManager'
				 *	- Global JS displayManager will process as required and pass event object to all associated flash host JS class instances which in turn pass through to their swf instances
				 *
				 * param	e	AS 'ItemSelectionEvent'. Should be treatable as simple object (Recast when passed back in)
				 */
					
					broadcastItemSelection : function (e)
					{
						this._displayManager.updateSelection(e);
					},
					
				
				
								//////////
								//////////
								//////////
								//////////
								//////////
								
								testValueSelectionUpdate : function ()
								{
									alert("testValueSelectionUpdate :: RangeDataDisplay");
									this._displayManager.testValueSelectionUpdate();
								},
								
								testSelectionSummaryUpdate : function ()
								{
									alert("testValueSelectionUpdate :: testSelectionSummaryUpdate");
									this._displayManager.testSelectionSummaryUpdate();
								},
								
								//////////
								//////////
								//////////
								//////////
								//////////
				
				
				
				
				/*
				 =============================
				 UPDATE METHODS
				 
				 - These are called by the Global JS displayManager
				 =============================
				 */
				 
					handleDataUpdate : function (e)
					{
						
					},
				 
					handleItemSelectionUpdate : function (e)
					{
						// [ -FF fix (start)- ] Update internal data to reflect update
						var scope	= e[this.UPDATE_EVENT_DATA_LABEL_SCOPE];
						var select	= e[this.UPDATE_EVENT_DATA_LABEL_SELECT];
						
						if (scope == this.DATA_VALUE_SCOPE_LEVEL_1)			this._updateAllDataItems(select);
						else if (scope == this.DATA_VALUE_SCOPE_LEVEL_2)	this._updateSingleDataItem(e[this.UPDATE_EVENT_DATA_LABEL_LEVEL2ID], select);
						// [ -FF fix (end)- ]
						
						// Update swf object (if ready)
						if (this.getSwfObjectReady()) this.getSwfObject().handleRemoteItemSelection(e);
					},
				
					
					
					handleValueSelectionUpdate : function (e)
					{
						// [ -FF fix (start)- ] Update internal data to reflect update
						this._updateSelectedData(e);
						// [ -FF fix (end)- ]
						
						// Update swf object (if ready [ -FF fix- ])
						if (this.getSwfObjectReady()) this.getSwfObject().handleRemoteValueSelection(e);
					},
					
					
					
					handleSelectionSummaryUpdate : function (e)
					{
						// [ -FF fix (start)- ] Update internal data to reflect update
						this._updateSelectionSummaryData(e);
						// [ -FF fix (end)- ]
						
						// Update swf object (if ready [ -FF fix- ])
						if (this.getSwfObjectReady()) this.getSwfObject().handleRemoteSelectionSummaryUpdate(e);
					},
					
					
					
					enterLoadingState : function ()
					{
					  this._removeSwfObject();
					  this._displayLoadingState();
					  //this.setupRecreate();
					},
					
					
					clearLoadingState : function ()
					{
						// Remove loading state
						this._removeLoadingState();
					},
					
					
					handleDisplayDataUpdate : function (data)
					{
						// If data category matches instance category then update
						if (data[this.DATA_LABEL_CATEGORY] == this._getDataCategory())
						{
							// Save new data
							this._swfData = data;
							
							// Remove loading state [ TODO :: Call remotely from displayManager before html destruction ]
							this.clearLoadingState();
							
							// Call remove again to ensure that the object was sucessfully removed (in certain instances not removed as timing means that not added to DOM B4 remove called)
							this._removeSwfObject();
							
							// Regenerate display
							this._setupSwfObject();
						}
					},
					
					
					
					/////////
					/////////
					/////////
						/*
						setupRecreate : function ()
						{
							setTimeout(mrm.runtime.advancedSettings.dataDisplayPriceRange.testMethod, 2000);
						},
						
						testMethod : function ()
						{
							mrm.runtime.advancedSettings.dataDisplayPriceRange._generateSwfObject();
						}
						*/
					/////////
					/////////
					/////////
			
			
			
			
			/*
			 =============================
			 INTERNAL METHODS
			 =============================
			 */
				/*
				 =============================
				 UTILITY METHODS
				 =============================
				 */
					/*
					 ***********
					 * DATA RETRIVAL
					 ***********
					 */
						/**
						 * [ -FF fix (method)- ]
						 */
						_getLevel1Id : function ()
						{
							var d = this._swfData[this.DATA_LABEL_DATA_SUMMARY];
							if (d) return d[this.DATA_LABEL_ID];
						},
						
						
						/**
						 * [ -FF fix (method)- ]
						 */
						_getDataCategory : function ()
						{
							return this._swfData[this.DATA_LABEL_CATEGORY];
						},
					
					
						/**
						 * [ -FF fix (method)- ]
						 */
						_getSelectionSummaryData : function ()
						{
							return this._swfData[this.DATA_LABEL_SELECTION_SUMMARY];
						},
						
					
					
					/*
					 ***********
					 * DATA MANIPULATION
					 ***********
					 */
						/**
						 * [ -FF fix (method)- ]
						 *
						 * Update single, targeted data item to selected state
						 * 
						 * @param	id		Target item ID
						 * @param	select	Desired selected state
						 */
						_updateSingleDataItem : function (id, select)
						{
							var a = this._swfData[this.DATA_LABEL_DATA];
							var i;
							
							for (i = 0; i < a.length; i++)
							{
								var item = a[i];
								if (item[this.DATA_LABEL_ID] == id) item[this.DATA_LABEL_SELECTED] = select;
							}
						},
						
						
						/**
						 * [ -FF fix (method)- ]
						 *
						 * Update all data items to selected state
						 * 
						 * @param	select	Desired selected state to apply to all data items
						 */
						_updateAllDataItems : function (select)
						{
							var a = this._swfData[this.DATA_LABEL_DATA];
							
							for (var i = 0; i < a.length; a++) { a[i][this.DATA_LABEL_SELECTED] = select; }
						},
					
					
						/**
						 * [ -FF fix (method)- ]
						 *
						 * Update data to store updated current selection values
						 * 
						 * @param	e	testValueSelectionUpdate Object
						 */
						_updateSelectedData : function (e)
						{
							var d = this._getSelectionSummaryData();
							
							//alert("_updateSelectedData : \n e[this.UPDATE_EVENT_DATA_LABEL_VALUE_MAX] = " + e[this.UPDATE_EVENT_DATA_LABEL_VALUE_MAX] + "\n e[this.UPDATE_EVENT_DATA_LABEL_VALUE_MIN] = " + e[this.UPDATE_EVENT_DATA_LABEL_VALUE_MIN]);
							
							if (d && (e[this.UPDATE_EVENT_DATA_LABEL_CATEGORY] == this._getDataCategory()))
							{
								d[this.DATA_LABEL_SELECTED_MAX] = e[this.UPDATE_EVENT_DATA_LABEL_VALUE_MAX];
								d[this.DATA_LABEL_SELECTED_MIN] = e[this.UPDATE_EVENT_DATA_LABEL_VALUE_MIN];
							}
						},
				
						
						/**
						 * [ -FF fix (method)- ]
						 *
						 * Update data to store updated selection summary data
						 * 
						 * @param	e	testValueSelectionUpdate Object
						 */
						_updateSelectionSummaryData : function (e)
						{
							var d = this._getSelectionSummaryData();
							
							if (d && (e[this.UPDATE_EVENT_DATA_LABEL_CATEGORY] == this._getDataCategory()))
							{
								d[this.DATA_LABEL_SELECTABLE_MIN] = e[this.UPDATE_EVENT_DATA_LABEL_SELECTABLE_MIN];
								d[this.DATA_LABEL_SELECTABLE_MAX] = e[this.UPDATE_EVENT_DATA_LABEL_SELECTABLE_MAX];
								
								d[this.DATA_LABEL_SELECTED_MIN] = e[this.UPDATE_EVENT_DATA_LABEL_SELECTED_MIN];
								d[this.DATA_LABEL_SELECTED_MAX] = e[this.UPDATE_EVENT_DATA_LABEL_SELECTED_MAX];
							}
						},
						
						
				
				/*
				 =============================
				 DISPLAY CONTROL / UPDATE
				 =============================
				 */
					_displayLoadingState : function ()
					{
						// Create swf container with internal replaceID'd element
						var parent = "#" + this._replacementTargetParentID;
						var target = parent + " #" + this._replacementTargetID;
						
						var replaceContent = "<div id='" + this._loadingDataFlashReplaceID + "'></div>";
						
						if ($(target).length == 0) $(parent).append("<div id='" + this._replacementTargetID + "' class='" + this.CLASS_LOADING_ICON_CONTAINER + "'>" + replaceContent + "</div>");
						else $(target).append(replaceContent);
						
						// Add loading class
						$(parent).addClass(this._loadingDataClass);
						
						// Add flash object
						this._loadingDataSwf = swfobject.createSWF(this._loadingDataSwfAttributes, this._loadingDataSwfParameters, this._loadingDataFlashReplaceID);
					},
					
					
					
					_removeLoadingState : function ()
					{
						// Remove loading class
						var parent = "#" + this._replacementTargetParentID;
						$(parent).removeClass(this._loadingDataClass);
						
						// Remove flash object
						swfobject.removeSWF(this._loadingDataSwfID);
						this._loadingDataSwf = null;
					}
			
			
			
			/*
			 =============================
			 INTERNAL EVENT HANDLERS
			 =============================
			 */
			
				
		}
	);
	
	//alert("mrm.common.display.RangeDataDisplay ::  Class definition successful");
