var closeTimeout	= 500; //delay after mouse leaves menu before it closes
var openTimeout 	= 200; //delay after mouse enters menu title before it opens
var closetimer	= 0;
var opentimer = 0;
var openMenuItem	= null;
var sticky = false

function makeVisible(menu, setSticky)
{
	if (openMenuItem && openMenuItem!=menu)
		openMenuItem.style.visibility = 'hidden'
	menu.style.visibility = 'visible'
	openMenuItem = menu
	sticky = setSticky
}

function openMenu(id)
{	
		
	if (openMenuItem)
	{	
		if (openMenuItem.id==id)
		{
			cancelTimer()
			return
		}
		wait=0
	}
	else
	{	wait = openTimeout} //200
		
	cancelTimer();
	menuItem = document.getElementById(id);
	opentimer = window.setTimeout(function() {makeVisible(menuItem, false);	
}, wait);
	

}

function closeMenu()
{
	if(openMenuItem && !sticky) 
	{
		openMenuItem.style.visibility = 'hidden';
		openMenuItem=null;
	}
}

function setTimer()
{
	if (opentimer)
	{
		window.clearTimeout(opentimer);
		opentimer = null;
	}
	if (closetimer)
	{
		window.clearTimeout(closetimer);
		closetimer = null;
	}
 	closetimer = window.setTimeout(closeMenu, closeTimeout);
}

// called when user clicks on a menu which has a dropdown. If the dropdown is showing
// (most likely because the mouseover opened it) then we return false to go to the target
// URL. If the dropdown is not visible (which might happen on a mobile device), it shows
// the menu.
function showMenu(menuName)
{
	menu = document.getElementById(menuName)
	if (openMenuItem == menu)
		return true	// return true means go to URL pointed to by <a> tag
	else
	{
		closeMenu()		// if there's another menu open
		cancelTimer()	// if waiting for a menu to open
		makeVisible(menu, true)
		return false
	}
}

// cancel close timer
function cancelTimer()
{
	if(closetimer)
	{
		window.clearTimeout(closetimer);
		closetimer = null;
	}
	if (opentimer)
	{
		window.clearTimeout(opentimer);
		opentimer = null;
	}
}

// used for login form
function keypress(e)
{
if (!e) e = window.event
if ((e.which && e.which == 13) || 
    (e.keyCode && e.keyCode == 13))
    {
		document.forms['loginform'].submit();
		return false;
	} 
    else return true;
}

// used for search form
function keypress1(e)
{
if (!e) e = window.event
if ((e.which && e.which == 13) || 
    (e.keyCode && e.keyCode == 13))
    {
		document.forms['siteSearchForm'].submit();
		return false;
	} 
    else return true;
}

var showingSearchFrame = false;

function showSearchFrame()
{
	if (showingSearchFrame) {return;}
	hideLoginFrame();	// ignored if already hidden
	showingSearchFrame = true;
	frame = document.getElementById('searchFrame');
	document.getElementById('searchFrame').style.visibility = 'visible';
	document.getElementById('search').focus();
}
function hideSearchFrame()
{
	if (!showingSearchFrame) {return;}
	showingSearchFrame = false;
	frame = document.getElementById('searchFrame');
	frame.style.visibility = 'hidden';
	//window.event.cancelBubble = true
	//if (window.event.stopPropagation) window.event.stopPropagation();
	
}

// called when user clicks, check for empty search value
function checkEmptySearch()
{
	if (document.getElementById('search').value=='')
	{
		hideSearchFrame();
		return false;
	}
	else
		return true;
}

document.onClick = closeMenu;

function applyToAll()
{
	form = document.getElementById('custom-form')
	for (i=0; i<form.elements.length; i++)
		if (form.elements[i].name=='options-default')
			value = form.elements[i].selectedIndex
	for (i=0; i<form.elements.length; i++)
	{
		child = form.elements[i]
		if (child.type=='select-one')
		{
			child.selectedIndex = value
		}
	}
}

var showing = false;

function showLoginFrame()
{
	if (showing) {return false;}
	hideSearchFrame(); // does nothing if not visible
	showing = true;
	frame = document.getElementById('loginframe');
	document.getElementById('loginframe').style.visibility = 'visible';
	document.getElementById('loginname').focus();
}
function hideLoginFrame()
{
	if (!showing) {return;}
	showing = false;
	frame = document.getElementById('loginframe');
	frame.style.visibility = 'hidden';
	// window.event.cancelBubble = true
	// if (window.event.stopPropagation) window.event.stopPropagation();
	
}

/*	deprecated
var numFlashes = 0;
var isChanged = false;
var saveBackground;

function popMod()
{
	// get the moderate menu
	mod = document.getElementById('modMenu')
	if (!mod) return
	
	if (!isChanged)
	{
		saveBackground = mod.style.backgroundImage
		mod.style.backgroundImage = "url(/images/bg/rust.png)"
		isChanged = true
		numFlashes++
	}
	else
	{
		mod.style.backgroundImage = saveBackground
		isChanged = false
	}
	
	if (numFlashes < 3  || isChanged)
		setTimeout("popMod()", 500)
	
	
}
*/

function replyComment(id, bottom)
{
	if (bottom)
	{
	oAnch = document.getElementById('bottom'+id);
	}
	else
	{
	oAnch = document.getElementById('cmt'+id);
	}
	ocblk = document.getElementById('commentblock');
	olta = ocblk.getElementsByTagName("TEXTAREA");
	if (olta[0].value != '')
	{
		if (!confirm("Your comment will not be saved")) return;
	}
	//strangely, reusing editor instances causes the focus to not work
	if (CKEDITOR.instances.comment)
	{
		CKEDITOR.instances.comment.destroy();
	}

	document.getElementById('cmtparent').value = id;
	olta[0].value = '';
	oAnch.appendChild(ocblk);
	ocblk.style.position = 'relative';
	ocblk.style.visibility = 'visible';
	olta[0].focus();
	
	CKEDITOR.replace(olta[0],
		{
			toolbar: 'Reply',
			on:
				{
					'instanceReady': function(evt) {
					//Set the focus to your editor
					CKEDITOR.instances.comment.focus();
				}
			}
	});

}

function pop(id)
{
	itm = document.getElementById('a'+id);
	img = document.getElementById('li'+id);
	if (itm.style.display=='block')
	{
		itm.style.display='none';
		img.src = '/images/plus.gif';
	}
	else
	{
		itm.style.display='block';
		img.src = '/images/minus.gif';
	}
	return false;
}

/** Periodic function to check the Communities menu for active chats
*/
function checkChats()
{
	$.get('/chatlist/group', function(result) {
			try {
			var chatlist = $.parseJSON(result)
			}
			catch (e) {	// invalid JSON, probably an error message
				try {
					var chatlist = eval(result)
				}
				catch(e) {
					alert("checkChats error:\n" + result)
					return
				}
			}
			var notify = false
			$('#menu-communities a')
				.each( function() {
					var gid = $(this).attr('href').split('/').pop()
					var found = false
					if (chatlist.length)
						for (var i = 0; i < chatlist.length; i++)
							if (chatlist[i] == gid)
								found=true
					if (found != $(this).hasClass('chatMenuItem'))
					{
						$(this).toggleClass('chatMenuItem')
						if (found)
						{
							$(this).text($(this).text() + ' CHAT')
							$(this).attr('href', '/chat/'+gid)
							notify = true
						}
						else
						{
							tx = $(this).text()
							$(this).text(tx.substr(0, tx.length - 5))
							$(this).attr('href', '/view/'+gid)
						}
					}
				} )
			if (notify)
			{
				$menu = $('#menu-communities').parent()
				var orig = $menu.css('backgroundImage')
				var rust = 'url(/images/bg/rust.png)'
				setTimeout( function() {$menu.css('backgroundImage', rust)}, 500)
				setTimeout( function() {$menu.css('backgroundImage', orig)}, 1000)
				setTimeout( function() {$menu.css('backgroundImage', rust)}, 1500)
				setTimeout( function() {$menu.css('backgroundImage', orig)}, 2000)
				setTimeout( function() {$menu.css('backgroundImage', rust)}, 2500)
				setTimeout( function() {$menu.css('backgroundImage', orig)}, 3000)
			}
			setTimeout(checkChats, 5000)
		} )
}

$(function() {
	checkChats()
	} )

