/// <reference path="C:\References\jquery-1.4.1-vsdoc.js" />

// ********************************************************************
// 
// Manheim Retail Marketing
// ********************************************************************
// Copyright © 2010 Manheim Retail Marketing. 
//
//  Summary
// ******************************
// $Workfile: BaseControl.js $
// Notes:		Provides base control functionality, like binding the control to the html.
//
// File History Information
// ************************
// Created on:					    
// Last Modified:					$Modtime: 23/07/10 8:40 $
// Original Author:					Marc Lancashire
// Last Modified by:				$Author: Lancashirem $
// Last JS Lint:					07/12/2009 11:40
//
// Source Control Information
// **************************
// File Version:					$Revision: 17 $
// VSS Location:					$Archive: /Manheim.Portfolio/Manheim.Portfolio.Web.UI.Client.Assets/assets/js/mrm/lib/ui/controls/1.0/BaseControl.js $
//
// ********************************************************************

// Check / create namespace
if (!mrm.global.isNamespaceDefined("mrm.lib.ui.controls.basecontrol"))
{
	mrm.global.createNamespace("mrm.lib.ui.controls.basecontrol", "1.0");
}
if(!mrm.global.isNamespaceDefined("mrm.global.Utilities"))
{
	mrm.global.createNamespace("mrm.global.Utilities", "1.0") 
}


// Class definition
mrm.lib.ui.controls.BaseControl = Object.subClass
(
{
	
	/*
	=============================
	CONSTANTS
	=============================
	*/
	"TRACE"					: false, // this can be overidden on a control by control basis to refine the tracing output
	"TRACE_PREFIX"			: "mrm.lib.ui.controls.basecontrol",	// this can be overidden on a control by control basis to set the trace prefix
	"_tracing"				: {}, // internal varible that hold the tracing utility
	
	"SELECTOR_CONTROL"		: null, // please override, this is used to filter the bindable controls
	"BINDER_NAME"			: null, // please override, this is used to identify the binder and is u
	
	"MRM_RUN_ALL"			: "MRM-RUN-CONTROL-BINDINGS",
	"MRM_RUN_ONE"			: "MRM-RUN-CONTROL-BINDING",
	
	/*
	=============================
	CONSTRUCTOR
	=============================
	*/
	
	/* construct new instance of this control.
	   'tracing' is the provided tracing util.
	   'settings' are runtime overides of any properties on the instance of this control.
	 */
	"init" : function (tracing, settings)
	{
		this._tracing = tracing;
		
		if (this._tracing)
		{
			// skip
		}
		else if(mrm.global.utilities.tracing)
		{
			this._tracing = mrm.global.utilities.tracing;
		}
		else if (mrm.global.isClassDefined("mrm.lib.utilities.Tracing"))
		{
			this._tracing = new mrm.lib.utilities.Tracing();
		}
		
		// check if we have settings to overide our control builder at instance creation
		if (settings)
		{
			// use provided settings object to set any matching properties on the control binder
			for (property in settings)
			{
				if (property in this)
				{
					// change our instance property value to the property value on provided settings
					this[property] = settings[property];
				}
			}
		}
	},
	
	/*
	=============================
	PUBLIC MEMBERS
	=============================
	*/
	
	/*	binds any control instances in jqElements.
		'jqElements' are the provided jq elements that we should search in to bind up our ui functionality.
		'instanceId' is a unquie instance id (not currently used).
		'settings' NB these are bindtime only overides of the properties on this control
	*/
	"bindControl" : function (jqElements, instanceId, settings)
	{
		var status = true, jqSelectedElements, defaultSettings = {};
		
		// check if we have any settings to overide
		if (settings)
		{
			// use provided settings object to set any matching properties on the control binder
			for (property in settings)
			{
				if (property in this)
				{
					// store current property setting, ready to restore the value later
					defaultSettings[property] = this[property];
					
					// change our instance property value to the property value on provided settings
					this[property] = settings[property];
				}
			}
		}
		
		// check if we have a selector to filter
		if (this.SELECTOR_CONTROL === null)
		{
			this.tracing().addTrace("SELECTOR_CONTROL is null please provide a selector filter");
			return false;
		}
		
		// get the correct elements for the control
		jqSelectedElements = jqElements.filter(this.SELECTOR_CONTROL);
		
		// if there are none in the selection, then check children
		if (jqSelectedElements.length < 1)
		{
			jqSelectedElements = jqElements.find(this.SELECTOR_CONTROL);
		}
		
		// if there are still no controls to bind then set status
		if (jqSelectedElements.length === 0)
		{
			status = false;
		}
		// otherwise bind each selected element as a control
		else
		{
			for (var i = 0; i < jqSelectedElements.length; i = i + 1)
			{
				if (!this._initControl(jqSelectedElements[i], instanceId, settings) && status)
				{
					status = false;	
				}
			}
		}
		
		// check if we have an settings to restore
		if (settings)
		{
			// use provided settings object to set any matching properties on the control binder
			for (property in defaultSettings)
			{
				if (property in this)
				{
					// restore our instance property value
					this[property] = defaultSettings[property];
				}
			}
		}
		
		return status;
	},
	
	"toString" : function ()
	{
		return this.BINDER_NAME;
	},
	
	// get internal tracing utility
	"tracing" : function ()
	{
		return this._tracing;
	},
	
	/*
	=============================
	PRIVATE MEMBERS
	=============================
	*/
	
	/* inits a control instance for a html element */
	"_initControl" : function (element, instangeId, settings)
	{
		alert("please overide the base initControl on " + this);
	}
	
}
);

