// -- context class --

function context_class() {
	var self = this;

	this.collector = null;
	this.ic_collector = null;
	this.context = null;
	
	this.window_ref = window;
	this.document_ref = document;
	
	this.evt_destroy = null;
}

context_class.prototype.ifc_path = "http://perfecto-new.designplus.local/ifc.php";

context_class.prototype.construct = function(widget_id, widget_ident) {
	this.collector = new http_request_collector(this.ifc_path);
	this.ic_collector = new ic_collector(this);
}

context_class.prototype.destroy = function() {
	if (this.evt_destroy != null)
		this.evt_destroy();
}

context_class.prototype.createElement = function(input) {
	return this.document_ref.createElement(input);
}

context_class.prototype.confirm_wnd = function(message) {
	return confirm(message);
}

context_class.prototype.catch_error_flags = function(elms) {
	if (elms.length > 0) {
		for (f = 0; f < elms.length; f++) {
			switch (this.element_value(elms[f])) {
				case "unauthorized" :
					
					//this.env.inactive();
				
					break;
			}
		}
	
		return true;
	}
	
	return false;
}

context_class.prototype.request = function(data, evt_oncreate, evt_ondone, structure) {
	// http request api
	this.collector.enqueue(data, evt_oncreate, evt_ondone, structure.root);
}

context_class.prototype.layerX = function(evt, pos) {
	if (evt.layerX) return evt.layerX;
	else return evt.clientX - pos;
}

context_class.prototype.layerY = function(evt, pos) {
	if (evt.layerY) return evt.layerY;
	else return evt.clientY - pos;
}

context_class.prototype.wnd_x = function() {
	var result = 0;
	
	if (typeof(this.window_ref.innerWidth) == 'number') result = this.window_ref.innerWidth;
	else if (this.document_ref.documentElement && (this.document_ref.documentElement.clientWidth || this.document_ref.documentElement.clientHeight)) result = this.document_ref.documentElement.clientWidth;
	else if (this.document_ref.body && (this.document_ref.body.clientWidth || this.document_ref.body.clientHeight)) result = this.document_ref.body.clientWidth;

	return result;
}

context_class.prototype.wnd_y = function() {
	var result = 0;
	
	if (typeof(this.window_ref.innerWidth) == 'number') result = this.window_ref.innerHeight;
	else if (this.document_ref.documentElement && (this.document_ref.documentElement.clientWidth || this.document_ref.documentElement.clientHeight)) result = this.document_ref.documentElement.clientHeight;
	else if (this.document_ref.body && (this.document_ref.body.clientWidth || this.document_ref.body.clientHeight)) result = this.document_ref.body.clientHeight;

	return result;
}

context_class.prototype.get_scX = function() {
	var a = this.document_ref.body.scrollLeft;
	var b = this.document_ref.documentElement.scrollLeft;
	
	return a != 0 ? a : b;
}

context_class.prototype.get_scY = function() {
	var a = this.document_ref.body.scrollTop;
	var b = this.document_ref.documentElement.scrollTop;
	
	return a != 0 ? a : b;
}

context_class.prototype.get_elementX = function(ref) {
	var loop = ref;
	var out = 0;
	
	while (loop != null) {
		out += loop.offsetLeft;
		loop = loop.offsetParent;
	}
	
	return out;
}

context_class.prototype.get_elementY = function(ref) {
	var loop = ref;
	var out = 0;
	
	while (loop != null) {
		out += loop.offsetTop;
		loop = loop.offsetParent;
	}
	
	return out;
}

context_class.prototype.get_elementWidth = function(ref) {
	return ref.offsetWidth;
}

context_class.prototype.get_elementHeight = function(ref) {
	return ref.offsetHeight;
}

context_class.prototype.get_parent_index = function(ref) {
	var founded = false;
	var pos = 0;
	
	while (!founded && pos < ref.parentNode.childNodes.length)
		if (ref.parentNode.childNodes[pos] === ref) founded = true;
		else pos++;
		
	return founded ? pos : null;
}

context_class.prototype.center_by = function(target, source) {
	// vycentruje target na stred source
	var sX = this.get_elementX(source);
	var sY = this.get_elementY(source);
	var sW = this.get_elementWidth(source);
	var sH = this.get_elementHeight(source);
	
	var tW = this.get_elementWidth(target);
	var tH = this.get_elementHeight(target);
	
	var pX = sW > tW ? (sX + (sW - tW) / 2) : sX;
	var pY = sH > tH ? (sY + (sH - tH) / 2) : sY;
	
	target.style.position = "absolute";
	target.style.left = pX + "px";
	target.style.top = pY + "px";
}

context_class.prototype.element_value = function(ref) {
	return ref.firstChild == null ? '' : ref.firstChild.nodeValue;
}

context_class.prototype.element_value_null = function(ref) {
	return ref.firstChild == null ? null : ref.firstChild.nodeValue;
}

context_class.prototype.sub_element_value = function(ref, name) {
	var subs = ref.getElementsByTagName(name);
	if (subs != null && subs.length > 0) return this.element_value(subs[0]);
	return "";
}

context_class.prototype.sub_element_value_null = function(ref, name) {
	var subs = ref.getElementsByTagName(name);
	if (subs != null && subs.length > 0) return this.element_value_null(subs[0]);
	return null;
}

context_class.prototype.error = function(value) {
	// !!! ERROR PRO WEB NEBUDE
	alert("error: `" + value + "`");
}

context_class.prototype.object_create = function(ref) {
	return ref;
}

context_class.prototype.object_destroy = function(ref) {
}

context_class.prototype.path_explode = function(input) {
	var out = new Array();
	var f, ch, last = '', len = input.length;
	
	for (f = 0; f <= len; f++) {
		ch = f == len ? '/' : input.substr(f, 1);
		
		switch (ch) {
			case '/' :
				if (last != '') out[out.length] = last;
				last = '';
				break;
				
			default :
				last += ch;
				break;
		}
	}
	
	return out;
}

context_class.prototype.purge = function(d) {
	var a = d.attributes, i, l, n;
	if (a) {
		l = a.length;
		for (i = 0; i < l; i++) {
			n = a[i].name;
			if (typeof d[n] === 'function') d[n] = null;
		}
	}
}

context_class.prototype.structure_destroy = function(ref) {
	if (ref != null) {
		var stack = new Array();
		var current, f;
		stack[stack.length] = ref;
		
		while (stack.length > 0) {
			current = stack.pop();

			if (current.childNodes != null && current.childNodes.length > 0) {
				stack[stack.length] = current;
				for (f = 0; f < current.childNodes.length; f++)
					stack[stack.length] = current.childNodes[f];
				
			} else {
				this.purge(current);
				if (current.parentNode != null) current.parentNode.removeChild(current);
			}
		}
	}
}

context_class.prototype.object_destroy = function(ref) {
	for (var i in ref) {
		ref[i] = null;
	}
}

