// WebGalileo Faces Product Line

// Copyright (c) 2003-2005 JSCAPE, SoftAspects
// All rights reserved.

// This software is the confidential and proprietary information of
// JSCAPE, SoftAspects ("Confidential Information").  You shall not disclose such
// Confidential Information and shall use it only in accordance with
// the terms of the license agreement you entered into with
// JSCAPE, SoftAspects

/**
 * Constructor for Container component.
 */
function Container(aId, aVarName, aVisible, aLazy)
{
    this.id = aId;
    this.varName = aVarName;
    this.isVisible = aVisible;
    this.isLazy = aLazy;

    this.objContainer = document.getElementById(this.id);

    this.lazyContentState = "Unloaded";
    //this.lazyContentRequestUrl = "";

    this.displayValue = "";

    if (this.objContainer.style.display != "none") {
        this.displayValue = this.objContainer.style.display;
    } else {
        this.isVisible = false;
    }

    this.hide = container_hide;
    this.show = container_show;
    this.init = container_init;

    this.isLoaded = container_isLoaded;
    this.isLoading = container_isLoading;

    this.loadLazyContent = container_loadLazyContent;
    this.getWrappingElement = container_getWrappingElement;
    var containerObj = this;
}

/**
 * Hides container by setting display: none.
 */
function container_hide() {
    if (this.objContainer) {
        if (this.objContainer.style.display != "none") {
            this.displayValue = this.objContainer.style.display;
        }
        this.objContainer.style.display = "none";
    }
    this.isVisible = false;
}

/**
 * Displays container and, if necessary, loads contents from server.
 */
function container_show() {
    if (this.objContainer) {
        this.objContainer.style.display = this.displayValue;
    }
    this.isVisible = true;
    if (this.isLazy && !this.isLoaded()) {
        this.loadLazyContent();
    }
}

/**
 * Displays container and, if necessary, initializes it.
 */
function container_init() {
    if (this.isVisible) {
        if (this.isLazy && !this.isLoaded()) {
            this.loadLazyContent();
        }
    }
    else {
        this.hide();
    }
}

/**
 * Performs container server-side lazy load using Ajax.Updater class from
 * Prototype library.
 *
 */
function container_loadLazyContent() {
    this.lazyContentState = "Loading";
    var charSetString = "";

    // Detecting current character set
    if (document.characterSet) {
        charSetString = '&charSet=' + document.characterSet;
    } else if (window.document.charset) {
        charSetString = '&charSet=' + window.document.charset;
    }

    // Preparing server-side URL to be asked for content
    var lazyLoadServerURL = this.lazyContentRequestUrl +
                            "?container-lazy-content-request=true"
            + "&containerId=" + this.id
            + charSetString;

    // Setting evalScripts parameter to force Ajax.Updater to evaluate
    // scripts received from the server
    var options = new Object();
    options.evalScripts = true;

    // Setting event handlers
    if (this.userFunctionOnLoading) {
        try {
            options.onLoading = eval(this.userFunctionOnLoading);
        } catch (e) {
        }
    }

    if (this.userFunctionOnComplete) {
        try {
            options.onComplete = eval(this.userFunctionOnComplete);
        } catch (e) {
        }
    }


    //ajax progress supported
    if(this.ajaxProgressId){
        this.ajaxProgressId.renderProgressBar();
        this.ajaxProgressId.centreProgressBar();
    }

    // Initiating request to the server and updating dummy element
    var request =
            new Ajax.Updater(this.objContainer, lazyLoadServerURL, options);


    //ajax progress supported
    if(this.ajaxProgressId){
        this.ajaxProgressId.complete();
    }

    // Setting state
    this.lazyContentState = "Complete";
}

/**
 * Returns whether lazy content is currently being loaded.
 */
function container_isLoaded() {
    return (this.lazyContentState == "Complete");
}

/**
 * Returns whether lazy content is already loaded.
 */
function container_isLoading() {
    return (this.lazyContentState == "Loading");
}

/**
 * Returns wrapping HTML element object.
 */
function container_getWrappingElement() {
    return this.objContainer;
}
