function initOnDomLoaded() { // 2.0
	i=new imager(new okno('jsWin'), 'jsGal', false);
}

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.0
	this.addElem=function(elem) { win.appendChild(elem) }
	this.repaint=function(calc) {
		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';
			}
			if(win.style.display != 'block') { win.style.display='block' }
		} else { win.style.display='none' }
	}
	if(!margin) { margin=20 }
	var win=document.createElement('DIV');
	win.setAttribute('class', cssClass);
	win.setAttribute('className', cssClass);
	document.body.appendChild(win);
}

function imager(win, cssClass, gal, rotate, alts) { // 2.1
	this.s=function(link) {
		this.display();
		if(link) {
			if(img.src != link.href) {
				this.loading(true);
				img.src=link.href;
				img.title=link.firstChild.alt ? link.firstChild.alt : '';
				if(gal[1]) {
					for(var i=0; i<=lastImg; i++) {
						if(gal[i] == link.href) {
							pos=i;
							sbox.value=pos;
							break;
						}
					}
				}
			}
		} else {
			if(img.src != gal[pos]) {
				this.loading(true);
				img.src=gal[pos];
				img.title=alts[pos] ? alts[pos] : 'Next';
				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)) {
			temp=temp.getElementsByTagName('a');
			for(var i=0,temp2=temp.length,gal=new Array(),alts=new Array();i<temp2;i++) {
				if(temp[i].firstChild && temp[i].firstChild.nodeName == 'IMG') {
					alts.push(temp[i].firstChild.alt);
					gal.push(temp[i].href);
					if(!temp[i].onclick) {
						temp[i].imager=this;
						temp[i].onclick=function(e) { stopEventBubble(e); return this.imager.s(this) }
					}
				}
			}
		}
	}
	if(gal[1]) { // gal init
		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 pos=0, lastImg=gal.length - 1, tbox=document.createElement('DIV');
		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.getElementsByTagName('select')[0];
	} else {
		img.onclick=function(e) { stopEventBubble(e); this.imager.display(true) }
	}
	temp=temp2=undefined;
	div.appendChild(img);
	div.appendChild(p);
	win.addElem(div);
}

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 blank(link) { // v1.0 input: object reference
	link.target='_blank';
}

// martin js
var focus_changing = false;
var dont_check = false;

function focus_alert(s, f) {
	f.focus();
	alert(s);
	return false;
}

function form_correct(s, f) {
	for (i=0; i < f.elements.length; i++) {
		if (f.elements[i].onblur != undefined && !f.elements[i].onblur()) {
			return false;
		}
	}
	for (i=2; i < arguments.length; i++) {
		if (f[arguments[i]].value == '') {
			return focus_alert(s, f[arguments[i]]);
		}
	}
	return true;
}

function ereg_correct(s, f, regexp) {
	if (f.value == '' || regexp.test(f.value)) {
		return true;
	}
	if (!focus_changing) {
		focus_changing = true;
		focus_alert(s, f);
		focus_changing = false;
	}
	return false;
}

function number_correct(s, f, negative, d1, d2) {
	f.value = f.value.replace(',', '.');
	return ereg_correct(s, f, new RegExp('^'+ (negative ? '-?' : '') +'[0-9]{0,'+ (d1 ? d1 : '') +'}'+ (d2 ? '([.][0-9]{1,'+ d2 +'})?' : '') +'$'));
}

function email_correct(s, f) {
	return ereg_correct(s, f, /^[^ @]+@[^ @]+\.[^ @]+$/);
}

function url_correct(s, f) {
	return ereg_correct(s, f, /^https?:\/\/[^ \/]+\.[^ ]+/);
}
