/*
*************************************
** main web application file (webapp)
*************************************
*/

/***********************************************************************************************/

/* Container zur Ablage globaler Zustands-Variablen (Session) */
/* Just for testing purposes                                  */
var wms_session_data = null;
function webapp_session_data() {
	this.xyz = false;
}

var webapp = null;
function webapplication() {
	/********** object variable section **********/

	/* just to control debug outputs */
	this.debug = false;
	this.debug_counter = 0;
	this.modhandler='/mtpw/application/common/classes/modhandler.php';
	this.modcachehandler='/mtpw/application/common/classes/modcachehandler.php';
	/********** object method section **********/

	/* debug output */
	this.debug_Output = function( textstr ) {
		this.debug_counter++;
		$('lytwindow_debug').insert( { top: '<br/>' + this.debug_counter + ': ' + textstr } );
	}
	/* Module loading methods */
	this.load_Module_Complete = function( xhr, lytarea_id, mod_id ) {
		if( this.debug==true )
			this.debug_Output('webapp: Layout-Area: ' + lytarea_id + ' ,Module: ' + mod_id + ' loaded');
	}
	
	this.load_Module_CompleteDisplay = function( xhr, lytarea_id, mod_id ) {
		m2ws_checkCompleteDisplay(mod_id);
		if( this.debug==true )
			this.debug_Output('webapp: Layout-Area: ' + lytarea_id + ' ,Module: ' + mod_id + ' loaded');
	}
	
	this.load_Module_CompleteD = function( req) {
		alert(req.responseText);
		
	}
	/*
	** Inserts a module identified by mod_id into a layout element
	** identified by lytarea_id at a given order identified by
	** position. mod_id must be without the module_ prefix
	** position : 1 - N as 1.-N. module
	**            0     as first module
	**           -1     as last  module
	**
	** example:
	**  webapp.load_Module( 'lytarea_titlestage', 'titlestage_000000_01', 0 );
	**
	** Parameter position currently not used
	*/
	this.load_Module = function( lytarea_id, mod_id, position ) {
		if( this.debug ==true)
			this.debug_Output('webapp: Layout-Area: ' + lytarea_id + ' ,Module: ' + mod_id + ' loading');
		new Ajax.Updater( lytarea_id, this.modhandler+'?time='+(new Date).getTime(), {
			method: "get",
			parameters: {mod: mod_id},
			evalScripts: true,
			evalJS: false,
			onComplete: (function(xhr) { this.load_Module_Complete(xhr,lytarea_id,mod_id) }).bind(this)
		} );
	}
	/*
	** Same as above, but synchronous request
	*/
	this.loads_Module = function( lytarea_id, mod_id, order, isedit, username,type ) {
		
		if( this.debug==true ){
			this.debug_Output('webapp: Layout-Area: ' + lytarea_id + ' ,Module: ' + mod_id + ' loading');
		}
		//document.getElementById("log").innerHTML=document.getElementById("log").innerHTML+"<br/>"+'webapp: Layout-Area: ' + lytarea_id + ' ,Module: ' + mod_id + ' loading';
		new Ajax.Updater( lytarea_id, this.modhandler+'?time='+(new Date).getTime(), {
			method: "get",
			parameters: {mod: mod_id,edit: isedit,user:username,type:type},
			asynchronous: false,
			insertion: order,
			evalScripts: true,
			evalJS: false,
			onComplete: (function(xhr) { this.load_Module_CompleteDisplay(xhr,lytarea_id,mod_id) }).bind(this)
		} );
	}
	this.loads_ModuleD = function( lytarea_id, mod_id, order, isedit, username ) {
		
		if( this.debug==true ){
			this.debug_Output('webapp: Layout-Area: ' + lytarea_id + ' ,Module: ' + mod_id + ' loading');
		}
		//alert(isedit+'--'+username);
		new Ajax.Updater( lytarea_id, this.modhandler+'?time='+(new Date).getTime(), {
			method: "get",
			parameters: {mod: mod_id,edit: isedit,user:username},
			asynchronous: false,
			insertion: order,
			evalScripts: true,
			evalJS: false,
			onComplete: this.load_Module_Display
		} );
	}
	
	this.loads_HistoryModule = function( lytarea_id, mod_id, order, isedit, username ) {
	
	}
	
		/*
	** Same as above, but synchronous request
	*/
	this.loads_ModuleCache = function( seite_id,lytarea_id, mod_id, order, isedit, username, forcedelete,type ) {
		//document.getElementById("log").innerHTML=document.getElementById("log").innerHTML+"<br/>"+'webapp: Layout-Area: ' + lytarea_id + ' ,Module: ' + mod_id + ' caching';
		if( this.debug==true ){
			this.debug_Output('webapp: Layout-Area: ' + lytarea_id + ' ,Module: ' + mod_id + ' loading');
		}
		//alert(isedit+'--'+username);
		new Ajax.Updater( lytarea_id, this.modcachehandler+'?time='+(new Date).getTime(), {
			method: "get",
			parameters: {mod: mod_id,edit: isedit,user:username,seite:seite_id,del:forcedelete,type:type},
			asynchronous: false,
			insertion: order,
			evalScripts: true,
			evalJS: false,
			//onComplete: (function(xhr) { this.load_Module_Complete(xhr,lytarea_id,mod_id) }).bind(this)
			onComplete: (function(xhr) { this.load_Module_Complete(xhr,lytarea_id,mod_id) }).bind(this)
		} );
	}
	this.dummy=function(){
		
	}
	/*
	** Removes a module identified by mod_id from a layout element
	** identified by lytarea_id. Other modules in lytarea_id leave
	** untouched. mod_id must be without the module_ prefix
	**
	** example:
	**   webapp.remove_Module( 'lytarea_titlestage', 'titlestage_000000_01');
	*/
	this.remove_Module = function(  lytarea_id, mod_id  ) {
		if( $(mod_id) ) {
			$(mod_id ).remove();
			if( this.debug )
				this.debug_Output('webapp: Layout-Area: ' + lytarea_id + ' ,Module: ' + mod_id + ' removed');
		}
	}
	/*
	** Removes all modules from a layout element identified
	** by lytarea_id
	** example:
	**   webapp.remove_Modules('lytarea_titlestage');
	*/
	this.remove_Modules = function( lytarea_id ) {
		mods = $(lytarea_id).childElements();
		if( mods.size() > 0 ) {
			mods.each( function(n) {
				$(n).remove()
			});
			if( this.debug )
				this.debug_Output('webapp: Layout-Area: ' + lytarea_id + ' , Module: all removed');
		}
	}
	

	/* Initialization methods */
	this.init = function( dbg ) {
		this.debug = dbg;
		if( this.debug ) {
			new Draggable('lytwindow_debug');
			$('lytwindow_debug').show();
		}
		
		/* creating it here, just for testing purposes */
		wms_session_data = new webapp_session_data();

		if(this.debug)
			this.debug_Output( 'webapp: initialized !' );
	}
}
