var mwe_disable_bgColor = ""; // = "#CECECE";
var mwe_onLoadStart = function(container) {};
var mwe_onLoadEnd = function(container) {};
var mwe_refreshArray = new Array();

mwe_registerOnDomReadyFunction(function() {while(mwe_refreshArray.length) {mwe_refreshArray.shift().refreshAsync();}})

function mweEnableElements(element, enable, isBaseElement)
{
	if(isBaseElement && !mwe_isNull(element) && element.id)
	{
		var d = document.getElementsByTagName("BODY")[0];
		var tmpDiv = null;
		if(enable) {
			tmpDiv = mwe_getElement('_tmp_disableDiv_' + element.id);
			if(tmpDiv) {
				d.removeChild(tmpDiv);
			}
		} else {
			tmpDiv = mwe_getElement('_tmp_disableDiv_' + element.id);
			var doAppendDiv = false;
			var position = mwe_getPosition(element);
			xPos = position.x;
		    yPos = position.y;
		    oWidth = mwe_getWidth(element);
		    oHeight = mwe_getHeight(element);
            if(!tmpDiv) {
			    tmpDiv = document.createElement("DIV");
			    tmpDiv.id = '_tmp_disableDiv_' + element.id;
			    doAppendDiv = true;
			}
		    tmpDiv.style.width = oWidth+"px";
		    tmpDiv.style.height = oHeight+"px";
		    tmpDiv.style.position = "absolute";
		    tmpDiv.style.left = xPos+"px";
		    tmpDiv.style.top = yPos+"px";
            tmpDiv.style.zIndex = mwe_getZIndex(element) + 1;
			if(mwe_disable_bgColor.length > 0)
			{
				tmpDiv.style.backgroundColor = mwe_disable_bgColor;
			}
		    tmpDiv.style.opacity = .6;
		    tmpDiv.style.filter = "alpha(opacity=60)";
			if(doAppendDiv) {
			    d.appendChild(tmpDiv);
			}
		}
	}
	try
	{
		element.disabled = !enable;
	} catch(e) {}
    if(!mwe_isNull(element))
    {
    	var oNodes = element.childNodes;
        for (var i=0;i<oNodes.length;i++)
    	{
            mweEnableElements(oNodes[i], enable, false);
        }
    }
}

//gets an url with container-position value for the mwe-framework
function mweGetUrlWithContainerPosition(url, containerName)
{
	var containerValue = containerName.split("/").join(".");
	if(containerValue.length > 0)
	{
        var paramValues = url.split("?");
        var urlValues = paramValues[0];
		urlValues = urlValues.split(".");
		var fileExt = urlValues.pop();
		urlValues.push(containerName.split("/").join("."), fileExt);
        if(paramValues.length > 1)
        {
            return urlValues.join(".") + "?" + paramValues[1];
        } else {
            return urlValues.join(".");
        }
	}
	return url;
}

//construct for the mwe/containerHandler
//the container-handler holds all container-info for the current page-state
var mweContainerHandler = function() {
	this.container = new Array();
	this.containerAccessNames = new Object();
	this.parent = null;
	this.accessName = "";
	this.elementId = "";
	this.onLoadStart = function() { };
	this.onLoadEnd = function() { };
	this.isEnabled = true;
};

// mweContainerMember: getFullAccessName; gets the access-name for the main container-handler
mweContainerHandler.prototype.getFullAccessName = function() {
	var sAccessName = "";
	if(this.parent != null)
	{
		sAccessName = this.parent.getFullAccessName();
	}
	if(sAccessName != "")
	{
		sAccessName += "/";
	}
	sAccessName += this.accessName;
	return sAccessName;
};

// mweContainerMember: registerContainer
mweContainerHandler.prototype.registerContainer = function (accessName, containerObject) {
	if(this.isregisterContainer(accessName))
	{
		this.unregisterContainer(accessName);
	}
	this.container.push(containerObject);
	this.containerAccessNames[accessName] = this.container.length - 1;
	if(this.parent != null)
	{
		this.parent.registerContainer(this.accessName + "/" + accessName, containerObject);
	}
	containerObject.setParent(this, accessName);
};

//mweContainerMember: unregisterContainer
mweContainerHandler.prototype.unregisterContainer = function (accessName) {
	if(this.parent != null)
	{
		this.parent.unregisterContainer(this.accessName + "/" + accessName);
	}
	this.container[this.containerAccessNames[accessName]] = null;
};

mweContainerHandler.prototype.unregisterSubContainer = function () {
	try
	{
		var tmpDiv = null;
		var d = document.getElementsByTagName("BODY")[0];
		for(var i = 0; i < this.container.length; i++)
		{
			if(this.container[i])
			{
				tmpDiv = mwe_getElement('_tmp_disableDiv_' + this.container[i].elementId);
				if(tmpDiv) {
					d.removeChild(tmpDiv);
				}
				this.container[i].unregisterSubContainer();
			}
		}
		this.container = new Array();
		this.containerAccessNames = new Object();
	} catch(error) {}
}

//mweContainerMember: isregisterContainer
mweContainerHandler.prototype.isregisterContainer = function (accessName) {
	try 
	{
		if(this.containerAccessNames[accessName] != null && this.container[this.containerAccessNames[accessName]] != null)
		{
			return true;
		} else {
			return false;
		}
	} catch(error) {
		return false;
	}
};

//mweContainerMember: getContainer
mweContainerHandler.prototype.getContainer = function (accessName) { 
	return this.container[this.containerAccessNames[accessName]];
};

//raise load-events
mweContainerHandler.prototype.raiseEventOnLoadStart = function() {
	document.body.style.cursor='progress'; 
	if(this.isEnabled) { 
			mweEnableElements(mwe_getElement(this.elementId), false, true);
	}
	if(this.onLoadStart) {
		this.onLoadStart();
	}
	if(mwe_onLoadStart)	{
		mwe_onLoadStart(this);
	}
};
mweContainerHandler.prototype.raiseEventOnLoadEnd = function() {
	document.body.style.cursor='auto'; 
	if(this.isEnabled) { 
		mweEnableElements(mwe_getElement(this.elementId), true, true);
	}
	if(this.onLoadEnd) {
		this.onLoadEnd();
	}
	if(mwe_onLoadEnd)	{
		mwe_onLoadEnd(this);
	}
    while(mwe_refreshArray.length) {mwe_refreshArray.shift().refreshAsync();}
};

//enable/disable child-elements
mweContainerHandler.prototype.disable = function() {
	mweEnableElements(mwe_getElement(this.elementId), false, true);
	this.isEnabled = false;
};
mweContainerHandler.prototype.enable = function() {
	mweEnableElements(mwe_getElement(this.elementId), true, true);
	this.isEnabled = true;
};




//containerList - construct
//a container-list is a group of container and container-events.
var mweContainerList = function(elementId) {
	this.elementId = elementId;
	this.accessName = "";
	this.container = new Array();
	this.containerAccessNames = new Object();
	this.isEnabled = true;
	this.visible = true;
	this.parent = null;
};

//container-list extends containerHandler.
mweContainerList.prototype = new mweContainerHandler();

//visible-state for the container-list
mweContainerList.prototype.setVisible = function(isVisible) {
	this.visible = isVisible;
	if(this.visible)
	{
		mwe_getElement(this.elementId).style.display = "";
		if(this.isEnabled == false)
		{
			this.disable();
		}
	} else {
		var tmpDiv = mwe_getElement('_tmp_disableDiv_' + this.elementId);
		if(tmpDiv)
		{
			var d = document.getElementsByTagName("BODY")[0];
			d.removeChild(tmpDiv);
		}
		mwe_getElement(this.elementId).style.display = "none";
	}
};
mweContainerList.prototype.getVisible = function() {
	return this.visible;
};
//set/get the parent of the container
mweContainerList.prototype.setParent = function(parentContainer, accessName) {
	this.parent = parentContainer;
	this.accessName = accessName;
};
mweContainerList.prototype.getParent = function() {
	return this.parent;
};

//container - construct
//a container inherits the content
var mweContainer = function(elementId, refreshUrl, refreshParameter, doLoadUrl) {
	this.elementId = elementId;
	this.container = new Array();
	this.containerAccessNames = new Object();
	this.parent = null;
	this.accessName = "";
	this.visible = true;
	this.isEnabled = true;
	this.loadedUrl = "";
	if(refreshUrl)
	{
		this.loadedUrl = refreshUrl;
	}
	this.loadedParameter = { };
	if(refreshParameter) {
		this.loadedParameter = refreshParameter;
	}
	this.periodicalTimer = null;
	if(doLoadUrl)
	{
        mwe_refreshArray.push(this);
	}
};
//container extends containerHandler.
mweContainer.prototype = new mweContainerHandler();
//visible-state for the container
mweContainer.prototype.setVisible = mweContainerList.prototype.setVisible;
mweContainer.prototype.getVisible = mweContainerList.prototype.getVisible;
//set/get the parent of the container
mweContainer.prototype.setParent = mweContainerList.prototype.setParent;
mweContainer.prototype.getParent = mweContainerList.prototype.getParent;
//set a new xhtml-value in a container.
mweContainer.prototype.setContent = function(xhtmlData) {
	this.unregisterSubContainer();
	mwe_setContent(this.elementId, xhtmlData);
	//document.getElementById(this.elementId).innerHTML = xhtmlData;
};
//append a new xhtml-value in a container
mweContainer.prototype.appendContent = function (xhtmlData) {
	mwe_setContent(this.elementId, this.getContent() + xhtmlData);
}
//get the current xhtml-value
mweContainer.prototype.getContent = function () {
	return mwe_getContent(this.elementId);
	//return document.getElementById(this.elementId).innerHTML;
};
//set content from async request
mweContainer.prototype.loadAsync = function(url, parameter) {
	this.loadedUrl = url;
	this.loadedParameter = parameter;
	url = mweGetUrlWithContainerPosition(url, this.getFullAccessName());
	this.raiseEventOnLoadStart();
	mwe_getHtmlAsync(this, url, parameter);
};
//set content from syncron request
mweContainer.prototype.load = function(url, parameter) {
	this.loadedUrl = url;
	this.loadedParameter = parameter;
	url = mweGetUrlWithContainerPosition(url, this.getFullAccessName());
	this.raiseEventOnLoadStart();
	mwe_getHtmlSync(this, url, parameter);
};

//refreshs the content with the request-data from the last request
mweContainer.prototype.refreshAsync = function() {
	if(this.loadedUrl == "") {
		return false;
	}
    this.loadAsync(this.loadedUrl, this.loadedParameter);
	return true;
};
//refreshs the content with the request-data from the last request (syncron)
mweContainer.prototype.refresh = function() {
	if(this.loadedUrl == "") {
		return false;
	}
    this.load(this.loadedUrl, this.loadedParameter);
    return true;
};

//refresh container periodical
mweContainer.prototype.periodicalRefresh = function(timeInSecounds) {
	if(!timeInSecounds)
	{
		return;
	}
	if(this.periodicalTimer != null){
		clearTimeout(this.periodicalTimer);
	}
	var sFunctionString = "var container = mwe_container_handler.getContainer('" + this.getFullAccessName() + "'); if(!container.refreshAsync()) { container.periodicalRefreshStop(); }";
	this.periodicalTimer = setInterval(sFunctionString, timeInSecounds * 1000);
};
//stops the periodical refresh of the container 
mweContainer.prototype.periodicalRefreshStop = function() {
	if(this.periodicalTimer != null){
		clearInterval(this.periodicalTimer);
	}
};

//new global container-handler
var mwe_container_handler = new mweContainerHandler();
