var gomb = 0;
var kaaa = 0;

function del_kedvenc(gazda_id,kedvenc_id) {
if (!confirm('\n\Biztos törölni akarja?')) {return false;}
	else{


ajaxRequest = new Ajax.Request(
				'index.php',
				{
					method: 'post',
					parameters: 'hidden=kedvenc_torol&gazda_id=' + gazda_id + '&kedvenc_id=' + kedvenc_id,
					onComplete: function(request){
								var notice = $('ajax_loading');
								var box = $(kedvenc_id);
								new Effect.Fade(kedvenc_id);
							
						
									}, 
	 			        onFailure: function(){
					alert('Valami hiba csuszott be...');
							}	
				}
			);


		}		
}




function login_ok(request){
var button = $('login_submit');
var notice = $('login_ajax');
var pass = $('login_password');
var email = $('login_email');
var response = request.responseText;
var real_response = response.split(':');

		if (real_response[0] == 0){
				  button.disabled = false;
				  email.addClassName('error'); 	
				  pass.addClassName('error'); 	
				  notice.addClassName('error'); 	
				Event.observe(email, 'focus', function(){email.removeClassName('error');notice.update(""); notice.removeClassName('error');} );
				Event.observe(pass, 'focus', function(){pass.removeClassName('error');notice.update(""); notice.removeClassName('error');} );
	        		  notice.update(real_response[1]);
				}else{
					window.location = real_response[1];
					}
}


function loading(what,button){
		  	var button = $(button);
	                var loading = $(what);
	       		loading.update("Töltök...");
			button.disabled = true;
			var img = document.createElement("img");
			img.id = "loading_pic";
			img.src = "images/ajaxupdating.gif";
			loading.appendChild(img);

	                
}

function login(){
var pass = $('login_password').value;
var email = $('login_email').value;

var ajaxRequest = 
		new Ajax.Request(
				'index.php',
				{
					method: 'get',
					parameters: 'a=login&pass='+ pass +'&email='+ email,
					onLoading: 		function(){loading('login_ajax','login_submit');},
					onComplete: function(request){login_ok(request);}, 
	 			        onFailure: function(){
					alert('Valami hiba csuszott be...');
							}	
				}
			);


}








function reg_check(where,type){

var proba = $(where).value;

if (proba.length > 0){

var ajaxRequest = new Ajax.Request(
				'index.php',
				{
					method: 'post',
					parameters: 'hidden=gazda_reg&what=' + proba + '&where=' + where +'&ajax=1&type=' + type,
					onComplete: function(request){
					reg_check_done(request,where,type);
					
				 	}, 
	 			        onFailure: function(){
					alert('Valami hiba csuszott be...');
							}
					
				}
			);

	}

}





function reg_check_done(request,where,type){
var notice = $('ajax_message');
var response = request.responseText;
var real_response = response.split(':');
var box = $(where);
button = $('submit');

var login_ajax = $("login_ajax");

var rdiv = "errordiv" + where;
var div_exist = $(rdiv);

var eee = where + "_t";

if (real_response[0] == 0){
	
	$(eee).value = 0;

	if (!div_exist){
	var div = document.createElement('div');
	var text = document.createTextNode(real_response[1]);
	div.id = "errordiv" + where;	
	Element.addClassName(div, 'error_text');
	div.appendChild(text);
	box.parentNode.appendChild(div);
	}



}else{	
	
	button.disabled = false;
	$(eee).value = 1;

	if (div_exist){

	div_exist.remove();
	div.removeClassName('error_text');
	}

	}

}






function pic_album_move_show(num,aaa){
	
	var pic_move = $('pic_move');
	var kep_num = $('kep_num');
	
var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {

    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {

    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {

    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }

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;

  }

	pic_move.style.left = ((myWidth/2)-50) + 'px';
	pic_move.style.top = (scrOfY+(myHeight/2)) + 'px'; 
	pic_move.style.display = 'block';


	kep_num.value = num;
}



function pic_album_move_ajax(){
	var pic_move = $('pic_move');

	var kep_id_r = $('kep_id');
        var kep_id = kep_id_r.value;
        var album_id_r = $('album_id');
	var album_id = album_id_r.value;
	var kep_num = $('kep_num');
	var num = kep_num.value;
	var to_r = $('pic_album_move');
	var to = to_r.value;


ajaxRequest = new Ajax.Request(
				'index.php',
				{
					method: 'get',
					parameters: 'a=pic_move&kep_id=' + kep_id + '&num=' + num + '&from=' + album_id + '&to=' + to,
					onComplete: function(request){
var response = request.responseText;
var real_response = response.split(':');

if (real_response[0] == 0){
var div_exist = $('album_full');
if (!div_exist){
	var div = document.createElement('div');
	var text = document.createTextNode(real_response[1]);
	div.id = "album_full";
	div.appendChild(text);
	div.addClassName('error_text');
	pic_move.appendChild(div);
	}
}
else{
					pic_move.style.display = 'none';
					window.location.reload();
}

			}, 
	 			        onFailure: function(){
					alert('Valami hiba csuszott be...');
							}	
				}
			);


}

function album_del(id){

if (!confirm('\n\Biztos törölni akarod?')) {return false;}
	else{

ajaxRequest = new Ajax.Request(
				'index.php',
				{
					method: 'get',
					parameters: 'a=album_del&id=' + id,
					onComplete: function(request){new Effect.Fade(id);}, 
	 			        onFailure: function(){
					alert('Valami hiba csuszott be...');
							}	
				}
			);
	
	    }
}


function album_name_chg(id,old_name){

var aa = 'name_chg_ajax_'+id;
var old_content = $(aa);


if (old_content.innerHTML.length == 0){


					var divitem = document.createElement('div');
					var item = document.createElement('input');
					var savebtn = document.createElement('input');
					
					divitem.id= "div_"+id;
					item.type = "text";
					item.value = old_name;
					item.id = "item"+id;
					Element.addClassName(item, 'login_form'); 
									
					savebtn.type = "button";
					savebtn.value = "Módosítás";
					savebtn.id = "save";
					
					
					divitem.appendChild(item);
					divitem.appendChild(savebtn);
						
					old_content.appendChild(divitem);

					var button = $('save');

Event.observe(button,'click',function(request){savebtn.disabled = true;var new_name = item.value; album_name_chg_done(id,new_name);});
}
	
}


function album_name_chg_done(id,new_name){


ajaxRequest = new Ajax.Request(
				'index.php',
				{
					method: 'get',
					parameters: 'a=album_name_chg&id=' + id + '&nev=' + new_name,
					onComplete: function(request){var response = request.responseText;
									if (response == '1'){window.location.reload();}
											   else{var savebtn = $('save'); savebtn.disabled = false;
												var button = $('item'+id);
												button.value = 'Érvénytelen név!';
												Event.observe(button,'focus',function(){button.value ='';});
												}
								    }, 
	 			        onFailure: function(){
					alert('Valami hiba csuszott be...');
							}	
				}
			);


}



function forget(){

var email_input = $('email');
var email = email_input.value;

if (email.length > 0){
ajaxRequest = new Ajax.Request(
				'index.php',
				{
					method: 'get',
					parameters: 'a=forget&email=' + email + '&ajax=1',
					onLoading: function(){loading('message','forget_submit');},
					onComplete: function(request){forget_done(request);},							 
	 			        onFailure: function(){
					alert('Valami hiba csuszott be...');
							}	
				}
			);
		     }

}


function forget_done(request){

var response = request.responseText;
var button = $('forget_submit');

var real_response = response.split(':');
var box = $('message');

var rdiv = "errordiv";
var div_exist = $(rdiv);

button.disabled = false;
box.update('');
if (real_response[0] == 0){
	if (!div_exist){
	var div = document.createElement('div');
	var text = document.createTextNode(real_response[1]);
	div.id = "errordiv";
	Element.addClassName(div, 'error_text');
 	div.appendChild(text);
	box.appendChild(div);
	}


}else{	
	
	if (div_exist){
	div_exist.remove();
	div_exist.removeClassName('error_text');
	}
	
	box.update(real_response[1]);
	button.disabled = false;
	}	

	
}


function vote(id){

ajaxRequest = new Ajax.Request(
				'index.php',
				{
					method: 'get',
					parameters: 'a=vote&id=' + id,
					onLoading: function(){loading('vote');},
					onComplete: function(request){vote_done(request);},							 
	 			        onFailure: function(){
					alert('Valami hiba csuszott be...');
							}	
				}
			);

}


function vote_done(request){

var response = request.responseText;
var button = $('vote_button');

var real_response = response.split(':');
var box = $('vote');

var rdiv = "errordiv";
var div_exist = $(rdiv);

button.disabled = false;
box.innerHTML ='';

if (real_response[0] == 0){
	if (!div_exist){
	var div = document.createElement('div');
	var text = document.createTextNode(real_response[1]);
	div.id = "errordiv";
 	div.appendChild(text);
	box.appendChild(div);
	}

	box.addClassName('error_text');
}else{	
	if (div_exist){
	div_exist.remove();
	box.removeClassName('error_text');
	}
	
	box.innerHTML = real_response[1];
	button.disabled = false;
	}	


}


function rank(w,page){

var qr = 'a=rank&w=' + w + '&ajax=1';

if (page){
qr+= '&page='+ page;
}

ajaxRequest = new Ajax.Request(
				'index.php',
				{
					method: 'get',
					parameters: qr,
					onLoading: function(){loading('content');},
					onComplete: function(request){rankxml(request,w);},							 
	 			        onFailure: function(){
					alert('Valami hiba csuszott be...');
							}	
				}
			);

}

function rankxml(request,w){

var content = $('content');
var page = $('pages');
var xml = request.responseXML;
var root = xml.documentElement;
content.update("");

var item_page = root.childNodes[0];
var oldalszam = getChildNodeText(item_page, 'page');
var akt_oldal = getChildNodeText(item_page, 'actpage');
oldalszam++;

if (oldalszam >= 6){

k = eval(akt_oldal)-3;
if (k <= 0){k = 1;}

t = eval(akt_oldal)+3;
if (t >= oldalszam){t = oldalszam;}

while ((t-k)<6 && t<oldalszam){t++;}

}else{
k = 1;
t = oldalszam;
}


if (akt_oldal != 1 || page.innerHTML.length < 1){
				page.innerHTML = '';
				for (k; k < t; k++)
				{
					page.innerHTML+="[<a class=\"link2\" onclick=\"rank(\'"+ w+ "\',\'" + k + "\')\">" + k + "</a>]";
				}
			}	
				for (var i = 0; i < root.childNodes.length; i++)
				{
					var item = root.childNodes[i];
					var nev = getChildNodeText(item, 'nev');
					var id = getChildNodeText(item, 'id');
					var sorszam = getChildNodeText(item, 'sorszam');
					
				if (nev){
				 	content.innerHTML+=""+ sorszam + ". <a href=\"index.php?a=kedvenc_show&id=" + id + "\" class=\"link2\">" + nev + "</a><br>"; 
					}
				}		
}

function komment(where,id,page){

if (where == "gazda"){var w="gazda_show";}
if (where == "kedvenc"){var w="kedvenc_show";}
if (where == "album"){var w="album_show";}

var qr = 'a='+ w +'&id=' + id + '&ajax=1';

if (page){
qr+= '&page='+ page;
}

ajaxRequest = new Ajax.Request(
				'index.php',
				{
					method: 'get',
					parameters: qr,
					onLoading: function(){loading('loading_komments');},
					onComplete: function(request){kommentxml(request,where,id);},							 
	 			        onFailure: function(){
					alert('Valami hiba csuszott be...');
							}	
				}
			);

}



function kommentxml(request,where,id){

var xml = request.responseXML;
var root = xml.documentElement;

var w = "gazda_show";

var output = '';
var ttt = id;
var loading_komments = $('loading_komments'); 
var content = $('komments');
var page = $('pages');

loading_komments.update("");
content.update("");

var item_page = root.childNodes[0];
var oldalszam = getChildNodeText(item_page, 'page');
var akt_oldal = getChildNodeText(item_page, 'actpage');
var log = getChildNodeText(item_page, 'log');
oldalszam++;

if (oldalszam >= 6){

k = eval(akt_oldal)-3;
if (k <= 0){k = 1;}

t = eval(akt_oldal)+3;
if (t >= oldalszam){t = oldalszam;}

while ((t-k)<6 && t<oldalszam){t++;}

}else{
k = 1;
t = oldalszam;
}


if (akt_oldal != 1 || page.innerHTML.length < 1){
				page.innerHTML = '';
				for (k; k < t; k++)
				{
					page.innerHTML+="[<a onclick=\"komment(\'"+ where + "\',\'"+ id+ "\',\'" + k + "\')\" class=\"link2\">" + k + "</a>]";
				}
			}	
				for (var i = 0; i < root.childNodes.length; i++)
				{
					var item = root.childNodes[i];
					var nev = getChildNodeText(item, 'nev');
					var gazda_id = getChildNodeText(item, 'gazdaid');
					var id = getChildNodeText(item, 'id');
					var datum = getChildNodeText(item, 'datum');
					var szoveg = getChildNodeText(item, 'szoveg');
					var sorszam = getChildNodeText(item, 'sorszam');
					var private = getChildNodeText(item, 'private');
					
				if (nev){
				 	output+="<div id=\""+ id +"\"><div id=\"komment\"><div id=\"komment_name\">#"+ sorszam +" <a href=\"?a="+ w +"&id=" + gazda_id + "\">" + nev + " </a>"; if (private == 1){output+="<b>(Privát üzenet)</b>";} output+="</div><div id=\"komment_content\">" + szoveg + "</div><div id=\"komment_date\">" + datum + "</div>";
					if (log == 1){
						     output+="<a onclick=\"kommentdel(\'" + where + "\',\'" + id + "\',\'" + akt_oldal + "\',\'" + ttt + "\')\"  class=\"link2\">komment törlése</a>";
						     }
					output+="<div class=\"divider\">&nbsp;</div></div></div>";
					}
				}
content.innerHTML = output;
}


function kommentdel(where,id,page,ttt){

if (!confirm('\n\Biztos törölni akarod?')) {return false;}
	else{

var loading_komments = $('loading_komments'); 
var pages = $('pages');
var qr = 'a=komment_del&w='+ where +'&id=' + id;

ajaxRequest = new Ajax.Request(
				'index.php',
				{
					method: 'get',
					parameters: qr,
					onLoading: function(){loading('loading_komments');},
					onComplete: function(request){
									var response = request.responseText;
									var real_response = response.split(':');
									if (real_response[0] == 0){loading_komments.update(real_response[1]);}
									if (real_response[0] == 1){new Effect.Fade(id); pages.innerHTML = ''; setTimeout("komment('"+where+"','"+ttt+"','"+page+"');",1500);}

									
								     },							 
	 			        onFailure: function(){
					alert('Valami hiba csuszott be...');
							}	
				}
			);
	}

}


function album_pic_sort(pic_id){
ajaxRequest = new Ajax.Request(
				'index.php',
				{
					method: 'post',
					onLoading: function(){loading('loading');},
					parameters: { hidden: 'album_pic_sort' ,id: pic_id , data: Sortable.serialize("kepek") },
					onComplete: function(){$('loading').innerHTML = "A képek sorrendje elmentve." },
					 onFailure: function(){
					alert('Valami hiba csuszott be...');
							}	
				}
			);
}



function main_page(w){

var qr = 'a=main_page&w=' + w;

ajaxRequest = new Ajax.Request(
				'index.php',
				{
					method: 'get',
					parameters: qr,
					onLoading: function(){loading('main_ajax');},
					onComplete: function(request){main_page_done(request,w);},							 
	 			        onFailure: function(){
					alert('Valami hiba csuszott be...');
							}	
				}
			);

}

function main_page_done(request,w){

document.getElementById("main_ajax").style.top = 0 + 'px';
var content = $('main_ajax');
var xml = request.responseXML;
var root = xml.documentElement;
content.update("");

				for (var i = 0; i < root.childNodes.length; i++)
				{
					var item = root.childNodes[i];
					var nev = getChildNodeText(item, 'nev');
					var id = getChildNodeText(item, 'id');
					var kep_id = getChildNodeText(item, 'kep_id');
					var kepek_szama = getChildNodeText(item, 'kepek_szama');
if (w == "kedvenc"){
					var fajta = getChildNodeText(item, 'fajta');
					var fajta2 = getChildNodeText(item, 'fajta2');
					if (!fajta2) {var fajta2 = "";}
}

if (w == "gazda"){

					var lakhely = getChildNodeText(item, 'lakhely');
					if (!lakhely) {var lakhely = "";}
}


					
				if (nev){

if (w == "kedvenc"){
				 	content.innerHTML+="<div id=\"main_ajax_content\"> "+ nev +"<br/><a href=\"index.php?a=kedvenc_show&id=" + id + "\" class=\"link2\"><img src=\"pic/allat_avatar/thumb/" + kep_id + "1.jpg\" border=\"0\" alt=\"\" /></a><br/>"+ fajta +" "+ fajta2 +"</div>";
}

if (w == "gazda"){
content.innerHTML+="<div id=\"main_ajax_content\"> "+ nev +"<br/><a href=\"index.php?a=gazda_show&id=" + id + "\" class=\"link2\"><img src=\"pic/gazda/thumb/" + kep_id + "1.jpg\" border=\"0\" alt=\"\" /></a><br/>"+ lakhely +"</div>";
}

if (w == "kep"){
content.innerHTML+="<div id=\"main_ajax_content\"> "+ nev +"<br/><a href=\"index.php?a=album_show&id=" + id + "\" class=\"link2\"><img src=\"pic/allat/thumb/" + kep_id +""+ kepek_szama +".jpg\" border=\"0\" alt=\"\" /></a><br/></div>";
}



					}
				}		
}


function getChildNodeText(element, childTagName) {
		var elements = element.getElementsByTagName(childTagName);
			if (elements.length == 0) {
				return '';
			}			
			else if (elements.item(0).textContent) {
				return elements.item(0).textContent;
			}
			else {	
			      return elements.item(0).text;
			}
} 