var indicator_html    = "<img src='img/indicator.gif' alt='' />";
var indicator_div_use = false;
var indicator_div_id  = "";

/*var menupics = new Array();

menupics[0] = new Image();
menupics[0].src = 'img/menuitem_1_h.jpg';
menupics[1] = new Image();
menupics[1].src = 'img/menuitem_2_h.jpg';
menupics[2] = new Image();
menupics[2].src = 'img/menuitem_3_h.jpg';
menupics[3] = new Image();
menupics[3].src = 'img/menuitem_4_h.jpg';
*/
//-----------------------------------------------------------------------------------------------

function getElementId(obj) {
	if (document.all) {
		return obj.getAttribute('id');
	} else {
		return obj.id;	
	}
}

function xml_get_node_value(obj,tag) {
	return obj.getElementsByTagName(tag)[0].firstChild.nodeValue;
}
function xml_get_list(request,tag) {
	return request.responseXML.getElementsByTagName(tag);
}

function indicator_show(divId) {
	indicatorDiv = (indicator_div_use) ? $(indicator_div_id) : $(divId);
	indicatorDiv.innerHTML = indicator_html;
	indicatorDiv.style.visibility = "visible";
}

function indicator_hide() {
	if ( indicator_div_use ) {
		indicatorDiv = $(indicator_div_id);
		indicatorDiv.style.visibility = "hidden";
	}
}

function get_command_param(responseText, command, params_count) {
	var params_count = (params_count == null) ? 1 : params_count;
	
	var params = new Array();
	baseIndex = responseText.indexOf("<!--%"+command+"%-->");
	temp = responseText.substring(baseIndex+ 5+command.length+4 + 4);
	for ( var i=0; i<params_count; i++ ) {
		param = temp.substring(0, temp.indexOf('-->'));
		params.push(param);
		temp = temp.substring(param.length+3 + 4);
	}
	return params;
}

function ajaxRequestResponse(div, responseText, module) {
	var div = $(div);
	
	Element.update(div, responseText);
	
	indicator_hide();
	
	// alert
	if ( responseText.indexOf("<!--%alert%-->") >= 0 ) {
		params = get_command_param(responseText, "alert");
		alert(params[0]);
	}	
	// blink
	if ( responseText.indexOf("<!--%blink%-->") >= 0 ) {
		blink(div);
	}
	// Loadmodule
	if ( responseText.indexOf("<!--%loadmodule%-->") >= 0 ) {
		params = get_command_param(responseText, "loadmodule", 2);
		module2 = params[0];
		params2 = params[1];
		if ( module2 == module ) {
			alert("Nem megengedett rekurzió!");
		} else {
			loadModule(div, module2, params2);
		}
	}
	// location
	if ( responseText.indexOf("<!--%location%-->") >= 0 ) {
		params = get_command_param(responseText, "location");
		document.location.href = params[0];
	}	
	// reload
	if ( responseText.indexOf('<!--%reload%-->') >= 0 ) {
		document.location.reload();
	}
	// javascript eval
	if ( responseText.indexOf("<!--%javascript%-->") >= 0 ) {
		js = get_command_param(responseText, "javascript");
		eval(js[0]);
	}	
}

function loadModule(div, module, params) {
	indicator_show(div);

	var params = "_file=" + module + "&" + params;
	
	var myAjax = new Ajax.Request(
		'ajax.php', {
			method:     'get',
			parameters: params,
			onFailure:  reportError,
			onComplete: showResponse
		}
	);
	
	function reportError(request) {
		alert('Az oldal átmenetileg nem érhető el!');
	}
	
	function showResponse(request) {
		ajaxRequestResponse(div, request.responseText, module);
	}
	
	//window.scrollTo(0,$(div).offsetTop);
	return false;
}

function loadModuleForm(div, module, senderForm) {
	indicator_show(div);
	
	var elements = Form.getElements(senderForm);
	for ( var i=0; i<elements.length; i++ ) {
		if ( elements[i].name.length > 0 ) {
			Try.these(
				function() {$(elements[i].name+"_error").innerHTML = ""}
			);
		}
	}
	
	var params = "_file=" + module + "&_xml=1&" + Form.serialize(senderForm);
	
	if (params.indexOf('_file=pressEditDo')>-1){
		alert('ez így '+params.length+' karakter hosszú');
	}
	
	var myAjax = new Ajax.Request(
		'ajax.php', {
			method:     'get',
			parameters: params,
			onFailure:  reportError,
			onComplete: showResponse
		}
	);
	
	function reportError(request) {
		alert('Az oldal átmenetileg nem érhető el!');
	}
	
	function showResponse(request) {
		indicator_hide();
		
		var jss = xml_get_list(request, "js");
		for ( var j=0; j<jss.length; j++ ) {
			try {
				js = xml_get_node_value(jss[j], 'js_command');
				eval(js);
			}catch(e){}
	
		}
		
		var error_exists = false;
		var errors = xml_get_list(request, "error");
		var alert_errors = "";
		for ( var i=0; i<errors.length; i++ ) {
			field = xml_get_node_value(errors[i], 'field');
			text  = xml_get_node_value(errors[i], 'text');
			Try.these(
				function() {$(field+"_error").innerHTML = text;},
				function() {alert_errors += text + "\n";}
			);
			error_exists = true;
		}
		
		if ( error_exists ) {
			if ( alert_errors.length > 0 ) {
				alert(alert_errors);
			}
			$(div).innerHTML = "";
		} else {
			ajaxRequestResponse(div, request.responseText, module);
		} 
	}
	return false;
}

function optionsLoad(sel, file, params) {
	var sel = $(sel);
	sel.disabled = true;

	var params = "_file=" + file + "&_xml=1&" + params;
	
	var myAjax = new Ajax.Request(
		'ajax.php', {
			method:     'get',
			parameters: params,
			onFailure:  reportError,
			onComplete: showResponse
		}
	);

	function reportError(request) {
		alert('Az oldal átmenetileg nem érhető el!');
	}
	
	function showResponse(request) {
		if ( request.responseText.substring(0,14) == "<!--%alert%-->" ) {
			temp = request.responseText.substring(18);
			alert(temp.substring(0, temp.indexOf('-->')));
		}
		
		sel.options.length = 0;
		var list = xml_get_list(request, "option");
		for (var i=0; i<list.length; i++) {
			value = xml_get_node_value(list[i], 'value');
			text  = xml_get_node_value(list[i], 'text');
			sel.options[sel.options.length] = new Option(text,value);
		}
		
		sel.disabled = false;
	}
}


// Blinking
function blink(div) {
	new Effect.Highlight(div);
}


function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}

function popup(title, module, params, level) {
	win = new Window({
		className: "peet", 
		title: title, 
		width:570, 
		height:500, 
		destroyOnClose: true, 
		recenterAuto:false
	});
	win.setZIndex(2000);
	win.showCenter(true);
	// átadjuk a megnyitott ablak id-jét is:
	if (params.length > 0) {
		params += "&";
	}
	params += "&_popup_id=" + win.element.id;
	loadModule(win.element.id + "_content", module, params);
}

function closepopup(popup_id) {
	try{
		Windows.close(popup_id, this);
	}catch(e){
		//alert(e);
	}
}

function moveDivTo(id,px){
	margintop += px;
	//alert($(id).offsetHeight);
	//alert($(id).parentNode.offsetHeight);
	if (margintop <= 0 && margintop+5 >= -($(id).offsetHeight-$(id).parentNode.offsetHeight)){
		//alert(margintop);
		Element.setStyle($(id),"margin-top: "+margintop+"px;");
	}
}

function scrollDiv(id,px){
	stopScrollDiv();
	div = $(id);
	if (div.style.marginTop=='' || div.style.marginTop=='undefined'){
		div.style.marginTop = '0px';
	}
	margintop = parseInt(Element.getStyle(div,"margin-top").substr(0,Element.getStyle(div,"margin-top").indexOf('px')));
	interval = setInterval("moveDivTo('"+id+"',"+px+");",10);
}

function stopScrollDiv(){
	try{
		clearInterval(interval);
	}catch(e){}
}

function show_topic(cat,cat_id,topic_id){
	$(cat+'_topicbox_'+cat_id).style.display="block";
	$(cat+'_topiclink1_'+cat_id).style.display="none";
	$(cat+'_topiclink2_'+cat_id).style.display="inline";
	//popup('','topic','id='+topic_id,1);
	loadModule(cat+'_topicbox_'+cat_id+'_inner','topic','id='+topic_id);
	$(cat+'_topicbox_'+cat_id).style.top = '0px';
	new Draggable(cat+'_topicbox_'+cat_id,{zindex: 2000,constraint:'vertical'});
}

function hide_topic(cat,cat_id){
	$(cat+'_topicbox_'+cat_id).style.display="none";
	$(cat+'_topiclink1_'+cat_id).style.display="inline";
	$(cat+'_topiclink2_'+cat_id).style.display="none";
	$(cat+'_topicbox_'+cat_id+'_inner').innerHTML='';
}


function open_gallery(g_id){
	new Effect.Fade('galleries_box');
	loadModule('gallery_box','gallery','id='+g_id);
	setTimeout("Effect.SlideDown('gallery_box');",1000);
}

function close_gallery(){
	new Effect.SlideUp('gallery_box');
	setTimeout("Effect.Appear('galleries_box');",1000);
}

function add_file() {
	str = "<tr><td>Útvonal: <input type=\"text\" class=\"input\" name=\"new_files_path[]\" /> vagy: <input type=\"file\" class=\"input\" name=\"new_files[]\" /></td><td><input type=\"button\" onclick=\"Element.remove(this.parentNode.parentNode);\" class=\"button\" value=\"Törlés\" /></td></tr>";
	new Insertion.After($('picsFirstLine'),str);

}

function add_file2() {
	str = "<tr><td>Név: <input type=\"text\" class=\"input\" name=\"new_files_title[]\" /></td><td>Útvonal: <input type=\"text\" class=\"input\" name=\"new_files_path[]\" /><br/> vagy: <input type=\"file\" class=\"input\" name=\"new_files[]\" /></td><td><input type=\"button\" onclick=\"Element.remove(this.parentNode.parentNode);\" class=\"button\" value=\"Törlés\" /></td></tr>";
	new Insertion.After($('picsFirstLine'),str);

}


function load_img(g_id,filename){
	$('picture_box').innerHTML='<img src="../galleries/pics/'+g_id+'/'+filename+'" width="600" height="400" alt="" />';
}


function fck_plus(editor_name){
var oEditor = FCKeditorAPI.GetInstance(editor_name);
$(editor_name).value = oEditor.GetHTML();
}
