// QUIRCKSMODE

// Detect if the browser is IE or not.
var IE = document.all?true:false				// If it is not IE, we assume that the browser is NS.
if (!IE && document.captureEvents && Event.MOUSEMOVE)
{
    document.captureEvents(Event.MOUSEMOVE)     // If NS -- that is, !IE -- then set up for mouse capture
}

document.onmousemove = function(e)
{
	getMouseXY(e);
	if (!uistat && document.getElementById("userinfo")) hideUserinfo();
}

var tempX;
var tempY;

var uistat = false;

// Main function to retrieve mouse x-y pos.s
function getMouseXY(e)
{
    if (window.event)
    {
        // grab the x-y pos.s if browser is IE
        tempX = window.event.clientX + document.body.scrollLeft;
        tempY = window.event.clientY + document.body.scrollTop;
    }
    else
    {
        // grab the x-y pos.s if browser is NS
        tempX = e.pageX;
        tempY = e.pageY;
    }  
    // catch possible negative values in NS4
    if (tempX < 0){tempX = 0}
    if (tempY < 0){tempY = 0}  
    // show the position values in the form named Show
    // in the text fields named MouseX and MouseY
    return true
}

function findPosY(obj)
{
	martop = obj.style.marginTop.split("px");
	var curtop = 0;
	if (!IE) curtop += martop[0]-1;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

// MACROMEDIA
function MM_preloadImages() { //v3.0
	var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
	var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
	if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
	var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
	var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
	d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
	if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
	if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
	var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
	if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


// SAJÁT COOL
function setClassName(obj,clsname)
{
	if (obj.getAttribute("class")) obj.setAttribute("class",clsname);
	else obj.setAttribute("className",clsname);
}

function getClassName(obj)
{
	if (obj.getAttribute("class")) return obj.getAttribute("class");
	else return obj.getAttribute("className");
}


function selectthis(obj,cleartext)
{
	ass = obj.parentNode.getElementsByTagName("A");
	for (i=0;i<ass.length;i++) setClassName(ass[i],"normal");
	setClassName(obj,"selected");
	if (cleartext == "null") cleartext = "";
	else if (cleartext) cleartext = "<div style='font-size: 16px; color: #BBBBBB; font-weight: bold; text-align: center; padding: 10px 40px; width: 310px;'>"+cleartext+"</div>";
	else cleartext = "<div style='font-size: 16px; color: #BBBBBB; font-weight: bold; text-align: center; padding: 10px 40px;'>Válassz egy feladatot a bal oldali listából!</div>";
	clearFeladatNezet("feladatnezet",cleartext);
}


// ÉSATÖBBI
function closeIt(obj1,obj2)
{
	document.getElementById(obj2).style.display="none";
}

function chnge_kiir(x)
{
	if (x<4 || x==7)
	{
		document.getElementById("nap2").style.display="block";
		closeIt("datum1","datum2");
		closeIt("hanyadika1","hanyadika2");
		document.getElementById("gyak").value="nap";
	}
	if (x==5)
	{	
		closeIt("nap1","nap2");
		closeIt("datum1","datum2");
		closeIt("hanyadika1","hanyadika2");
		document.getElementById("gyak").value="nap";
	}
	if (x==4 || x==8)
	{
		document.getElementById("datum2").style.display="block";
		closeIt("nap1","nap2");
		closeIt("hanyadika1","hanyadika2");
		document.getElementById("gyak").value="datum";
	}
	if (x==6)
	{
		document.getElementById("hanyadika2").style.display="block";
		closeIt("datum1","datum2");
		closeIt("nap1","nap2");
		document.getElementById("gyak").value="hanyadika";
	}
}

function hideValueOnFocus(obj, value)
{
	obj.style.color = "#000000";
	if (obj.value == value || obj.value == "")
	{
		obj.value = "";
	}
}
	
function showValueOnBlur(obj, value)
{
	if (obj.value == "")
	{
		obj.value = value;
		obj.style.color = "#999999";
	}
}

function clearValue(obj, str)
{
	if (obj.value == str) obj.value = "";
}

function checkMaster(typ) 
{
	tab = document.getElementsByTagName('input');
	if (!tab[typ].checked)
	{
		for (i = typ; i < tab.length; i++) 
		{
			 tab[i++].checked = false;
		}
	}
	else
	{
		for (i = typ; i < tab.length; i++) 
		{
			 tab[i++].checked = true;
		}	
	}
}

function showText(text)
{
	obj = document.getElementById("megj");
	var textNode = document.createTextNode(text);
	obj.appendChild(textNode);
}

function hideText()
{
	obj = document.getElementById("megj");
	obj.removeChild(obj.firstChild);
}


function addLine(obj,num)
{
	newline = obj.parentNode.parentNode.cloneNode(true);
	changeLine(newline.firstChild.getElementsByTagName("select")[0],num);
	newline.firstChild.getElementsByTagName("a")[1].style.visibility = "visible";
	obj.parentNode.parentNode.parentNode.appendChild(newline);
}

function removeLine(obj,num)
{
	if (obj.parentNode.parentNode.parentNode.getElementsByTagName("tr")[num-1] != obj.parentNode.parentNode)
	{
		obj.parentNode.parentNode.parentNode.removeChild(obj.parentNode.parentNode);
	}
}

function changeLine(obj,num)
{
	sel1 = obj.parentNode.getElementsByTagName("select")[1];
	if (num > 1) sel2 = obj.parentNode.getElementsByTagName("select")[2];
	if (num > 2) sel3 = obj.parentNode.getElementsByTagName("select")[3];
	
	obj1 = obj.getElementsByTagName("option")[0];
	if (num > 1) obj2 = obj.getElementsByTagName("option")[1];
	if (num > 2) obj3 = obj.getElementsByTagName("option")[2];
	
	if (obj1.selected) 
	{ 
		sel1.style.display = "inline"; 
		if (num > 1) sel2.style.display = "none"; 
		if (num > 2) sel3.style.display = "none";
	}
	if (num > 1) if (obj2.selected) 
	{ 
		sel2.style.display = "inline"; 
		sel1.style.display = "none"; 
		if (num > 2) sel3.style.display = "none";
	}
	if (num > 2) if (obj3.selected) 
	{ 
		sel3.style.display = "inline"; 
		sel1.style.display = "none"; 
		sel2.style.display = "none";
	}
}

function showit(obj1)
{
	if (obj1) document.getElementById(obj1).style.display="block";
}

function showproj(pid,ex)
{
	var imge = document.getElementById("img"+pid);
	var proj = document.getElementById("pid"+pid);
	if (ex) var expi = document.getElementById("exp"+pid);
	if (proj.style.display == "block")
	{
		proj.style.display = "none";
		imge.src = "images/expand.gif";
		if (ex) expi.style.display = "block";
	}
	else
	{
		proj.style.display = "block";
		imge.src = "images/contract.gif";
		if (ex) expi.style.display = "none";
	}
	
}

function showfood(pid)
{
	var imge = "#imgf"+pid;
	var food = "#food"+pid;
	if ($(food).is(":visible"))
	{
		$(food).hide();
		$(imge).attr('src',"images/expand.gif");
	}
	else
	{
		$(food).show();
		$(imge).attr('src',"images/contract.gif");
	}
	
}

function closefood(pid)
{
	var imge = document.getElementById("imgf"+pid);
	var food = document.getElementById("food"+pid);
	food.style.display = "none";
	imge.src = "images/expand.gif";
}

function show(obj1,obj2)
{
	if (obj1) document.getElementById(obj1).style.display="block";
	if (obj2) document.getElementById(obj2).style.display="none";
}

function showetlap(obj1)
{
	hideetlap();
	if (obj1) document.getElementById(obj1).style.display="block";
}

function hideetlap()
{
	document.getElementById("etlap0").style.display="none";
	document.getElementById("etlap1").style.display="none";
	document.getElementById("etlap2").style.display="none";
	document.getElementById("etlap3").style.display="none";
	document.getElementById("etlap4").style.display="none";
}

function faststat(script,id,hnp,np,st)
{
//	st = document.getElementById("stt").value;
	if (st != 0)location.href=script+'?userstats=faststat&stat='+st+'&id='+id+'&hnp='+hnp+'&np='+np;
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return [curleft,curtop];
}

function open_faststat(obj,np)
{
	pos = findPos(obj);
	document.getElementById("faststat_popup").style.left = pos[0]-2+"px";
	document.getElementById("faststat_popup").style.top = pos[1]-2+"px";
	document.getElementById("faststat_popup").style.display = "block";
	document.getElementById("aznap").value = np;
	doit = function() { document.getElementById("shownapedit").style.display = "none"; }
	window.setTimeout("doit()",100);
}

var faststat_popup_to = null;
$(function() {
    
    $("[id^=fstat_roll_]").click(function() {
        nap = $(this).attr("id").split("_")[2];
        open_faststat(this,nap);
        return false;
    });
    
    $("#faststat_popup").mouseover(function() {
        window.clearTimeout(faststat_popup_to);
    }).mouseout(function() {
        faststat_popup_to = window.setTimeout(function() {
            $("#faststat_popup").hide();
        },500);
    });

});

function hideHivasok(obj, color)
{
	hivs = getDivsByClass("hivasok");
	for (i=0;i<hivs.length;i++) hivs[i].style.display = "none";
	obj.style.border = "1px solid "+color;
	obj.style.background = "url('kamu.png')";
}

function showHivasok(obj,hiv,color)
{
	hideHivasok(obj,color);
	document.getElementById("hivas"+hiv).style.display = "block";
	obj.style.border = "1px solid #FFCC33";
	obj.style.background = "#FFFFEF";
}

function getDivsByClass(cls)
{
	divs = document.getElementsByTagName("DIV");
	var j = 0;
	var buttons = new Array();
	for (i=0; i<divs.length; i++)
	{
		slc = getClassName(divs[i]);
		if (slc == cls) buttons[j++] = divs[i];
	}
	if (j == 0) return false;
	return buttons;
}

function showTime(obj,str,color)
{
	obj.style.background = color;
	t = findPosY(obj);
	l = findPosX(obj);
	mgj = document.getElementById("graftime");
	mgj.innerHTML = str;
	mgj.style.top = t+"px";
	mgj.style.left = l+"px";
	mgj.style.display = "block";
}

function hideTime(obj, color)
{
	obj.style.background = color;
	document.getElementById("graftime").style.display = "none";
}

function showMegj(obj,str,off)
{
	t = findPosY(obj)+20+off;
	l = findPosX(obj)+20;
	mgj = document.getElementById("megjegyzes");
	mgj.innerHTML = unescape(str);
	mgj.style.top = t+"px";
	mgj.style.left = l+"px";
	mgj.style.display = "block";
}

function hideMegj()
{
	document.getElementById("megjegyzes").style.display = "none";
}



// AJAX BOXES
function editUser2(id,honnan,cegneki)
{
	url = "index.php?munka2=uj&cegneki="+cegneki;
	if (id > 0) url += "&id="+id;
	url += "&honnan="+honnan;
	editURL("feladatnezet",url);
}

function editNap(userid,datum,keses)
{
	document.getElementById("shownapedit").style.display = "block";
	url = "index.php?userstats=edit&id="+userid+"&datum="+datum+"&keses="+keses;
	editURL("napedit",url);
}

function editEbed(ev,het,honnan,lezar)
{
	url = "index.php?ebed=lista&honnan="+honnan;
	if (lezar) url += "&lezar="+lezar;
	if (het && ev) url += "&ev="+ev+"&het="+het;
	loadingImage("ebededit");
	jQuery.get(url, function(data) {
		jQuery("#ebededit").html(data);
	});
}

function loadingImage(id)
{
	$("#"+id).html("<div style='text-align: center; padding: 3px;'><img src=\"http://check.hu/images/loading.gif\"></div>").show();
}

function editFeladat(id,userid,kinek,obj,hol)
{
	url = "index.php?feladat=lista&id="+id+"&kinek="+kinek+"&hol="+hol;
	if (userid) url += "&userid="+userid;
	editURL(obj,url);
}

function editFeladat_v2(id,userid,kinek,obj,hol,callback)
{
	url = "index.php?feladat=lista&id="+id+"&kinek="+kinek+"&hol="+hol;
	if (userid) url += "&userid="+userid;
	editURL_v2(obj,url,callback);
}

function editHiba_v2(userid,id,tipus)
{
	url = "index.php?hiba=ajax&id="+userid+"&tipus="+tipus;
    $("#"+id).show().html("<div class='loading'>&nbsp;</div>");
    $.get(url,function(data) {
        $("#"+id).show().html(data);
        $("#"+id+" .datum").dphun();
    });
}

function editFeladatNezet(userid,feladatid,obj,kinek)
{
	if (!feladatid)
	{
		clearFeladatNezet(obj,"<div style='font-size: 16px; color: #BBBBBB; font-weight: bold; text-align: center; padding: 10px 40px;'>Válassz egy feladatot a bal oldali listából!</div>");
	}
	else
	{
		if (document.getElementById('feladatlink'+feladatid)) document.getElementById('feladatlink'+feladatid).parentNode.style.fontWeight = 'normal';
		if (document.getElementById('news'+feladatid)) document.getElementById('news'+feladatid).style.display = 'none';
		url = "index.php?hiba=2&id="+userid+"&hibaid="+feladatid+"&kinek="+kinek+"&ajaxfeladat=1"+"&ajax=1";
        var selector = typeof(obj) == "object" ? obj : "#"+obj;
        $(selector).show().html("<div class='loading'>&nbsp;</div>");
        $.get(url,function(data) {
            $(selector).html(data);
            $(selector).each(function() { $(".datum",this).dphun(); });
        });

	}
}

function clearFeladatNezet(obj,str)
{
	if (document.getElementById(obj))
	{
		var editbox = document.getElementById(obj);
		editbox.innerHTML = str;
	}
}

function editOrder(userid,obj,listorder)
{
	url = "index.php?hiba=3&id="+userid+"&ordertable=1&listorder="+listorder;
	editURL(obj,url);
}

function editStat(id,userid,obj,opts,ev,ho)
{
	url = "index.php?hiba=3&id="+id+"&userid="+userid+"&stat="+opts;
	if (ev && ho) url += "&ev="+ev+"&ho="+ho;
	editURL(obj,url);
}

function editTipusok(id,userid,obj)
{
	url = "index.php?hiba=feladattipusok&id="+id+"&userid="+userid;
	editURL(obj,url);
}

chklist_to = null;
chklist_user = null;
function editChecklist(userid)
{
	window.clearTimeout(chklist_to);
	chklist_user = userid;
	loadingImage("checklist");
	checkListUpdater();
}
function checkListUpdater()
{
	url = "index.php?checklist=1&id="+chklist_user+"&layout=1";
	jQuery.get(url, function(data) {
		jQuery("#checklist").html(data);
		chklist_to = window.setTimeout(function() {
			checkListUpdater();
		},60000);
	});
}

function editHiba(userid,obj,tipus)
{
	var editbox = document.getElementById("leveledit");
	url = "index.php?hiba=ajax&id="+userid+"&tipus="+tipus;
	y = findPosY(obj);
	editbox.style.top = y+"px";
	editURL("leveledit",url);
}

function editAlfeladat_v2(userid,id,tipus,osfeladat)
{
	url = "index.php?hiba=ajax&id="+userid+"&tipus="+tipus+"&osfeladat="+osfeladat;
	editURL(id,url);
}

function editPost(userid,obj,tipus)
{
	var editbox = document.getElementById("leveledit");
	url = "index.php?hiba=ajax&id="+userid+"&tipus=100";
	y = findPosY(obj)-70;
	editbox.style.top = y+"px";
	editURL("leveledit",url);
}

function editURL(obj,url)
{
	loadingImage(obj);
	editURL2(obj,url);
}

function editURL_v2(obj,url,callback)
{
    loadingImage(obj);
	jQuery.get(url+"&ajax=1", function(data) {
		if (typeof(obj) == "object")
		{
			jQuery(obj).html(data);
		}
		else if (typeof(obj) == "string")
		{
			jQuery("#"+obj).html(data);
		}
        if (callback)
        {
            if (typeof(callback) == "string") eval(callback);
            else callback();
        }
	});
}

function editURL2(obj,url)
{
	jQuery.get(url+"&ajax=1", function(data) {
		if (typeof(obj) == "object")
		{
			jQuery(obj).html(data);
		}
		else if (typeof(obj) == "string")
		{
			jQuery("#"+obj).html(data);
		}
	});
}


var uitimeout = null;

function showUserinfo(userid,getid)
{
	uistat = true;
	window.clearTimeout(uitimeout);
	doit = function()
	{
		var editbox = document.getElementById("userinfo");
		editbox.innerHTML = "<img src='images/loading2.gif'>";
		editbox.style.top = tempY+"px";
		editbox.style.left = tempX+"px";
		editbox.style.display = "block";
		editbox.style.background = "#FFFFD8";
		editbox.style.border = "1px solid #666666";
		url = "index.php?userinfo=1&id="+userid+"&getid="+getid;
		editURL2(editbox,url);
	}
	uitimeout = window.setTimeout("doit()",500);
}

function hideUserinfo()
{
	window.clearTimeout(uitimeout);	
	document.getElementById("userinfo").style.display = "none";
	uistat = false;
}


function ajaxBox(url,editbox)
{
	if (window.XMLHttpRequest) { var req = new XMLHttpRequest(); } 
	else if (window.ActiveXObject) { var req = new ActiveXObject("Microsoft.XMLHTTP"); }
	
	url += "&ajax=1";
//	alert(url);
	
	req.onreadystatechange = function() 
	{
		if (req.readyState > 0 && req.readyState < 4)
		{	
			editbox.style.display = "block";
			editbox.innerHTML = "<div style='text-align: center; padding: 3px;'><img src=\"http://check.hu/images/loading.gif\"></div>";  
		}
		if (req.readyState == 4) {
			while (editbox.firstChild) editbox.removeChild(editbox.firstChild);
			if (req.status==200 && req.responseText)
			{
				rekk = req.responseText;
				if (rekk)
				{
					// editbox.style.background = "none";
					editbox.innerHTML = rekk;
					editbox.style.display = "block"; 
				}	
				else editbox.style.display = "none";
				// setBaseHeight();
			}
		}
	}
	req.open("GET", url, true);
	req.send(null);
	return true;
}


function ajaxBoxNoLoading(url,editbox)
{
	if (window.XMLHttpRequest) { var req = new XMLHttpRequest(); } 
	else if (window.ActiveXObject) { var req = new ActiveXObject("Microsoft.XMLHTTP"); }
	
	url += "&ajax=1";
//	alert(url);
	
	req.onreadystatechange = function() 
	{
		if (req.readyState > 0 && req.readyState < 4) {	/* Loading... */ }
		if (req.readyState == 4) {
			while (editbox.firstChild) editbox.removeChild(editbox.firstChild);
			if (req.status==200 && req.responseText)
			{
				rekk = req.responseText;
				if (rekk)
				{
					// editbox.style.background = "none";
					editbox.innerHTML = rekk;
					editbox.style.display = "block"; 
				}	
				else editbox.style.display = "none";
			}
		}
	}
	req.open("GET", url, true);
	req.send(null);
	return true;
}


function ebedCommand(userid,command,honnan)
{
	url = "index.php?ebed="+command+"&id="+userid+"&honnan="+honnan+"&ajax=1";
	jQuery.get(url, function(data) {
		url = "index.php?ebed=lista&"+data;
		editURL2("ebededit",url);
	});
}

function checkName(obj)
{
	nme = obj.value;
	url = "index.php?munka2=chknev&name="+nme;
	
	if (window.XMLHttpRequest) { var req = new XMLHttpRequest(); } 
	else if (window.ActiveXObject) { var req = new ActiveXObject("Microsoft.XMLHTTP"); }
		
	req.onreadystatechange = function() 
	{
		if (req.readyState > 0 && req.readyState < 4)
		{ /* loading... */  }
		if (req.readyState == 4) {
			if (req.status==200 && req.responseText)
			{
				rekk = req.responseText;
				if (rekk.indexOf("van") > -1) obj.style.color = "#FF0000";
				else obj.style.color = "#000000";
			}
		}
	}
	req.open("GET", url, true);
	req.send(null);
}

function fastOK(obj,url)
{
	ablak = document.createElement("DIV");
	ablak.style.position = "absolute";
	ablak.style.left = tempX+"px";
	ablak.style.top = tempY+"px";
	rndnum = Math.floor(Math.random()*500);
	boxid = "ba"+rndnum;
	hidid = "ha"+rndnum;
	divid = "dda"+rndnum;
	gombid = "a"+rndnum;
	mgombid = "da"+rndnum;
	ablak.innerHTML = "<div id='"+divid+"' class='precentage'><input type='hidden' id='"+hidid+"' value='"+url+"' />Értékelés: <select id='"+boxid+"' style='width: 80px; font-size: 10px;'><option value='0'>Válasszá...</option><option value='5'>5%</option><option value='10'>10%</option><option value='15'>15%</option><option value='20'>20%</option><option value='25'>25%</option><option value='30'>30%</option><option value='35'>35%</option><option value='40'>40%</option><option value='45'>45%</option><option value='50'>50%</option><option value='55'>55%</option><option value='60'>60%</option><option value='65'>65%</option><option value='70'>70%</option><option value='75'>75%</option><option value='80'>80%</option><option value='85'>85%</option><option value='90'>90%</option><option value='95'>95%</option><option value='100'>100%</option><option value='105'>105%</option><option value='110'>110%</option><option value='115'>115%</option><option value='120'>120%</option></select><br /><input id='"+gombid+"' type='button' value='Elfogadás' /> <input id='"+mgombid+"' type='button' value='Mégse' /></div>";
	document.body.appendChild(ablak);
	document.getElementById(gombid).onclick = function() {
		idm = this.getAttribute("id");
		valu = document.getElementById("b"+idm).value;
		url = document.getElementById("h"+idm).value+"&precentizs="+valu;
		document.getElementById("dd"+idm).style.display = "none";
		editURL('feladatlista',url);
	}
	document.getElementById(mgombid).onclick = function() {
		idm = this.getAttribute("id");
		document.getElementById("d"+idm).style.display = "none";
	}
}