
  /////////////////////////////////////////////////////
  //                                                 //
  //          C-NOOFS Forecast Viewer v0.6           //
  //               Web User Interface                //
  //                                                 //
  /////////////////////////////////////////////////////

UserInputFactory = {}

/**
 * Construct a user input manager instance and return it
 * @param name Name of the input manager
 * @param cfg Configuration Object
 */
UserInputFactory.createManager = function(name, cfg)
{
	if ( !YAHOO.lang.isUndefined(cfg.type) )
	{
		for ( fkey in CFG.UserInputFactory.Types )
		{
			if ( fkey == cfg.type )
			{
				return new UserInputUI[fkey](name, cfg);
			}
		}
	}
	
	return null;
}


/**
 * Container object for the implementations created by the factory
 */
UserInputUI = {}


/**
 * Base class that all implementations inherit from
 * @param name Name of the input manager
 * @param cfg Configuration Object
 */
UserInputUI.Base = function(name, cfg)
{
	this.name = name;
	this.cfg = cfg;
}


/**
 * The name of this manager (ie: it's configuration key)
 * @type String
 */
UserInputUI.Base.prototype.name = "";


/**
 * The configuration object for this manager
 * @type associative array
 */
UserInputUI.Base.prototype.cfg = {};


/**
 * Object for storing the HTMLDivElement container
 * @type HTMLDivElement
 */
UserInputUI.Base.prototype.container = {};


UserInputUI.Base.prototype.getContainer = function()
{
	return this.container;
}



/**
 * Implementation of a calendar-based user input control
 * @param name Name of the input manager
 * @param cfg Configuration Object
 */
UserInputUI.Calendar = function(name, cfg)
{
	UserInputUI.Calendar.superclass.constructor.call(this, name, cfg); 

	this.container = document.createElement("div");
	
		// Create the calendar-launching button
		var image = document.createElement("img");
		image.className="openCalendarButtonImage";
		image.src = "images/cal_icon.jpg";
		image.title = Lang.DateChooser.ImageTitle;
		
	this.container.appendChild(image);
		
		var span = document.createElement("span");
		span.className="openCalendarButtonText";
		span.innerHTML = Lang.DateChooser.OpenCalendarText;
		
	this.container.appendChild(span);
	this.DateChooser = new DateChooser();
	
	var CAL = this;
	var cnt = new YAHOO.util.Element(this.container);
	cnt.on("click", function()
	{
		CAL.DateChooser.Show();
	});

}
YAHOO.lang.extend(UserInputUI.Calendar, UserInputUI.Base); 

/**
 * Stores an instance of the DateChooser UI
 */
UserInputUI.Calendar.prototype.DateChooser = {}

/**
 * Returns an array of dates that have been selected
 * @return Array of selected dates
 */
UserInputUI.Calendar.prototype.getSelected = function()
{
	var SelDates = this.DateChooser.getSelected();
/*	var ret = [];
	for ( key in SelDates )
	{
		var dt = SelDates[key];
		dt.setHours(12);
		ret.push(dt);
	}
	return ret;
*/
	return SelDates;
}

/**
 * Returns an XML fragment containing dates that have been selected
 * @return XML fragment (string)
 */
UserInputUI.Calendar.prototype.getSelectedXML = function()
{
	var ret = "<dates>\n";
	var dates = this.getSelected();
	var date = "";
	
	for ( date in dates )
	{
		ret += "\t<date timestamp=\"" + (dates[date].getTime() / 1000) + "\" />\n";
	}

	return ret + "</dates>\n";
}


/**
 * Set the handler to callback when the user changes an input
 * @param callback Callback function to call on change of input
 */
UserInputUI.Calendar.prototype.setChangeHandler = function(callback)
{
	this.DateChooser.setSaveHandler(callback);
}




/**
 * Implementation of checkbox-based user input control
 * @param name Name of the input manager
 * @param cfg Configuration Object
 */
UserInputUI.CheckboxGroup = function(name, cfg)
{
	UserInputUI.CheckboxGroup.superclass.constructor.call(this, name, cfg); 

	if ( !YAHOO.lang.isUndefined("options") )
	{
		this.BoxGroup = new CheckboxGroup(name,cfg.options,Lang.FieldValues[name]);
		this.container = this.BoxGroup.getContainer();
	}	
}
YAHOO.lang.extend(UserInputUI.CheckboxGroup, UserInputUI.Base); 

/**
 * Stores an instance of the CheckboxGroup object
 */
UserInputUI.CheckboxGroup.prototype.BoxGroup = {}

/**
 * Returns a list of selected checkboxes
 * @return Array
 */
UserInputUI.CheckboxGroup.prototype.getSelected = function()
{
	return this.BoxGroup.getSelected();
}

/**
 * Returns an XML fragment containing the fields that have been selected
 * @return XML fragment (string)
 */
UserInputUI.CheckboxGroup.prototype.getSelectedXML = function()
{
	return "<"  + this.name + " selected=\"" + (this.getSelected()) + "\" />\n";
}

/**
 * Set the handler to callback when the user changes an input
 * @param callback Callback function to call on change of input
 */
UserInputUI.CheckboxGroup.prototype.setChangeHandler = function(callback)
{
	this.BoxGroup.setChangeHandler(callback);
}
