// Prepinanie class tlacitka
function button(obj, oldClass) {
	if ( !oldClass ) {
		oldClass = "button";
	}	
	if ( obj.className == oldClass ) {
		obj.className+= " active";
	} else {
		obj.className = oldClass;
	}
}

// Prepinanie display block / none
function hideUnhide(object) {
	if ( document.getElementById(object).style.display == "block" ) {	
		document.getElementById(object).style.display = "none";
	} else {
		document.getElementById(object).style.display = "block";
	}
	return false;
}


/*
			PHP funkcie
*/
function nl2br (str) {
    var breakTag = '<br />';
    return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1'+ breakTag +'$2');
}
function str_replace (search, replace, subject) {
	var result = "";
	var oldi = 0;
	for (i = subject.indexOf (search); i > -1; i = subject.indexOf (search, i)) {
		result += subject.substring (oldi, i);
		result += replace;
		i += search.length;
		oldi = i;
	}
	return result + subject.substring (oldi, subject.length);
}
/*
			end of PHP funkcie
*/


var ie5 = (document.getElementById && document.all); 
var ns6 = (document.getElementById && !document.all);

var mouseX = -60;
var mouseY = 22;

var help = false;

function getDocumentScrollTop()
{
	var scrOfY = 0;
	if(typeof(window.pageYOffset) == 'number') {
		scrOfY = window.pageYOffset;
	} else if(document.body && (document.body.scrollLeft || document.body.scrollTop)) {
		scrOfY = document.body.scrollTop;
	} else if(document.documentElement &&
		(document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
	  	scrOfY = document.documentElement.scrollTop;
	}
	return scrOfY
}


function getDocumentScrollLeft() {
	var scrOfX = 0;
	if(typeof( window.pageYOffset) == 'number') {
		 scrOfX = window.pageXOffset;
	} else if(document.body && (document.body.scrollLeft || document.body.scrollTop )) {
		 scrOfX = document.body.scrollLeft;
	} else if(document.documentElement &&
	    (document.documentElement.scrollLeft || document.documentElement.scrollTop )) {
		 scrOfX = document.documentElement.scrollLeft;
	}
	return scrOfX;
}


function changeHelp(object)
{
	if (help)
	{		
		help.style.visibility = 'visible';
		help.style.display = 'block';
		velky = str_replace("small","big",object.src);
		help.innerHTML = '<div ><img id="obr_nahlad" src="'+velky+'" alt="" /></div>';
	}
}

function hideHelp()
{
	if (help)
	{
		help.style.visibility='hidden';
		help.style.display='none';
		
	}	
}

function getMouseMove(e){
	if(ie5){
		help.style.left = getDocumentScrollLeft() + event.clientX + mouseX - (document.getElementById('obr_nahlad').width/2);
		help.style.top = getDocumentScrollTop() + event.clientY - mouseY - document.getElementById('obr_nahlad').height;
	}
	else if(ns6){
		help.style.left = window.pageXOffset + e.clientX + mouseX - (document.getElementById('obr_nahlad').width/2) + 'px';
		help.style.top = window.pageYOffset + e.clientY - mouseY - document.getElementById('obr_nahlad').height + 'px';
	}
}




function helpInit()
{
	if (ie5 || ns6) 
	{	
		help = document.getElementById('helpbox');
		if ( document.getElementById('obr_nahlad') ) {
			document.onmousemove=getMouseMove;
		}
	}
}

var helpText = new Array();

var ajaxVykonaj = {
	req: null,
	init: function() {
		this.req = this.getObj( "Msxml2.XMLHTTP" );
		if( !this.req ) 
			this.req = this.getObj( "Microsoft.XMLHTTP" );
		if( !this.req ) {
			if( typeof XMLHttpRequest!="undefined" )
			this.req = new XMLHttpRequest();
		};
		if( !this.req ) {
			alert( "Ajax nefunguje ve vašem " + "neschopném prohlížeci." );
		}
		self.encFunc = encodeURIComponent ?
		encodeURIComponent : escape;
	},
	
	getObj: function( obj ) {
		try { return new ActiveXObject( obj ); }
		catch(e) { return null; }
	},
	
	request: function( urlCode , funkcia, parametre) {
		urlCode = str_replace( "&amp;","&", urlCode ); 
		//alert(urlCode);
		this.funkcia = funkcia;
		this.parametre = parametre;
		this.init();
		var url = urlCode;
		if( this.req != null ) {
			this.req.onreadystatechange = this.result;
			this.req.open( "GET", url, true );
			this.req.send( null );
		}
	},
	
	result: function() {
		if( ajaxVykonaj.req.readyState == 4 ) {
			if( ajaxVykonaj.req.status == 200 ) {
				try {
					//alert(eval("ajaxVykonaj.req.responseText"));
					if ( ajaxVykonaj.funkcia == "zmen_obsah" ) {
						ajaxVykonaj.zmen_obsah(ajaxVykonaj.parametre,eval("ajaxVykonaj.req.responseText"));
					} else if ( ajaxVykonaj.funkcia == "skontroluj" ) {
						eval(ajaxVykonaj.parametre+'('+eval("ajaxVykonaj.req.responseText")+')');		
					} else if ( typeof(ajaxVykonaj.funkcia) != "undefined" ) {
						if ( typeof(ajaxVykonaj.parametre) != "undefined" ) {
							eval(ajaxVykonaj.funkcia+'("'+ajaxVykonaj.parametre+'")');									
						} else {
							eval(ajaxVykonaj.funkcia+'()');									
						}
					}
				}
				catch( e ) {
					alert( e.description );
				}					
			}
			else {
				alert("Problem: " + ajaxVykonaj.req.statusText);
			}	
		}
	},
	
	zmen_obsah: function(id_div, obsah) {	
		document.getElementById(id_div).innerHTML = obsah;
		if (document.getElementById(id_div).style.display == "none" ) {
			document.getElementById(id_div).style.display = "block";
		}
	}
}

var ajaxOkno = {
	req: null,
	div_id: null,
	nadpis: "",
	init: function() {
		this.req = this.getObj( "Msxml2.XMLHTTP" );
		if( !this.req ) 
			this.req = this.getObj( "Microsoft.XMLHTTP" );
		if( !this.req ) {
			if( typeof XMLHttpRequest!="undefined" )
			this.req = new XMLHttpRequest();
		};
		if( !this.req ) {
			alert( "Ajax nefunguje ve vašem " + "neschopném prohlížeci." );
		}
		self.encFunc = encodeURIComponent ?
		encodeURIComponent : escape;
	},
	
	getObj: function( obj ) {
		try { return new ActiveXObject( obj ); }
		catch(e) { return null; }
	},
	
	request: function( urlCode , nadpis, funkcie, parametre ) {
		urlCode = str_replace( "&amp;","&", urlCode ); 
		//alert(urlCode);
		this.nadpis = nadpis;
		this.funkcie = funkcie;
		this.parametre = parametre;
		this.init();
		this.div_id = 'ajax_okno';
		var url = urlCode;
		if( this.req != null ) {
			this.loading();
			this.req.onreadystatechange = this.result;
			this.req.open( "GET", url, true );
			this.req.send( null );
		}
	},
	
	result: function() {
		if( ajaxOkno.req.readyState == 4 ) {
			if( ajaxOkno.req.status == 200 ) {
				try {
					ajaxOkno.okno(eval("ajaxOkno.req.responseText"));
					if ( typeof(ajaxOkno.funkcie) != "undefined" ) {
						if ( typeof(ajaxOkno.parametre) != "undefined" ) {
							eval(ajaxOkno.funkcie+'("'+ajaxOkno.parametre+'")');									
						} else {
							eval(ajaxOkno.funkcie+'()');									
						}
					}
					// ak existuje element s idckom focus ..
					var focus_element = document.getElementById('focus');
					if ( focus_element != null ) {	
						focus_element.focus();
					}
				}
				catch( e ) {
					alert( e.description );
				}					
			}
			else {
				//alert("Problem: " + ajaxOkno.req.statusText);
				ajaxOkno.okno("Problem: " + ajaxOkno.req.statusText);
			}	
		}
	},
	
	loading: function() {
		document.getElementById(this.div_id).className = 'ajax_okno loading';
		document.getElementById(this.div_id).innerHTML = '<div class="obal"><div class="tien" id="tien"></div><div class="okno loading" id="ajax_vnutro"><table><tr class="vrch"><td><div>'+this.nadpis+'<a class="escape" href="#" onclick="ajaxOkno.zavriOkno() ;return false;" ></a></div></td></tr><tr class="stred" ><td><div id="ajax_obsah"></div><div class="loading" id="loader"><object height="36" width="36" data="'+absolut_cesta+'admin/img/design/ajax_loader.swf" type="application/x-shockwave-flash"><param value="'+absolut_cesta+'admin/img/design/ajax_loader.swf" name="movie"/><param value="high" name="quality"/></object></div></td></tr></table></div></div>';
		ajaxOkno.centruj();
		ajaxOkno.setOpacity(6);
	},
	
	okno: function(text) {
		document.getElementById("ajax_obsah").style.display = "none";			
		document.getElementById("ajax_obsah").className = 'ajax_okno_obsah';
		document.getElementById("ajax_obsah").innerHTML = text;
		document.getElementById("ajax_obsah").onload = ajaxOkno.prehod();
	},
	
	centruj: function() {
		// pozicia
		document.getElementById("ajax_vnutro").style.left = ((screen.availWidth - 850) )/screen.availWidth*50-2+"%";
	},
	
	prehod: function() {
		document.getElementById("ajax_vnutro").className = 'okno';
		document.getElementById("loader").style.display = "none";		
		document.getElementById("ajax_obsah").style.display = "block";		
	},
	
	setOpacity: function(value) {
		document.getElementById('tien').style.opacity = value/10;
		document.getElementById('tien').style.filter = 'alpha(opacity=' + value*10 + ')';
	},
	
	zavriOkno: function() {
		document.getElementById(this.div_id).className = 'ajax_okno';
		document.getElementById(this.div_id).innerHTML = "";
	} 
}


// po stlaceni escape 
document.onkeyup = function (e) {
	var KeyID = (window.event) ? event.keyCode : e.keyCode;
	if (KeyID == 27) { // esc
		ajaxOkno.zavriOkno();
	}
}

