/**
 * @filename ActivityTypeFormField.js
 * 
 * @description Class to get activity type drop down 
 * 
 * @author Devon Lindsey
 * @email devon.lindsey@gmail.com
 *
 * Copyright(c) 2008, Garmin International
 */
Ext.namespace("Garmin", "Garmin.form");
Garmin.form.ActivityTypeFormField = {
	/**
	 * 
	 * @param {Object} Ext.form.ComboBox config
	 * 
	 * @return {Ext.form.ComboBox}
	 */
	getActivityTypesCombo: function(config, includeAll, includeUncategorized, json) {
		var store = this._getActivityTypesStore(json);
		var combo = new Ext.form.ComboBox(Ext.apply(config,Ext.apply(ActivityFormFieldsConstants.COMBO_BOX_DEFAULT_CONFIG_OPTIONS, {
				name: 'activityTypeField',
		        store: store,
				mode: 'local',
		        displayField:'defaultDisplay',
				valueField:'key'
			}))
		);
		return combo;
	},

	/**
	 * 
	 * @return {Ext.data.Store}
	 */
	_getActivityTypesStore : function(json) {
		//This value is populated via a call to the activityTypes tag in the usertag lib.  
		//Store it as a var called ACTIVITY_TYPES_JSON
		var activityTypeData = json;

		var store = new Ext.data.JsonStore({
			data: activityTypeData,
		    root: 'dictionary',
			id: 'key',
			fields: [
	            {name: 'key'},
	            {name: 'defaultDisplay', 
	             convert: function(v){
	             	return unescape(v);
	             },
	             mapping: 'display'},
	            {name: 'isDefault'}
	        ]
		});
		store.on('load', function(s, r) {
   			Ext.each(r, function(rec) {
	        	rec.data.title = unescape(rec.data.title);
	    	});
		});
		
		return store;
	},
	
	/**
	 * Sets the value that the user sees to be a value stripped of all the special 
	 * characters.  We have to do this because ext is stupid and even though it's
	 * list decodes special characters, the field element that displays the value
	 * selected after it's selected encodes the special characters, which we don't want.
	 * This function returns the key of the value the user chooses and stores it
	 * as a var so that we can use it throughout the form submit.
	 * 
	 * @param combo the combo box selected
	 * @param record the selected value
	 * @param index the index of the selected value
	 */
	setSubmittableFormElement : function(combo, record, index) {
		var activityTypeSelectedValue = combo.value;
		var value = combo.lastSelectionText.replace(/&#160;/g, "");
		combo.setValue(value);
		return activityTypeSelectedValue;
	},
	
	/**
	 * Makes a copy of the JSON dictionary, replaces all 
	 * leading spaces with the empty string, and returns the copy
	 * 
	 * @param jsonDictionary dictionary to copy
	 */
	removeLeadingSpaces : function(jsonDictionary) {
		var newJsonDictionary = Object.clone(jsonDictionary);
		var dict = newJsonDictionary.dictionary;
		for (var item in dict) {
			if (typeof dict[item] != 'function')
			    dict[item].display = dict[item].display.replace(/&#160;/g, "");
		}
		
		return newJsonDictionary;
		
	}
};