Component = new Object();

/**
Klasse AjaxLoadedComponent
*/
Component.i42Component = Class.create();
Object.extend(Object.extend(Component.i42Component.prototype, Object.prototype), {
    initialize: function(id)
    {
        this.i42ComponentInitialize(id);
    },

    i42ComponentInitialize : function(id)
    {
        this.domElement = $(id);
        this.id = id;
    },

    hide: function()
    {
        this.domElement.style.display = 'none';
    },

    show: function()
    {
        this.domElement.style.display = 'block';
    },

    flap: function()
    {
        if (this.domElement.style.display == 'none')
        {
            this.show();
        }
        else
        {
            this.hide();
        }
    },

    activate: function()
    {
        var element = this.getDeactivationDiv();

        element.style.display = 'none';

        this.domElement.deactivated = false;
    },

    deactivate: function()
    {
        var element = this.getDeactivationDiv();
        if(window.getComputedStyle)
            var style = document.defaultView.getComputedStyle(this.domElement, null).getPropertyValue('position');
        else
            var style = this.domElement.currentStyle.position;

        if (style != 'relative' && style != 'absolute')
        {
            this.domElement.style.position = 'relative';
        }

        element.style.display = 'block';
        element.style.position = 'absolute';
        element.style.top = '0px';
        element.style.left = '0px';

        element.style.height = this.domElement.clientHeight + 'px';
        element.style.width  = this.domElement.clientWidth + 'px';

        this.domElement.deactivated = true;
    },

    getDeactivationDiv : function()
    {
        var id = this.id + 'Deactivation';
        var result = document.getElementById(id);
        if (!result)
        {
            result = document.createElement('div');
            result.id = id;
            result.className = 'deactivation';

            this.domElement.appendChild(result);
        }
        return result;
    },

    toggleActivation: function()
    {
        if (this.domElement.deactivated)
        {
            this.activate();
        }
        else
        {
            this.deactivate();
        }
    },


    dummy: function() {}

});

/*
    Klasse TabComponent
*/
Component.TabComponent = Class.create();
Object.extend(Object.extend(Component.TabComponent.prototype, Component.i42Component.prototype), {
    initialize: function(id, count)
    {
        this.i42ComponentInitialize(id);
        this.tabComponentInitialize(count);
    },

    tabComponentInitialize: function(count)
    {
        this.count = count;
    },

    showTab: function(id)
    {
        for (var i = 1; i <= this.count; i++)
        {
            $(this.id+'Tab'+i).style.display="none";
            $(this.id+'Tab'+i+'Indicator').className="";
        }
        $(this.id+'Tab'+id).style.display="block";
        $(this.id+'Tab'+id+'Indicator').className="active";
    }
});

/**
Klasse AjaxLoadedComponent
*/
Component.AjaxLoadedComponent = Class.create();
Object.extend(Object.extend(Component.AjaxLoadedComponent.prototype, Component.i42Component.prototype), {
    initialize: function(id, updateUrl, parameterList)
    {
        this.ajaxLoadedComponentInitialize(id, updateUrl, parameterList);
    },

    ajaxLoadedComponentInitialize : function(id, updateUrl, parameterList)
    {
        this.i42ComponentInitialize(id);
        this.domElement = $(id);
        this.containerId = id;
        this.contentId = id + "Content";
        this.contentDomElement = $(this.containerId);
        this.url = updateUrl;
        this.showIndicator = true;
        this.loadingContent = "Loading ...";
        this.completeContent = "Complete!";
        this.savedParameterSet = this.buildParameterSet(parameterList);
        this.setIgnoreFilter(false);
    },

    /*
    Callback f�r eigene onComplete Aktionen
    */
    onCompleteCallback: function(request, json)
    {

    },

    setOnComplete: function(f)
    {
    	this.onCompleteCallback = f;
    },

    /* bool: soll das IndicatorFeld angezeigt werden */
    setShowIndicator: function(value)
    {
        this.showIndicator = value;
    },

    /* String: Anzeige w�hrend des Ladens */

    setLoadingContent: function(content)
    {
        this.loadingContent = content;
    },

    /* String: Anzeige w�hrend des Ladens */
    setCompleteContent: function(content)
    {
        this.completeContent = content;
    },


    buildParameterSet : function(parameter)
    {
        var result = new Object();
        // Input: parameter=value&parameter2=value2
        // oder: paremeter/value/parameter2/value2
        if (parameter.substr(0,1) == '?' || parameter.substr(0,1) == '/')
        {
            parameter = parameter.substr(1);
        }

        var parameterSet = parameter.split('&');
        for (var i = 0; i < parameterSet.length; i++)
        {
            if (parameterSet[i].indexOf('=') != -1)
            {
                var parameterSet2 = parameterSet[i].split('=');
                result[parameterSet2[0]] = parameterSet2[1];
            }
            else
            {
                var parameterSet2 = parameterSet[i].split('/');
                var isKey = true;
                var key = "";
                for (var j = 0; j < parameterSet2.length; j++)
                {
                    if (isKey)
                    {
                        key = parameterSet2[j];
                    }
                    else
                    {
                        result[key] = parameterSet2[j];
                    }
                    isKey = !isKey;
                }
            }
        }
        return result;
    },

    buildParameterList : function(parameterSet)
    {
        result = "";
        for (key in parameterSet)
        {
            if (parameterSet[key] != '')
            {
                result += key + '/' + parameterSet[key] + '/';
            }
        }
        return result;
    },
    /*
    public update()
    Aktualisiert die Komponente
    */
	update: function(parameter, mergeParameter)
	{
		//MB hinzugef�gt um die Filter nicht zu verlieren ;)
		if(typeof Filter != "undefined")
		{
    	    if(Filter.FilterManager.instance && !this.ignoreFilter)
            {
                if(!parameter)
                    parameter += Filter.FilterManager.instance.getParameterList();
                else
                {
                    var tmp = Filter.FilterManager.instance.getParameterList();
                    if(tmp.substr(0,1) == '?')
                        tmp = "&"+tmp.substr(1);
                    parameter += tmp;
                }
            }
		}
	    var currentParameterSet = this.buildParameterSet(parameter);

		if (mergeParameter)
	    {
	        var mergedParameterSet = this.savedParameterSet;
	        for (key in currentParameterSet)
	        {
	            mergedParameterSet[key] = currentParameterSet[key];
	        }
	        currentParameterSet = mergedParameterSet;
	    }
	    this.savedParameterSet = currentParameterSet;
		parameter = this.buildParameterList(currentParameterSet);

		var completeUrl = this.url + '/parentId/'+this.containerId;
		if (parameter != null)
	    {
	        completeUrl += '/' + parameter;
	    }

        if (this.showIndicator)
	    {
            $(this.containerId+'Indicator').innerHTML = this.loadingContent;
	        new Effect.Appear(this.containerId+'Indicator', {duration:0.4, from:0, to:1.0});
	    }
	    new Ajax.Updater(this.contentId,
	                     completeUrl,
	                     {
	                         asynchronous :    true,
	                         evalScripts  :    true,
	                         script       :    true,
	                         onComplete   :    document[this.containerId].onComplete.bind(this)
	                     }
	                    );
	},

	/* wird aufgerufen, wenn der Request beendet ist */
	onComplete: function(request, json)
    {
        if (this.showIndicator)
	    {
            $(this.containerId+'Indicator').innerHTML = this.completeContent;
	    }
	    this.onCompleteCallback(request, json);
		if (this.showIndicator)
	    {
            new Effect.SwitchOff(this.containerId+'Indicator');
	    }
	},

	/* l�scht den Inhalt der Komponente */
	clear: function()
	{
        this.contentDomElement.innerHTML = '';
	},

	setIgnoreFilter: function (value)
	{
	   this.ignoreFilter = value;
	}
});


/**
Klasse FilterableComponent
*/
Component.FilterableComponent = Class.create();
Object.extend(Object.extend(Component.FilterableComponent.prototype, Component.i42Component.prototype), {
    initialize: function(id, updateUrl, parent, ignoreFilter)
    {
        this.filterableComponentInitialize(id, updateUrl, parent, ignoreFilter);
    },

    filterableComponentInitialize : function(id, updateUrl, parent, ignoreFilter)
    {
        this.i42ComponentInitialize(id, updateUrl);
        this.domElement = $(id);
        this.containerId = id;
        this.contentId = id + "Content";
        this.contentDomElement = $(this.containerId);
        this.url = updateUrl;
        this.showIndicator = true;
        this.loadingContent = "Loading ...";
        this.completeContent = "Complete!";
        this.parentComponent = parent;
        this.parentComponent.setIgnoreFilter(ignoreFilter);
    },
    /*
    public update()
    Aktualisiert die Komponente
    */
	update: function(parameter)
	{
	    if (!parameter)
		{
		    parameter = "";
		}

		//MB => �bernimmt nun die AjaxLoadedComponent->update()
		//parameter += Filter.FilterManager.instance.getParameterList();
        this.parentComponent.update(parameter, true);
	}


});



Component.getYCoordOfElement = function(obj,relative)
{
    var y = 0;
    if(typeof(obj) == "object" && document.getElementById) {
        y = obj.offsetTop;
        if(obj.offsetParent && !relative) {
            y += Component.getYCoordOfElement(obj.offsetParent);
        }
        return y;
    }
    else{
        return false;
    }    
}

Component.getXCoordOfElement = function(obj,relative)
{
    var x = 0;
    if(typeof(obj) == "object" && document.getElementById) {
        x = obj.offsetLeft;
        if(obj.offsetParent && !relative) {
            x += Component.getXCoordOfElement(obj.offsetParent);
        }
        return x;
    }
    else{
        return false;
    }  
}

Component.center = function(id)
{
    var scroll = Component.getScrollPosition();
	var positionOffsetY = 100; // offset Y
	var positionOffsetX = 300; //offset X
	
	$(id).style.position = 'absolute';
    $(id).style.top = (positionOffsetY + scroll[1])+'px'; //new top position with scroll offset
	$(id).style.left = (positionOffsetX)+'px';	
}

Component.getWindowSize = function()
{
    var myWidth = 0, myHeight = 0;
    if(typeof(window.innerWidth) == 'number')
    {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    } 
    else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
    {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    } 
    else if(document.body && (document.body.clientWidth || document.body.clientHeight))
    {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }
    return [myWidth, myHeight];
}

Component.getScrollPosition = function()
{
    var scrOfX = 0, scrOfY = 0;
    if(typeof( window.pageYOffset ) == 'number')
    {
        //Netscape compliant
        scrOfY = window.pageYOffset;
        scrOfX = window.pageXOffset;
    } 
    else if(document.body && ( document.body.scrollLeft || document.body.scrollTop))
    {
        //DOM compliant
        scrOfY = document.body.scrollTop;
        scrOfX = document.body.scrollLeft;
    } 
    else if(document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop))
    {
        //IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop;
        scrOfX = document.documentElement.scrollLeft;
    }    
    return [scrOfX, scrOfY];
}

getValueFromFrame = function(frameName, formName, fieldName){

    return window.frames[frameName].document.forms[formName].elements[fieldName].value;
 
}