function initWGuiTables() {
	var WGtables=document.getElementsByTagName('table');
	var arrindex=0;

	for (var i=0;i<WGtables.length;i++) {		//najdeme vsetky tabulky
		if (WGtables[i].className.indexOf('WGuiTableList')==-1) {continue;}
		var tbody=WGtables[i].getElementsByTagName('tbody');

		for (var j=0;j<tbody.length;j++) {	//prebehneme vsetky tbody elementy
			var row=tbody[j].getElementsByTagName('tr');
			for (var k=0;k<row.length;k++) {	// nastavime vsetky riadky tabulky
				row[k].onmouseover = function () {
					if (this.className=='checked') return;
					this.oldClass=this.className;
					this.className='hover';
				}
				row[k].onmouseout = function () {
					if(this.className!='checked') this.className = this.oldClass;
				}
				row[k].onmousedown = function () {
					var input=this.getElementsByTagName('input');
					for (var i=0;i<input.length;i++) {
						if (input[i].getAttribute('id').indexOf('_wcheckbox')>-1) {
							this.onclick=function () {return false;}
							if (input[i].checked==true) {
								input[i].checked=false;
								this.className='hover';
							} else {
								input[i].checked=true;
								this.className='checked';
							}
						}
					}
				}
			}
		}
	}
}

function initWGuiWindows() {
	var WGSpan=document.getElementsByTagName('span'); //najdeme tlacitka
	var arrindex=0;
	
	for (var i=0;i<WGSpan.length;i++) {		//prebehneme vsetky DIVka
		if (WGSpan[i].getAttribute('rel')=='WGuiWindowclose') {		//nastavime zatvaraci button
			WGSpan[i].onmousedown = function () {
				var dbox=this.parentNode.parentNode;

				dbox.style.display='none';
			}
		}
		
		if (WGSpan[i].getAttribute('rel')=='WGuiWindowhide') {		//nastavime skryvaci button
			WGSpan[i].onmousedown = function () {
				var cbox=this.parentNode.nextSibling;
				
				if (cbox.style.display!='none') {
					cbox.style.display='none';
					this.innerHTML='[ zobraziť ]';
				} else {
					cbox.style.display='block';
					this.innerHTML='[ skryť ]';
				}
			}
		}
	}
}

function initWGui() {
	initWGuiTables();
	initWGuiWindows();
}

/* Wgui pop-up windows */

function WGuiOpenDialog(params) {
	var tparams;
	
	if (!params.width) params.width=600;
	if (!params.height) params.height=500;

	if (params.modal==true) {
	} else {
		for(i in params) {
			if (i=='url' || i=='name') continue;
			tparams+=', '+i+'='+params[i];
		}
		window.open(params.url, params.name, 'status=0, scrollbars=1'+tparams);
	}
}

/* Wgui color selector */

function WGuishowColSelector(name) {
	var SLObj=document.getElementById(name+'WGC');			//opener
	var CSObj=document.getElementById('WGuiColorSelector');		//color selector object
	
	CSObj.openername=name;
	CSObj.style.top=(icore.getElYPos(SLObj))+'px';
	CSObj.style.left=(icore.getElXPos(SLObj)+SLObj.offsetWidth)+'px';
	if (CSObj.style.visibility!='visible') CSObj.style.visibility='visible'; else CSObj.style.visibility='hidden';
}

function WGuiPreviewColor(obj) {
	var CVObj=document.getElementById('WGuiColorValue');
	var CPObj=document.getElementById('WGuiColorPreview');

	re=/rgb\((\d{1,3}),\s?(\d{1,3}),\s?(\d{1,3})\)/i;	//rozbijeme rgb od js
	args=re.exec(obj.style.backgroundColor);

	CVObj.value='#'+icore.strpad(icore.dec2hex(args[1]), 2, '0', 0)+icore.strpad(icore.dec2hex(args[2]), 2, '0', 0)+icore.strpad(icore.dec2hex(args[3]), 2, '0', 0);
	CPObj.style.backgroundColor=obj.style.backgroundColor;
}

function WGuiSetColor(obj) {
	var CSObj=document.getElementById('WGuiColorSelector');		//color selector object
	var SLObj=document.getElementById(CSObj.openername+'WGC');	//color select
	var IHObj=document.getElementById(CSObj.openername);		//input hidden

	re=/rgb\((\d{1,3}),\s?(\d{1,3}),\s?(\d{1,3})\)/i;	//rozbijeme rgb od js
	args=re.exec(obj.style.backgroundColor);

	SLObj.style.backgroundColor=obj.style.backgroundColor;
	IHObj.value='#'+icore.strpad(icore.dec2hex(args[1]), 2, '0', 0)+icore.strpad(icore.dec2hex(args[2]), 2, '0', 0)+icore.strpad(icore.dec2hex(args[3]), 2, '0', 0);
	CSObj.style.visibility='hidden';
}


