function initOnDomLoaded() { // 2.0
	var loc=window.location.toString();
	if(loc.indexOf('iframe') == -1) {
		new imager(new okno('jsWin'), 'jsGal', 'pgC', true);
	}
}
function isDomLoaded() {
	(/loaded|complete/.test(document.readyState) ? initOnDomLoaded() : setTimeout(arguments.callee, 10));
}
if(document.addEventListener) {
	document.addEventListener('DOMContentLoaded', initOnDomLoaded, false); // firefox, opera9.02
	if((window.opera && window.opera.version() < 9.02) || /WebKit|Khtml/i.test(navigator.userAgent)) { // opera<9.02, safari, konqueror
		isDomLoaded();
	}
} else { // ie5,6,7
	initOnDomLoaded();
}

function okno(cssClass, margin) { // 1.2
	this.addElem=function(elem) { win.appendChild(elem) }
	this.repaint=function(calc) {
		var display=false, width=0, height=0, max=win.childNodes.length;
		for(var i=0,display=false,width=0,height=0,temp=0,max=win.childNodes.length; i<max; i++) {
			if(win.childNodes[i].style.display == 'block') {
				display=true;
				if(!calc) { break }
				temp=win.childNodes[i].offsetWidth + 2 * win.childNodes[i].offsetLeft;
				if(temp > width) { width=temp }
				height += win.childNodes[i].offsetHeight + 2 * win.childNodes[i].offsetTop;
			}
		}
		if(display) {
			if(calc) {
				var winXY=getWindowInnerXY();
				if(width + margin > winXY[0]) { width=winXY[0] - margin }
				if(height + margin > winXY[1]) { height=winXY[1] - margin }
				win.style.width=width +'px';
				win.style.height=height +'px';
				win.style.top=getScrollTop() + parseInt((winXY[1] - height) / 2) +'px';
				win.style.marginLeft=- parseInt(width / 2) +'px';
			}
			flashVisibility(false);
			trans.style.visibility='visible';
			win.style.display='block';
		} else {
			flashVisibility(true);
 			trans.style.visibility='hidden';
			win.style.display='none';
		}
	}
	var flashVisibility=function(visibility) {
		visibility=visibility ? 'visible' : 'hidden';
		for(var i=0; i<flashLen; i++) {
			flash[i].style.visibility=visibility;
		}
	}
	if(!margin) { margin=20 }
	var trans=document.getElementById('trans');
	var win=document.createElement('DIV');
	var flash=document.getElementsByTagName('OBJECT');
	var flashLen=flash.length;
	window.onload=function() { 
			window.onload;
			document.getElementById('trans').style.height=document.body.scrollHeight+'px';
	}
	win['class']=win['className']=cssClass;
	document.body.appendChild(win);
}

function getWindowInnerXY() { // 1.0
	if(typeof(window.innerWidth) == 'number') { return Array(window.innerWidth, window.innerHeight) }
	else if(document.documentElement && document.documentElement.clientWidth) { return Array(document.documentElement.clientWidth, document.documentElement.clientHeight) }
	else if(document.body && document.body.clientWidth) { return Array(document.body.clientWidth, document.body.clientHeight) }
}

function imager(win, cssClass, gal, rotate, forceBox, preload, alts) { // 2.22
	this.s=function(link) {
		this.display();
		if(link) {
			for(var i=0; i<=lastImg; i++) { // find image id
				if(gal[i][0] == link.href) {
					pos=i;
					break;
				}
			}
		}
		if(img.src != gal[pos][0]) {
			if(!gal[pos][1] && !preload) {
				this.loading(true);
				temp=new Image();
				temp.src=gal[pos][0];
				gal[pos].push(temp);
			}
			img.src=gal[pos][0];
			img.title=alts[pos] ? alts[pos] : 'Next';
			if(forceBox || gal[1]) sbox.value=pos;
		}
		return false;
	}
	this.display=function(hide) {
		div.style.display=(hide ? 'none' : 'block');
		win.repaint();
	}
	this.loading=function(show) {
		img.style.visibility=(show ? 'hidden':'visible');
		p.style.visibility=(show ? 'visible':'hidden');
	}
	this.repaint=function(calc) {
		if(calc) {
			var width=img.offsetWidth,height=img.offsetHeight;
			if(gal[1]) {
				if(tbox.offsetWidth > width) { width=tbox.offsetWidth }
				height+=tbox.offsetHeight +2;
			}
			div.style.width=width +'px';
			div.style.height=height +'px';
		}
		win.repaint(calc);
	}
	var temp, temp2, div=document.createElement('DIV'), img=document.createElement('IMG'), p=document.createElement('P');
	p.innerHTML='Loading image / Nahrávám obrázek';
	div.imager=this;
	div.setAttribute('class', cssClass);
	div.setAttribute('className', cssClass);
	img.imager=this;
	img.onload=function() { this.imager.loading(); this.imager.repaint(true) }
	if(typeof gal!='object') { // no array => crawl
		if(!(temp=document.getElementById(gal))) return false; // element not found => end
		temp=temp.getElementsByTagName('a');
		for(var i=0,temp3,temp4='',temp2=temp.length,gal=new Array,alts=new Array;i<temp2;i++) { // connect DOM with imager
			temp3=temp[i].firstChild;
			if(temp3 && temp3.nodeName.toUpperCase() == 'IMG') {
				temp4=''; 
				if(temp[i].onclick) {
					temp4=temp[i].onclick.toString();
					if(temp4.match(/i\.s\(this\)/)) temp4=''; // onclick="return i.s(this)"
				}
				if(!temp4) {
					alts.push(temp3.alt);
					if(preload) {
						temp3=new Image();
						temp3.src=temp[i].href;
						gal.push(new Array(temp[i].href, temp3));
					} else {
						gal.push(new Array(temp[i].href));
					}
					temp[i].imager=this;
					temp[i].onclick=function(e) { stopEventBubble(e); return this.imager.s(this) }
				}
			}
		}
	}
	var pos=0, lastImg=gal.length;
	if(lastImg > 0) { // images found
		if(lastImg > 1 || forceBox) { // with box
			this.move=function(id) {
				if(id) {
					if(gal[id]) {
						pos=id;
					} else if(--pos < 0) { pos=rotate ? lastImg : 0 }
				} else if (++pos > lastImg) { pos=rotate ? 0 : lastImg }
				this.s();
			}
			var tbox=document.createElement('DIV');
			lastImg--; // last image id
			img.onclick=function(e) { stopEventBubble(e); this.imager.move() }
			tbox.imager=this; // toolbox
			temp2='<input type="button" value="&lt;" onclick="this.parentNode.imager.move(-1)"/><input type="button" value="&gt;" onclick="this.parentNode.imager.move()"/><input type="button" value="Close" onclick="this.parentNode.imager.display(true); this.parentNode.imager.repaint()"/><select onchange="this.parentNode.imager.move(this.value)">';
			for(var i=0;i<=lastImg;i++) {
				temp2+='<option value="'+ i +'">'+ (i+1) + (alts[i] ? ' - '+ alts[i] : '') +'</option>';
			}
			tbox.innerHTML=temp2 +'</select>';
			div.appendChild(tbox);
			var sbox=tbox.childNodes[3];
		} else { // without box
			img.onclick=function(e) { stopEventBubble(e); this.imager.display(true) }
		}
		temp=temp2=null;
		div.appendChild(img);
		div.appendChild(p);
		win.addElem(div);
	}
}

function getScrollTop() { // 1.0
	if(window.innerHeight) { return window.pageYOffset }
	else if (document.documentElement && document.documentElement.scrollTop) { return document.documentElement.scrollTop }
	else if (document.body) { return document.body.scrollTop }
}

function stopEventBubble(e) { // 1.1
	(e ? e.stopPropagation() : window.event.cancelBubble=true);
}

function blank(link) { // 1.0
	link.target='_blank';
}

function formCheck(f, txtEmpty, txtInvalid) { // 1.1
	var err='', elem='';
	for(i=3, len=arguments.length; i<len; i++) {
		elem=f[arguments[i][0]];
		if(elem.nodeName) { // select, input, textarea
			if(arguments[i][2] || !arguments[i][3]) {
				if((typeof(elem.selectedIndex) != 'undefined' && !elem.selectedIndex) || (typeof(elem.selectedIndex) == 'undefined' && elem.value == '')) err+=txtEmpty +' : '+ arguments[i][1] +"\n";
			} 
			if(arguments[i][3] && (arguments[i][2] || elem.value) && !checkFormat(elem.value, arguments[i][3])) err+=txtInvalid +' : '+ arguments[i][1] +"\n";
		} else if(elem[0]['type'] == 'checkbox') { // chbox
			for(x=0, len2=elem.length, ch=false; x<len2; x++) {
				if(elem[x]['checked']) { ch=true; break; }
			}
			if(!ch) err+=txtEmpty +' : '+ arguments[i][1] +"\n";
		}
	}
	if(err) alert(err);
	return (err ? false : true);
}

function checkFormat(val, type) { // 1.0
	switch(type) {
		case 'email': if(val.match(/^[^@]+@.+\.[a-z]+$/)) return true; break;
		case 'isnum': if(val.match(/^\d+$/)) return true; break;
		case 'date_cz': if(val.match(/^(3[01]|[0-2]*\d|)\.([0]*\d|1[0-2])\.\d{4}$/)) return true; break; // DD.MM.YYYY nebo D.M.YYYY
		case 'date_iso': if(val.match(/^\d{4}-(0\d|1[0-2])-(3[01]|[0-2]\d)$/)) return true; break; // YYYY-MM-DD
		case 'phone_cz': val=val.replace(/ /g, ''); if(val.match(/^\d{9,12}$/)) return true; break;
	}
	return false;
}

function formRedir(select) { // 1.1
	if(select.value) {
		if(window.parent) {
			window.parent.location=select.value;
		} else {
			self.location=select.value;
		}
	}
	return false;
}

// CMS
function menu_onoff(item)
{
    with (document.getElementById(item).style) {
        display = ((display == "none") ? "block" : "none");
        return true;
    }
    return false;
}

// onBlur="emailCheck(this, lang('Zadejte platný email!'));
// @require eregCheck
function emailCheck(field, alert_string)
{
    if (alert_string == "") { alert_string = "Zadejte e-mail."; }
	return eregCheck(alert_string, field, /^[^ @]+@[^ @]+\.[^ @]+$/);
}

function eregCheck(alert_string, field, regexp)
{
	if (field.value == '' || regexp.test(field.value)) {
		return true;
	} else {
        field.focus();
        alert(alert_string);
        return false;
    }
}

// return ulozitCheck(this.form, lang('Nejsou zadány povinné údaje, formulář nelze odeslat!'), 'col1', 'col2');
function ulozitCheck(f)
{
    if (arguments[1] == "") {
        alert_string = "Nejsou zadány povinné údaje,\n formulář nelze odeslat!";
    } else {
        alert_string = arguments[1];
    }
    for (i=2; i<arguments.length; i++) {
        if ((f[arguments[i]].tagName == 'SELECT' && !f[arguments[i]].selectedIndex) || !f[arguments[i]].value) {
            f[arguments[i]].focus();
            window.alert(alert_string);
            return false;
        }
    }
    return true;
}
