/*

ViewFromFields Widget

This custom YUI widget is used to switch the display of form segments between Edit, Empty and View modes.

This behavior does not affect the existing backend validation.
It has been built to provide a clear pattern to save time while giving enough control for the particularities of any form.

Refer to the provided sample for usage examples.

Features:
	- Form block can switch between 3 modes: view, edit, empty
	- Binds Edit and Delete buttons automatically by class association
	- Custom function call to test if block contains error (to be put in edit mode automatically)
	- Custom function to populate the "view" mode from form data
	- Custom function to reset the fields of the edit mode before put in empty mode
	- Custom function to qualify if the block is deemed empty

*/


YAHOO.namespace('NURUN');

(function(){

    YAHOO.NURUN.ViewFormFields = function (el, userConfig) {
        YAHOO.NURUN.ViewFormFields.superclass.constructor.call(this, el, userConfig);
    };

    var Dom = YAHOO.util.Dom,
		Event = YAHOO.util.Event,
		Module = YAHOO.widget.Module,
		Panel = YAHOO.widget.Panel,
        CustomEvent = YAHOO.util.CustomEvent,
		ViewFormFields = YAHOO.NURUN.ViewFormFields,
        /**
        * Constant representing the SimpleDialog's configuration properties
        * @property DEFAULT_CONFIG
        * @private
        * @final
        * @type Object
        */
        DEFAULT_CONFIG = {
            "EDITBUTTONCLASS": { 
                key: "editButtonClass", 
                value: "openEditMode"
            },
            "DELETEBUTTONCLASS": { 
                key: "deleteButtonClass", 
                value: "emptyAll"
            },
            "TESTHASERROR" : {
                key: "testHasError", 
                value: function(){return false;}
            },
            "TESTISEMPTY" : {
                key: "testIsEmpty", 
                value: function(){return false;}
            },
            "ONVIEWMODE" : {
                key: "onViewMode", 
                value: function(){return false;}
            },
            "ONEMPTYMODE" : {
                key: "onEmptyMode", 
                value: function(){return false;}
            }
        };	

    YAHOO.extend(ViewFormFields, Module, {
    	"CSS_VIEWFORMFIELD": "VIEWFORMFIELD_",
    	init: function (el, userConfig) {
            /*
                Note that we don't pass the user config in here yet because we 
                only want it executed once, at the lowest subclass level
            */
            ViewFormFields.superclass.init.call(this, el/*, userConfig*/);
            this.beforeInitEvent.fire(ViewFormFields);
	        Dom.addClass(this.element, ViewFormFields.CSS_VIEWFORMFIELD);
            if (userConfig) {
                this.cfg.applyConfig(userConfig, true);
            }
            this.beforeRenderEvent.subscribe(function () {
            	/*
                if (! this.body) {
                    this.setBody("");
                }
                */
            }, this, true);
            this.initEvent.fire(ViewFormFields);


			this.el = el;

			// Catch the "onViewMode"  handler function
			if (this.cfg.getProperty("onViewMode")) this.onViewMode = this.cfg.getProperty("onViewMode")
			else this.onViewMode = function(){};

			// Catch the "onEmptyMode"  handler function
			if (this.cfg.getProperty("onEmptyMode")) this.onEmptyMode = this.cfg.getProperty("onEmptyMode")
			else this.onEmptyMode = function(){};

            var emptyModeElem = YAHOO.util.Dom.getElementsByClassName("emptyMode", null, el)[0];
			if (emptyModeElem) this.emptyModeModule = new Module(emptyModeElem); 

			var viewModeElem = YAHOO.util.Dom.getElementsByClassName("viewMode", null, el)[0];
			if (viewModeElem) this.viewModeModule = new Module(viewModeElem); 

            var editModeElem = YAHOO.util.Dom.getElementsByClassName("editMode", null, el)[0];
			if (editModeElem) this.editModeModule = new Module(editModeElem); 

			this.switchToEditMode = function() {
				if (this.emptyModeModule) this.emptyModeModule.hide();
				if (this.viewModeModule) this.viewModeModule.hide();
				if (this.editModeModule) this.editModeModule.show();
			};
			this.switchToEmptyMode = function() {
				if (this.emptyModeModule) this.emptyModeModule.show();
				if (this.viewModeModule) this.viewModeModule.hide();
				if (this.editModeModule) this.editModeModule.hide();
				this.onEmptyMode(this, this.el);
			};
			this.switchToViewMode = function() {
				if (this.emptyModeModule) this.emptyModeModule.hide();
				if (this.viewModeModule) this.viewModeModule.show();
				if (this.editModeModule) this.editModeModule.hide();
				this.onViewMode(this, this.el);
			};

			// Finds and binds the edit button
			var editButton = YAHOO.util.Dom.getElementsByClassName(this.cfg.getProperty("editButtonClass"), null, el);
			if (editButton.length > 0) {
				Event.addListener(editButton, "click", function(e, obj) {
					Event.preventDefault(e);
					obj.switchToEditMode();
				}, this); 
			};

			// Finds and binds the delete button
			var deleteButton = YAHOO.util.Dom.getElementsByClassName(this.cfg.getProperty("deleteButtonClass"), null, el);
			if (deleteButton.length > 0) {
				Event.addListener(deleteButton, "click", function(e, obj) {
					Event.preventDefault(e);
					obj.switchToEmptyMode();
				}, this);
			};

			// Test if the fields contains errors and need to be shown in edit mode right away
			this.testHasError = this.cfg.getProperty("testHasError")
		
			if ((this.testHasError && this.testHasError(el))|| YAHOO.util.Dom.getElementsByClassName("card-security-code","input",el).length > 0 ) {
				this.switchToEditMode();
			    return;
			};

			this.testIsEmpty = this.cfg.getProperty("testIsEmpty")
		    
		    if (this.testIsEmpty) if (this.testIsEmpty(el)) {
			    this.switchToEmptyMode();
			    return;
			};

		    this.switchToViewMode();
        },
        initEvents: function () {

            ViewFormFields.superclass.initEvents.call(this);

            var SIGNATURE = CustomEvent.LIST;

            /**
            * CustomEvent fired prior to class initalization.
            * @event beforeInitEvent
            * @param {class} classRef class reference of the initializing 
            * class, such as this.beforeInitEvent.fire(Module)
            */
            /*
            this.onEditMode = this.createEvent("onEditMode");
            this.onEditMode.signature = SIGNATURE;

            this.onViewMode = this.createEvent("onViewMode");
            this.onViewMode.signature = SIGNATURE;
            */
        },
        initDefaultConfig: function () {
            // Add properties //
            ViewFormFields.superclass.initDefaultConfig.call(this);

            this.cfg.addProperty(DEFAULT_CONFIG.EDITBUTTONCLASS.key, {
                handler: this.configEditButtonClass,
                value: DEFAULT_CONFIG.EDITBUTTONCLASS.value
            });

            this.cfg.addProperty(DEFAULT_CONFIG.DELETEBUTTONCLASS.key, {
                handler: this.configDeleteButtonClass, 
                value: DEFAULT_CONFIG.DELETEBUTTONCLASS.value
            });

            this.cfg.addProperty(DEFAULT_CONFIG.TESTHASERROR.key, {
                //handler: this.configTestHasError, 
                value: DEFAULT_CONFIG.TESTHASERROR.value
            });

            this.cfg.addProperty(DEFAULT_CONFIG.TESTISEMPTY.key, {
                //handler: this.configTestIsEmpty, 
                value: DEFAULT_CONFIG.TESTISEMPTY.value
            });

            this.cfg.addProperty(DEFAULT_CONFIG.ONVIEWMODE.key, {
                value: DEFAULT_CONFIG.ONVIEWMODE.value
            });

            this.cfg.addProperty(DEFAULT_CONFIG.ONEMPTYMODE.key, {
                value: DEFAULT_CONFIG.ONEMPTYMODE.value
            });

		},
		configEditButtonClass: function (type,args,obj) {
            var text = args[0];
            if (text) {
                this.editButtonClass = text;
            }
        },
		configDeleteButtonClass: function (type,args,obj) {
            var text = args[0];
            if (text) {
                this.deleteButtonClass = text;
            }
        }
        
    });

})();

// It is important that loaded objects register themselves so the YUI Loader, and the YUI library as a whole,
// knows what it has loaded.  
// If this declaration is missing, the Loader would wait indefinitely for it to finish loading and initializing
// (well, at least that is what it would assume it is doing)
// It is important that this line be the last one.
YAHOO.register('NURUN.ViewFormFields', YAHOO.NURUN.ViewFormFields, {version: "0.1", build: '1'});

