$(document).ready(function()
{
	/*
	* Checks which frame is open. If the account menu is open, checks if application is started
	* through ajax call. If the application is not started yet, opens it.
	*/
	if ($("#openManager").length > 0)
	{
		var src = $("#openManager").attr('class').split(/\s+/)[0];
		var request = '/webframework/library/Acato/Includes/ajax.php?request=autoStart';
		$.post(request, function(data)
		{
			if(data.error === false && data.val === true)
				openLink(src);
			
		}, "json");
	}
	
	/*
	* If Java is not enabled, display a message saying so, and hide the start application menu.
	*/
	if( navigator.javaEnabled() )
	{
		$("#openManager").show();
		$("#installJava").hide();
	}
	else
	{
		$("#installJava").show();
		$("#openManager").hide();
	}
	
	/*
	* Check which page is opened. Only extend the login menu when on the home page. 
	*/
	var pathname = window.location.pathname;
	var open = (pathname.endsWith('home/') || pathname.endsWith('home') || $(".lm_error").length > 0) ? 0 : false;
	$(".accordion").show();
	$( ".accordion" ).accordion(
	{
		collapsible: true,
		autoHeight: true,
		active: open
	});
	
	/*
	* Clicking the submit button posts the login form.
	*/
	$( ".submit" ).click(function()
	{
		$('#loginform').submit();
	});
	
	/*
	* Clicking anywhere on the page retracts the login menu.
	*
	* @update: 'click' event has been replaced by 'mousedown' event in order
	* to handle right clicking on the page (MANTIS: 74).
	*/
	$(document).mousedown(function()
	{ 	$(".accordion").accordion( "activate" , false );
	});
	
	/*
	* Clicking on the menu stops any propagation, in order to stop the
	* retraction of the menu, caused by previous described event.
	*
	* @update: 'click' event has been replaced by 'mousedown' event in order
	* to handle right clicking on the page (MANTIS: 74).
	*/
	$(".login_menu").mousedown(function(e)
	{
		e.stopPropagation();
	});
	
	/*
	* The text input turns light green when focused on, and white when not.
	*/
	$(".login_menu .text").blur(function()
	{	$(this).css("background-color","#FFFFFF");
	});
	
	$(".login_menu .text").focus(function()
	{	$(this).css("background-color","#DCEBA3");
	});
	
	/*
	* IE fix: Internet Explorer 8 and previous do not handle pressing the 'Enter'
	* button as a form post. Checking for keycode 13 (Enter) simulates same functionality.
	*/
	$(".login_menu .text").keypress(function()
	{
		var keycode = (event.keyCode ? event.keyCode : event.which);
		if(keycode == '13')
			$('#loginform').submit();
	});
	
	/*
	* Opens the application. An ajax call is used to check if the user is still logged in.
	* If the user is no longer logged in, the page refreshes in order to display the login
	* menu again.
	* The correct JNLP source is attached as the first class to the '#openManager' element,
	* and is here retrieved in order to open the correct JNLP, belong to the given user.
	*/
	$("#openManager").click(function()
	{
		var src = $(this).attr('class').split(/\s+/)[0];
		var request = '/webframework/library/Acato/Includes/ajax.php?request=isLoggedIn';
		
		try
		{	
			$.post(request, function(data)
			{
				if(data.error === false && data.val === true)
					openLink(src);
				else
					location.reload();
			}, "json");
		}
		catch(err)
		{
			location.reload();
		}
	});
});

String.prototype.endsWith = function(suffix) { 
    return this.indexOf(suffix, this.length - suffix.length) !== -1; 
}; 
