function initOnDomLoaded() { // 2.0
	f=new flasher('flash');
	new imager(new okno('jsWin'), 'jsGal', 'cont', 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();
}

var onDOMLoaded = function() { // 1.1
    if(document.readyState == 'complete' || (typeof document.readyState == 'undefined' && document.body != null)) {
	} else {
		setTimeout('onDOMLoaded()', 10);
	}
}
onDOMLoaded();

function flasher(id) {
	this.s = function(link, width, height) {
		this.div.innerHTML = '<object type="application/x-shockwave-flash" data="'+ link.href +'" width="'+ width +'" height="'+ height +'" onActivate="parentNode.style.display=\'none\'"><param name="movie" value="'+ link.href +'"/></object>';
		var winXY = getWindowInnerXY();
		var half = parseInt(width / 2);
		this.div.style.marginLeft = '-' + (half > winXY[0] / 2 ? parseInt(winXY[0] / 2) - 10 : half ) + 'px';
		this.div.style.height = (winXY[1] - 40 < height ? winXY[1] - 40 + 'px' : '');
		this.div.style.top = getScrollTop() + 10 + 'px';
		this.div.style.display = 'block';
		return false;
	}
	
	this.div = document.createElement('DIV');
	this.div.id = id;
	this.div.onclick = function() { this.style.display = 'none' };
	document.body.appendChild(this.div);
}

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) { 
	link.target='_blank';
}
