var container_id = 'container';

/* layer megjelenitese */

function splash_Engine() {

	this.layerNr        = 0;
	this.opacity        = 90;
	this.zIndex         = 100;
	this.queue          = new Object();

};

splash_Engine.prototype = {

	newLayer: function(p, dim, windowName) {

		var splash = document.createElement('div');
		splash.setAttribute('id', windowName + 'splash' + this.layerNr);

		splash.style.left = 0;
		splash.style.top = 0;

        var swidth = document.getElementById('container').scrollWidth;
        var sheight = document.getElementById('container').scrollHeight;

	    splash.style.width = swidth+'px';

        splash.style.height = sheight+'px';

		splash.style.visibility = 'visible';
		splash.style.position = 'absolute';
		splash.style.backgroundColor = '#000000';

		splash.style.zIndex = getZIndex();

		splash.style.opacity = (this.opacity / 100);
		splash.style.MozOpacity = (this.opacity / 100);
		splash.style.KhtmlOpacity = (this.opacity / 100);
		splash.style.filter = "alpha(opacity=" + this.opacity + ")";

		p.appendChild(splash);

		this.queue[windowName + 'splash' + this.layerNr] = splash;

//        splash.setAttribute('onClick', 'wPropertyDatapage.close(); enableSelect();');
//        splash.setAttribute('onkeypress', 'checkKeyCode(event.keyCode);');

        this.layerNr++;
	},

	remove: function(p, windowName) {
		if ( this.layerNr == 0 ) {
			return;
		}

		var splash = this.queue[windowName + 'splash' + (this.layerNr - 1) ];
		p.removeChild(splash);

		this.layerNr--;
	}

}

var splash = new splash_Engine();

/* window megjelenitese */

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;

    this.containerId    = container_id;

}

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.documentRoot = this.documentDimension = document.body;

		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() {

		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);

        var w = sizeCalculation.getWindowWidth();
        var h = sizeCalculation.getWindowHeight();

        var ox = sizeCalculation.getScrollLeft();
        var oy = sizeCalculation.getScrollTop();

        var l = ((w) / 2) - this.containerStyle.width / 2 + ox;
        /*var t = ((h) / 2) - this.containerStyle.height / 2 + oy;*/

        var t = 200;

        if ( t < 60 ) {
            t = 60;
        }

        if ( l < 5 ) {
            l = 5;
        }

        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.height				= this.containerStyle.height;
        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);

//        this.container.setAttribute('onkeypress', 'checkKeyCode(event.keyCode);');

	},

	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;
	}

}

/* meret szamitasok */

sizeCalculation = {

    getWindowWidth: function() {
	    return (document.layers||(document.getElementById&&!document.all)) ? window.outerWidth : (document.all ? document.body.clientWidth : 0);
	},

	getWindowHeight: function() {
	    return window.innerHeight ? window.innerHeight :(document.getBoxObjectFor ? Math.min(document.documentElement.clientHeight, document.body.clientHeight) : ((document.documentElement.clientHeight != 0) ? document.documentElement.clientHeight : (document.body ? document.body.clientHeight : 0)));
	},

	getScrollWidth: function() {
	return document.all ? Math.max(Math.max(document.documentElement.offsetWidth, document.documentElement.scrollWidth), document.body.scrollWidth) : (document.body ? document.body.scrollWidth : ((document.documentElement.scrollWidth != 0) ? document.documentElement.scrollWidth : 0));
	},

	getScrollHeight: function(){
		return document.all ? Math.max(Math.max(document.documentElement.offsetHeight, document.documentElement.scrollHeight), Math.max(document.body.offsetHeight, document.body.scrollHeight)) : (document.body ? document.body.scrollHeight : ((document.documentElement.scrollHeight != 0) ? document.documentElement.scrollHeight : 0));
	},

	getScrollLeft: function() {
		return document.all ? (!document.documentElement.scrollLeft ? document.body.scrollLeft : document.documentElement.scrollLeft) : ((window.pageXOffset != 0) ? window.pageXOffset : 0);
	},

	getScrollTop: function() {
		return document.all ? (!document.documentElement.scrollTop ? document.body.scrollTop : document.documentElement.scrollTop) : ((window.pageYOffset != 0) ? window.pageYOffset : 0);
	}

}

var layerContent = new window_Engine('layerContent');
var layerDatapage = new window_Engine('layerDatapageFrame');

layerDatapage.setContainerStyle(
  {
	width: '715',
    height: 'auto',
	padding: '0',
	position: 'absolute',
    border: 'none',
	backgroundColor: 'transparent'
  }
);
