// függőségek
if ("undefined" == typeof AITIA) {
	alert("AITIA include missing!");
}

if ("undefined" == typeof AITIA_COOKIE) {
	alert("AITIA_COOKIE include missing!");
}

if ("undefined" == typeof AITIA_UTIL) {
	alert("AITIA_UTIL include missing!");
}

if ("undefined" == typeof AITIA_EVENT) {
	alert("AITIA_EVENT include missing!");
}

// tĂśbbszĂśrĂśs include
if ("undefined" != typeof AITIA_USER) {
	 alert("AITIA_USER multiple insert!");
}

AITIA_USER = 1;

Aitia.User = {};

Aitia.User.user = Aitia.Cookie.getCookie("lastuser");

Aitia.User.profile = langprofile;

/*
Aitia.User.profile.ST = {};

Aitia.User.profile.TREE = {};

Aitia.User.profile.TREE.cookies = {};

Aitia.User.profile.empty = 1;
*/

Aitia.User.session = langsession;

if(typeof profileURL != 'undefined') {
	Aitia.User.url = profileURL;
}

this.profileStr = '';
 
Aitia.User.setProfile = function(text) {
	try {
		if(text != '') {
			try {
				eval(unescape(text));
			} 
			catch(e) {
				;
			}
			this.profile.empty = 0;
			if(typeof this.profile.ST == "undefined") {
				this.profile.ST = {};
			}
			if(typeof this.profile.TREE == "undefined") {
				this.profile.TREE = {};
			}
			if(typeof this.profile.TREE.cookies == "undefined") {
				this.profile.TREE.cookies = {};
			}
			this.handlePanels();
			//this.handleSortableTables();
			if(typeof document.body != "undefined") {
		//		loadingIndicator(0);
			}
		}
	}
	catch(e) {
		;
	}
}

Aitia.User.getPanel = function(side) {
	var currentObject = 'empty';
	if(typeof tree != "undefined") {
		currentObject = tree.sUniqueName;
	}
	if(typeof xmlEditor != "undefined") {
		currentObject = 'xmlEditor';
	}
	var viewName = 'a';
	if(typeof Aitia.User.session != "undefined" && typeof Aitia.User.session['tree_setup_' + currentObject] != "undefined") {
		viewName = Aitia.User.session['tree_setup_' + currentObject]['selectedView'];
	}
	if(typeof this.profile.TREE == "undefined") {
		this.profile.TREE = {};
	}
	var p = 0;
	if(typeof this.profile.TREE[currentObject] != "undefined") {
		if(typeof this.profile.TREE[currentObject][viewName] != "undefined") {
			if(typeof this.profile.TREE[currentObject][viewName][side + '_panel'] != "undefined") {
				return this.profile.TREE[currentObject][viewName][side + '_panel'];
			}
		}
	}
}


Aitia.User.savePanelBoxes = function(target_body, clicked_arrow, open) {
	var str = 'panelbox##' + target_body + '##' + clicked_arrow.id;
	var currentObject = 'empty';
	var viewName = 'a';
	if(typeof tree != "undefined") {
		currentObject = tree.sUniqueName;
	}
	if(typeof xmlEditor != "undefined") {
		currentObject = 'xmlEditor';
	}	
	//fa nézete a sessionből
	if(typeof Aitia.User.session != "undefined" && typeof Aitia.User.session['tree_setup_' + currentObject] != "undefined") {
		viewName = Aitia.User.session['tree_setup_' + currentObject]['selectedView'];
	}
	Aitia.User.add2profile(this.profile, 'TREE');
	var o;
	o = Aitia.User.add2profile(this.profile.TREE, currentObject);
	o = Aitia.User.add2profile(o, viewName);
	
	if(!(o[str] && o[str] == open)) {
		o[str] = open;
	}
}

Aitia.User.loadPanelBoxes = function() {
	if(this.profile.empty  == 1) {
		this.loadFromProfile();
	}
	
	var currentObject = 'empty';
	if(typeof tree != "undefined") {
		currentObject = tree.sUniqueName;
	}
	if(typeof xmlEditor != "undefined") {
		currentObject = 'xmlEditor';
	}
	var viewName = 'a';
	if(typeof Aitia.User.session != "undefined" && typeof Aitia.User.session['tree_setup_' + currentObject] != "undefined") {
		viewName = Aitia.User.session['tree_setup_' + currentObject]['selectedView'];
	}
	if(typeof this.profile.TREE == "undefined") {
		this.profile.TREE = {};
	}
	var p = 0;
	if(typeof this.profile.TREE[currentObject] != "undefined") {
		if(typeof this.profile.TREE[currentObject][viewName] != "undefined") {
			var current = this.profile.TREE[currentObject][viewName];
			for(var i in current) {
				var tmp = i.split('##');
				if(3 == tmp.size()) {
					if(1 == current[i] && $(tmp[2])) {
						if(!$(tmp[2]).hasClassName('side_box_header_toggle_on')) {
							toggle_side_box(tmp[1], $(tmp[2]), true);
						}
					}
				}
			}
		}
	}
}

Aitia.User.setPanel = function(side, val) {
	var panel = side + '_panel';
	var currentObject = 'empty';
	var viewName = 'a';
	if(typeof tree != "undefined") {
		currentObject = tree.sUniqueName;
	}
	if(typeof xmlEditor != "undefined") {
		currentObject = 'xmlEditor';
	}	
	//fa nézete a sessionből
	if(typeof Aitia.User.session != "undefined" && typeof Aitia.User.session['tree_setup_' + currentObject] != "undefined") {
		viewName = Aitia.User.session['tree_setup_' + currentObject]['selectedView'];
	}
	Aitia.User.add2profile(this.profile, 'TREE');
	var o;
	o = Aitia.User.add2profile(this.profile.TREE, currentObject);
	o = Aitia.User.add2profile(o, viewName);
	
	if(!(o[panel] && o[panel] == val)) {
		o[panel] = val;
	}
}

Aitia.User.handleTrees = function() {
	if(this.profile.empty  == 1) {
		this.loadFromProfile();
	}
}

/**
 * panelek beĂĄllĂ­tĂĄsa
 * jelenleg fa illetve cikkszerkesztő jelenlétében működik
 *
 */
Aitia.User.handlePanels = function() {
	var lp = this.getPanel('left');
	var rp = this.getPanel('right');
	
	if(typeof lp != "undefined") {
		if(lp == 0) {
			toggle_w3('left_block', true);
		} else {
			toggle_w3('center_left', true);
		}
	}
	if(typeof rp != "undefined") {
		if(rp == 0) {
			toggle_w3('right_block', true);
		} else {
			toggle_w3('center_right', true);
		}
	}
}

Aitia.User.handleSortableTables = function() {
	if(typeof Aitia.User.profile.ST != "undefined") {
		try {
			var pst = Aitia.User.profile.ST;
			if(typeof webra_sorttables != "undefined") {
				for(s in webra_sorttables) {
					var currentST = webra_sorttables[s];
					if(typeof pst[currentST.unique_name] != "undefined") {
						var viewName = getParamValue(currentST.configParams, "viewName");
						if(typeof pst[currentST.unique_name][viewName] != "undefined") {
							var currentSTprefs = pst[currentST.unique_name][viewName];
							//rendezni is kell
							//currentST.sort(currentSTprefs.sortColumnPos, currentSTprefs.sortDir_descending, currentSTprefs.sSortType);
							
							//lĂĄthatĂł oszlopok beĂĄllĂ­tĂĄsa
							var reload = false;
							if(currentSTprefs.visibleheaders) {
								if('' != currentSTprefs.visibleheaders) {
									/*
									currentST.status_visibleHeaders = currentSTprefs.visibleheaders;
									currentST.status_removeHeader = "";
									reload = true;
									*/
								}
							}
							if(currentSTprefs.grouping) {
								// csak a csoportosĂ­tĂĄssal kell foglalkozni
								if('' != currentSTprefs.grouping) {
									//currentST.status_groupField = currentSTprefs.grouping;
									//reload = true;
								}
							}
							if(reload) {
								currentST.ST_resubmit(currentST.addURLParam(currentST.homeURL, "ctag", "reload"), "SortableTable.ST_callback_reload");
							}
							currentST.selectFirstItem();
						}
					}
				}
			}
		}
		catch(e) {}
	}
}

/**
 * létrehoz egy gyereket a szülőobjektumon belül
 *
 * @param object szülő
 * @param string gyerek azonosĂ­tĂłja
 *
 * @return object gyerek objektum
 */
Aitia.User.add2profile = function(parentO, childStr) {
	if('' == childStr) {
		childStr = 'xn';
	}
	if(typeof parentO[childStr] == "undefined") {
		parentO[childStr] = {};
	}
	return parentO[childStr];
}


Aitia.User.saveToProfile = function(name, value) {
	if(this.profile.name != value) {
		this.profile[name] = value;
		var params = setParamValue(params, "nodoctype", 1);
		params = setParamValue(params, "webra_action", 'jsprofile');
		params = setParamValue(params, "action", 'set');
		params = setParamValue(params, "name", name);
		params = setParamValue(params, "value", value);
		if(this.url) {
			addRequest(this.url, '', params);
		}
	}
}

/**
 * elkĂźldi a profil objektumot JSON formĂĄtumban
 *
 */
Aitia.User.saveToProfileAll = function() {
	if(this.profile.empty == 1) {
		this.loadFromProfile();
	}
	var s = "{}";
	try {
		s = Object.toJSON(this.profile);
	} 
	catch(e) {
		s = "{}";
	}
	var params = setParamValue(params, "nodoctype", 1);
	params = setParamValue(params, "webra_action", 'jsprofile');
	params = setParamValue(params, "action", 'saveAll');
	params = setParamValue(params, "profile", s);
	profileUrl = this.url;
	profileParams = params;
	
	Aitia.Util.timer('addRequest(profileUrl, \'\', profileParams, profileParams, \'saveToProfileAll\');', 'profilesaver', 1000);
}

Aitia.User.loadFromProfile = function() {
	try {
		if(this.url) {
			var params = setParamValue(params, "nodoctype", 1);
			params = setParamValue(params, "webra_action", 'jsprofile');
			params = setParamValue(params, "action", 'get');
			resp = processXmlHttpRequest(this.url, '', params, Array(), 0);
			this.setProfile(resp);
		}
	}
	catch(e) {
		;
	}
}

Aitia.User.deleteFromProfile = function(name) {
	var params = setParamValue(params, "nodoctype", 1);
	params = setParamValue(params, "webra_action", 'jsprofile');
	params = setParamValue(params, "name", name);
	params = setParamValue(params, "action", 'del');
	try {
		addRequest(this.url, '', params);
		delete this.profile.name;
	}
	catch(e) {}
}