/**
* @fileoverview This file contains the static object  EditPredefinedList.
* @author Sara Lin
* @version 0.2 (updated by Nick Cappadona)
*/

/**
* EditPredefinedList handles predefined list editing functionalities in the admin tab.
* @namespace
*/
AdminTab.editPredefinedList = {
	list: null,
	/** @type DOM element  */
	errorMsg: null,
	/** @type DOM element  */
	optionDesc: null,
	/** @type DOM element  */
	optionValue: null,
	/** @type object holds the input description for the predefined value for URL link*/
	defaultValue: {},
	/** @type object hodes the input description for the predefined value for URL title*/
	defaultDesc: {},
	
	/**
	* initialize object properties; retrives the edit predefined view file and attaches event handlers.
	*/
	load: function() {
		if ($('listEditPage')==undefined) {
			thisObj = this;
			new Ajax.Request(base_url+'admin/getPage/list', {
				evalScripts: true,
				onSuccess: function(response) {
					$('adminContent').innerHTML = response.responseText;
					Event.observe('predefinedLists', 'change', thisObj.listOnChange);
					Event.observe('listEditPage', 'submit', thisObj.addOption);
					thisObj.errorMsg = $($$('#listEditPage .errorMsg')[0]);
					thisObj.optionDesc = $('optionDesc');
					thisObj.optionValue = $('optionValue');
				}
			});
			
			this.defaultValue.value = "";
			this.defaultDesc.value = "";
		}
	},
	
	/**
	* invoked when the administrator selects a predefined list; refreshes the current values field for the new predefined list.
	* @event
	*/
	listOnChange: function(event) {
		var list = Event.element(event).value;
		AdminTab.editPredefinedList.errorMsg.hide();
		if(list!=""){
			AdminTab.editPredefinedList.refreshList(list);
		} else {
			$('optionContents').innerHTML = "";
		}
	},
	
	/**
	* refreshes the current values field to display values for the selected predefined list.  
	* If the new list URL, change the add new value field to display two text boxes for URL link and title with descriptions.
	* @param {String} list
	*/
	refreshList: function(list) {
		$$('#optionContents a').each(function(element){
			Event.stopObserving(element, 'click', this.deleteOption);
		});
		
		var url = encodeURL(base_url+'admin/printOptionValues', list);
		new Ajax.Request(url, {
			onSuccess: function(response){
				$('optionContents').innerHTML = response.responseText;
				$$('#optionContents a.delete').each(function(element){
					Event.observe(element, 'click', AdminTab.editPredefinedList.deleteOption);
				}); 
				$('optionValue').value="";

				if (list=="URL") {
					$('optionDescDiv').show();
					AdminTab.editPredefinedList.defaultDesc = DefaultInput.setDefaultInput("enter URL link here", AdminTab.editPredefinedList.optionDesc);
					AdminTab.editPredefinedList.defaultValue = DefaultInput.setDefaultInput("enter URL title here", AdminTab.editPredefinedList.optionValue);
				} else {
					$('optionDescDiv').hide();
					DefaultInput.unsetDefaultInput(AdminTab.editPredefinedList.defaultValue);
				}
			}
		});
	},
	
	/**
	* if the value inside the add new value field is not empty or equal to the default value, add the new value to the predefined list; 
	* refreshes the current values to reflect on the change.
	* @event
	*/
	addOption: function(event) {
		var list = Event.element(event).serialize(true).predefinedLists;
		
		if($('optionValue').value!=AdminTab.editPredefinedList.defaultValue.value){
			var formValues = Event.element(event).serialize(true);
			
			//encode URL
			var url = base_url+'admin/insertIntoList';
			//replace all forward slashes (/) with _-_ to avoid breaking codeigniter handling parameters in URL
			var optionDesc = formValues.optionDesc.replace(/\//g, "_-_");
			var queryArray = new Array(list,formValues.optionValue, optionDesc);
			url = encodeURL(url, queryArray);
			
			new Ajax.Request(url, {
				onSuccess: function(response) {
					AdminTab.editPredefinedList.refreshList(list);
				}
			});
		}
		else {
			AdminTab.editPredefinedList.errorMsg.show();
		}
	},
	
	/**
	* deletes the selected current value for the predefined list; refreshes the current values field to show the changes.
	* @event
	*/
	deleteOption: function(event) {
		var list = Event.element(event).classNames().entries()[0];
		var valueToDelete = Event.element(event).id;
		var a = confirm("Items that are associated with this value will no longer be associated. \n"+
						"Continue?");
						
		if (a==true){
			//encode URL
			var url = base_url+'admin/deleteItemFromList';
			var queryArray = new Array(list,valueToDelete);
			url = encodeURL(url, queryArray);
			
			new Ajax.Request(url, {
				onSuccess: function(response){
					AdminTab.editPredefinedList.refreshList(list);
				}
			});
		}
	}
}