// GETvar myAjax = new Ajax.Request(url,{method:'get',parameters:Form.serialize('form_id'),onComplete:showResponse,onFailure:reportError});

// GLOBAL
var stred			= '#cde890';	// String - bublifuk ve stredu
var obsah			= '#40799E';	// String - v obsahu bublifuku
var menu			= '#639aba';	// String - menu
var obsah2			= '#4788ae';	// String - pozadi u spodu stranky
var login_okno		= null;			// okno pro login
var posledni		= 'last';		// String - po vlozeni se aktualizuje tento input;
var scroll_offset	= -40;			// Integer - po vlozeni se posune +- parametr urceni
/**
 * common. major part by marek.malysz@gmail.com
 *
 */
Effect.Bublifuk = function(e,x) {
	var el = $(e);
	new Effect.BlindDown(el,{queue:{position:'end',scope:'bloky'}});
	new Effect.Highlight(el,{queue:{position:'end',scope:'bloky'},endcolor:x});
}
var debug = function(t){
//	alert("DBG: \n"+t.responseText);
}
// a123 => 123
var parseId = function (s){
	return s.substring(1,s.length);
}
/**
 * prida do divu s textama tlacitka pro adminy
 */
function adminLinks(){
	var vlozOdkazy = function(di){
		if (di.firstChild.nodeName.toUpperCase() != 'B'){
  			new Insertion.Top(di.id, '<b title="čekej"   class="admin wait visible" id="w_'+di.id+'" style="display:none;"></b>');
  			new Insertion.Top(di.id, '<b title="uložit"  class="admin save visible" id="s_'+di.id+'" onclick="save(\''+di.id+'\')" style="display:none;"></b>');
			new Insertion.Top(di.id, '<b title="upravit" class="admin edit" id="e_'+di.id+'" onclick="upravit(\''+di.id+'\')"></b>');
  			new Insertion.Top(di.id, '<b title="smazat"  class="admin del"  id="d_'+di.id+'"  onclick="smazat(\''+di.id+'\')"></b>');
  			new Insertion.Top(di.id, '<b title="zavřít"  class="admin del visible"  id="q_'+di.id+'" onclick="nic(\''+di.id+'\')" style="display:none;"></b>');
		}
	}
	$$('div.'+xmlChild).each(vlozOdkazy);
}
/**
 * prida do divu s textama tlacitka pro adminy
 */
function removeAdminLinks(){
	$$('div.'+xmlChild+' b.admin').each(Element.remove);
}
/**
 * prida do stredu formy pro adminy
 */
function adminForms(){
	$$('div.stred div.proadmina').each(function(x){
		new Effect.Bublifuk(x,stred);
	});
	$$('div.obsah div.proadmina').each(function(x){
		new Effect.Bublifuk(x,obsah);
	});
}
function removeAdminForms(){
	$$('div.proadmina').each(Element.hide);
}

/**
 * fid - string id formulare
 * sid - string id submitu
 * url - string
 * err - string id chyboveho vystupu
 * out - string id mista vysptupu
 *
 * ex.: pridej('formnastenka','nastenkasubmit','http://','err', 'tukej','vzkaz');
 */
function pridej(fid,sid,url,err,out){ //,xml
	var casNaOpravu = new Array();// 60; // sekund
	casNaOpravu['vzkaz']		= 30;
	casNaOpravu['komentar']	= 30;
	casNaOpravu['clanek']	= 60;
	casNaOpravu['jednaakce']= 60;

	var sim = ['vzkaz','clanek','jednaakce','komentar']; // fce must exist
	if (!sim.exists(xmlChild)) {  //xml - string root_child of xml && eXtra fce
		alert('Method&time  ' + xmlChild + ' in pridej() not implemented!');
		return;
	}
	
 	var vzkaz = function(doc){
		var atts = doc.attributes;
		var cssClass = '';
		var id =atts.getNamedItem('id').value;
		//var txt ='<div class="vzkaz" style="display:none" id="a'+id+'">';
		var txt ='<div class="vzkaz" style="display:none" id="a'+id+'">';
		if (atts.length>1){ 
			cssClass=atts.getNamedItem('pohlavi').value;
		}
		txt +='<h2 class="'+cssClass+'">'+doc.getElementsByTagName('autor')[0].firstChild.data+'</h2>';
		txt +='<h3>'+doc.getElementsByTagName('datum')[0].firstChild.data;
		if (doc.getElementsByTagName('datum2').length>0){
			txt += '<br/><span class="cas">' + doc.getElementsByTagName('datum2')[0].firstChild.data + '</span>';
		}
		txt +='</h3>';
		if (doc.getElementsByTagName('ip').length>0){
			txt += '<span class="ip">(' + doc.getElementsByTagName('ip')[0].firstChild.data + ')</span>';
		}
		txt +='<div id="nahleda'+id+'">';
		//txt +='<p>'+doc.getElementsByTagName('txt')[0].firstChild.data+'</p>';
		txt +=doc.getElementsByTagName('txt')[0].firstChild.data;
		txt +='</div></div>';
		//txt +='<div style="clear:both"></div>';// asi ne kvuli inline txt2html
		new Insertion.After(out,txt);
		new Effect.Bublifuk('a'+id,obsah);
		// aktualizuju posledni id - pro select
		if (posledni!='')	$(posledni).value = id;
	}
 	var komentar = function(doc){
		var atts = doc.attributes;
		var cssClass = '';
		var id =atts.getNamedItem('id').value;
		//var txt ='<div class="vzkaz" style="display:none" id="a'+id+'">';
		var txt ='<div class="komentar" style="display:none" id="a'+id+'">';
		if (atts.length>1){ 
			cssClass=atts.getNamedItem('pohlavi').value;
		}
		txt +='<h2 class="'+cssClass+'">'+doc.getElementsByTagName('autor')[0].firstChild.data+'</h2>';
		txt +='<h3>'+doc.getElementsByTagName('datum')[0].firstChild.data;
		if (doc.getElementsByTagName('datum2').length>0){
			txt += '<br/><span class="cas">' + doc.getElementsByTagName('datum2')[0].firstChild.data + '</span>';
		}
		txt +='</h3>';
		if (doc.getElementsByTagName('ip').length>0){
			txt += '<span class="ip">(' + doc.getElementsByTagName('ip')[0].firstChild.data + ')</span>';
		}
		txt +='<div id="nahleda'+id+'">';
		txt +=doc.getElementsByTagName('txt')[0].firstChild.data;
		txt +='</div></div>';
		//txt +='<div style="clear:both"></div>';// asi ne kvuli inline txt2html
		new Insertion.Before(out,txt);
		new Effect.Bublifuk('a'+id,obsah);
		// aktualizuju posledni id - pro select
		if (posledni!='')	$(posledni).value = id;
	}
	var clanek = function(doc){
		var url_clanku = '/clanek/?c=';
		var atts = doc.attributes;
		var id = atts.getNamedItem('id').value;
		var ssid = atts.getNamedItem('sid').value;
		//var txt ='<div class="clanek" style="display:none" id="a'+id+'">';
		var txt ='<div class="clanek" style="display:none" id="a'+id+'">';
		txt +='<h2><a href="'+url_clanku+ssid+'">'+doc.getElementsByTagName('nadpis')[0].firstChild.data+'</a></h2>';
		txt +='<h3>'+doc.getElementsByTagName('autor')[0].firstChild.data;
		txt +='<br/>'+doc.getElementsByTagName('datum')[0].firstChild.data+'</h3>';
		txt +='<div id="nahleda'+id+'">';
		txt +=doc.getElementsByTagName('txt')[0].firstChild.data;
		txt +='</div><p class="links">';
		txt +='<a href="'+url_clanku+ssid+'#komentare">0 komentářů</a>';
		txt +='</p></div>';
		new Insertion.After(out,txt);
		new Effect.Bublifuk('a'+id,obsah);
		// aktualizuju posledni id - pro select
		if (posledni!='')	$(posledni).value = id;
	}
	var jednaakce = function(doc){
		var atts = doc.attributes;
		var id = atts.getNamedItem('id').value;
		var koncovka='';
		if (atts.length>1){ 
			koncovka=atts.getNamedItem('pohlavi').value=='on' ? '' : 'a';
		}
		var txt ='<div class="jednaakce" style="display:none" id="a'+id+'">';
		txt +='<h2>'+doc.getElementsByTagName('nadpis')[0].firstChild.data+'</h2>';
		txt +='<div id="nahleda'+id+'">';
		txt +='<h3>'+doc.getElementsByTagName('datum')[0].firstChild.data;
		if (doc.getElementsByTagName('datum2').length>0){
			txt +=' &mdash; '+doc.getElementsByTagName('datum2')[0].firstChild.data;
		}
		txt +='<span class="nick">, přidal'+ koncovka+ ' '+doc.getElementsByTagName('autor')[0].firstChild.data+'</span></h3>';
		txt +=doc.getElementsByTagName('txt')[0].firstChild.data;
		txt +='</div></div>';
		new Insertion.After(out,txt);
		new Effect.Bublifuk('a'+id,obsah);
		// aktualizuju posledni id - pro select
		if (posledni!='')	$(posledni).value = id;
	}
	
	var reportError = function(request){
		var d = $(err);
		d.style.display='none';
		d.innerHTML = 'Chyba na serveru. Zkus to za chvilku...';
		new Effect.Bublifuk(err,stred);
		Form.enable(fid);
		$(sid).value = "Odeslat";
	}
	
	var showResponse	= function (originalRequest){
		debug(originalRequest);
		var d = $(err);
		if (originalRequest.responseXML.getElementsByTagName('error').length>0){
			d.style.display='none';
			d.innerHTML = originalRequest.responseXML.getElementsByTagName('error')[0].firstChild.data;
			new Effect.Bublifuk(err,(xmlChild=='komentar'?obsah : stred));
		}
		else{
			d.style.display='none';//vypnu error, pokud se pridalo
			new Effect.ScrollTo(out,{offset:scroll_offset,queue: {position:'front',scope:'bloky'}});
			var tagy = originalRequest.responseXML.getElementsByTagName(xmlChild);
			// volam fci xml()
			$A(tagy).each(eval(xmlChild));
			// necham moznost upravy
			if (posledni!=''){
				var posledni_id = 'a'+$F(posledni);
	  			new Insertion.Top(posledni_id, '<b title="čekej"   class="admin wait visible" id="w_'+posledni_id+'" style="display:none;"></b>');
	  			new Insertion.Top(posledni_id, '<b title="uložit"  class="admin save visible" id="s_'+posledni_id+'" onclick="save(\''+posledni_id+'\')" style="display:none;"></b>');
				new Insertion.Top(posledni_id, '<b title="upravit" class="admin edit visible" id="e_'+posledni_id+'" onclick="upravit(\''+posledni_id+'\')"></b>');
	  			new Insertion.Top(posledni_id, '<b title="zavřít"  class="admin del visible"  id="q_'+posledni_id+'" onclick="nic(\''+posledni_id+'\')" style="display:none;"></b>');
	  			new Insertion.Top(posledni_id, '<b title="zbývá"   class="admin time visible" id="t_'+posledni_id+'">'+casNaOpravu[xmlChild]+'</b>');
	  			
		  		var opt = {
		  			queue:{position:'end',scope:'odpocitavani',limit:1},
		  			afterFinish:function(effect){
		  				var id_str = effect.element.id.substring(2);
		  				['e','s','w','q','t'].each(function(x){
		  					new Effect.Fade(x+'_'+id_str,{queue:{position:'end',scope:'tlacitka'},duration:.2,afterFinish:function(){Element.remove(x+'_'+id_str)}});
		  				});
		  				if ($('zaloha'+id_str)){
		  					$('nahled'+id_str).innerHTML =$('zaloha'+id_str).innerHTML;
	  					}
	  				},fps:1,duration:casNaOpravu[xmlChild],transition:Effect.Transitions.linear
		  		}
				new Effect.CashRegister('t_'+posledni_id,0,opt);
			}
			Form.reset(fid);
		}
		Form.enable(fid);
		$(sid).value = "Odeslat";
	}
	
	Form.disable(fid);
	$(sid).value = 'počkej...';

	var myAjax=new Ajax.Request(url,{method:'post',postBody:Form.serialize(fid),onSuccess:showResponse,onFailure:reportError});
}

/**
* uses global posledni
* out / string id mista vystupu
*/
function stahujNove() {
	var out = "tukej";
	
	var sim = ['vzkaz']; // fce must exist
	if (!sim.exists(xmlChild)) {  //xml - string root_child of xml && eXtra fce
		alert('Method  ' + xmlChild + ' in stahujNove() not implemented!');
		return;
	}
	
	var vzkaz = function(doc){
		var atts = doc.attributes;
		var cssClass = '';
		var id =atts.getNamedItem('id').value;
		//var txt ='<div class="vzkaz" style="display:none" id="a'+id+'">';
		var txt ='<div class="vzkaz" style="display:none" id="a'+id+'">';
		if (atts.length>1){ 
			cssClass=atts.getNamedItem('pohlavi').value;
		}
		txt +='<h2 class="'+cssClass+'">'+doc.getElementsByTagName('autor')[0].firstChild.data+'</h2>';
		txt +='<h3>'+doc.getElementsByTagName('datum')[0].firstChild.data;
		if (doc.getElementsByTagName('datum2').length>0){
			txt += '<br/><span class="cas">' + doc.getElementsByTagName('datum2')[0].firstChild.data + '</span>';
		}
		txt +='</h3>';
		if (doc.getElementsByTagName('ip').length>0){
			txt += '<span class="ip">(' + doc.getElementsByTagName('ip')[0].firstChild.data + ')</span>';
		}
		txt +='<div id="nahleda'+id+'">';
		//txt +='<p>'+doc.getElementsByTagName('txt')[0].firstChild.data+'</p>';
		txt +=doc.getElementsByTagName('txt')[0].firstChild.data;
		txt +='</div></div>';
		//txt +='<div style="clear:both"></div>';// asi ne kvuli inline txt2html
		new Insertion.After(out,txt);
		new Effect.Bublifuk('a'+id,obsah);
		// aktualizuju posledni id - pro select
		if (posledni!='')	$(posledni).value = id;
	}
			
	var showResponse = function (originalRequest){
		debug(originalRequest);
		if (originalRequest.responseXML.getElementsByTagName(xmlChild).length>0){
			var tagy = originalRequest.responseXML.getElementsByTagName(xmlChild);
			// volam fci xml()
			$A(tagy).each(eval(xmlChild));
		}
	}

	var url = '/xmlNove.php'; //file:///home/www/new/source.xml';
	
	var qs = 'from='+$(posledni).value+'&table='+xmlChild;
	var myAjax =  new Ajax.Request(url, {method:'post',postBody:qs,onSuccess:showResponse});
}

/**
 * out - string id mista vysptupu
 * err - string id chyboveho vystupu
 * buton - string id tlacitka
 *
 * ex.: starsi('addon','addonerr');
 */
function starsi(out,err,buton){
	var sim = ['vzkaz']; // fce must exist
	if (!sim.exists(xmlChild)) {  //xml - string root_child of xml && eXtra fce
		alert('Method  ' + xmlChild + ' in starsi() not implemented!');
		return;
	}
	
 	var vzkaz = function(doc){
		var atts = doc.attributes;
		var cssClass = '';
		var id =atts.getNamedItem('id').value;
		//var txt ='<div class="vzkaz" style="display:none" id="a'+id+'">';
		var txt ='<div class="vzkaz" style="display:none" id="a'+id+'">';
		if (atts.length>1){ 
			cssClass=atts.getNamedItem('pohlavi').value;
		}
		txt +='<h2 class="'+cssClass+'">'+doc.getElementsByTagName('autor')[0].firstChild.data+'</h2>';
		txt +='<h3>'+doc.getElementsByTagName('datum')[0].firstChild.data;
		if (doc.getElementsByTagName('datum2').length>0){
			txt += '<br/><span class="cas">' + doc.getElementsByTagName('datum2')[0].firstChild.data + '</span>';
		}
		txt +='</h3>';
		if (doc.getElementsByTagName('ip').length>0){
			txt += '<span class="ip">(' + doc.getElementsByTagName('ip')[0].firstChild.data + ')</span>';
		}
		txt +='<div id="nahleda'+id+'">';
		//txt +='<p>'+doc.getElementsByTagName('txt')[0].firstChild.data+'</p>';
		txt +=doc.getElementsByTagName('txt')[0].firstChild.data;
		txt +='</div></div>';
		//txt +='<div style="clear:both"></div>';// asi ne kvuli inline txt2html
		new Insertion.Before(out,txt);
		//new Effect.Bublifuk('a'+id,obsah2);
		new Effect.BlindDown('a'+id,{queue:{position:'end',scope:'bloky'}});
		new Effect.ScrollTo(out,{queue:{position:'end',scope:'bloky'}});
		// aktualizuju posledni id - pro select
		$(out).setAttribute('title', id);
	}
 		
	var reportError = function(request){
		var d = $(err);
		d.style.display='none';
		d.innerHTML = 'Chyba na serveru. Zkus to za chvilku...';
		new Effect.Bublifuk(err,obsah2);
		$(buton).innerHTML = "zobrazit starší";
	}
	
	var showResponse = function (originalRequest){
		debug(originalRequest);
		var d = $(err);
		if (originalRequest.responseXML.getElementsByTagName('error').length>0){
			d.style.display='none';
			d.innerHTML = originalRequest.responseXML.getElementsByTagName('error')[0].firstChild.data;
			new Effect.Bublifuk(err,obsah2);
		}
		else{
			d.style.display='none';//vypnu error, pokud se pridalo
			var tagy = originalRequest.responseXML.getElementsByTagName(xmlChild);
			// volam fci xml()
			$A(tagy).each(eval(xmlChild));
		}
		$(buton).innerHTML = "zobrazit starší";
	}
	
	$(buton).innerHTML = 'počkej...';

	var url = '/xmlStarsi.php'; //file:///home/www/new/source.xml';
	
	var qs = 'from='+$(out).getAttribute('title')+'&table='+xmlChild;
	var myAjax=new Ajax.Request(url,{method:'post',postBody:qs,onSuccess:showResponse,onFailure:reportError});
}
/**
 * Tlacitko
 * 1) vytvori zalohu s puvodnim obsahem, textareu se zdrojem 
 * 0) 
 * 
 */
function upravit(str){
	var sim = ['vzkaz','clanek','jednaakce','komentar','clovek']; // fce must exist
	var h = 20; // + vyska v px 
	if (!sim.exists(xmlChild)) {  //xml - string root_child of xml && eXtra fce
		alert('Method ' + xmlChild + ' in upravit() not implemented!');
		return;
	}

	var txt = '';
	var clovek = function(doc){
		function x(p_s){
			if (doc.getElementsByTagName(p_s)[0].firstChild.data=='y'){
				return 'checked="checked" ';
			}
			return '';
		}
		txt +='<label><input type="checkbox" value="y" name="mladez" '+x('mladez')+'/> mládež</label><br />';
		txt +='<label><input type="checkbox" value="y" name="clanek" '+x('clanek')+'/> blog</label><br />';
		txt +='<label><input type="checkbox" value="y" name="myclanek" '+x('myclanek')+'/> vlastní článek</label><br />';
		txt +='<label><input type="checkbox" value="y" name="jednaakce" '+x('jednaakce')+'/> akce</label><br />';
		txt +='<label><input type="checkbox" value="y" name="myjednaakce" '+x('myjednaakce')+'/> vlastní akce</label><br />';
		txt +='<label><input type="checkbox" value="y" name="komentar" '+x('komentar')+'/> komentář</label><br />';
		txt +='<label><input type="checkbox" value="y" name="vzkaz" '+x('vzkaz')+'/> nástěnka</label>';
	}

	var jednaakce = function(doc){
		var d2 = 'datum konce';
		if (doc.getElementsByTagName('datum2').length>0){
			d2 = doc.getElementsByTagName('datum2')[0].firstChild.data; // datum poku neni null v db
		}
		txt +='<input type="text" class="text" name="dat1" id="d1'+str+'" value="'+doc.getElementsByTagName('datum1')[0].firstChild.data+'"/> <input type="button" class="button" value="vyber" onclick="cal1_okno=new LITBox(\'\',{type:\'message\',titulek:\'Datum začátku\',overlay:false,width:300,height:200,cssClass:\'kalyndorz\'});kal1=new Kalyndorz(cal1_okno.getWindowElement(),\'kal1\',{potom:function(){$(\'d1'+str+'\').value=kal1.getDatum();cal1_okno.remove();},previous:[parseDate($F(\'d1'+str+'\')),parseDate($F(\'d2'+str+'\'))]});" />';
		txt +='<input type="text" class="text" name="dat2" id="d2'+str+'" value="'+d2+'"/> <input type="button" class="button" value="vyber" onclick="cal2_okno=new LITBox(\'\',{type:\'message\',titulek:\'Datum konce\'  ,overlay:false,width:300,height:200,cssClass:\'kalyndorz\'});kal2=new Kalyndorz(cal2_okno.getWindowElement(),\'kal2\',{potom:function(){$(\'d2'+str+'\').value=kal2.getDatum();cal2_okno.remove();},previous:[parseDate($F(\'d2'+str+'\')),parseDate($F(\'d1'+str+'\'))]});" />';
		txt +='<textarea id="txt'+str+'" name="txt" style="height:'+(Element.getHeight(str)+h)+'px" >'+doc.getElementsByTagName('txt')[0].firstChild.data + '</textarea>';
	}
	var vzkaz = function(doc){
		txt += '<textarea id="txt'+str+'" name="txt" style="height:'+(Element.getHeight(str)+h)+'px" >'+doc.getElementsByTagName('txt')[0].firstChild.data + '</textarea>';
	}
	var komentar = function(doc){
		txt += '<textarea id="txt'+str+'" name="txt" style="height:'+(Element.getHeight(str)+h)+'px" >'+doc.getElementsByTagName('txt')[0].firstChild.data + '</textarea>';
	}
	var clanek = function(doc){
		txt += '<textarea id="txt'+str+'" name="txt" style="height:'+(Element.getHeight(str)+h)+'px" >'+doc.getElementsByTagName('txt')[0].firstChild.data + '</textarea>';
	}
	
	var showResponse = function (originalRequest){
		debug(originalRequest);
		new Insertion.After('nahled'+str, '<div style="display:none" id="zaloha'+str+'">'+$('nahled'+str).innerHTML+'</div>');
		var tagy = originalRequest.responseXML.getElementsByTagName('source'+xmlChild);
		// volam fci xml()
		$A(tagy).each(eval(xmlChild));
		// vytvoreny $txt se vlozi do puvodniho nahledu
		//$('nahled'+str).innerHTML = '<p><form id="form'+str+'">'+txt+'</form></p>';
		$('nahled'+str).innerHTML = '<form id="form'+str+'">'+txt+'</form>';
		
		//$('txt'+str).focus();
		//Form.focusFirstElement('form'+str);
		
		Element.hide('e_'+str);		
		if ($('d_'+str))Element.hide('d_'+str);
		Element.show('s_'+str);
		Element.show('q_'+str);
		if ($('d_'+str)){
			new Effect.Pulsate('q_'+str,{duration:1.5});
		}
		Element.hide('w_'+str);
	}
	
	var reportError = function (originalRequest){
		Element.hide('w_'+str);
	}
	
	var url = '/xmlSource.php'; //file:///home/www/new/source.xml';
	
	Element.show('w_'+str);

	var qs = 'id='+parseId(str)+'&table='+xmlChild;
	var myAjax = new Ajax.Request(url,{method:'post',postBody:qs,onSuccess:showResponse,onFailure:reportError});
}
/**
 * nacte puvodni obsah i tlacitka
 * zrusi textarea, zalohu
 */
function nic(str){
	$('nahled'+str).innerHTML =$('zaloha'+str).innerHTML;
	Element.remove('zaloha'+str);
	Element.hide('q_'+str);
	Element.hide('s_'+str);
	Element.show('e_'+str);
	if ($('d_'+str)){
		Element.show('d_'+str);
		new Effect.Pulsate('d_'+str,{duration:1.5});
	}
	
	if ($('w_'+str)) Element.hide('w_'+str);
}
/**
 * Tlačítko uložit
 * posle pozadavek s novym textem
 */
function save(str){
	var sim = ['vzkaz','clanek','jednaakce','komentar','clovek']; // fce must exist
	if (!sim.exists(xmlChild)) {  //xml - string root_child of xml && eXtra fce
		alert('Method&handle ' + xmlChild + ' in save() not implemented!');
		return;
	}
 	var jednaakce = function(doc){
 		var txt = '<h3><span class="datum">'+doc.getElementsByTagName('datum')[0].firstChild.data;
		if (doc.getElementsByTagName('datum2').length>0){
			txt +=' &mdash; '+doc.getElementsByTagName('datum2')[0].firstChild.data;
		}
		// v pridej musi byt dobra struktura html aby to haslo $A(....
		txt +='</span><span class="nick">' + $A($$('#zaloha'+str+' span.nick'))[0].innerHTML + '</span></h3>';
 		$('zaloha'+str).innerHTML = txt + doc.getElementsByTagName('txt')[0].firstChild.data;
	}
 	var clovek = function(doc){
 		$('zaloha'+str).innerHTML = '<p>'+doc.getElementsByTagName('txt')[0].firstChild.data+'</p>';
	}
 	var vzkaz = function(doc){
 		$('zaloha'+str).innerHTML = doc.getElementsByTagName('txt')[0].firstChild.data;
	}
 	var komentar = function(doc){
 		$('zaloha'+str).innerHTML = doc.getElementsByTagName('txt')[0].firstChild.data;
	}
	var clanek = function(doc){
 		$('zaloha'+str).innerHTML = doc.getElementsByTagName('txt')[0].firstChild.data;
	}
	
	var reportError = function (originalRequest){
		alert('Problem na serveru.');
		nic(str);
	}
	var showResponse	= function (originalRequest){
		debug(originalRequest);
		if (originalRequest.responseXML.getElementsByTagName('error').length>0){
			alert(originalRequest.responseXML.getElementsByTagName('error')[0].firstChild.data);
		}
		else{
			var tagy = originalRequest.responseXML.getElementsByTagName(xmlChild);
			// volam fci xml(), ve ktere se nove nacte do zalohy
			$A(tagy).each(eval(xmlChild));
		}
		nic(str); // obnovi se zaloha
	}
	var handle = new Array();
	handle['vzkaz']	= 'Vzkaz';
	handle['clanek']	= 'Clanek';
	handle['jednaakce']= 'Akce';	
	handle['komentar']= 'Komentar';	
	handle['clovek']	= 'Clovek';	
	var url = '/xml'+handle[xmlChild]+'.php';
	
	Element.show('w_'+str);
	
	var qs = Form.serialize('form'+str)+'&update=1&id='+parseId(str);
	var myAjax = new Ajax.Request(url,{method:'post',postBody:qs,onSuccess:showResponse,onFailure:reportError});
}

/**
 * str - string id of div
 * musi mit id=nahled+str
 */
function smazat(str){
	var url = '/xmlDel.php';
	var delka = 30;
	var text = $('nahled'+str).innerHTML.stripTags();
	var h2 = $A($$('div#'+str+' h2'))[0].innerHTML.stripTags();
	
	if (text.length<delka){
		text = text.trim();
	}
	else{
		text = text.substr(0,delka)+' ...';
	}
	if (!confirm('Jsi si jist, že chceš natrvalo odstranit položku\n'+h2+'\n'+text+'?')){return;}

	var showResponse = function (originalRequest){
		debug(originalRequest);
		if (originalRequest.responseXML.getElementsByTagName('error').length>0){
			reportError(originalRequest);
		}
		else{
			if ($A(originalRequest.responseXML.getElementsByTagName('answer'))[0].firstChild.data == 'ok'){
				if (xmlChild=='clovek'){
					new Effect.Opacity(str,{duration:0.5, from:1.0, to:0.3});
				}else{
					new Effect.Fade(str,{afterFinish:function(){Element.remove(str)}});
				}
				Element.hide('w_'+str);
				Element.show('e_'+str);		
				Element.show('d_'+str);
			}
			else{
				reportError(originalRequest);
			}
		}
	}
	var reportError = function (originalRequest){
		if (originalRequest.responseXML.getElementsByTagName('error').length>0){
			alert(originalRequest.responseXML.getElementsByTagName('error')[0].firstChild.data);
		}else if (originalRequest.responseXML.getElementsByTagName('answer').length>0){
			alert(originalRequest.responseXML.getElementsByTagName('answer')[0].firstChild.data);
		}
		//alert(originalRequest.responseText);
		Element.hide('w_'+str);
		Element.show('e_'+str);		
		Element.show('d_'+str);
		new Effect.Shake(str);
	}

	Element.show('w_'+str);
	Element.hide('e_'+str);		
	Element.hide('d_'+str);
	
	var qs = 'id='+parseId(str)+'&table='+xmlChild;
	var myAjax = new Ajax.Request(url,{method:'post',postBody:qs,onSuccess:showResponse,onFailure:reportError});
}

/**
 *http://www.thinkvitamin.com/features/ajax/create-your-own-ajax-effects
 */
Effect.CashRegister = Class.create();
Object.extend(Object.extend(Effect.CashRegister.prototype,Effect.Base.prototype), {
	initialize: function(element, price) {
		var options = arguments[2] || {};
		this.element = $(element);
		//this.startPrice = parseFloat(this.element.innerHTML.substring(1));
		this.startPrice = parseInt(this.element.innerHTML.substring(0));
		this.finishPrice = price;
		this.delta = (this.finishPrice-this.startPrice);
		this.start(options);
	},
	update: function(position) {
		//var value = (this.startPrice + (this.delta*position)).toString().split(’.');
		//var value = (this.startPrice + (this.delta*position)).toString();
		var value = this.startPrice + (this.delta*position);
		Element.update(this.element, '' + Math.round(value));
	}
});

/**
 *	fotky
 */
nahledy = function(){
	var mct = new curvyCorners({tl:{radius:7},tr:{radius:7},bl:{radius:7},br:{radius:7},antiAlias:true,autoPad:false}, "nahled");
	mct.applyCornersToAll();
}

/**
 * css drop shadows
 */
stiny = function(){
	if (!isIe){
		$$('.stred h1').each(function(i){
			i.setAttribute('title',i.innerHTML);
			i.innerHTML='<span>'+i.innerHTML+'</span>';
		});
		$$('.stred h2').each(function(i){
			i.setAttribute('title',i.innerHTML);
			i.innerHTML='<span>'+i.innerHTML+'</span>';
		});
		$$('.stredobsah h1').each(function(i){
			i.setAttribute('title',i.innerHTML);
			i.innerHTML='<span>'+i.innerHTML+'</span>';
		});
		$$('.stredobsah h4').each(function(i){
			i.setAttribute('title',i.innerHTML);
			i.innerHTML='<span>'+i.innerHTML+'</span>';
		});
		$$('.index h2').each(function(i){
			i.setAttribute('title',i.innerHTML);
			i.innerHTML='<span>'+i.innerHTML+'</span>';
		});
	}
}
//addLoadEvent(stiny);
Event.onDOMReady(function() {
	stiny();
});

/**
 * boolean t - nastavi nebo ne priznak disabled
 */
function enable(){setEnabled(true)}
function disable(){setEnabled(false)}
function setEnabled(t){
	if (!isIe){
		var i = document.getElementsByTagName("body").item(0);
		if(t){
			i.removeClassName('disabled');
		}else{
			i.addClassName('disabled');
		}
	//	$$('.stred')
		document.getElementsByClassName("stred").each(function(i){
			if(t)i.removeClassName('disabled');
			else i.addClassName('disabled');
		});
		document.getElementsByClassName("stredobsah").each(function(i){
			if(t)i.removeClassName('disabled');
			else i.addClassName('disabled');
		});
		document.getElementsByClassName("obsah").each(function(i){
			if(t)i.removeClassName('disabled');
			else i.addClassName('disabled');
		});
	}
}

/**
 * fid - string id formulare
 * sid - string id submitu
 * url - string
 * err - string id chyboveho vystupu
 * user - string id kde se zapise prihlaseny nick (NE inputu nicku)
 * nick - string id inputu nicku
 * pas - string id inputu hesla - pro vymazani
 * mem - string id checkboxu - pamatuj nick
 *
 * ex.: <form action="http://" id="formlogin" onsubmit="login('formlogin','loginsubmit','file:///home/www/new/login.xml','oknoerr'); return false;">
 */

function login(fid,sid,url,err,user,nick,pas,mem){
	var puvodniSubmit = $F(sid);
	
	var reportError = function(originalRequest){
		$(pas).value='';
		var d = $(err);
		d.style.display='none';
		d.innerHTML = 'Chyba na serveru. Zkus to za chvilku...';
		new Effect.Bublifuk(err,stred);
		Form.enable(fid);
		$(sid).value = puvodniSubmit;
	}
	
	var showResponse	= function (originalRequest){
		$(pas).value='';
		debug(originalRequest);
		if (originalRequest.responseXML.getElementsByTagName('error').length>0){
			var d = $(err);
			d.style.display='none';
			d.innerHTML = originalRequest.responseXML.getElementsByTagName('error')[0].firstChild.data;
			new Effect.Shake(login_okno.getId(),{queue:{position:'front',scope:'bloky'}});
			new Effect.Bublifuk(err,stred);
		}else{
			if ($A(originalRequest.responseXML.getElementsByTagName('logged'))[0].firstChild.data=='1'){
				Element.hide('m-prihlaseni');
				new Effect.Bublifuk('m-odhlaseni',menu);
				login_okno.remove();
				$(user).innerHTML=$A(originalRequest.responseXML.getElementsByTagName('jmeno'))[0].firstChild.data+'';
				if ($A(originalRequest.responseXML.getElementsByTagName(xmlChild))[0].firstChild.data=='y' || 
					 $A(originalRequest.responseXML.getElementsByTagName('my'+xmlChild))[0].firstChild.data=='y'
				){
					adminLinks();
					adminForms();
				}
			}
		}
		Form.enable(fid);
		$(sid).value = puvodniSubmit;
	}
	Form.disable(fid);
	$(sid).value = 'ověřuji...';

	var vqs = 'username='+$F(nick);
	var vyzva;
	var mam = function (originalRequest){
			var heslo = hex_md5($F(pas));
			vyzva = $A(originalRequest.responseXML.getElementsByTagName('vyzva'))[0].firstChild.data;
			//alert(vyzva+'='+hex_hmac_md5(heslo,vyzva.toString()));
			//alert(vyzva+'='+hex_md5(hex_hmac_md5(heslo,vyzva.toString())));
			var qs = vqs+'&old='+hex_hmac_md5(heslo,vyzva.toString());
			vyzva++;
			//alert(vyzva+'='+hex_md5(hex_hmac_md5(heslo,vyzva.toString())));
			qs 	+= 	 '&new='+hex_md5(hex_hmac_md5(heslo,vyzva.toString()));
			qs 	+= 	 '&pamatuj='+$F(mem);		
			var myAjax = new Ajax.Request(url,{method:'post',postBody:qs,onSuccess:showResponse,onFailure:reportError});	
	}
	
	var myAjax1 = new Ajax.Request('/xmlVyzva.php',{method:'post',postBody:vqs,onSuccess:mam,onFailure:reportError});
}

/**
 * url - string souboru ktery zajisti odhlaseni
 * err - string id chyboveho vystupu
 *
 * ex.: 
 */
function logout(url,err){
//	var xmlChild = 'response';
	
	var reportError = function(request){
		$(err).innerHTML =  'chybné odhlášení';
	}
	
	var showResponse	= function (originalRequest){
		if ($A(originalRequest.responseXML.getElementsByTagName('logged'))[0].firstChild.data=='0'){
			$(err).innerHTML = 'OK';
			new Effect.SlideUp('m-odhlaseni',{afterFinish:function(effect){Element.show('m-prihlaseni');$(err).innerHTML = 'odhlášení';}});
			removeAdminForms();
			removeAdminLinks();
			if (isIe){
				Element.hide('m-odhlaseni');
				Element.hide('odhlaseni');
				Element.hide('user');
			}
		}
		else{
			reportError(originalRequest);
		}
	}
	
	$(err).innerHTML = 'odhlašuji...';

	var myAjax = new Ajax.Request(url,{method:'post',onSuccess:showResponse,onFailure:reportError});
}


/**
 * http://www.svendtofte.com/code/usefull_prototypes/
 * <array>.exists(<value>)
 * <string>.trim()
 */
Array.prototype.exists = function (x) {
	for (var i = 0; i < this.length; i++) {
		if (this[i] == x) return true;
	}
	return false;
}
String.prototype.trim = function() {
    return(this.replace(/^\s+/,'').replace(/\s+$/,''));
}

