/* TODO: cHANGE THIS FILE ENTIRELY!!! CHANGE CALLED ID'S AND CLASSES */

/* Functions that run when the page loads */
$(document).ready(function() {

	/* Makes the bannermenu scrollable horizontally but not vertically */
	$('#bannerBody').scrollToFixed();

	$("#dropdownUserMenu").click(function(e) {
		e.stopPropagation();
	});

	/* Sets the click-function for the usermenubutton */
	$('#bannerUserMenuButton').click(function(e){
		e.stopPropagation();
		$.get('ajax/isLoggedIn', function(response) {
			if(response == 'yes') {
				// If the user is logged in and the usermenu-div is hidden it's shown.
					// TODO: Secure that the courseblock-div is hidden when the usermenu-div is oppened.
					/*
					if($('#dropdownMenuCourseBlock').is(':visible')) {
						$('#dropdownMenuCourseBlock').hide();
					}
					$('#dropdownUserMenu').load('bannermenu.php #dropdownUserMenu');
					*/
					//$("#dropdownMenuCourseBlock").hide();
				// If the user is logged in and the usermenu-div is shown it's hidden.
				$('#dropdownMenuCourseBlock').hide();
				$('#dropdownUserMenu').toggle();
			}

			else {
				// If the user is not logged in and the popup-div is hidden it's filled and shown.
				if($('#popup').is(':hidden')) {
					$('#popup').load('ajax/popup/login', function(){
						$('#popup').show().click(function(e){e.stopPropagation();});
						$("#popup .hint").click(loginHint);
						$("#popup #username").focus().keydown(function() {
							$("#popup .hint").addClass("available");
						});
						$("#popup form").submit(loginSubmit);
					});
				}
				// If the user is not logged in and the popup-div shows it's hidden.
				else {
					$('#popup').hide();
				}
			}
		});
		
	});
	
	// TODO: Refresh the div with it's php before show.
	/* Expand and collapse courses in dropdown menu */
	$('#dropdownMenuExpandCourses').click(function(){
	
		// If the course block is collapsed it's expanded.
		if($('#dropdownMenuCourseBlock').is(':hidden')) {
			$('#dropdownMenuCourseBlock').show();
		}
		// If the course block is expanded it's collapsed .
		else {
			$('#dropdownMenuCourseBlock').hide();
		}

	});

	$("#dropdownMenuLogout").click(function(e) {
		e.stopPropagation();
		window.location.href = "./start/logout";
	});

	$("html").click(function(e) {
		e.stopPropagation();
		$("#dropdownUserMenu").hide();
		$("#popup").hide();
	});
});

function loginSubmit(e) {
	e.stopPropagation();
	e.preventDefault();

	var un = $(this).find("#username").val();
	var pw = $(this).find("#password").val();

	$.post(
		"start/login",
		{ username: un, password: pw},
		function(data) {
			if(data == "true") {
				window.location.href ="./cms";
			} else {
				if($("#popup .popup").hasClass("error")) {
					$("#popup .popup form .errorContainer").html('Error! '+data+' Please try again.');
				} else {
					$("#popup .popup").addClass("error");
					$("#popup .popup form").prepend('<div class="errorContainer">Error! '+data+' Please try again. </div>');
				}
			}
		}
	);
}

function loginHint(e) {
	e.preventDefault();
	e.stopPropagation();

	// If username field is empty, inform user and exit function
	var uname = $("#login #username").val();
	if(uname == "") {
		return;
	}

	// POST username to the function /ajax/pwdhint which returns a json object with password hint
	$.post(
		// Replace with final production URL!!!

		"ajax/pwdhint",
		// Data sent through POST
		{ username: uname },

		// Callback function
		function(data) {
			var result = $.parseJSON(data)["hint"];

			// result["hint"] is either a boolean false or a string containing the hint
			if(result === false) {
				$("#hinttext").html('<p class="error">No password hint available</p>');
			} else {
				$("#hinttext").html('<p>Hint: ' + result + '</p>');
			}

			$("#login").toggleClass("expanded");
			$("#hinttext").toggle();

		}
	); // End $.post {}

} // End loginHint()