// auth functions //

	function Login(uName, pWord) {                    
		Sys.Services.AuthenticationService.login(uName, pWord, false, null, null, LoginServiceCompleted, LoginServiceFailed, "Context Info");
	}

	function isLoggedIn(){
		if (Sys.Services.AuthenticationService.get_isLoggedIn())      
		{ return true; } 
		else
		{ return false; }
	}

	function Logout() {
		Sys.Services.AuthenticationService.logout("javascript:void(0)", LogoutServiceCompleted, LoginServiceFailed, "Context Info");
	}
	
	function LoginServiceFailed(error, userContext, methodName) {
		//error connecting...
		$("#errContainer")
			.html($('een fout heeft zich voorgedaan tijdens het verbinden met de server.<span class="hideme"><br/><br/>' + error + '</span>'))
			.hide()
			.fadeIn(500);
	}
	
	function LoginServiceCompleted(validCredentials, userContext, methodName) {
		if (validCredentials) {
			//login success
			var uName = $('#username').val();
			removeThisModal();
			$('#loggedInStatus').find('#lnkLoginName').text(uName);
			initializeLoginSequence();
		}
		else {
			//login failed
			$("#errContainer")
				.html($('Whoops, de combinatie de opgegeven login naam en paswoord zijn niet terug te vinden.'))
				.hide()
				.fadeIn(500);
		}
	}
	
	function LogoutServiceCompleted(result, userContext, methodName) {                     
		//logout success 
		$('#sidebar .mdwmenu').fadeOut(500);
		setTimeout(function(){
			$('#sidebar .mdwmenu').remove();
		}, 1000);
		

		if(!checkModalExist('hasModal'))
		{
			$height = $(document).height();
			$('#overlay').css('height', $height +'px').css('width', $(document).width() +'px').show().fadeTo(500, 0.75);
			$message = $('<div class="modal"></div>');
			$messageSpan = $('<div class="message"></div>').text('Je bent succesvol uitgelogd!').appendTo($message);
			$btnCLOSE = $('<a href="javascript:void(0)" class="btnCLOSE">x</a>').click(removeThisModal).appendTo($message);
			$message.appendTo('body');
			$('body').addClass('hasModal');
			setTimeout(removeThisModal, 1500);
			$('#loggedInStatus').addClass('hideme');
		}
	} 
// end auth functions //


// Start Modal box Functions //
	removeThisModal = function(){
		$overlay = $('#overlay');
		$message = $('.modal');
		$message.fadeOut(500, function(){
			$(this).remove();
			$overlay.fadeTo(500, 0, function(){$(this).hide();});
	
		});
		$('body').removeClass('hasModal');
	};
	
	checkModalExist = function(classToCheckOn){
		if($('body').is('.' + classToCheckOn))
		{ return true; }
		else
		{ return false; }
	};

	initializeBeheerModal = function(o){
		if(!checkModalExist('hasModal'))
		{
			$height = $(document).height();
			$('#overlay').css('height', $height +'px').css('width', $(document).width() +'px').show().fadeTo(500, 0.75);
			$message = $('<div class="modal"></div>');
			$messageSpan = $('<div class="message"></div>').text(o.message).appendTo($message);
			$btnCLOSE = $('<a href="javascript:void(0)" class="btnCLOSE">x</a>').click(removeThisModal).appendTo($message);
			$messageNAV = $('<div class="messageNav"></div>');
			$btnOK = $('<a href="http://www.komtuveu.be/umbraco/umbraco.aspx" class="btnOK">Ja</a>').appendTo($messageNAV);
			$btnCancel = $('<a href="javascript:void(0)" class="btnCANCEL">Nee</a>').click(removeThisModal).appendTo($messageNAV);
			$clearDIV = $('<div class="clear"></div>').appendTo($messageNAV);
			$messageNAV.appendTo($message);
			$message.appendTo('body');
			$('body').addClass('hasModal');
		}
	};

	initializeLoginModal = function(o){
		if(!checkModalExist('hasModal'))
		{	
			$height = $(document).height();
			$('#overlay').css('height', $height +'px').css('width', $(document).width() +'px').show().fadeTo(500, 0.75);
			$message = $('<div class="modal"></div>').text(o.message);
			$messageLoading = $('<div class="modalLoading>&nbsp;</div>');
			$btnCLOSE = $('<a href="javascript:void(0)" class="btnCLOSE">x</a>').click(removeThisModal).appendTo($message);

			// form creation
			$loginForm = $('<form id="loginForm" action="javascript:void(0)" method="get"></form>');
			$loginFormList = $('<ul class="loginFormList"></ul>');
			$emailFormList = $('<ul class="emailFormList"></ul>');
			$logintitleRowField = $('<li><h3>Login</h3></li>').appendTo($loginFormList);
			$emailtitleRowField = $('<li><h3>Wachtwoord vergeten?</h3></li>').appendTo($emailFormList);
			$usernameField = $('<li><label for="username">Naam</label><input type="text" id="username" name="username"/></li>').appendTo($loginFormList);
			$lostMailField = $('<li><label for="lostmail">Email</label><input type="text" id="lostmail" name="lostmail"/></li>').appendTo($emailFormList);
			$passwordField = $('<li><label for="password">Wachtwoord</label><input type="password" id="password" name="password"/></li>').appendTo($loginFormList);
			$btnLoginSubmitForm = $('<li><a href="javascript:void(0)" class="forgotPass">ik ben mijn paswoord vergeten.</a><input type="submit" id="btnSubmitLogin" value="Login"></input><div class="clear"></div></li>').appendTo($loginFormList);
			$btnEmailSubmitForm = $('<li><a href="javascript:void(0)" class="backtologin">terug naar het login formulier.</a><input type="submit" id="btnSubmitEmail" value="Submit"></input><div class="clear"></div></li>').appendTo($emailFormList);
			$loginFormList.appendTo($loginForm);
			$emailFormList.appendTo($loginForm);
			$ErrMessageBox = $('<div id="errContainer">&nbsp;</div>').appendTo($loginForm);
			$loginForm.appendTo($message);
			$message.appendTo('body');
			$messageLoading.appendTo('body').hide();
			setTimeout(function(){$("input#username").focus();}, 1000);
			$('.emailFormList').hide();

			$('a.backtologin').bind("click", function() {
				$(this).blur();
				$('.emailFormList').fadeOut(250, function(){
					$('#errContainer').fadeOut(500);				
					$('.error').fadeOut(500).remove();
					$('#lostmail').val('');
					$('.loginFormList').fadeIn(250, function(){
						$('#username').focus();
					});
				});
			});
			$('a.forgotPass').bind("click", function() {
				$(this).blur();
				$('.loginFormList').fadeOut(250, function(){
					$('#errContainer').fadeOut(500);				
					$('.error').fadeOut(500).remove();
					$('.emailFormList').fadeIn(250, function(){
						$('#lostmail').focus();
					});
				});
			});
			$('#btnSubmitEmail').bind("click", function() {
				$(this).blur();
				$('.error').fadeOut(500).remove();
				$('#errContainer').fadeOut(500);

				var hasError = false;

				if($("#lostmail").val() == '') 
				{
					$("#lostmail").after('<span class="error">*</span>');
					hasError = true;
				}

				if(hasError == false) 
				{
					$eMail =$("#lostmail").val();
					// do everything
					$.ajax({
						type: "POST",
						url: "/umbraco/webservices/komtuveu.asmx/requestPassword",
						data: "{'eMail':'" + $eMail + "'}",
						contentType: "application/json; charset=utf-8",
						dataType: "xml",
						complete: function(data){  
							var json = $.xmlToJSON(data.responseXML);
							var iStatus = json.status[0].Text
							if(iStatus < 200 ) {
								$("#errContainer")
									.html(json.response[0].Text + ' (Err:' + iStatus + ')')
									.hide()
									.fadeIn(500);
							}
							else if(json.status[0].Text == "200") {
								$('#lostmail').val('');
								$("#errContainer")
									.html(json.response[0].Text)
									.hide()
									.fadeIn(500);
								setTimeout(function(){
									$('.emailFormList').fadeOut(250, function(){
										$('#errContainer').fadeOut(500);				
										$('.error').fadeOut(500).remove();
										$('.loginFormList').fadeIn(250, function(){
											$('#username').focus();
										});
									});
								}, 1500);
								
							}
							else
							{
								// uncaught status...
							}
						}  
					});
				}
				else
				{
					$('#errContainer')
						.text('Je moet een email adres opgeven!')
						.fadeIn(500);
				}
				return false;
			});
			$('#btnSubmitLogin').bind("click", function() {
				$(this).blur();
				$('.error').fadeOut(500).remove();
				$('#errContainer').fadeOut(500);
				

				var hasError = false;

				if($("#username").val() == '') 
				{
					$("#username").after('<span class="error">*</span>');
					hasError = true;
				}
				if($("#password").val() == '') 
				{
					$("#password").after('<span class="error">*</span>');
					hasError = true;
				}

				if(hasError == false) 
				{
					$uName =$("#username").val();
					$pWord = $("#password").val();
					// do everything
					$.ajax({
						type: "POST",
						url: "/umbraco/webservices/komtuveu.asmx/checkLogin",
						data: "{'uName':'" + $uName + "', 'pWord':'" + $pWord + "'}",
						contentType: "application/json; charset=utf-8",
						dataType: "xml",
						complete: function(data){  
							var json = $.xmlToJSON(data.responseXML);
							var iStatus = json.status[0].Text
							if(iStatus < 200 ) {
								$("#errContainer")
									.html(json.response[0].Text + ' (Err:' + iStatus + ')')
									.hide()
									.fadeIn(500);
							}
							else if(json.status[0].Text == "200") {
								$("#errContainer")
									.html(json.response[0].Text)
									.hide();
									//.fadeIn(500);
								Login($uName, $pWord);
							}
							else
							{
								// uncaught status...
							}
						}  
					});
				}
				else
				{
						$('#errContainer')
							.text('Je moet een gebruikersnaam en paswoord opgeven!')
							.fadeIn(500);
				}
				return false;
			});

			$('#errContainer').hide();
			$('body').addClass('hasModal');
		}
	};
// End Modal Box Functions //




// Start Random other functions //
updateMedewerker = function(){
	$.ajax({
		type: "POST",
		url: "/umbraco/webservices/komtuveu.asmx/getCurrentMedewerker",
		data: "{}",
		contentType: "application/xml; charset=utf-8",
		dataType: "json",
		complete: function(data){  
			var json = $.xmlToJSON(data.responseXML);
			var iStatus = json.status[0].Text
			if(iStatus < 200 ) {
				// show err msg...
				//console.log(json.response[0].statusText[0].Text);
			}
			else if(json.status[0].Text == "200") {
				// success...
				var mdw = json.response[0].medewerker[0];
				var mdwName = mdw.name[0].Text;
				var mdwAvatarLink = mdw.avatar[0].link[0].Text;
				var mdwProfileLink = mdw.profileLink[0].Text;
				//console.info(mdwAvatarLink);
				$('#loggedInStatus img.avatar').attr('src', mdwAvatarLink);
				$('#loggedInStatus a.avatar').attr('href', mdwProfileLink);
				$('#loggedInStatus #lnkLoginName').text(mdwName);
				$('#loggedInStatus #lnkLoginName').attr('href', mdwProfileLink);
				$('#currentAvatar img.avatar').each(function(){ $(this).attr('src', mdwAvatarLink); });
				$('#loggedInStatus').removeClass('hideme');
			}
			else
			{
				// uncaught status...
			}			
		}  
	});

};
initializeLoginSequence = function(){
	// if is login then show loggedin else hide loggedin...
	var sidebar = $('#sidebar');
	$('#sidebar .mdwmenu').fadeOut(500);
	setTimeout(function(){
		$('#sidebar .mdwmenu').remove();
	}, 1000);
	setTimeout(function(){
		if(isLoggedIn())
		{
			var uName = $('#uName').val();
			if(uName == '')
			{ uName = 'onbekende'; }
			$('#lnkLoginName').text(uName);
			$.ajax({
				type: "POST",
				url: "/umbraco/webservices/komtuveu.asmx/getMedewerkerMenu",
				data: "{}",
				contentType: "application/json; charset=utf-8",
				dataType: "xml",
				complete: function(data){  
					var json = $.xmlToJSON(data.responseXML);
					var iStatus = json.status[0].Text
					if(iStatus < 200 ) {
						// show err msg...
						$(json.response[0].Text).prependTo(sidebar);
					}
					else if(json.status[0].Text == "200") {
						$(json.response[0].Text).prependTo(sidebar);

						var profileLink = $('a#link-1187').attr('href');
						$('#lnkLoginName').attr('href', profileLink);

						// animate mdw menu
						initializeMDWMenu();
					}
					else
					{
						// uncaught status...
					}
				}  
			});
				

			updateMedewerker();
//			setTimeout(function(){$('#loggedInStatus').removeClass('hideme');}, 1000);
		}
		else
		{
			$('#loggedInStatus').addClass('hideme');
		}
	}, 1000);
};
initializeMDWMenu = function() {
	$('.mdwmenu').hide().fadeIn(500);
	$('.mdwmenu ul li a').bind('mouseenter', function (e) {	
		$(this).children('span.icon-arrow').remove();
		$('<span class="icon-arrow">&nbsp;</span>').prependTo($(this)).stop().animate({paddingLeft: '15px'}, { 'duration': 'slow', 'easing': 'easeOutBounce' });
	})
	.bind('mouseleave', function (e) {
		$(this).find('span.icon-arrow').stop().animate({paddingLeft: '0px'}, 'fast', function(){ $(this).remove(); });
	});
};
initializeMenu = function() {
	$('img.links').each(function(){
		$(this).wrap('<div class="links"></div>');
		$(this).removeClass('links');
		var alt = $(this).attr('alt');
		$('<span class="caption"></span>').css('width', $(this).width()).text(alt).appendTo($(this).parent()).hide();
	});
	$('img.rechts').each(function(){
		$(this).wrap('<div class="rechts"></div>');
		$(this).removeClass('rechts');
		var alt = $(this).attr('alt');
		$('<span class="caption"></span>').css('width', $(this).width()).text(alt).appendTo($(this).parent()).hide();
	});


	$('ul#nav ul li a').bind('mouseenter', function (e) {	
		$(this).children('span.icon-arrow').remove();
		$('<span class="icon-arrow">&nbsp;</span>').prependTo($(this)).stop().animate({paddingLeft: '15px'}, { 'duration': 'slow', 'easing': 'easeOutBounce' });
	})
	.bind('mouseleave', function (e) {
		$(this).find('span.icon-arrow').stop().animate({paddingLeft: '0px'}, 'fast', function(){ $(this).remove(); });
	});
	/*$('.mdwmenu ul li a').bind('mouseenter', function (e) {	
		$(this).children('span.icon-arrow').remove();
		$('<span class="icon-arrow">&nbsp;</span>').prependTo($(this)).stop().animate({paddingLeft: '10px'}, 'fast');
	})
	.bind('mouseleave', function (e) {
		$(this).find('span.icon-arrow').stop().animate({paddingLeft: '0px'}, { 'duration': 'fast', 'easing': 'easeOutBounce' }, function(){ $(this).remove(); });
	});*/
};
	
ToggleTitle = function(direction)
{
	var _small = "0px";
	var _targetSmall = "4em";
	var _big = "21em";
	var _speed = 200;

	var target = _big;
	var bgtarget = _small;
	var titleHeight = $('#title').css('marginTop');
	var iconTarget = _small;

	if(direction == 'up')
	{ target = _targetSmall; direction = "down"; }
	else if (direction == 'down')
	{ target = _big; direction = "up"; }
	else
	{
		if(titleHeight == _targetSmall)
		{ target = _big; direction = "up"; }
		else
		{ target = _targetSmall; direction = "down"; }
	}
	$('#titletoggle').removeClass("toggleup").removeClass("toggledown").addClass("toggle" + direction);
	$('#title').stop().animate( { 'marginTop': target }, _speed );
	if(direction == 'up')
	{
		bgtarget = _small;
		icontarget = _small;
	}
	else
	{
		bgtarget = "-" + _big;
		icontarget = "40px";
	}
	//$('#titletoggle').stop().animate({'top': icontarget}, _speed);
	$('#titletoggle').stop().animate({'top': icontarget}, { 'duration': 1000, 'easing': 'easeOutBounce' });


}