Content = (function() {
	
	// MENU //
	var openDelay	= 100;
	var closeDelay	= 500;

	var ddmenuitem	= null;
	var closetimer	= null;
	var opentimer	= null;
	
	var mouseFocus	= null;

	// LOGIN //
	var username, password;



	// MENU //
	function onMenuMouseOver() {
		// Cancel the close timer (if any)
		cancelCloseTimer();
		
		mouseFocus = this;
		
		// Star the opening timer
		opentimer = window.setTimeout(openMenu, openDelay);
	}
	
	function onMenuMouseOut() {
		// Cancel the opening timer (if any)
		cancelOpenTimer();
		
		mouseFocus = null;
		
		// Start closing the menu (if open)
		if(ddmenuitem != null)
			closetimer = window.setTimeout(closeMenu, closeDelay);
	}
	
	function openMenu() {
		closeMenu();
		ddmenuitem = $(mouseFocus).find('ul').css('visibility', 'visible');
	}

	function closeMenu() {
		if(ddmenuitem)
		{
			ddmenuitem.css('visibility', 'hidden');
			ddmenuitem = null;
		}
	}

	function cancelOpenTimer() {
		if(opentimer) {
			window.clearTimeout(opentimer);
			opentimer = null;
		}
	}

	function cancelCloseTimer() {
		if(closetimer) {
			window.clearTimeout(closetimer);
			closetimer = null;
		}
	}

	function noAction( event ) {
		// Cancels the links effect (href=#)
		event.preventDefault();
		// Prevents the event from bubbling up the DOM tree
		event.stopPropagation();
	}
	
	// LOGIN //
	function fieldColoring(field,fSwitch){
		if(fSwitch){
			field.animate({
						backgroundColor: "#fcdfdf",
						borderColor: "#d03535",
						color: "#d03535"
					}, 1000 );
		}else{
			field.animate({
						backgroundColor: "#e3e3e3",
						borderColor: "#000000",
						color: "#103877"
					}, 1000 );
		}
	}
	
	// WIDE CONTENT //
	function resizeWideContent() {
		
		var tHeight = $('#tablelayout_table').outerHeight();
		var cHeight = $('#tablelayout_console').outerHeight();
		if(cHeight >= tHeight){
			if(cHeight < 457) cHeight = 457;
			$('#wc_left').height(cHeight+15);
			$('#wc_right').height(cHeight+15);
			$('#c_content').height(cHeight+54);
		
		}else{
			if(tHeight < 457) tHeight = 457;
			$('#wc_left').height(tHeight+15);
			$('#wc_right').height(tHeight+15);
			$('#c_content').height(tHeight+54);
		}

	}


	// ENTRY POINT //
	$(document).ready(function() {
	
		// MENU //
		$('#menu > li').mouseover(onMenuMouseOver).mouseout(onMenuMouseOut);
		$('#menu a[href="#"]').click(noAction);
		for(var i=1; i < 6; i++)
			$("#menu" + i + " li a:even").addClass("alt");

		$(document).click(closeMenu);
		
		// LOGIN //
		username = $('#login_username');
		password = $('#login_password');
		
		$("#login").click(function( event ) {
			event.preventDefault();
			$('#login_form').fadeToggle('medium');
			username.val('');
			password.val('');
			username.focus();
		});
	
		$('#logout').click(function( event ) {
			event.preventDefault();

			var url = '';
			if(window.location.search == '')
				url = '?methode=ajax';
			else
				url = window.location.search + '&methode=ajax';

			var postData = { };
			postData['logout_admin'] = true;

			$.ajax({
					type: "POST",
					url: url,
					contentType: "application/x-www-form-urlencoded",
					dataType: "html",
					data: postData,
					success: function( data ) { 
						if(data != "error"){
							// Reload the page
							window.location.reload();
						}
					 }
			});
		});
		
		username.focus(function(){
			fieldColoring(username,false);
		});

		password.focus(function(){
			fieldColoring(password,false);
		});

		$('#login_form_the_one_and_only').submit(function() {

			// If not all fields are filled we're done
			var user_check = $.trim(username.val()) == '';
			var pass_check = $.trim(password.val()) == '';

			if(user_check && pass_check){
				fieldColoring(username, true);
				fieldColoring(password, true);
				return false;
			}else if(user_check){
				fieldColoring(username, true);
				return false;
			}else if(pass_check){
				fieldColoring(password, true);
				return false;
			}

			// Assemble the data and post it with ajax
			var postData = { };
			postData['login_admin'] = true;
			postData['login_username'] = username.val();
			postData['login_password'] = password.val();

			var url = '';
			if(window.location.search == '')
				url = '?methode=ajax';
			else
				url = window.location.search + '&methode=ajax';

			$.ajax({
					type: "POST",
					url: url,
					contentType: "application/x-www-form-urlencoded",
					dataType: "html",
					data: postData,
					success: function( data ) {
						if(data == "error"){
							// Redden the input fields
							fieldColoring(username,true);
							fieldColoring(password,true);
						} else if(data == "notactivated") {
							alert('Your account has not been activated yet.');
							$('#login').trigger('click');
						}else{
							// Reload the page
							window.location.reload();
						}
					 }
				  });

			// Cancel the original submit
			return false;
		});

	});	

	return {
		resizeWideContent: resizeWideContent
	}

})();

