var STARTUP_TIMEOUT = 1; // vychozi timeout pro spusteni operaci
var NORMAL_TIMEOUT = 10; // vychozi timeout pro zpracovavani dat a elementu
var ADMIN_TIMEOUT = 2000; // timeout pro zahajeni zpracovani admnistracnich funkci
var ADMIN_OPACITY = 40; // pruhlednost editacnich prvku pro administraci

var API = {
	configAnimate : null,	
	obj : null,
	zindex : 10,
	maxZ : 1000,
	title : '<p style="font-weight: bold;">API</p>',
	browser : null,
	page : null,
	state : 'start',
	container : null,
	comm : null,
	q : null,
	help : null,
	helpText : null,
	tmpFunction : null,
	mx : null,
	my : null,
	working : null,
	scrollTop : null,
	ContentBackup : null,
	list : new Array(),
	scrollList : new Array(),
	CSSlist : new Array(),
	modules : new Array(),
	modulesTarget : new Array(),
	modulesAdmin : new Array(),
	
	init : function() {
		
		API.removeElement('APLjsError'); // odstraneni hlaseni nepritomnosti JavaScriptu
		
		API.maxZ = 1000;
		
		API.browser = new API.getBrowserInfo();
		API.page = new API.getPageInfo();
		
		if (API.browser.ie) IEmarginFix.init(); // IE margin fix

		if (typeof THEME_LOAD != 'undefined') {
			setTimeout(THEME_LOAD, STARTUP_TIMEOUT);
		}

		c = API.container = new API.createDiv('APIContainer', -15, 6, 0, 0, API.zindex++);
		o = API.obj = new API.createDiv('APIController', -1, 0, 0, 0, API.zindex++);
		q = API.q = new API.createDiv('APIQueue', -1, 0, 0, 0, API.zindex++);
		o.style.backgroundColor = c.style.backgroundColor = q.style.backgroundColor = 'white';
		q.style.position = 'relative';
		o.style.border = c.style.border = '1px solid black';
		o.style.padding = '2px 5px 2px 5px';
		c.style.padding = '2px 25px 2px 5px';
		o.style.fontSize = c.style.fontSize = '7pt';
		o.style.fontFamily = c.style.fontFamily = 'Verdana';
		if (!API.browser.ie5) o.style.cursor = 'pointer';
		c.style.display = 'none';
		o.innerHTML = API.title;
		
		if (APL_USER_ROLE_ID < 100) o.style.display = 'none'; // pouze developer vidi okno
		
		if (APL_USER_ROLE_ID >= 90 && (!API.browser.ie || !API.browser.ie7))  setTimeout('APIAdmin.init()', ADMIN_TIMEOUT);
		
		document.body.appendChild(o);
		document.body.appendChild(c);
		
		h = API.help = new API.createDiv('APLHelp', 0, 0, 0, 0, API.maxZ + 5);
		h.style.display = 'none';
		
		document.body.appendChild(h);
		
		API.obj.onclick = API.restore;
		API.obj.onmouseover = API.mouseover;
		API.obj.onmouseout = API.mouseout;
		API.container.onmouseover = API.mouseover;
		API.container.onmouseout = API.mouseout;
		
		API.checkContent(); // kontrola obsahu a preneseni debug informaci
		API.initComm(); // inicializace komunikace se serverem
		API.tick(500); // zapnuti casove smycky API, 500msec
		cHTML.init(); // inicializace cHTML tridy
		document.onmousemove = API.TrackMouse; // zaveseni udalosti na pohyb mysi
		window.onscroll = API.Scroll;
		window.onresize = API.resize;
		
		if (getCookie('APIstate') == 'max') setTimeout('API.restore()', STARTUP_TIMEOUT); // prip. otevreni info okna pro developera
		
//		API.findCSSRule('select');

//		setInterval(function() {$$('.menubar').each(function(node){API.appendComm(node.offsetHeight + '<br/>');});}, 1000);
//		setInterval(function() {API.showComm('');}, 5000);
		

		var rules = {
			'a' : function(element){
				if (typeof element.onmouseover != 'function') {
					element.onmouseover = function(){
						if ((element.getAttribute('title')!='') && (!API.browser.ie)) {
							_title = element.getAttribute('title');
							element.setAttribute('title', '');
							element.setAttribute('_title', _title);
							API.showHelp(_title);
							return false;
						} else {
						    var els = element.getElementsByTagName("img");
						    var divs = element.getElementsByTagName("div");
							if (Element.hasClassName(element, 'nohover')) {
								void(0);
							} else if (els.length == 0 && divs.length == 0) {
								API.showHelp(this.innerHTML);
						    } else if (els.length == 0 && divs.length > 0) {
								API.showHelp(divs[0].innerHTML);
						    } else {
	//							API.showHelp();
						    }
						}
					}
				};
				if (typeof element.onmouseout != 'function') {
					element.onmouseout = function(){
						if ((element.getAttribute('_title')!='') && (!API.browser.ie)) {
							element.setAttribute('title', element.getAttribute('_title'));
						}
						h = API.help;
						API.showHelp();
						h.style.backgroundColor = '#c0c0c0';
					}
				};
				if (element.innerHTML.toLowerCase() == 'x') {
					element.innerHTML = '<img src="' + APL_ROOT + '/themes/icons/close.jpg"/>';
				};
			},
			'form img' : function(element){
				element.onmouseover = function(){
					void(0);					
				}
			},
			'form a' : function(element){
				element.onmouseover = function(){
					void(0);					
				}
			},
			'.nohover a' : function(element){
				element.onmouseover = function(){
					void(0);					
				}
			},
			'#APLLayout a' : function(element){
				if (typeof element.onclick != 'function') {
					element.onclick = function(){
						if (element.getAttribute('target') != '_blank' && element.getAttribute('href').indexOf('mailto') == -1) cHTML.show();
					};
				}
			},
			'#APLLayout img' : function(element){
				if (typeof element.onmouseover != 'function') {
					element.onmouseover = function(){
						_hover = '';
						if (element.getAttribute('alt') != '') _hover = element.getAttribute('alt');
						if (element.getAttribute('title') != '') {
							_hover = element.getAttribute('title');
							element.setAttribute('title', '');
							element.setAttribute('origtitle', _hover);
						}
						if (element.getAttribute('origtitle') != '') _hover = element.getAttribute('origtitle');
						API.showHelp(_hover);
					};
					element.onmouseout = function(){
						if (element.getAttribute('origtitle') != '') {
							element.setAttribute('title', element.getAttribute('origtitle'));
						}
						API.showHelp();
					};
				}
			}
		};
		
		Behaviour.register(rules);
		
		if (RULES) Behaviour.register(RULES);
		
		setTimeout('Behaviour.apply()', NORMAL_TIMEOUT);
		
		if (USERNAME == 'fumia') {
//			setTimeout(function () {Developer.upload();}, 1000)
		}
		
		
//		if (APL_USER_ROLE_ID == 100) setTimeout(function () {API.settingsModule('novinky');}, 3000);
		if (APL_USER_ROLE_ID >= 90) setTimeout(function () {API.getAdminFunction('DisplayAs', 'APIAdminControllerDisplayAsP', 'user_role_id=' + APL_USER_ROLE_ID);}, ADMIN_TIMEOUT);
//		if (APL_USER_ROLE_ID == 100) setTimeout(function () {API.getAdminFunction('DevelDuplicates', 'APLLayout', API.modules.serialize() + 'user_role_id=' + APL_USER_ROLE_ID);}, 3000);

		if (APL_USER_ROLE_ID == 100) {
		
			js = new cHTML.createElement('script', 'APIAdminController_script', 'absolute', -5, 18, 0, 0, 9999);
			js.setAttribute('src', APL_ROOT + '/js/Developer.js');
			document.body.appendChild(js);
			
		}
		
		if (APL_USER_ROLE_ID >= 90) {
/*		
			js = new cHTML.createElement('script', 'APIAdminscriptaculous_script', 'absolute', -5, 18, 0, 0, API.zindex++);
			js.setAttribute('src', APL_ROOT + '/js/scriptaculous/scriptaculous.js');
			document.body.appendChild(js);
*/			
		}
		
	},
	
	findCSSRule : function(selector) {

		if (document.styleSheets[0].rules) {
			
			API.showHelp(document.styleSheets[0].rules[0].selectorText);
			
		} else if(document.styleSheets[0].cssRules) {

//			document.styleSheets[0].cssRules[0].selectorText;
			found = new Array;
			for (i = 0; i < document.styleSheets.length; i++) {

				if (document.styleSheets[i].cssRules.length > 0) for (j = 0; j < document.styleSheets[i].cssRules.length; j++) {
				
					t = document.styleSheets[i].cssRules[j].selectorText;
					
					if (t.indexOf(selector) >= 0) {
						found.push(document.styleSheets[i].cssRules[j].cssText);
					}
					
				}
				
			}

			API.showHelp(found.serializeC());
			
		}
		
	},
	
	showHelp : function(text) {
		
		h = API.help;
		dx = 10;
		dy = 10;
		
		if (text == 0 || !text || typeof text == 'object' || text == 'null' || text == null) {
			API.helpText = null;
			h.style.display = 'none';	
		} else {
			h.style.display = 'block';	
			
			h.innerHTML = '<p>' + text + '</p>';
			
			nmx = API.mx + dx;
			nmy = API.my + dy;
			nw = h.offsetWidth;
			nh = h.offsetHeight;
			
			if (nmx + nw >= API.page.x2 - 5) {
				nmx = API.mx - dx - nw;
			}
			if (nmy + nh >= API.page.y2 - 5) {
				nmy = API.my - dy - nh;
			}
			
			if (API.mx != null && API.my != null) {
				h.style.left = nmx + 'px';
				h.style.top = nmy + 'px';
			}
			
			API.helpText = text;

		}
		
	},
	
	Scroll : function() {
		
		API.scrollTop = (API.browser.ns6) ? window.pageYOffset : document.body.scrollTop;
		
		c = API.container;
		c.style.top = API.scrollTop + 6 + 'px';
		API.obj.style.top = API.scrollTop + 'px';
		
		if (API.scrollList.length > 0) for (i = 0; i < API.scrollList.length; i++) { // zavolani vsech funkci pro repozice elementu
			setTimeout(API.scrollList[i] + '()', NORMAL_TIMEOUT + (10 * i));
		}

		
	},

	TrackMouse : function(evt) {

		API.mx = (API.browser.ns6) ? evt.pageX:event.clientX + document.body.scrollLeft;
		API.my = (API.browser.ns6) ? evt.pageY:event.clientY + document.body.scrollTop;
		
	},
	
	initComm : function() {
		
			API.container.appendChild(API.q);
			
			c = API.comm = new API.createDiv('APLComm', 0, 0, 0, 0, API.zindex++);
			c.style.position = 'relative';
			API.container.appendChild(c);
			c.title = '<b>Communication : </b><br/>';
			
			c.innerHTML = c.title;
			
	},
	
	showComm : function(text) {

		c = API.comm;
		
		c.innerHTML = c.title + text;
		
	},
	
	appendComm : function(text) {
		
		API.comm.innerHTML += text;
		
	},
	
	sendForm : function(id, success, target, resize) {
		
//		alert(Form.serialize(id));

		API.showHelp();
		cHTML.show();
//		API.working = true;

		if (!target) target = id.parentNode.getAttribute('id');

		if (target.indexOf('|') != -1) {
			
			ar = target.split('|');
			
			for (i = 0; i < ar.length; i++) {

				if (document.getElementById(ar[i])) {
					target = ar[i];
				}
				
			}
			
		}
		
		if (resize) {
			$(target).style.overflow = 'auto';
			$(target).style.maxHeight = $(target).parentNode.offsetHeight + 'px'
		}

		API.appendComm(target + '<br/>');

		pars = Form.serialize(id) + '&SendForm=Y';
		
		url = id.parentNode.getAttribute('ElementURL');
		
		if (url == '') {
			
			url = id.getAttribute('action');
//			alert(url);
			
		} else {
		
			url = APL_ROOT + '/mod' + url;
		
		}
		
		Element.update(target, '');

		API.appendComm(url + '<br/>');
		
		API.throbber(target);
		
		if (!success) {
			var myAjax = new Ajax.Updater(
							{success: target}, 
							url, 
							{
								method: 'post',
								evalScripts : true,
								parameters: pars,
								asynchronous : true,
								onSuccess: API.reportModuleSuccess
							});
		} else {
			var myAjax = new Ajax.Updater(
							{success: target}, 
							url, 
							{
								method: 'post',
								evalScripts : true,
								parameters: pars,
								asynchronous : true
							});
		}		
		return false;
		
	},
	
	reloadModule : function (name) {
		
		if (!name) return;
		
		cHTML.show();
		
		if (API.modules.length > 0) for (i = 0; i < API.modules.length; i++) {
			
			if (name == API.modules[i]) API.queue("API.LoadModule('/" + API.modules[i] + "/index/', '" + API.modulesTarget[i] + "');");
			
		}
		
	},
	
	settingsModule : function (name) {
		
		if (!name) return;
		
		cHTML.hide();
		
		var url = APL_ROOT + '/mod/' + name + '/settings.html';
		var raw_css_url = APL_ROOT + '/themes/' + THEME_NAME + '/' + name + '/settings.css';

		css_url = '<link href="' + raw_css_url + '" rel="stylesheet" type="text/css"/>';
		
		API.obj.innerHTML += css_url; 
		
		API.CSSlist.push(css_url);

		API.appendComm(raw_css_url + ' - <b>ok</b><br/>');
		
		API.appendComm(url + '<br/>');
		
		url += '?switch=settings';

		cHTML.show();
		cHTML.active = 'MsgBoxSettinsModule';
		bl = 40;
		bt = 20;
		w = API.page.x2 - ((API.browser.ie && !API.browser.ie7)? 0 : 20) - (2 * bl);
		h = API.page.y2 - 20 - (2 * bt);
		o = new cHTML.createElement('div', cHTML.active, 'absolute', API.page.x50, API.page.y50, 1, 1, 999999);
		API.maxZ=999999;
		o  = API.stylize(o);
		o.style.overflow = 'auto';
		o.style.MozBorderRadius = '5px';
		document.body.appendChild(o);
		o.innerHTML = '<p id="APIAdminSettinsModule" style="padding: 5px;">&nbsp;</p>';

		new Animation.EaseIn(cHTML.active, bl, bt, w, h);

		c = new cHTML.createElement('div', cHTML.active + '_close', 'absolute', w - 25, 2, 10, 10, API.maxZ + 1);
		o.appendChild(c);
		c.innerHTML = '<a href="javascript:void(0);" onclick="cHTML.hide(true);API.reloadModule(\''+ name + '\');">x</a>';
		
		API.throbber('APIAdminSettinsModule');

		setTimeout('Behaviour.apply()', NORMAL_TIMEOUT);
		
		var myAjax = new Ajax.Updater(
						{success: 'APIAdminSettinsModule'}, 
						url, 
						{
							method: 'post',
							evalScripts : true,
							asynchronous : true
						});
		
		return false;
		
	},
	
	getModuleTarget : function (name) {
		
		if (!name) return;
		
		cHTML.show();
		
		if (API.modules.length > 0) for (i = 0; i < API.modules.length; i++) {
			
			if (name == API.modules[i]) return API.modulesTarget[i];
			
		}
		
	},
	
	LoadURL : function (url, target, throbber, success) {
		
		if (!target) return;
		if (!url) return;
		
		if (target.indexOf('|') != -1) {
			
			ar = target.split('|');
			
			for (i = 0; i < ar.length; i++) {

				if (document.getElementById(ar[i])) {
					target = ar[i];
				}
				
			}
			
		}
		
		while(url.indexOf('&amp;')>0){
			url=url.replace("&amp;", "&");
		}

		if (!throbber) Element.update(target, '<p class="aCenter" style="margin-top: 20px;"><img alt="" src="' + APL_ROOT + '/themes/images/throbber.gif"/></p>');
		
		API.appendComm(url + '<br/>');

		if (!success) {
			
			var myAjax = new Ajax.Updater(
							{success: target}, 
							url, 
							{
								method: 'get',
								evalScripts : true,
								asynchronous : true
							});
		} else {
			
			var myAjax = new Ajax.Updater(
							{success: target}, 
							url, 
							{
								method: 'get',
								evalScripts : true,
								asynchronous : true,
								onSuccess: API.reportModuleSuccess
							});
		}
						
	},
	
	LoadModule : function (url, target, throbber) {

		if (!target) return;
		
		if (!API.comm) {
			
			API.queue("API.LoadModule('" + url + "', '" + target + "');");
			return;
			
		}
		API.working = true;
		
		module = url.substr(url.indexOf('/'), url.lastIndexOf('/'));
		module = module.substr(module.indexOf('/') + 1, module.lastIndexOf('/') - 1);
		
		found = 'no';
		if (API.modules.length > 0) for (i = 0; i < API.modules.length; i++) { // zavolani vsech funkci pro repozice elementu || API.working
			if (API.modules[i] == module) {
				found = 'yes';
			}
		}
		if (found == 'no') {
				API.modules.push(module);
				API.modulesTarget.push(target);
		}
		
//		API.appendComm(API.modules.serializeC() + '<br/>');
//		alert(APL_ROOT);
		var css_url = APL_ROOT + '/themes/' + THEME_NAME + url;

		css_url = css_url.substr(0, css_url.lastIndexOf('/'));
		css_url = css_url.substr(0, css_url.lastIndexOf('/'));
		
		raw_css_url = css_url + '.css';
		
		css_url = '<link href="' + raw_css_url + '" rel="stylesheet" type="text/css"/>';
		
		$(target).setAttribute('ElementURL', url);
		
		var url = APL_ROOT + '/mod' + url;
		
		if (module.indexOf('vlajky') != -1) throbber = 'no';
		if (module.indexOf('menu') != -1) throbber = 'no';
		if (module.indexOf('google\/vstup') != -1) throbber = 'no';
		if (module.indexOf('menu\/panacci') != -1) throbber = 'no';
		if (module.indexOf('panacci') != -1) throbber = 'no';
		
		if (!throbber) Element.update(target, '<p class="aCenter" style="margin-top: 20px;"><img alt="" src="' + APL_ROOT + '/themes/images/throbber.gif"/></p>');
		
		API.obj.innerHTML += css_url; 
		
		API.CSSlist.push(css_url);

		API.appendComm(raw_css_url + ' - <b>ok</b><br/>');
		
		API.appendComm(url + '<br/>');
		
		var myAjax = new Ajax.Updater(
						{success: target}, 
						url, 
						{
							method: 'get',
							evalScripts : true,
							asynchronous : true,
							onSuccess: API.reportModuleSuccess
						});
//							parameters: pars,
	
	},	
	
	throbber : function(target) {
		
		if (!target) return;
		
		if (document.getElementById(target)) Element.update(target, '<p class="aCenter" style="margin-top: 20px;"><img alt="" src="' + APL_ROOT + '/themes/images/throbber.gif"/></p>');
		
	},
	
	reportModuleSuccess : function(request) {
		
//		API.appendComm(' - <b>ok</b><br/>');
		setTimeout('API.checkContent()', NORMAL_TIMEOUT);
		API.working = null;
		cHTML.hide();
		setTimeout('Behaviour.apply()', NORMAL_TIMEOUT);
		
	},
	
	getPhrase : function (phrase_id, target, module_id, success) {
		
		if (!target || !phrase_id) return;
		
		if (!API.comm) {
			
			if (!module_id) {
				API.queue("API.getPhrase('" + phrase_id + "', '" + target + "');");
			} else {
				API.queue("API.getPhrase('" + phrase_id + "', '" + target + "', '" + module_id + "');");
			}
			return;
			
		}
		
		var url = APL_ROOT + '/phrase/' + phrase_id + '/';

		if (module_id) url += '?module_id=' + module_id;
		
		if (!success) {
			var myAjax = new Ajax.Updater(
							{success: target}, 
							url, 
							{
								method: 'get',
								evalScripts : true,
								asynchronous : true/*,
								onSuccess: Behaviour.apply*/
							});
		} else {
			var myAjax = new Ajax.Updater(
							{success: target}, 
							url, 
							{
								method: 'get',
								evalScripts : true,
								asynchronous : true,
								onSuccess: success
							});
		}
		
	},
	
	getAdminFunction : function (module, target, pars) {
		
		if (!pars) {pars = 't=1234567890';}
		
		if (!target || !module || !pars) return;
		
		if (!API.comm) {
			
			API.queue("API.getAdminFunction('" + module + "', '" + target + "', '" + pars + "');");
			return;
			
		}
		
		pars += '&module=admin&func=' + module + '&APL_OUTPUT=admin';
		
		var url = APL_ROOT + '/modules/admin/' + module + '.php';
		
//		API.appendComm(url + "?" + pars + "<br/>");
				
//		API.q.innerHTML += '<b>admin : </b>' + module + '<br/>';

		Behaviour.apply();

		var myAjax = new Ajax.Updater(
						{success: target}, 
						url, 
						{
							method: 'get',
							parameters: pars,
							evalScripts : true,
							asynchronous : true,
							onSuccess: Behaviour.apply
						});
		
		
	},
	
	queue : function(str) {
		
		if (!API.tmpFunction) {
			API.tmpFunction = str;
		} else {
			if (API.tmpFunction.indexOf(str) == -1) API.tmpFunction += str;
		}
		
	},
	
	resize : function() {

		API.page = new API.getPageInfo(); // reinicializace objektu stranky
		
		API.checkContent(); // zpracovani prip. informaci v obsahu stranky
		
		if (API.list.length > 0) for (i = 0; i < API.list.length; i++) { // zavolani vsech funkci pro repozice elementu
			setTimeout(API.list[i] + '()', NORMAL_TIMEOUT + (10 * i));
		}

	},
	
	unload : function() {
		cHTML.show();
	},
	
	restore : function() {

		if (DebugResize && API.state == 'start') { //
			setTimeout('DebugResize()', STARTUP_TIMEOUT);
			API.mouseout();
		}
		
		API.obj.onmouseover = null;
		if (API.state == 'max') {
			
			API.state = 'min';
			a = new Animator({
				duration: Math.round(ADMIN_TIMEOUT / 12),
				interval: Math.round(ADMIN_TIMEOUT / 150),
				transition: Animator.easeOut,
				onComplete: function() {
					API.container.style.display = 'none';
					API.obj.onmouseover = API.mouseover;
				}
			});
			a.addSubject(new NumericalStyleSubject(API.container, 'opacity', 1, 0.1));
			a.play();

		} else {
			
			API.state = 'max';
			API.container.style.display = 'block';
			a = new Animator({
				duration: Math.round(ADMIN_TIMEOUT / 12),
				interval: Math.round(ADMIN_TIMEOUT / 200),
				transition: Animator.easeOut,
				onComplete: function() {
					API.obj.onmouseover = API.mouseover;
				}
			});
			a.addSubject(new NumericalStyleSubject(API.container, 'opacity', 0.1, 1));
			a.play();
			
		}
		
		setCookie('APIstate', API.state);
		
	},
	
	tick : function(next) {
		
//		API.showHelp(API.list.length);
		
		if (document.getElementById('MouseCoordinates')) {
			
			if (API.mx != null && API.my != null) $('MouseCoordinates').innerHTML = '<br/>' + API.scrollTop + '<br/>' + API.mx + ' : ' + API.my;
			
		}
		
		if (API.tmpFunction) {
			
			tmp = API.tmpFunction;
			
			exec = tmp.substr(0, tmp.indexOf(';') + 1);
			que = tmp.substr(tmp.indexOf(';') + 1, tmp.length);
				
			eval(exec);
			API.q.innerHTML += '<b>executed : </b>' + exec + '<br/>';
			API.tmpFunction = que;
			if (que == '') API.tmpFunction = null;
			
		}
		
		if (API.helpText) {
			API.showHelp(API.helpText);
		}
		
		setTimeout('API.tick()', next);
		
	},
	
	mouseover : function() {
		
		API.setOpacity(API.obj.id, 99);
		API.setOpacity(API.container.id, 99);
		
	},
	
	mouseout : function() {
		
		API.setOpacity(API.obj.id, 10);
		API.setOpacity(API.container.id, 70);
		
	},
	
	checkContent : function() {
		
//		if (document.getElementById('APLContentBox')) $('APLContentBox').style.height = API.page.y2 + 'px';
		
		if (document.getElementById('APLDebug')) {
			if (document.getElementById('ContainerDebug')) {
				c = $('ContainerDebug');
			} else {
				c = new API.createDiv('ContainerDebug', 0, 0, 0, 0, API.zindex++);
				c.style.position = 'relative';
				API.container.appendChild(c);
	
			}

			c.innerHTML += $('APLDebug').innerHTML;
			
			Element.remove('APLDebug');
			
			setTimeout('API.checkModulesSettings()', ADMIN_TIMEOUT);
		
		}
		
	},
	
	checkModulesSettings : function () {
		
		if (API.modulesAdmin.length > 0) for (i = 0; i < API.modulesAdmin.length; i++) {
			
			n = API.modulesAdmin[i];
			
			x = getElementsByClassName('moduleSettings_' + n);
			
			if (x.length > 0) for (j = 0; j < x.length; j++) {
				
				$(x[j].getAttribute('id')).style.visibility = 'visible';
				
//				API.appendComm(x[j].getAttribute('id'));
				
			}

			
		}
		
	},
	
	setOpacity : function(id, opac) {
		
		if (!API.browser.ie) {
			$(id).style.opacity = opac / 100;	
			
		} else {
			$(id).style.filter = 'alpha(opacity='+opac+')';
			
		}

	},
	
	setObjOpacity : function(obj, opac) {
		
		if (!API.browser.ie) {
		
			obj.style.opacity = opac / 100;	
			
		} else {
			obj.style.filter = 'alpha(opacity='+opac+')';
			
		}
	},
	
	removeElement: function(id) {
		
		if (document.getElementById(id)) {
			Element.remove(id);
		}
		
	},
	
	createDiv : function(id, x, y, w, h, zindex) {

		API.removeElement(id);
		
		el = document.createElement('div');
		el.id = id;
		el.setAttribute('id', id);
		el.style.position = 'absolute';
		if (x < 0) {
			el.style.right = (0 - x) + 'px';	
		} else {
			el.style.left = x + 'px';
		}
		if (y < 0) {
			el.style.bottom = (0 - y) + 'px';	
		} else {
			el.style.top = y + 'px';
		}
		if (w == 0) {
			el.style.width = 'auto';
		} else {
			el.style.width = w + 'px';
		}
		if (h == 0) {
			el.style.height = 'auto';
		} else {
			el.style.height = h + 'px';
		}
		el.style.visibility = 'visible';
		el.style.display = 'block';
//		el.style.color = 'black';
//		el.style.padding = '0px';
//		el.style.margin = '0px';
		el.style.zIndex = zindex;
		
		return el;
		
	},
	
	stylize : function(o) {

		o.style.backgroundColor = 'white';
		o.style.border = '1px solid black';
		o.style.padding = '0px';
		o.style.margin = '0px';
		o.style.fontSize = '7pt';
		o.style.fontFamily = 'Verdana';
		o.style.cursor = 'pointer';
		o.style.overflow = 'hidden';
		
		return o;
		
	},
	
	getBrowserInfo : function() {
		
		this.ver = navigator.appVersion;
		this.agent = navigator.userAgent;
		this.dom = document.getElementById ? 1 : 0;
		this.opera5 = this.agent.indexOf("Opera 5") > -1;
		this.opera = this.agent.indexOf("Opera") > -1;
		this.ie5 = (this.ver.indexOf("MSIE 5") > -1 && this.dom && !this.opera5) ? 1 : 0;
		this.ie6 = (this.ver.indexOf("MSIE 6") > -1 && this.dom && !this.opera5) ? 1 : 0;
		this.ie7 = (this.ver.indexOf("MSIE 7") > -1 && this.dom && !this.opera5) ? 1 : 0;
		this.ie8 = (this.ver.indexOf("MSIE 8") > -1 && !this.opera5) ? 1 : 0;
		this.ie4 = (document.all && !this.dom && !this.opera5) ? 1 : 0;
		this.ie = this.ie5 || this.ie6 || this.ie7 || this.ie8;
		this.mac = this.agent.indexOf("Mac") > -1;
		this.ns6 = (this.dom && parseInt(this.ver) >= 5) ? 1 : 0;
		this.ns4 = (document.layers && !this.dom) ? 1 : 0;
		this.bw = (this.ie7 || this.ie6 || this.ie5 || this.ie4 || this.ns4 || this.ns6 || this.opera5);
		return this;
				
	},
	
	getPageInfo : function() {

		this.x1 = 0;this.y1 = 0;
		this.x2 = (API.browser.ie7)?document.documentElement.clientWidth:(API.browser.ns6) ? innerWidth : document.body.clientWidth;
		this.y2 = (API.browser.ie7)?document.documentElement.clientHeight:(API.browser.ns6) ? innerHeight : document.body.clientHeight;
		this.x50 = Math.round(this.x2 / 2);
		this.y50 = Math.round(this.y2 / 2);		
		
	},
	
	ParseInt : function(s) {

		var ret = parseInt(s);
  		return (isNaN(ret) ? 0 : ret);
  		
	}
	
	
};

var APIAdmin = {
	
	obj : null,
	started : false,
	w : 100,
	h : 18,
	
	init : function () {
		
		if (API.browser.ie && !API.browser.ie7) APIAdmin.h = 18;
		
		APIAdmin.state = 'min';
		APIAdminstate = 'min';
		APIAdmin.started = false;
		o = APIAdmin.obj = new API.createDiv('APIAdminController', 0, 0, APIAdmin.w, APIAdmin.h, 9999);
		o.style.backgroundColor = 'white';
		o.style.border = '1px solid black';
		o.style.padding = '0px';
		o.style.margin = '0px';
		o.style.fontSize = '7pt';
		o.style.fontFamily = 'Verdana';
		o.style.cursor = 'pointer';
		o.style.overflow = 'hidden';
		document.body.appendChild(o);
				
		cls = new cHTML.createElement('a', 'APIAdminController_close', 'absolute', -5, 18, 0, 0, 10000);
		cls.style.backgroundColor = 'white';
		cls.style.fontWeight = 'bold';
		o.appendChild(cls);
		cls.setAttribute('href', 'javascript:APIAdmin._mouseout();');
		cls.innerHTML = 'X';

		o.innerHTML += '<div id="APIAdminControllerPhraseP" class="aCenter" style="font-size: 9pt; margin: 2px; background: navy; color: white; width: auto; font-weight: bold;">&nbsp;</div>';
		
		o.innerHTML += '<a id="APIAdminControllerEditingP" style="padding-left: 5px;" onclick="APIAdmin.switchEditing();">&nbsp;</a>';
		o.innerHTML += ' |<a id="APIAdminControllerEmailA" style="padding-left: 5px;" onclick="setTimeout(function(){APIAdmin._mouseout();}, 100);if (API.ContentBackup == null) {API.ContentBackup = $(\'APLLayout\').innerHTML;API.throbber(\'APLLayout\');API.getAdminFunction(\'GetEmails\', \'APLLayout\', \'user_role_id=\'+ APL_USER_ROLE_ID);} else {$(\'APLLayout\').innerHTML = API.ContentBackup;API.ContentBackup = null;};">&nbsp;</a>';
		o.innerHTML += ' |<a id="APIAdminControllerPagesA" style="padding-left: 5px;" onclick="setTimeout(function(){APIAdmin._mouseout();}, 100);if (API.ContentBackup == null) {API.ContentBackup = $(\'APLLayout\').innerHTML;API.throbber(\'APLLayout\');API.getAdminFunction(\'GetPages\', \'APLLayout\', \'user_role_id=\' + APL_USER_ROLE_ID);} else {$(\'APLLayout\').innerHTML = API.ContentBackup;API.ContentBackup = null;};">&nbsp;</a>';
		
		if (APL_USER_ROLE_ID == 100) {
			o.innerHTML += '<div id="APIAdminDeveloperDiv"><p>';
			o.innerHTML += '<span id="APIAdminDeveloperDuplicatePhrasesP" style="padding-left: 5px;">&nbsp;</span><span id="APIAdminDeveloperDuplicatePhrasesRes">&nbsp;</span>';
			o.innerHTML += '</p></div>';
		} else {
			o.innerHTML += '<p>&nbsp;</p>';
		}
		
		o.innerHTML += '<div id="APIAdminControllerDisplayAsP" style="padding-left: 5px;">&nbsp;</div>';
		o.innerHTML += '<div id="APIAdminControllerTitleInputP" style="padding-left: 5px;">&nbsp;</div>';
		o.innerHTML += '<div id="APIAdminControllerDescriptionP" style="padding-left: 5px;">&nbsp;</div>';
		o.innerHTML += '<div id="APIAdminControllerKeywordsP" style="padding-left: 5px;">&nbsp;</div>';
		o.innerHTML += '<div id="APIAdminControllerPageLayoutP" style="padding-left: 5px;">&nbsp;</div>';
		o.innerHTML += '<div id="APIAdminControllerPhrasesP" style="padding-left: 5px;">&nbsp;</div>';

		API.setObjOpacity(o, 20);
		API.getPhrase(7, 'APIAdminControllerPhraseP', 2);
		API.getPhrase(4, 'APIAdminControllerPagesA', 2);
		API.getPhrase(14, 'APIAdminControllerEmailA', 2);

		APIAdmin.checkEditing();
		
		API.list.push('APIAdmin.resize'); // registrace resize funkce
		API.scrollList.push('APIAdmin.scroll'); // registrace resize funkce
		
		APIAdmin.obj.setAttribute('onclick', 'APIAdmin._mouseover();');
		
	},

	switchEditing : function() {
		
		if (getCookie('SOBEditing') != 'yes') {
			setCookie('SOBEditing', 'yes');
		} else {
			setCookie('SOBEditing', 'no');
		}
		APIAdmin.checkEditing();
		
	},
	
	checkEditing : function() {
		
		if (getCookie('SOBEditing') != 'yes') {
		
			API.getPhrase(9, 'APIAdminControllerEditingP', 2);
			setTimeout('APIAdmin.iconsHide()', NORMAL_TIMEOUT);
			
		} else {
			
			API.getPhrase(10, 'APIAdminControllerEditingP', 2);
			setTimeout('APIAdmin.iconsShow()', NORMAL_TIMEOUT);
			
		}
		
		API.q.innerHTML += 'editing...' + getCookie('SOBEditing') + '<br/>';
		
	},
	
	iconsHide : function () {
		
		icons = getElementsByClassName('AdminIcon');
		
		if (icons.length > 0) for (i = 0; i < icons.length; i++) {
			
			icons[i].style.display = 'none';
			
		}
		
		
	},
	
	iconsShow : function () {
		
		icons = getElementsByClassName('AdminIcon');
		
		if (icons.length > 0) for (i = 0; i < icons.length; i++) {
			
			icons[i].style.display = 'block';
			
		}
		
		
	},
	
	start : function() {
		API.removeElement('APIAdminChangePass_script');
		js = new cHTML.createElement('script', 'APIAdminChangePass_script', 'absolute', -5, 18, 0, 0, API.zindex++);
		js.setAttribute('src', APL_ROOT + '/js/modules/admin/ChangePass.js');
		document.body.appendChild(js);
		API.getAdminFunction('DisplayAs', 'APIAdminControllerDisplayAsP', 'user_role_id=' + APL_USER_ROLE_ID);
		API.getAdminFunction('EditPageTitleInput', 'APIAdminControllerTitleInputP', 'user_role_id=' + APL_USER_ROLE_ID);
		API.getAdminFunction('EditPageDescription', 'APIAdminControllerDescriptionP', 'user_role_id=' + APL_USER_ROLE_ID);
		API.getAdminFunction('EditPageKeywords', 'APIAdminControllerKeywordsP', 'user_role_id=' + APL_USER_ROLE_ID);
		API.getAdminFunction('EditPageLayout', 'APIAdminControllerPageLayoutP', 'user_role_id=' + APL_USER_ROLE_ID);
		API.getAdminFunction('EditPhrases', 'APIAdminControllerPhrasesP', API.modules.serialize() + 'user_role_id=' + APL_USER_ROLE_ID);
		if (APL_USER_ROLE_ID == 100) {
			setTimeout( function () {API.getPhrase(101, 'APIAdminDeveloperDuplicatePhrasesP', 2);}, NORMAL_TIMEOUT);
			setTimeout( function () {API.getAdminFunction('DevelDuplicates', 'APIAdminDeveloperDuplicatePhrasesRes', API.modules.serialize() + 'user_role_id=' + APL_USER_ROLE_ID);}, NORMAL_TIMEOUT);
		}
	},
	
	AdminEditPhrase : function(reference, module, id, text, desc) {

//		API.appendComm(reference + module + id + text + '<br/>');
		
		c = cHTML.bckg;
		cHTML.active = 'AdminEditPhraseDiv';
		cHTML.show();

		button1 = 'OK';
		button2 = 'Storno';
		
		box = new cHTML.createElement('div', cHTML.active, 'absolute', API.page.x50, API.scrollTop + API.page.y50, 1, 1, API.maxZ + 1);
		box.style.cursor = 'default';
		box.style.padding = '5px 10px';
		document.body.appendChild(box);
		new Animation.EaseIn(cHTML.active, API.page.x50 - (cHTML.w / 2), API.page.y50 - (cHTML.h / 2), cHTML.w, cHTML.h);

		box.innerHTML = '<div id="AdminEditPhraseDivPromptText" class="aCenter" style="color: black;">.</div><p class="aCenter"><input type="text" id="AdminEditPhraseDivPromptValue" style="width: ' + (cHTML.w - 30) + 'px;" max_length="255" value="' + text + '"/><textarea id="AdminEditPhraseDivPromptDescription" max_length="2000" style="width: ' + (cHTML.w - 30) + 'px;" rows="1">' + desc + '</textarea></p>';

		_onclick = "APIAdmin.AdminEditPhraseSave('" + module + "', '" + id + "');";
		_onclick2 = "cHTML.hide(true);";
		
		reference.setAttribute('id', 'AdminEditPhraseCurrent');
		
		butt = new cHTML.createElement('p', 'AdminEditPhraseDivbutt', 'absolute', 0, 0, cHTML.w, cHTML.h, API.maxZ + 2);
		butt.innerHTML = '<img src="' + APL_ROOT + '/button/' + button1 + '/2/" style="cursor: pointer" onclick="' + _onclick + '"/>';
		butt.innerHTML += '<img src="' + APL_ROOT + '/themes/images/empty.gif" width="20" height="0"/>';
		butt.innerHTML += '<img src="' + APL_ROOT + '/button/' + button2 + '/1/" style="cursor: pointer" onclick="' + _onclick2 + '"/>';
		butt.style.textAlign = 'center';
		butt.style.top = (cHTML.h - ((API.browser.ie)?25:15)) + 'px';

		box.appendChild(butt);

		
		API.queue("API.getPhrase(6, 'AdminEditPhraseDivPromptText', 2);");
		
	},
	
	AdminEditPhraseSave : function(module, id) {
		
		new_text = Base64.encode($('AdminEditPhraseDivPromptValue').value);
		
		new_desc = Base64.encode($('AdminEditPhraseDivPromptDescription').value);
		
//		API.appendComm(module + id + new_text + '<br/>');
		
		pars = 'user_role_id=' + APL_USER_ROLE_ID + '&module=admin&func=EditPhrases&APL_OUTPUT=admin&Phrase[id]=' + id + '&Phrase[module]=' + module + '&Phrase[text]=' + new_text + '&Phrase[desc]=' + new_desc;
		
		var url = APL_ROOT + '/modules/admin/EditPhrases.php';
		
//		API.appendComm(url + "?" + pars + "<br/>");

		API.appendComm('saving...');
		
		var myAjax = new Ajax.Request(
						url, 
						{
							method: 'get',
							parameters: pars,
							evalScripts : true,
							asynchronous : true,
							onSuccess: APIAdmin.AdminEditPhraseSuccessSave
						});

		$('AdminEditPhraseCurrent').innerHTML = new_text;
		$('AdminEditPhraseCurrent').setAttribute('id', '');
		
		API.throbber('APIAdminControllerPhrasesP');
		API.getAdminFunction('EditPhrases', 'APIAdminControllerPhrasesP', API.modules.serialize() + 'user_role_id=' + APL_USER_ROLE_ID);
		cHTML.hide(true);
		APIAdmin.redisplayPhraseBlock(module);
		
	},
	
	redisplayPhraseBlock : function(module) {
		if (document.getElementById('ModPhrasesP' + module)) {
			Effect.BlindDown('ModPhrasesP' + module);
		} else {
			setTimeout(function() {
				APIAdmin.redisplayPhraseBlock(module);
			}, NORMAL_TIMEOUT);
		}
	},
	
	AdminEditPhraseSuccessSave : function() {
		
		if (API.modules.length > 0) {
			
			for (i = 0; i < API.modules.length; i++) {
				
				url = '/' + API.modules[i] + '/index/';

//				API.appendComm(url + ' : ' + mt[i] + '<br/>');
				
				API.queue("API.LoadModule('" + url + "', '" + API.modulesTarget[i] + "');");
		
			}
			
		}
		
		API.appendComm('<b>ok</b><br/>');

	},
	
	_mouseover : function (e) {

		APIAdmin.obj.setAttribute('onmouseover', 'void(0);');
		o = APIAdmin.obj;
		if (APIAdmin.state == 'max') return;
			
		
		if (APIAdmin.started == false) {
			APIAdmin.start();
			APIAdmin.started = true;
		}
		
		if (!APIAdmin.animation) { 
			a = APIAdmin.animation = new Animator({
			    duration: Math.round(ADMIN_TIMEOUT / 10),
			    interval: Math.round(ADMIN_TIMEOUT / 100),
				transition: Animator.easeOut,
			    onComplete: function() {
					w = API.ParseInt(getCookie('APIAdminControllerW'));
					h = API.ParseInt(getCookie('APIAdminControllerH'));
					if (w > APIAdmin.w && h > APIAdmin.h) {

						$('APIAdminController').style.width = w + 'px';
						$('APIAdminController').style.height = h + 'px';
						setTimeout(function() {

							$('APIAdminController').style.width = 'auto';
							$('APIAdminController').style.height = 'auto';
							
						}, ADMIN_TIMEOUT);
						
					} else {
						
						$('APIAdminController').style.width = 'auto';
						$('APIAdminController').style.height = 'auto';
						setCookie('APIAdminControllerW', 440);
						setCookie('APIAdminControllerH', 220);
						
						setTimeout(function() {

							w = $('APIAdminController').offsetWidth;
							h = $('APIAdminController').offsetHeight;
							setCookie('APIAdminControllerW', w);
							setCookie('APIAdminControllerH', h);
							$('APIAdminController').style.width = 'auto';
							$('APIAdminController').style.height = 'auto';
							
						}, ADMIN_TIMEOUT);
						
					}
					
					API.removeElement('APIAdminTemp');
					
				}
			})
			APIAdmin.animation.addSubject(APIAdmin._mouseoverAnim);
			APIAdmin.animation.addSubject(new NumericalStyleSubject(APIAdmin.obj, 'opacity', 0.2, 1));
			a.addSubject(APIAdmin._mouseoverAnim);
			a.play();
			
			tmp = new cHTML.createElement('div', 'APIAdminTemp', 'absolute', 0, APIAdmin.h, API.ParseInt(getCookie('APIAdminControllerW')), API.ParseInt(getCookie('APIAdminControllerH')), API.maxZ + 1);
			API.stylize(tmp);
			tmp.innerHTML = '&nbsp;';
			o.appendChild(tmp);
			
			
		} else {
			if (!APIAdmin.animation && !APIAdmin.animation0) {
				a = APIAdmin.animation0 = new Animator({
				    duration: Math.round(ADMIN_TIMEOUT / 200),
				    interval: Math.round(ADMIN_TIMEOUT / 50),
					transition: Animator.easeOut,
					onComplete: function() {

						setTimeout(function() {

							$('APIAdminController').style.width = 'auto';
							$('APIAdminController').style.height = 'auto';
							
						}, APIAdmin.animation0.options.duration + 2);
						
					}
				});
				APIAdmin.animation0.addSubject(APIAdmin._mouseoverAnim);
				APIAdmin.animation0.addSubject(new NumericalStyleSubject(APIAdmin.obj, 'opacity', 0.2, 1));
				a.addSubject(APIAdmin._mouseoverAnim);
				a.play();
			} else {
				if (APIAdmin.state != 'max') a.play();
			}
		}
		
		APIAdmin.state = 'max';
		setCookie('APIAdminstate', APIAdmin.state);
		APIAdmin.obj.setAttribute('onclick', 'void(0);');
		API.queue("APIAdmin.obj.setAttribute('onclick', 'void(0)');");
		
	},
	
	_mouseoverAnim : function (value) {

		$('APIAdminController').style.width = APIAdmin.w + Math.round((API.ParseInt(getCookie('APIAdminControllerW')) - APIAdmin.w) * value) + 'px';
		$('APIAdminController').style.height = APIAdmin.h + Math.round((API.ParseInt(getCookie('APIAdminControllerH')) - APIAdmin.h) * value) + 'px';
		
	},
	
	_mouseclick : function (e) {
	
		if (!e) var e = window.event;
		var tg = (window.event) ? e.srcElement : e.target;
		if (tg.nodeName != 'DIV') return;

		if (tg.getAttribute('id') == 'APIAdminController') APIAdmin._mouseout();
		
	},
	
	_mouseout : function () {
		
		o = APIAdmin.obj;
		if (APIAdmin.state == 'min') return;
		
		if (!APIAdmin.animation0) {
			if (!APIAdmin.animation) {
				void(0);		
			} else {
				a = APIAdmin.animation;
				a.setOptions({onComplete: function() {
					API.setObjOpacity(APIAdmin.obj, 20);
					APIAdmin.animation.setOptions({onComplete: function() {
						setTimeout(function() {

							$('APIAdminController').style.width = 'auto';
							$('APIAdminController').style.height = 'auto';
							
						}, APIAdmin.animation.options.duration + 2);
						
					}});
				}});
				APIAdmin.animation.reverse();
				setTimeout(function () {APIAdmin.obj.setAttribute('onclick', 'APIAdmin._mouseover();');}, APIAdmin.animation.options.duration + 2);
			}
		} else {
			a = APIAdmin.animation0;
			a.setOptions({onComplete: function() {
				API.setObjOpacity(APIAdmin.obj, 20);
				APIAdmin.animation0.setOptions({onComplete: function() {
						setTimeout(function() {

							$('APIAdminController').style.width = 'auto';
							$('APIAdminController').style.height = 'auto';
							
						}, APIAdmin.animation0.options.duration + 2);
					
				}});
			}});
			APIAdmin.animation0.reverse();
			setTimeout(function () {APIAdmin.obj.setAttribute('onclick', 'APIAdmin._mouseover();');}, APIAdmin.animation0.options.duration + 2);
		}
		
		APIAdmin.state = 'min';
		setCookie('APIAdminstate', APIAdmin.state);
		
		APIAdmin.obj.setAttribute('onclick', 'APIAdmin._mouseover();');
		
	},
	
	scroll : function () {
		
		o = APIAdmin.obj;
		o.style.top = API.scrollTop + 0 + 'px';
		
	},
	
	resize : function () {
		
		o = APIAdmin.obj;
		
	}
	
};

window.onload = API.init;
window.onunload = API.unload;

function getElementsByClassName(classname) {
    var a = [];
    var re = new RegExp('\\b' + classname + '\\b');
    var els = document.getElementsByTagName("*");
    for(var i=0,j=els.length; i<j; i++)
        if(re.test(els[i].className))a.push(els[i]);
    return a;
}

function writetxt() {
	
	return;
	
}

Array.prototype.serialize=function(){
   var temp = '';
   for(var vars in this){
      if(!(this[vars] instanceof Function)){
         temp+=vars+"="+this[vars]+"&"
      }
   }
   return temp
}

Array.prototype.serializeC =
 function (){
  var s="new Array("
  for (var i=0;i < this.length;i++){
   s += "'" + this[i] + "'"
   if (i < this.length-1)
    s += ","
  }
  s += ");"
  return s
 }
 
 
ObjectSerialize = function(obj) {
	var response = "{";
	var compound = false;
	for (var i in obj) {
		compound = true;
		response += "\n\t" + i + ": " + obj[i] + ",";
	}
	response += "}";
	return compound? response : this.toString();
}