// FVWebComponent Singleton class in global namespace.
FVWebComponent = new Object();

// FVWebComponent debug level.  Default is 0.
FVWebComponent.debugLevel = 0;

// FVWebComponent component collection.
FVWebComponent.collection = new Object();

// FVWebComponent event trigger.  (public method)
FVWebComponent.fireEvent = function (eventName, sourceId, targetId, paramObj) {    
    var eventObj = new FVWebComponentEvent(eventName, sourceId, targetId, paramObj);
    if (FVWebComponent.debugLevel >= 5) {
        alert("FVWebComponent.fireEvent(" + eventObj.toString() + ")");
    }
    FVWebComponent.universalEvent.fire(eventObj);   
}

// Subscribe to FVWebComponent event with a handler and a custom object.  (public method)
// If custom object does not exist, then the subscriber is the custom object.
FVWebComponent.addEventHandler = function (subscriberObj, handlerFunc, customObj) {
    if (customObj == null) {
        customObj = subscriberObj;
    }
    FVWebComponent.universalEvent.subscribe(handlerFunc, customObj);
}

// Actual custom event for the FVWebComponent universal event.  (private variable)
FVWebComponent.universalEvent = new YAHOO.util.CustomEvent("FVWebComponentUniversalEvent");

// Initialize FVWebComponent framework.  (private method)
FVWebComponent.init = function () {
    FVWebComponent.fireEvent("INIT", "FVWebComponent", null, null);
}

// FVWebComponent universal event encapsulation.
function FVWebComponentEvent(eventName, sourceId, targetId, param) {
    this.eventName = eventName;
    this.sourceId = sourceId;
    this.targetId = targetId;
    this.param = param;
}

FVWebComponentEvent.prototype.toString = function () {
    var result;
    result = "[" + this.eventName + ";" + this.sourceId + ";" + this.targetId + ";{";
    
    var key;
    var value;
    var isFirst = true;
    
    for (key in this.param) {
        value = this.param[key];
        if (!isFirst) {
            result += ",";
        } else {
            isFirst = false;
        }
        result += key + "=>" + value;        
    }
    result += "}]";
    
    return result;
}

//
// Component Base... All FV web components extend from this class.
//
FVWebComponent.ComponentBase = function () {
    this.id = null;
    this.config = null;
    this.container = null;
    this.visible = true;
}

FVWebComponent.ComponentBase.prototype.init = function (id, config) {
    this.id = id;
    this.config = config;
    this.container = document.getElementById(this.id);
}

// Hide component.  Default action is to set "visibility" to "hidden"
FVWebComponent.ComponentBase.prototype.hide = function () {
    this.container.style.visibility = "hidden";
    this.visible = false;
}

// Reveal component.  Default action is to set "visibility" to "visible"
FVWebComponent.ComponentBase.prototype.reveal = function () {
    this.container.style.visibility = "visible";
    this.visible = true;
}

// Returns whether or not the component is visible.
FVWebComponent.ComponentBase.prototype.isVisible = function () {
    return (this.visible);
}