// info o browseru
var tmp = navigator.userAgent.toLowerCase();
browser = { 
  userAgent: tmp,
	version: (tmp.match(/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/) || [])[1],
	webkit: /webkit/.test(tmp),
	opera: /opera/.test(tmp),
	msie: /msie/.test(tmp) && !/opera/.test(tmp),
	mozilla: /mozilla/.test(tmp) && !/(compatible|webkit)/.test(tmp),
	khtml: /khtml/.test(tmp)
};

// init
function initOnDomLoaded() { // 2.0
	i=new imager(new okno('jsWin'), 'jsGal', 'main', 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();
}

// code
function imager(win, cssClass, gal, rotate, forceBox, preload, alts, wMark) { // 2.3
	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(wMark) {
				wMark.style.width=width +'px';
				wMark.style.height=height +'px';
			}
			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(wMark) {
		wMark=document.createElement('DIV');
		wMark['id']='wMark';
		wMark.imager=this;
		div.appendChild(wMark);
	}
	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');
			tbox['id']='toolbox';
			lastImg--; // last image id
			if(wMark) {
				wMark.onclick=function(e) { stopEventBubble(e); this.imager.move() }
			} else {
				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
			if(wMark) {
				wMark.onclick=function(e) { stopEventBubble(e); this.imager.display(true) }
			} else {
				img.onclick=function(e) { stopEventBubble(e); this.imager.display(true) }
			}
		}
		temp=temp2=null;
		div.appendChild(img);
		div.appendChild(p);
		win.addElem(div);
	}
}

function okno(winCssClass, margin, opacity) { // 1.31
	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 }
	if(!opacity) { opacity=80 }
	var win=document.createElement('DIV');
	win['class']=win['className']=winCssClass;
	var flash=document.getElementsByTagName('OBJECT');
	var flashLen=flash.length;
	var trans=document.getElementById('trans');
	if(!trans) {
		trans=document.createElement('DIV');
		trans['id']='jsTrans';
		if(browser.msie) { trans.style['filter']='alpha(opacity='+ opacity +')';
		} else if(browser.mozilla && browser.version.match(/^[0-1]\.[0-6]/)) { trans.style['-moz-opacity']=opacity / 100;
		} else { trans.style['opacity']=opacity / 100 }
		window.trans=trans;
		window.onload=window.onresize=function() {
			var winXY=getWindowInnerXY(), bodyHeight=document.body.offsetHeight;
			window.trans.style.height=(winXY[1] > bodyHeight ? winXY[1] : bodyHeight) +'px';
		}
		document.body.appendChild(trans);
	}
	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 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 win(link, width, height) { // v1.0 input: object reference or url
	if(link.href) {
		link = link.href;
	}
	window.open(link, '', 'width=' + width + ', height=' + height + ', top=50, left=50, resizable=yes, scrollbars=no, toolbar=no, location=no, directories=no, status=yes, menubar=no, copyhistory=no');
	return false;
}

function redirect(sel) {
	window.location=sel.value;
}

function blank(link) { // v1.0 input: object reference
	link.target='_blank';
}

function formRedir(select) { // 1.0
	if(select.value) {
		var temp=select.form.action;
		temp=temp.substr(0, temp.lastIndexOf('.')) +'_'+ select.name + select.value +'.htm';
		self.location=temp;
	}
	return false;
}

// martin js
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;
    }
}

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;
}

function numberCheck(field, alert_string, negative, d1, d2)
{
    if (alert_string == "") { alert_string = "Zadejte číslo."; }
	field.value = field.value.replace(",", ".");
	return eregCheck(alert_string, field, new RegExp('^'+ (negative ? '-?' : '') +'[0-9]{0,'+ (d1 ? d1 : '') +'}'+ (d2 ? '([.][0-9]{1,'+ d2 +'})?' : '') +'$'));
}