/* INITIALISE
----------------------------------------------------------------------*/
$(document).ready(initLogin);

/* COOKIES
----------------------------------------------------------------------*/
function getCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ';', len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

function setCookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name+'='+escape( value ) +
	( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) + //expires.toGMTString()
	( ( path ) ? ';path=' + path : '' ) +
	( ( domain ) ? ';domain=' + domain : '' ) +
	( ( secure ) ? ';secure' : '' );
}

function deleteCookie( name, path, domain ) {
	if ( getCookie( name ) ) document.cookie = name + '=' +
	( ( path ) ? ';path=' + path : '') +
	( ( domain ) ? ';domain=' + domain : '' ) +
	';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}

function logOut()
{
	deleteCookie('userID');
	deleteCookie('user');
	deleteCookie('dir');
	deleteCookie('title');
	window.location.reload();
}

/* LOG IN
----------------------------------------------------------------------*/
var authUserURL = "php/authUser.php";
var logout_txt = (getCookie('userID') != null) ? "log out" : "client logon";
var login_form;
var userID;
var user;
var pass;
var dir;
var title;
var qt_detect;

function initLogin()
{
	$("#nav").load("snippets/navigation.html", function(){ $("#navlist #current").html(logout_txt); });
	if (getCookie('userID') != null)
	{
		userID	= getCookie('userID');
		user	= getCookie('user');
		dir		= getCookie('dir');
		title	= getCookie('title');
		loadPage(userID, user, dir, title);
	}
	else
	{
		login_form = document.getElementById('login_form');
		login_form.user.value	= "";
		login_form.pass.value	= "";
		login_form.user.focus();
	}
}

function printError(login_form) {
	login_form.user.value	= "username or password incorrect";
	login_form.user.onfocus	= function(){ this.value = ""; };
	login_form.pass.value	= "";
}

function processLogin() {
	user = $("#user").val();
	pass = $("#pass").val();

	/* SEND & RECEIVE
	----------------------------------------------------------------------*/
	if (user && pass)
	{
		$.post(authUserURL, { username:user, password:pass }, function(resp) {
			var response = eval('(' + resp + ')');
			if (response.status == "authenticated")
			{
				userID	= response.userID;
				dir		= response.dir;
				title	= response.title;

				// Set cookies to allow automatic login if page refreshed
				setCookie('userID', userID);
				setCookie('user', user);
				setCookie('dir', dir);
				setCookie('title', title);

				$("#login").fadeOut("xslow", function(){
					loadPage(userID, user, dir, title);
					logout_txt = "log out";
					$("#navlist #current").html(logout_txt);
				});
			}
			else
			{
				printError(login_form);
			}
		});
	}
	else
	{
		printError(login_form);
	}
	delete response;
	return false;
}
function loadPage(userID, user, dir, title) {
	if (userID == "admin")
	{
		$("#right_col").append("<h1 class=\"heading\">JOB INFO</h1>");
		$("#right_col").append("<div id=\"job_info\"></div>");
		getAdminContent();
	}
	else
	{
		checkJob(dir);
		$("#left_col").load("snippets/instructions.html", function(){
			$("#contact").set("href", contact);
			var version_msg = "You have version " + quicktime.version + " installed."
			$("#qt_version").append(version_msg);
		});
	}
	$("#right_col").append("<h1 class=\"heading\">FILE INFO</h1>");
	$("#right_col").append("<div id=\"tool_tip\"></div>");
	
	$("#login_horizon").css({visibility:"hidden"})
}
