var t;
function addMenu(link,id_page) {
	ids = ',';
	$('.mCheckbox'+id_page).each(
		function() {
			if(this.checked) {
				ids += this.id.replace('menu_','')+',';
			}
		}
	);

	$.post(
		link+'ids/'+ids,
		{
		},
		function(data) {
			clearTimeout(t);
			clSuccess();
			
			$.closeOverlay();
			$_('pNotifyText').innerHTML = data;
			$('#pNotify').show(300);
			
			t = setTimeout('clSuccess()',5000);
		}
	);

	return false;
}

function clSuccess() {
	$_('pNotifyText').innerHTML = '';
	$('#pNotify').hide(300);	
}

function cRedir(txt,link) {
	if(confirm(txt)) {
		location.href = link;
	}else{
		return false;
	}
}

function popUp(id) {
	$('#'+id).jOverlay({
		color: 'white',
		opacity: 0.9
	});
}

function popUpAjax(url) {
	$().jOverlay({
		method : 'POST',
		url : url,
		color: 'white',
		opacity: 0.9,
		imgLoading : '/templates/images/icons/ajax-loader.gif'
	});
}

function highLight(id,flag) {
	if(flag) {
		$_('elem_'+id).className = 'admin-row-light';
	}else{
		$_('elem_'+id).className = 'admin-row-nolight';
	}
}

var codepass_flag = 'magic';
function codePass(fid,link,flg) {
	if(codepass_flag == 'magic') {
		if(flg == '1') {
			codepass_flag = true;
		}else{
			codepass_flag = false;
		}
	}

	if(!codepass_flag) {
		link += 'mode/decode/value/'+$_(fid).value;
	}else{
		link += 'mode/encode/value/'+$_(fid).value;
	}

	$.post(
		link,
		{
		},
		function(data) {
			codepass_flag = !codepass_flag;
			$_(fid).value = data;
		}
	);
}


function cRedirAjax(txt,link,id) {
	if(confirm(txt)) {
		data = id.split('|');
		$.post(
			link,
			{
			},
			function(tmp) {
				for(var i in data) {
					if($_(data[i]) != 'undefined') { 
						$_(data[i]).style.display = 'none';
					}
				}
			}
		);

		return false;
	}else{
		return false;
	}
}

function openTree(nick,id) {
	if($_('childs_'+id) != null) {
		$_('childs_'+id).style.display = '';
		$_('elem_image_'+id).src = $_('elem_image_'+id).src.replace('plus','minus');
		$_('elem_folder_'+id).src = $_('elem_folder_'+id).src.replace('.gif','open.gif');

		setCookie('trees['+nick+']['+id+']',true,30);
	}
}

function closeTree(nick,id) {
	$_('childs_'+id).style.display = 'none';
	$_('elem_image_'+id).src = $_('elem_image_'+id).src.replace('minus','plus');
	$_('elem_folder_'+id).src = $_('elem_folder_'+id).src.replace('open.gif','.gif');

	setCookie('trees['+nick+']['+id+']',false,30);
}

function changeTree(nick,id) {
	if($_('childs_'+id).style.display == 'none') {
		openTree(nick,id);
	}else{
		closeTree(nick,id);
	}
}

function getDebug(nick,button) {
	$('#debug-contents div').each(
			function() {
				if(this.id == 'debug-'+nick && this.style.display == 'none') {
					this.style.display = '';
				}else{
					this.style.display = 'none';
				}
			}
	);
}

function chAdmMenu(blck) {
	if($_(blck).style.display == 'none') {
		setCookie(blck,1,30);
	}else{
		setCookie(blck,0,30);
	}

	ShowOrHide('#'+blck);
}
// table section
function tableSearch(lnk,q) {
	tableReload(lnk+'?q='+q);
	return false;
}

function tableReload(lnk) {
	$_('table-container').style.height = $_('table-container').offsetHeight+'px';
	$_('table-container').className = 'waiter';
	$_('table-container').innerHTML = '';

	$.get(
		lnk,
		{
		},
		function(data) {
			$_('table-container').style.height = '';
			$_('table-container').className = '';
			$_('table-container').innerHTML = data;
		}
	);

	return false;
}

function tableMove(lnk,reload) {
	if(lnk != '') {
		$_('table-container').style.height = $_('table-container').offsetHeight+'px';
		$_('table-container').className = 'waiter';
		$_('table-container').innerHTML = '';

		$.post(
			lnk,
			{
			},
			function(data) {
				tableReload(reload);
			}
		);
	}
}

function tableDeleteOne(cfrm,elem,lnk) {
	if(confirm(cfrm)) {
		$.post(
			lnk,
			{
			},
			function(data) {
				$_(elem).style.display = 'none';
			}
		);
	}

	return false;
}

function tableDelete(btn,b,a,cls,lnk) {
	btn.innerHTML = b;
	$_('table-container').focus();

	var elems = ',';
	$('.'+cls).each(
		function() {
			if(this.checked) {
				elems += this.name.replace('elems_','')+',';
			}
		}
	);

	$.post(
		lnk+'id/'+elems,
		{
		},
		function(data) {
			$('.'+cls).each(
				function() {
					if(this.checked) {
						this.parentNode.parentNode.style.display = 'none';
					}
				}
			);

			btn.innerHTML = a;
		}
	);

	return false;
}
