//TODO: addEventHandler
if(!Array.indexOf){
	Array.prototype.indexOf = function(obj){
		for(var i=0; i<this.length; i++){
			if(this[i]==obj){
				return i;
			}
		}
		return -1;
	}
}

function Animator(pocetKroku,interval)  {
	this.timeouty = new Array();
	this.zacatek = 0;
	this.pocetKroku=0;
	this.interval=0;
	this.hraje=false;
	if (pocetKroku!=undefined) this.pocetKroku=pocetKroku;
	if (interval!=undefined) this.interval=interval;

	this.setup = function(pocetKroku,interval) {
		this.pocetKroku=pocetKroku;
		this.interval=interval;
	}

	this.start = function(akce,finalka) {
		this.stop();
		var i;
		var upravenaAkce=akce;
		for (i=this.zacatek+1;i<pocetKroku;i++) {
			upravenaAkce=tools.str_replace("§",i,akce);
			if (i==pocetKroku-1 && finalka) {
				upravenaAkce+="; "+finalka;
			}
			this.timeouty[i-1]=window.setTimeout(upravenaAkce,interval*(i-this.zacatek));
		}

		upravenaAkce=tools.str_replace("§",this.zacatek,akce);
		window.setTimeout(upravenaAkce,10);
	}

	this.reverseStart = function(akce,finalka) {
		this.stop();
		var i,j;
		var upravenaAkce=akce;
		j=1;
		for (i=pocetKroku-2;i>=this.zacatek;i--) {
			upravenaAkce=tools.str_replace("§",i,akce);
			if (i==pocetKroku-1 && finalka) {
				upravenaAkce+="; "+finalka;
			}
			this.timeouty[j-1]=window.setTimeout(upravenaAkce,interval*(j));
			j++;
		}

		upravenaAkce=tools.str_replace("§",this.pocetKroku-1,akce);
		window.setTimeout(upravenaAkce,10);
	}
	this.stop = function() {
		var i;
		for (i=0;i<this.timeouty.length;i++) {
			window.clearTimeout(this.timeouty[i]);
		}
	}
	this.pokracujOd = function(krok) {
		if (krok>=0) this.zacatek=krok;
	}
};
function idPrvku(zdroj) {
	if (zdroj.id=="") zdroj.setAttribute("id","autoAssignedId"+Math.ceil(Math.random()*100000));
	return zdroj.id;
}
function PageManager() {

	this.poziceX = function(zdroj) {
		var vrat = 0;
		kontrola=0;
		while( zdroj != null && kontrola<100) {
			vrat += zdroj.offsetLeft;
			zdroj = zdroj.offsetParent;
			kontrola++;
		}
		return vrat;
	}
	this.poziceY = function(zdroj) {
		var vrat = 0;
		kontrola=0;
		while( zdroj != null && kontrola<100) {
			vrat += zdroj.offsetTop;
			zdroj = zdroj.offsetParent;
			kontrola++;
		}
		return vrat;
	}
	this.alpha = function(el,kolik) {
		if (tools.browser("moz")) {
			el.style.MozOpacity=kolik/100;
			return;
		}
		if (tools.browser("ie")) {
			if (kolik<100) el.style.filter = 'alpha(opacity='+kolik+')';
			else el.style.filter = '';
			return;
		}
		el.style.opacity=kolik/100;
	}
	this.vnitrniSirka = function(zdroj) {
		var vrat = zdroj.clientWidth;
		if (zdroj.style.paddingLeft!="") vrat-=tools.ciselnaCast(zdroj.style.paddingLeft)*1;
		if (zdroj.style.paddingRight!="") vrat-=tools.ciselnaCast(zdroj.style.paddingRight)*1;
		return vrat;
	}
	this.borderSirka = function(zdroj,pocitatBorder) {
		if (pocitatBorder==undefined) pocitatBorder=true;
		var vrat = zdroj.offsetWidth;
		if (!pocitatBorder) vrat-=tools.ciselnaCast(zdroj.style.borderLeftWidth);
		if (!pocitatBorder) vrat-=tools.ciselnaCast(zdroj.style.borderRightWidth);
		return vrat;
	}
	this.vnejsiSirka = function(zdroj) {
		var vrat = zdroj.offsetWidth;
		if (zdroj.style.marginLeft!="") vrat+=tools.ciselnaCast(zdroj.style.marginLeft)*1;
		if (zdroj.style.marginRight!="") vrat+=tools.ciselnaCast(zdroj.style.marginRight)*1;
		return vrat;
	}
	this.vnitrniVyska = function(zdroj) {
		var vrat = zdroj.clientHeight;
		if (zdroj.style.paddingTop!="") vrat-=tools.ciselnaCast(zdroj.style.paddingTop)*1;
		if (zdroj.style.paddingBottom!="") vrat-=tools.ciselnaCast(zdroj.style.paddingBottom)*1;
		if (tools.browser("ie7")) {
			vrat-=tools.ciselnaCast(zdroj.style.borderTopWidth);
			vrat-=tools.ciselnaCast(zdroj.style.borderBottomWidth);			
		}
		return vrat;
	}
	this.borderVyska = function(zdroj,pocitatBorder) {
		if (pocitatBorder==undefined) pocitatBorder=true;
		var vrat = zdroj.offsetHeight;
		if (!pocitatBorder) vrat-=tools.ciselnaCast(zdroj.style.borderTopWidth);
		if (!pocitatBorder) vrat-=tools.ciselnaCast(zdroj.style.borderBottomWidth);
		return vrat;
	}
	this.vnejsiVyska = function(zdroj) {
		var vrat = zdroj.offsetHeight;
		if (zdroj.style.marginTop!="") vrat+=tools.ciselnaCast(zdroj.style.marginTop)*1;
		if (zdroj.style.marginBottom!="") vrat+=tools.ciselnaCast(zdroj.style.marginBottom)*1;
		return vrat;
	}
	this.setVyska = function(cil,kolik) {
		cil.style.height=kolik+"px";
	}
	this.setSirka = function(cil,kolik) {
		cil.style.width=kolik+"px";
	}
	this.setVnejsiVyska = function(cil,kolik) {
		cil.style.height=(kolik-(this.borderVyska(cil)-this.vnitrniVyska(cil)))+"px";
	}
	this.setVnejsiSirka = function(cil,kolik) {
		cil.style.width=(kolik-(this.borderSirka(cil)-this.vnitrniSirka(cil)))+"px";
	}	

	this.oknoSirka = function() {
		if (tools.browser("moz")) {
			var v1=window.innerWidth;
			var v2=document.body.clientWidth;
			return v1>v2?v1:v2;
		}
		return this.f_filterResults (
			window.innerWidth ? window.innerWidth : 0,
			document.documentElement ? document.documentElement.clientWidth : 0,
			document.body ? document.body.clientWidth : 0
		);

	}
	this.oknoVyska = function() {
		if (tools.browser("moz")) {
			var v1=window.innerHeight;
			var v2=document.body.clientHeight;
			return v1>v2?v1:v2;
		}
		return this.f_filterResults (
			window.innerHeight ? window.innerHeight : 0,
			document.documentElement ? document.documentElement.clientHeight : 0,
			document.body ? document.body.clientHeight : 0
		);
	}
	this.scrollX = function() {
		return this.f_filterResults (
			window.pageXOffset ? window.pageXOffset : 0,
			document.documentElement ? document.documentElement.scrollLeft : 0,
			document.body ? document.body.scrollLeft : 0
		);
	}

	this.scrollY = function() {
		return this.f_filterResults (
			window.pageYOffset ? window.pageYOffset : 0,
			document.documentElement ? document.documentElement.scrollTop : 0,
			document.body ? document.body.scrollTop : 0
		);
	}
	this.f_filterResults = function(n_win, n_docel, n_body) {
		var n_result = n_win ? n_win : 0;
		if (n_docel && (!n_result || (n_result > n_docel)))
			n_result = n_docel;
		return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
	}

	this.obalit = function(el,id) {
		var parent=el.parentNode;
		var el2=document.createElement("div");
		el2.setAttribute("id",id);
		parent.insertBefore(el2,el);
		parent.removeChild(el);
		el2.appendChild(el);
		return el2;
	}

	this.getElementsByClassName = function(className, tag, elm){
		var testClass = new RegExp("(^|\\s)" + className + "(\\s|$)");
		var tag = tag || "*";
		var elm = elm || document;
		var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
		var returnElements = [];
		var current;
		var length = elements.length;
		for(var i=0; i<length; i++){
			current = elements[i];
			if(testClass.test(current.className)){
				returnElements.push(current);
			}
		}
		return returnElements;
	}


	this.mousePosX=0;
	this.mousePosY=0;
	this.draggedElement=null;
	this.draggedElementOfX=0;
	this.draggedElementOfY=0;
	this.mouseSledovani=false;

	this.mouseSledovaniStart = function() {
		if (this.mouseSledovani || this.mousePosX!=0) return;
		//mouseIE = document.all?true:false;
		//if (!mouseIE) document.captureEvents(Event.MOUSEMOVE);
		document.onmousemove = this.getMouseXY;
		this.mouseSledovani=true;
	}

	this.mysX = function() {
		return this.mousePosX;
	}
	this.mysY = function() {
		return this.mousePosY;
	}

	this.getMouseXY = function(e) {
		pageman.mousePosX = e.pageX;
		pageman.mousePosY = e.pageY;
		if (pageman.mousePosX < 0) {pageman.mousePosX = 0;}
		if (pageman.mousePosY < 0) {pageman.mousePosY = 0;}
		if (pageman.draggedElement) {
			pageman.moveDraggedElement();
		}
	}
	this.startDrag = function(cil,ofX,ofY) {
		if (pageman.draggedElement) pageman.stopDrag(pageman.draggedElement);
		if (ofX==undefined) ofX=20;
		if (ofY==undefined) ofY=16;
		this.draggedElementOfX=ofX;
		this.draggedElementOfY=ofY;
		pageman.draggedElement=cil;
		pageman.moveDraggedElement();
	}
	this.stopDrag = function() {
		if (pageman.draggedElement) pageman.draggedElement=null;
	}
	this.moveDraggedElement = function() {
		var noveX=(this.mysX() + this.draggedElementOfX);
		var noveY=(this.mysY() + this.draggedElementOfY);

		//korekce zalezení za okraj prohlížeče
		if (this.oknoSirka() < noveX + this.vnejsiSirka(this.draggedElement)) {//Pravý okraj
			noveX = this.oknoSirka() - this.vnejsiSirka(this.draggedElement) - 5;
		}
		if (noveX < 0) {//Levý okraj
			noveX = 0 + 5;
		}
		if (noveY < 0) {//Horní okraj
			noveY = 0 + 5;
		}
		if (this.oknoVyska() < noveY + this.vnejsiVyska(this.draggedElement)) {//Dolní okraj
			noveY = this.oknoVyska() - this.vnejsiVyska(this.draggedElement) - 5;
		}


		this.draggedElement.style.left = noveX + "px";
		this.draggedElement.style.top  = noveY + "px";
	}
}
function Tools() {
	this.str_replace = function(from,to,str) {
	    var idx = str.indexOf(from);
	    var bezpecnostni_pojistka=0;
	    while ( idx > -1 ) {
		str = str.replace(from,to);
		idx = str.indexOf(from,idx+from.length);
		bezpecnostni_pojistka++;
		if (bezpecnostni_pojistka==1000) break;

	    }
	    return str;
	}
	this.ciselnaCast = function(zdroj,delka) {
		var i=0;
		var vrat="";
		var znak;
		if (delka==undefined) delka=zdroj.length;
		for (i=0;i<delka;i++) {
			znak=zdroj.charAt(i);
			if (znak.match('[0-9.,\-]')) {vrat+=znak;}
		}
		return vrat;
	}
	this.browser = function(browser,verze,smer) {
		if (verze==undefined && smer==undefined) {
			switch (browser) {
				case "ie6": return this.browser("ie","6","-");
				case "ie7": return this.browser("ie","7","-");
				case "ie8": return this.browser("ie","8","+");
				case "ie": return this.browser("ie","20","-");
				case "moz": return this.browser("moz","1","+");
				case "safari": return this.browser("safari","1","+");
				case "chrome": return this.browser("chrome","1","+");
				case "webkit": return (this.browser("safari","1","+") || this.browser("chrome","1","+"));
				case "opera": return this.browser("opera","8","+");
			}
		}

		var searchString="";
		var notString="";
		switch(browser) {
			case "ie": searchString="MSIE"; break;
			case "moz": searchString="Firefox"; break;
			case "safari": searchString="Safari"; notString="Chrome"; break;
			case "chrome": searchString="Chrome"; break;
			case "opera": searchString="Opera"; break;
			default: return false;
		}

		var ua = navigator.userAgent;
		var browserVer;

		if (ua.indexOf(searchString)==-1) return false;
		if (notString && ua.indexOf(notString)!=-1) return false;

		switch (browser) {
			case "ie": browserVer=ua.match("MSIE ([0-9]*)")[1]; break;
			case "moz": browserVer=ua.match("Firefox/([0-9]*)")[1]; break;
			case "opera": browserVer=ua.match("Opera/([0-9]*)")[1]; break;
			case "chrome": browserVer=ua.match("Chrome/([0-9]*)")[1]; break;
			case "safari": browserVer=ua.match("Version/([0-9]*)")[1]; break;
		}

		if (smer=="+") {
			return (parseInt(browserVer)>=parseInt(verze));
		}
		if (smer=="-") {
			return (parseInt(browserVer)<=parseInt(verze));
		}
		if (smer=="") {
			return (parseInt(browserVer)==parseInt(verze));
		}

		return false;
	}
	this.debug=function(text) {
		el=document.getElementById("debug_output");
		if (!el) {
			el=document.createElement("div");
			document.body.appendChild(el);
		}
		el.innerHTML+=text+"<br />";
	}
}

var pageman=new PageManager();
var tools=new Tools();
var udelejAzTimeouts=[];

function udelejAz(cilovyElement,vlastnost,hodnota,funkce,truefalse) {
	if (truefalse==undefined) truefalse=true;
	if (!cilovyElement) {tools.debug("udelejAz - cilovyElement nenalezen"); return;}
	var cilovaVlastnost;
	if (vlastnost.indexOf(".")!=-1) {
		var casti=vlastnost.split(".");
		cilovaVlastnost=cilovyElement[casti[0]][casti[1]];
	} else cilovaVlastnost=cilovyElement[vlastnost];
	if (cilovaVlastnost==undefined) {tools.debug("udelejAz - cilovyElement nemá zadanou vlastnost"); return;}
	if (!funkce) {tools.debug("udelejAz - nebyla zadána funkce"); return;}
	window.setTimeout(function() {
		if ((cilovaVlastnost==hodnota)==truefalse) funkce();
		else window.setTimeout(function() {
			udelejAz(cilovyElement,vlastnost,hodnota,funkce,truefalse);
		},150);
	},150);
}





