var windowStack = new Array();
var windowStackSize = 0;
var windowZIndex = 10;

function getZIndex() {
	return windowZIndex++;
}

function window_Engine( name ) {
	this.windowOpened = 0;

	this.requestURI = '';
	this.requestFunct = '';
	this.requestParam = null;

	this.documentRoot = document.body;
	this.documentDimension = document.body;

	this.parent = null;

	this.windowName = name;

	this.container = null;
	this.containerStyle = null;
	this.containerOffsetY = 0;
	this.containerOffsetX = 0;

	this.splash = new splash_Engine();

	this.windowNumber = windowStackSize;
	windowStack[this.windowNumber] = this;
	windowStackSize++;

	this.debug = false;

	this.events = new Object();

	this.childs = new Array();
	this.childsNumber = 0;

	this.fm = null;
}

window_Engine.prototype = {

	open: function(uri, module, subid) {

		if ( this.windowOpened != 0 ) {
			return;
		}

		if ( typeof(module) != 'undefined' )
		{
			this.moduleID = module;
		}

		if ( typeof(subid) != 'undefined' )
		{
			this.subID = subid;
		}

		if ( typeof(this.fm) == 'object' && this.fm != null )
		{
			this.fm.setWindow( this );
			this.fm.setModuleID( this.moduleID );
			this.fm.setSubID( this.subID );
		}

		this.windowOpened = 1;
		this.requestURI = uri;

		if ( this.requestURI ) {
			this.showSplash();
		}

		this.createContainer();

		if ( this.requestURI ) {
			this.request(this.requestURI, this.requestFunct, this.requestParam);
		}
	},

	d: function(f, t) {
		if (this.debug)
			alert('Window: ' + this.windowName + '\nFunction: ' + f + '\n' + t);
	},

	showSplash: function() {
		this.d('showSplash', 'this.documentRoot(id: '+this.documentRoot.id+'): ' + this.documentRoot);

		this.splash.newLayer( this.documentRoot, this.documentDimension,  this.windowName );
	},

	hideSplash: function() {
		this.splash.remove( this.documentRoot, this.windowName );
	},

	requestCallBack: function(payload, param) {
		this.container.innerHTML = payload;

		if ( typeof(this.requestFunct) == 'function' )
		{
			this.requestFunct(param);
		}

	},

	request: function(uri, funt, param) {
		this.requestURI = uri;
		this.requestFunct = funt;
		this.requestParam = param;

		var self = windowStack[this.windowNumber];
		gRPC.request(this.requestURI, function (payload, param) { self.requestCallBack(payload, param) }, this.requestParam);
	},

	close: function() {

		document.getElementById('hmenu').style.visibility='visible';
		this.d('close', 'opened: '+this.windowOpened+' childs: '+this.childsNumber);

		if ( this.childsNumber > 0 )
		{
			var c = this.childsNumber;
			for (i=1; i<=c; i++)
			{
				this.childs[i].close();
				this.childsNumber--;
			}
		}

		this.windowOpened = 0;

		this.hideSplash();

		this.documentRoot.removeChild( this.container );
	},

	setRequestCallBack: function(funct) {
		this.requestFunct = funct;
	},

	setParent: function(o) {
		this.parent = o;
		this.parent.setChild(this);

		this.moduleID = o.moduleID;
		this.subID = o.subID;
	},

	setChild: function(o) {
		this.childsNumber++;
		this.childs[this.childsNumber] = o;
	},

	setDocumentRoot: function(o, o2) {
 		this.d('setDocumentRoot', 'setDocumentRoot(id: '+o.id+'): ' + o);
		this.documentRoot = o;
		this.documentDimension = o2;

		if ( typeof(o2) == 'undefined' )
		{
			this.documentDimension = o;
		}
	},

	getContainer: function() {
		return this.container;
	},

	setContainerStyle: function(style) {
		this.containerStyle = style;
	},

	createContainer: function(object) {

		this.d('close', 'createContainer()');

		this.container = document.createElement('div');
		this.container.setAttribute('id', this.windowName);

		if ( this.containerStyle != null)
		{
			if ( this.containerStyle.palign == 'absmiddle' )
			{
				var w = innerDimension().width;
				var h = innerDimension().height;

				var ox = offsetDimension().x;
				var oy = offsetDimension().y;

				if (navigator.userAgent && navigator.userAgent.indexOf("MSIE")>=0 && (navigator.appVersion.indexOf("Win") != -1)) {
					var l = ((w) / 2) - 598 / 2 + ox;
					var t = ((h) / 2) - 440 / 2 + oy;
				} else {
					var l = ((w) / 2) - 598 / 2 + ox;
					var t = ((h) / 2) - 440 / 2 + oy;
				}

				this.containerOffsetY = t+'px';
				this.containerOffsetX = l+'px';

				this.container.style.top = t+'px';
				this.container.style.left = l+'px';

			}


			this.container.style.width				= this.containerStyle.width;
			this.container.style.position			= this.containerStyle.position;
		    this.container.style.border				= this.containerStyle.border;
		    this.container.style.backgroundColor	= this.containerStyle.backgroundColor;
		    this.container.style.padding			= this.containerStyle.padding;

		}

		this.container.style.zIndex = getZIndex();

		this.documentRoot.appendChild(this.container);
	},

	addEvent: function(event, funct) {
		this.events[event] = funct;
	},

	event: function(event) {
//		var self = windowStack[this.windowNumber];
		this.events[event]();
	},

	getContainerOffsets: function() {
		var info = {x: 0, y: 0};

		info.x = this.containerOffsetX;
		info.y = this.containerOffsetY;

		if ( this.parent != null )
		{
			var pinfo = this.parent.getContainerOffsets();

			info.x = info.x + pinfo.x;
			info.y = info.y + pinfo.y;
		}

		return info;
	},

	setFloatMenu: function(o) {
		this.fm = o;
	}

}

