﻿
/// <reference path="../../../../../jQuery/1.3.2/jquery-1.3.2-vsdoc.js" />


/*
 *****************************************************************
 * QuickSearch
 *****************************************************************
 *
 * Provides functionality to the quickserach control, including the hover state changes, and click propogation
 *
 * Author : Rob Earlam
 */

// Check that namespace into which the Class definition will be creates has been defined & if not then create
if (!mrm.global.isNamespaceDefined("mrm.lexus.display.controls")) mrm.global.createNamespace("mrm.lexus.display.controls", "1.0");


mrm.lexus.display.controls.QuickSearch = Object.subClass(
	{
		/*
		 =============================
		 CONSTANTS
		 =============================
		 */
			CONST_CLASS_NAME_UI_DROP_DOWN : "ui-drop-down-menu",
			CONST_CLASS_NAME_UI_DROP_DOWN_OPEN : "ui-drop-down-menu-open",
	
	
		/*
		 =============================
		 CONSTRUCTOR
		 =============================
		 */
			init : function ()
			{
				//scope pointer
				var _this = this;
				
				//setup the hover functionality
				_this._configureHoverFunctionality();
				
				//setup the click functionality
				_this._configureClickFunctionality();
			},
		
		
		
		/*
		 =============================
		 METHODS USED TO HANDLE THE PROPAGATION OF THE CHECKBOX CLICKS
		 =============================
		 */
			//bind the click events
			_configureClickFunctionality : function ()
			{
				//get the local vars
				var _this = this;
				var container = $('div.quick-search-models');		
				
				//bind the click event to all checkbox change events
				container.find('input.check').click(function(event) {
					event.stopPropagation();
					_this._checkbox_OnClick($(this));
				});
				
				//bind the click event to the checkbox container div's but stop the propogation to stop the event being called twice
				//if the user clicks directly on the checkbox
				container.find('div.active-checkbox-container').click(function(event) {
					event.stopPropagation();
					var checkbox = $(this).find('input:first');
					if (checkbox.attr('checked')) {
						checkbox.removeAttr('checked');
					}
					else {
						checkbox.attr('checked', 'true');
					}
					_this._checkbox_OnClick(checkbox);
				});
				
				// Find all 'a.explore-model-range' links and swallow click event 
				// otherwise when clicked the event propogates up the DOM and triggers '_checkbox_OnClick' on the range level checkbox as fires top-level parent 'div.active-checkbox-container' click event
				container.find("a.explore-model-range").click(function(event)
				{
					event.stopPropagation();
				});
				
			},	
			
			
			
			//checkbox click event handler
			_checkbox_OnClick : function (checkbox)
			{
				//get the checkboxes container
				var container = checkbox.closest('div.active-checkbox-container');
				var selected = checkbox.attr('checked');	//indicates whether we're selecting or deselecting
			
				//if this is a model check box set the selected status of all child checkboxes
				
				if(checkbox.hasClass('derivative'))
				{ 
					var parentModelId = '#' + checkbox.attr('name').replace('cb-is-f-', 'cb-2-cur-');				
					var parentmodelCheckbox = container.parent().find(parentModelId);
					if(container.find('input.derivative:checked').length > 0)
						parentmodelCheckbox.attr('checked', 'true');
					else
						parentmodelCheckbox.removeAttr('checked');	
				}
				else
				{
					container.find('input').each(function() {
						if(selected)
							container.find('input:not(:checked)').attr('checked', 'true');
						else
							container.find('input:checked').removeAttr('checked');
					});		
				}

				//if this is a model selection then we need to update to range level
				container.parents('div.active-checkbox-container').each(function() {
					//get the range checkbox and range container
					var rangeContainer = $(this);
					var rangeCheckbox = rangeContainer.find('input.derivative');
					var modelCheckboxContainer = rangeContainer.find('div.ui-treelist-level-2-container');
					var derivativeCheckboxContainer = modelCheckboxContainer.find('div.derivatives-container')
					var allCheckbox = modelCheckboxContainer.find('input.check-all');
										
					//handle the selection of the all checkbox
					if(checkbox.val() == 'all') {
						if (selected)
							modelCheckboxContainer.find('input:not(:checked)').attr('checked', 'true');
						else
							modelCheckboxContainer.find('input:checked').removeAttr('checked');
					}
					else {
						//now if we have selected all manually then check the 'all models' checkbox
						if(modelCheckboxContainer.find('input:not(:checked)').length == 1)				
							allCheckbox.attr('checked', 'true');
						else
							allCheckbox.removeAttr('checked');
					}
					
					var derivativeParentInput = derivativeCheckboxContainer.parent('div.active-checkbox-container');
					if(derivativeCheckboxContainer.find('input:checked').length > 0)
						derivativeParentInput.find('input.model').attr('checked', 'true');
					else
						derivativeParentInput.find('input.model').removeAttr('checked');
						
					
					//if we have any children checked then this must be checked
					if (modelCheckboxContainer.find('input:checked').length > 0)
						rangeCheckbox.attr('checked', 'true');
					else
						rangeCheckbox.removeAttr('checked');
				});
				

				//update the 'Please Select', 'Selected Models' label
				var selectedModelCount = $("div.quick-search-models > div.ui-treelist-container > div.ui-treelist-level-1-container > div.level-1-panel-body > div.model-item > div.model-item-body > input.check:checked").length;
				var defaultLabel = $('h6.default-text');
				if (selectedModelCount > 0) {
					defaultLabel.html(selectedModelCount + ' selected');
				}
				else {
					defaultLabel.html('Please select');
				}

				//update the selected derivativeIds
				var globalContainer = $('div.summary-quick-search, div.panel-quick-search');
				var selectedDerivativesInput = globalContainer.find('input.selected-models');
				var selectedDerivatives = '';
				globalContainer.find('input.derivative:checked').each(function() {
					selectedDerivatives = selectedDerivatives + $(this).val() + ',';
				});							
				selectedDerivativesInput.val(selectedDerivatives);								
			},			
		
		
		
		
		
		/*
		 =============================
		 METHODS USED TO GIVE THE HOVER FUNCTIONALITY
		 =============================
		 */
			_configureHoverFunctionality : function ()
			{
				var _this = this;
				
				$("div.ui-drop-down-menu > div.quick-search-summary").click(
					function(event) {
						_this._toggleHoverState($(this).parent("div.ui-drop-down-menu"));
					}
				);
					
				$("div.quick-search").hover(
					function() { },
					function() {
						_this._closeHoverState($(this));
					}
				);		
			},
			
			
			_toggleHoverState : function (container) 
			{
				var _this = this;
				if (container.hasClass(_this.CONST_CLASS_NAME_UI_DROP_DOWN_OPEN)) {
					container.removeClass(_this.CONST_CLASS_NAME_UI_DROP_DOWN_OPEN);
				}
				else {
					container.addClass(_this.CONST_CLASS_NAME_UI_DROP_DOWN_OPEN)
				}		
			},


			_closeHoverState: function(container)
			{
				var _this = this;
				container.removeClass(_this.CONST_CLASS_NAME_UI_DROP_DOWN_OPEN);
			}
	}
);
