		/*
		 ******* Jag basic START ***************************
		 Javascript common functions
         FIXME: v ostre verzi odstranit komenty
		 */

		function Jag(){}

		Jag.debug = function(obj)
		{
			var arr = new Array();
		    for(var i in obj)
			{
			    arr[arr.length] = i;
			}
			arr.sort();
			document.getElementById('debug').innerHTML = arr.join('<br>');
		}
	    Jag.getScroll = function()
		{
			if(typeof document.documentElement != "undefined"){
				var l = document.documentElement.scrollLeft;
				var t = document.documentElement.scrollTop;
			}else{
				var l = document.body.scrollLeft;
				var t = document.body.scrollTop;
			}
			return new Array(l,t);
		}
		Jag.getInnerWindowDimension = function(){
		    var w = window;
			var width  = w.innerWidth  || (w.document.documentElement.clientWidth || w.document.body.clientWidth);
            var height = w.innerHeight || (w.document.documentElement.clientHeight || w.document.body.clientHeight);
			return new Array(width, height);
		}
		Jag.centerElement = function(el)
		{
		    var scrolled = Jag.getScroll();
			var winDim = Jag.getInnerWindowDimension();
			el.style.position = 'absolute';
			var minTop = 10, minLeft = 10; //FIXME: nutno vzit v potaz nascrolovani
			el.style.left     = Math.max(minLeft, (winDim[0]/2  - el.offsetWidth/2  + scrolled[0] ) )  + 'px';
			el.style.top      = Math.max(minTop, (winDim[1]/2 -  el.offsetHeight/2 + scrolled[1] ) )  + 'px';
		}


        /* projde od korene root do hloubky vsechny uzly, pro kazdy uzel:
         * vola filter(uzel, iZanoreni) - pokud vrati true, znamena, ze se ma volat callback(uzel, iZanoreni)
         * - callback podle uzlu provede nejakou operaci a vrati boolean, jestli se ma v prohledavani pokracovat.
         * Tj. callback se vola jenom pro ty uzly, ktere projdou filtrem.
         */
		//FIXME: dodelat podminku pro volitelnost filtru: (filter==null || filter(root))
		Jag.depthWalk = function(root, filter, callback, _level)
		{
	       if(!_level) _level = 0;
	       //tzn. ukoncit prohledavani, pokud element patri do filtru a user def.callback vrati false
	       if(filter(root) && !callback(root, _level) ) return false;

	       if(root.hasChildNodes()) {
	          var node = root.firstChild;
	          while(node!=null) {
	             if( node.nodeType == 1 &&
	                 !Jag.depthWalk(node, filter, callback, _level+1) ) return false;
	             node = node.nextSibling;
	          }
	       }
	       return true;
	    }
        //oMap je ve tvaru {className = null, className2 = null, ... classNameN = null}
        //projit potomky oRoot a pokud maji jednu z className v oMap, ulozit odkaz na potomka do oMap (misto null)
		//pokud je bMultiple true, ukladaji se vsechny potomci do pole, jinak pouze prvni
		Jag.mapElementsByClassNames = function(oRoot, oMap, bMultiple )
		{
			 var callback = function(el){
			 	if(!el.className) return true;

				var classNames = el.className.split(" ");
	            for(var i=0;i<classNames.length;i++){
				    var cName = classNames[i];
					if(typeof oMap[cName] != "undefined"){
						if(bMultiple){
							if(oMap[cName] == null) oMap[cName] = new Array();
							oMap[cName][oMap[cName].length] = el;
						}else{
							if(!oMap[cName]) //get only first
							oMap[cName] = el;
						}
					}
				}
			 	return true;
			 }
			 Jag.depthWalk(oRoot, function(){return true}, callback);
		}


		/*
		 ******* Jag basic END ***************************
		 */

        /* zvazit zmenit logiku nastavovani velikosti. Ted se nastavuje velikost kontejneru (div) ve kterem
           je umisteny iframe. Lepsi zrejme bude nastavit tuhle velikost na 100% a pevne nastavovat velikost
           nejvyssiho obalu. */
		function JagUrlDialog()
		{
            //odkazy na dulezite htmlElementy
			this.es           = { main:null, shade:null, body:null, title:null, buttClose:null, buttonBar:null};
			this.CID          = JagUrlDialog.pref + JagUrlDialog.cnt++;
			this.iCorrHeight  = 50;

			this.closeRedirLink="";

			//TODO: binarni masku iButtons - pro defaultni zobr. ok, cancel
			this.open = function(sTitle, sUrl, iWidth, iHeight, bShowCloseButt)
			{

			    if(typeof bShowCloseButt == 'undefined') bShowCloseButt = true;

			    var iPom=iWidth*1-32;

				var inHtml =
					'<div class="head" style="width:'+iWidth+'px;">'+
					'<div class="begin">'+'</div>'+
					'<div class="middle" style="width:'+(iWidth-30)+'px;">'+
						'<div class="title"><img src="css/img/help.gif" alt="?" title="" style="vertical-align:middle;" /> '+sTitle+'</div>'+
						'<div class="buttClose">X</div>'+
					'</div>'+
					'<div class="end" style="left:'+(iWidth-15)+'px;">'+'</div>'+
					'<div class="body"><iframe id="if_'+ this.CID +'" frameborder="0" src="'+sUrl+'" style="">No iframes</iframe></div>'+
					'<div class="_buttonBar"></div>';

				var body   = document.getElementsByTagName('body')[0];
				var winDim = Jag.getInnerWindowDimension();

				/* create partialy transparent layer to cover document */
				var shade              = document.createElement('div');
				    shade.className    = 'Jag_shade';
					shade.style.width  = (Math.max(body.parentNode.scrollWidth, winDim[0])  -1)+ 'px';
					shade.style.height = (Math.max(body.parentNode.scrollHeight, winDim[1]) -1)+ 'px';
                    //if body has some margin
				    //shade.style.width  = (Math.max(body.parentNode.scrollWidth, winDim[0])  +20)+ 'px';
					//shade.style.height = (Math.max(body.parentNode.scrollHeight, winDim[1]) +20)+ 'px';
					shade.style.zIndex = 1000 + JagUrlDialog.cnt;
					this.es.shade      = shade;


				var el = document.createElement('div');
				    el.className    = 'JagDlg';
					el.style.zIndex = 1000 + JagUrlDialog.cnt +1;
					el.id           = this.CID;
					el.style.height = (iHeight + this.iCorrHeight) + 'px';
					el.innerHTML    = inHtml;
				    this.es.main    = el;

				    Jag.mapElementsByClassNames(el, this.es, false);

					//this.es.title="<b>test</b>";
					if( bShowCloseButt ){
                        var that = this;
					    this.es.buttClose.onclick = function(){
                            that.close();
					    }
					} else {
                          var butt = this.es.buttClose;
                              butt.parentNode.removeChild( butt );
                    }

                    body.appendChild(el);  //draw dialog
                    this.setSize(iWidth, iHeight, true);

                    var oIframe = document.getElementById('if_'+ this.CID);
                    //oIframe.src = sUrl;   // load page into iframe

					this.ShowHideSelects('hidden');
				    body.appendChild(shade);


				    window.onscroll = function(e){ //optional FIXME: turn to attach/addEventListener model
			        	Jag.centerElement(el);
		            }
				    window.onresize = window.onscroll;
			}
			this.close = function()
			{

				if( this.es.main && this.es.main.parentNode ) {
					this.ShowHideSelects('visible');
					this.es.main.parentNode.removeChild( this.es.main );
					this.es.shade.parentNode.removeChild( this.es.shade );
					window.onscroll = null;
					window.onresize = null;

				}
				//SEE: zvazit oddelit destroy / setVisible(boolean)

	        //pokud je do promene nejaky link probehne presmerovani parenta

   			if (this.closeRedirLink!="")
				{
				   //alert(this.closeRedirLink);
				  var reg=/.php$/;
				  if(!reg.test(parent.window.location))
				     {
				        parent.window.location.href=this.closeRedirLink;
				     }
				}
				delete JagUrlDialog._instances[this.CID];
			}
			this.addButton = function(sId, sLabel, sClassName, oOnClick){
                if(this.es[sId]) return false;

			    var el = document.createElement('input');
				    el.type = 'button';
					el.className = sClassName;
					el.value = sLabel;
					if(sId){
					   el.id = sId;
					   this.es[sId] = el;
					}
					if(oOnClick) el.onclick = oOnClick;
				this.es.buttonBar.appendChild(el);
				return true;
			}
			this.removeItem = function(sId){
                if(typeof this.es[sId]!='undefined'){
                    this.es[sId].parentNode.removeChild(this.es[sId]);
                    delete this.es[sId];
                }
			}
			this.setSize  = function(iW, iH, bCenter){
                if(typeof bCenter == "undefined") bCenter = true;
				this.es.body.style.width  = iW  + 'px';
			    this.es.body.style.height = iH  + 'px';
			    if(bCenter) Jag.centerElement(this.es.main);
			}

			this.ShowHideSelects = function(sVisibilityStyle) {
				if (BrowserDetect.browser == 'Explorer' && BrowserDetect.version <= 6) {
					var dom = document.getElementsByTagName('select');
					for (var i=0; i<=dom.length; i++) {
						try {dom[i].style.visibility = sVisibilityStyle;}
						catch (e) {}
					}
				}
			}

			JagUrlDialog._instances[this.CID] = this;
		}

		JagUrlDialog.pref = 'jg_urldlg_';
		JagUrlDialog.cnt  = 0;

		JagUrlDialog._instances      = new Object();

		JagUrlDialog.getInstanceById = function(sId){
			return JagUrlDialog._instances[sId];
		}

		/* volat staticky ve strance uvnitr popupu (v iframe) pro ziskani odkazu na instanci JagUrlDialog, ktera obaluje
		   popup obaluje... tj. neco na zpusob
		   parent.window.JagUrlDialog.getInstanceByIframeWindow(window);
		   Po ziskani instance je napr. mozne volat inst.close(), inst.addButton(...)...;
		   !!! Testovano jen ve FF a MIE - pro dalsi kompatibilitu nutno overit dostupnost iframeTag.contentWindow. Pro dostupne vlastnosti staci pouzit Jag.debug(iframeTag)
		*/
		JagUrlDialog.getInstanceByIframeWindow = function(iframeWindow)
		{
			var iframes = document.getElementsByTagName('iframe');
			for(var i=0; i<iframes.length; i++){
			   if(iframes.item(i).contentWindow == iframeWindow){
			   	   //get id of html component container
				   var el = iframes.item(i).parentNode;
				   while(el!=null){
				   	   if(el.className == 'JagDlg') return JagUrlDialog.getInstanceById(el.id);
					   el = el.parentNode;
				   }
				   break;
			   }
			}
		    return null;
		}
		/* jen pro pohodlnejsi volani */
		JagUrlDialog.open = function(sTitle, sUrl, iWidth, iHeight, bShowCloseButt)
        {
           var dlg = new JagUrlDialog();
               dlg.open(sTitle, sUrl, iWidth, iHeight, bShowCloseButt);
               return dlg;
        }

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
		return false;
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return false;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();
