// Get events and schedule a new poll
// INTLv4
function time()
{
	var t = new Date();
	return t.getTime()/1000;
}

function get_events()
{
	if (!madlog_page_loaded)
		madlog_page_loaded = time();
	if (time() - madlog_page_loaded < 3600)
		xajax_get_events();
}

// Called from ajax to set the current number of unread mails
function update_mail_count(cnt)
{
	var e = document.getElementById('notify_mail');
	if (!e)
		return;
	if (status_mail_count != cnt)
	{
		status_mail_count = cnt;
		if (cnt == 0)
			cnt = '&nbsp;';
//		document.getElementById('mail_count').innerHTML = cnt;
		if (cnt == '&nbsp;' && e.style.visibility != 'hidden')
			e.style.visibility = 'hidden';
		if (cnt != '&nbsp;' && e.style.visibility == 'hidden')
			e.style.visibility = 'visible';
	}
}

// Called from ajax to set the current number of unread forum topics
function update_forum_count(cnt, forums)
{
	var total = 0;
	var forum_array = forums.split(",");
	var cnt_array = cnt.split(",");
	var e, p;
	var t = new Array();

	container = document.getElementById('grouplist');
	if (!container)
	{
		if (cnt.length == 0)
			total = 0;
		else
			total = cnt_array.length;
	}
	else
	{
		var id, c, parm;
// Loop forum array and add counter information
		for (var i = 0; i < forum_array.length; i++)
		{
			id = forum_array[i];
			c = 0;
			// Loop cnt array for id to see if there are new messages
			for (var j = 0; j < cnt_array.length; j++)
			{
				parm = cnt_array[j].split("=");
				if (parm[0] == id)
				{
					c = parm[1];
					total = total + Number(c);
				}
			}
			forum_array[i] = id + '=' + c;
		}
	}


	e = document.getElementById('notify_group');
	if (!e)
		return;
	if (total == 0 && e.style.visibility != 'hidden')
	{
		e.style.visibility = 'hidden';
		e.style.display = 'none';
	}
	if (total > 0)
	{
		if (e.style.visibility == 'hidden')
		{
			e.style.visibility = 'visible';
			e.style.display = 'inline';
		}

		// Save top group
		top_group_id = cnt_array[0];

		if (e)
		{
			e.onclick = function()
				{
					var parm = top_group_id.split("=");
					var url = '/index.php?id=500&group_choose=' + parm[0];
					window.location = url;
				};
		}
	}

	// set forum bubbles
	if (container)
	{
		for (var j = 0; j < forum_array.length; j++)
		{
			parm = forum_array[j].split('=');
			p = document.getElementById('group_new_'+parm[0]);
			if (p)
			{
				e = p.firstChild;
				if (e && e.tagName != 'IMG')
					e = null;
				// Bubble is there, but should not be
				if (Number(parm[1]) == 0 && e)
				{
					p.removeChild(e);
				}
				// Bubble is not there, but should be
				if (Number(parm[1]) > 0 && !e)
				{
					var img = document.createElement('img'); 
					img.src = '/gfx/500/new_group_msg.png';
					p.appendChild(img);
				}
			}
		}
	}
}

var madlog_page_loaded;
var madlog_logout_warning;

function logout_timer()
{
	if (!madlog_page_loaded)
		madlog_page_loaded = time();
	if (!madlog_logout_warning)
		madlog_logout_warning = 1;
	if (time() - madlog_page_loaded > 3600+600)
	{
		if (readCookie('cookname') == null)
			window.location = "/scripts/logout.php";
	}
	if (time() - madlog_page_loaded > 3600 && madlog_logout_warning == 1)
	{
		madlog_logout_warning = 2;
		if (readCookie('cookname') == null)
		{
			var div = document.createElement("div");
			div.id = "logout_warning";
			div.innerHTML = '<p>Du har været væk fra MADLOG den sidste time.</p><p>Du logges af sikkerhedsmæssige hensyn af indenfor 10 minutter, medmindre du ønsker at <a href="#" style="color: #0000FF;" onclick="logout_timer_reset(); return false;">FORTSÆTTE</a>.';
			document.getElementById("main_container").appendChild(div);
		}
	}
}

function logout_timer_reset()
{
	madlog_page_loaded = time();
	madlog_logout_warning = 0;
	var e = document.getElementById("logout_warning");
	document.getElementById("main_container").removeChild(e);
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
