var Rup = {

    extend: function (Child, Parent) 
    {
        var F = function() { }
        F.prototype = Parent.prototype;
        Child.prototype = new F();
        Child.prototype.constructor = Child;
        Child.superclass = Parent.prototype;
    },
    
    get: {
        id: function( sId )
        {
            return document.getElementById( sId );
        }
    },
    
    createXML: function(sXml)
    {
        // Mozilla and Netscape browsers
        if (document.implementation.createDocument) 
        {
            var parser = new DOMParser();
            doc = parser.parseFromString(sXml, "text/xml");
        // MSIE
        } else if (window.ActiveXObject) {
            doc = new ActiveXObject("Microsoft.XMLDOM");
            doc.async="false";
            doc.loadXML(sXml);
        }
        return doc;
    },
    
    create: function(tagName)
    {
        return document.createElement(tagName);
    },
    
    replace: function(newNode, oldNode, parentNode)
    {
    	if( !parentNode.hasChildNodes() )
    	{
    		return undefined;
    	}
    	
    	for( var i = 0; i < parentNode.childNodes.length; i++ )
    	{
    		if( parentNode.childNodes[i] == oldNode )
    		{
    			return parentNode.replaceChild(newNode, oldNode);
    		}
    		
    		var ret = Rup.replace(newNode, oldNode, parentNode.childNodes[i]);
    		if( ret )
    		{
    			return ret;
    		}
    	}
    	return undefined;
    	
    },
    
    app: {
    	getLink: function(params){
    	
    		if( this.content.mainWindow )
    		{
    			return this.content.mainWindow.getLink( params );
    		}
    		
    		if( this.task.mainWindow )
    		{
    			return this.task.mainWindow.getLink( params );
    		}
    		
    		if( this.content.mainDiv )
    		{
    			return this.content.mainDiv.getLink( params );
    		}
    		
    		return new Link(params);
    		
    	},
        content: {
            mainDiv: undefined,
            mainWindow: undefined,
            tmpDiv: undefined,
            tmpMainDiv: undefined,
            init: function()
            {
            	if( this.tmpMainDiv )
            	{
            		this.mainDiv = this.tmpMainDiv;
            	}
            	else
            	{
	                this.mainDiv = new Main_Div();
                }
                this.mainDiv.render('workdv');
                this.tmpDiv = undefined;
                this.tmpMainDiv = undefined;
            },
            clear: function()
            {
            	delete this.mainDiv;
            	Rup.get.id("workdv").innerHTML = "";
            	this.tmpDiv = undefined;
                this.tmpMainDiv = undefined;
            },
            clearSafe: function()
            {
            	hideLeftTree();
            	this.tmpDiv = Rup.create("div");
            	this.tmpMainDiv = this.mainDiv;
            	this.mainDiv.render(this.tmpDiv);
            	delete this.mainDiv;
            	Rup.get.id("workdv").innerHTML = "";
            },
            go: function(params)
            {
                if( !this.mainDiv )
                {
                    this.init();
                }
                
                if( this.mainWindow )
                {
                    this.mainWindow.pannel.go(params);
                }
                else
                {
                    this.mainDiv.pannel.go(params);
                }
                
            },
            append: function(params)
            {
                if( !this.mainDiv )
                {
                    this.init();
                }
                
                if( this.mainWindow )
                {
                    this.mainWindow.pannel.append(params);
                }
                else
                {
                    this.mainDiv.pannel.append(params);
                }
            },
            goWindow: function(params)
            {
                this.closeWindow();
                this.openWindow();
                this.mainWindow.pannel.go(params);
            },
            openWindow: function()
            {
                if( !this.mainWindow )
                {
                    this.mainWindow = new Main_Window();
                }
            },
            closeWindow: function()
            {
                if( this.mainWindow )
                {
                    this.mainWindow.close();
                    delete this.mainWindow;
                }
            }
        },
        
        task: {
            mainWindow: undefined,
            taskPanel: undefined,
            /*goWindow: function(params)
            { 
                this.closeWindow();
                this.openWindow();
                this.mainWindow.pannel.go(params);
            },
            openWindow: function()
            {
                if( !this.mainWindow )
                {
                    this.mainWindow = new Test_Help_Window();
                }
            },
            closeWindow: function()
            {
                if( this.mainWindow )
                {
                    this.mainWindow.close();
                    delete this.mainWindow;
                }
            },*/
            showTest: function(TaskList, mode) //listXml, testid)
            { //debugger;
            	//Rup.app.content.clearSafe();
            	//currentTestMode = 0;
            	if( this.taskPanel )
            	{
            		delete this.taskPanel;
            	}
            	this.taskPanel = new Task_Panel(TaskList, mode);//listXml, testid, 0);
            }
            /*getPredefinedList: function( testid )
            {
            	    ProcessAjax({
				        method: "GET",
				        url: "pt" + testid + ".xml",
				        //data: "",
				        errback: Error.Show1,
				        callback: function( data )
				        {
				            Rup.app.task.showTest( data, testid );
				        }
				    });
				    return false;
            },
            getList: function(params, mode, kursname)
            {
            	// Rup.app.task.getList({url: "/tests.asmx/GetPredefinedList", data: "testId=711&iMode=3"}, 3);
            	
            	ProcessAjax({
			        method: "GET",
			        url: params.url,
			        data: params.data,
			        errback: Error.Show1,
			        callback: function( data )
			        {
			            Rup.app.task.showTest( data, 0, mode, kursname );
				    }
            	});
            	return false;
            }*/
        }
        
    },
    
    GetButton: function( title, handler )
    {
        var oButton = Rup.create("input");
        oButton.type = "button";
        oButton.value = title;
        oButton.onclick = handler;
        
        return oButton;
    },

    GetImgButton: function(arg)
    {
        var oButton = Rup.create("img");
        oButton.src = arg.src;
        if(arg.alt)
        {
            oButton.alt = arg.alt;
        }
        if(arg.title)
        {
            oButton.title = arg.title;
        }
        if(arg.cursor)
        {
            oButton.style.cursor = arg.cursor;
        }
        oButton.onclick = arg.handler;
        
        return oButton;
    },
    
    css:{
	    change: function( oElement, sClass )
	    {
	        if( Rup.css.exists( oElement, sClass ) )
	        {
	        	Rup.css.del( oElement, sClass );
	        }
	        else
	        {
	        	Rup.css.add( oElement, sClass );
	        }
	    },
	
	    del: function( oElement, sClass )
	    {
	        if( Rup.css.exists(oElement, sClass) )
	        {
	            var sClassNames = Rup.css.get(oElement);
	            var aClassNames = sClassNames.split(' ');
	            aClassNames.splice( aClassNames.indexOf(sClass), 1 );
	            Rup.css.set(oElement, aClassNames.join(' '));
	        }
	    },
	
	    add: function( oElement, sClass )
	    {
	        if( !Rup.css.exists( oElement, sClass ) )
	        {
	            var aClassNames = Rup.css.get(oElement).split(' ');
	            aClassNames.push(sClass);
	            Rup.css.set(oElement, aClassNames.join(' '));
	        }
	        
	    },
	
	    exists: function( oElement, sClass )
	    {
	        var sClassNames = Rup.css.get(oElement);
	        return (sClassNames.indexOf(sClass) != -1)
	    },
	
	    set: function( oElement, sClass )
	    {
	        oElement.className = sClass;
	    },
	
	    get: function( oElement )
	    {
	        return oElement.className || '';
	    }
    }
}


