// JavaScript Document

$(function(){
	$('.header .login').click(function(){
		if($(this).hasClass('selected')){
			hideLogin();
		}else{
			showLogin();
		}
	});
	$('.header .login-speechballoon .button').click(function(){
			var username = $("input[name='username']").val();
			var password = $("input[name='password']").val();
			
			
			$.ajax({
					type: "POST",
					url: BASE_URL+'login',
					dataType: "json",
					data: ({
					username:username, 
					password:password 
					}),
					success: function(data) {	
					$('.loggedIn #information').html(data.text);	
					
					if(data.logged_in == true){						
						setAsLoggedIn();
					}else{
						$('.header .login-speechballoon .error').hide().fadeIn();
					}
				}
			});
			/*$.ajax({
				type: "POST",
				url: '/php/proxy.php?url=http://cursisten.pepworldwide.nl/cursisten/login.php',
				data: ({
				userName:username, 
				password:password 
				}),
				success: function(data) {	
					alert(data);				
					/*if(data == 'true'){						
						setAsLoggedIn();
					}else{
						$('.header .login-speechballoon .error').hide().fadeIn();
					}*/
			//	}
			//});
			
						   
	});
	
	$('.logout').click(function(){
		$.ajax({	
			type: "POST",
			url: BASE_URL+'login/logout', 
			success: function(data) {	
				setAsLoggedOut();
			}
	   })
						
	});
	
	$('html').click(function() {
		hideLogin();
		hideSendaFriend();
	});
	
	$('.header .login-speechballoon, .header .login').click(function(event){
		 event.stopPropagation();
	});
});


function hideLogin(){
		$('.header .login').removeClass('selected');
		$('.login-speechballoon').fadeOut();
}

function showLogin(){
		$('.header .login').addClass('selected');
		$('.login-speechballoon').fadeIn();
		hideCountrySelect();
		hideSendaFriend();
}

function setAsLoggedIn(){
	$('.header a.login span').html('logged in')
	$('.login-speechballoon .content').css('height', $('.login-speechballoon .content').height()+'px');
	
	$('.header .login-speechballoon .error').hide();						
	$('.login-speechballoon .form').hide();
	$('.loggedIn').fadeIn();
	
	var contentHeight = $('.login-speechballoon .content .loggedIn').height();
	
	$('.login-speechballoon .content').animate({width:'350px',  height:contentHeight+'px'}, {duration:500});
	$('.login-speechballoon').animate({width:'390px', right:'20px'}, {duration:500});
	$('.login-speechballoon .arrow').animate({width:'390px'}, {duration:500});
}

function setAsLoggedOut(){
	$("input[name='username']").val("");
	$("input[name='password']").val("");
	
	$('.header a.login span').html('inloggen')
	$('.login-speechballoon .content').css('height', $('.login-speechballoon .content').height()+'px');
	
	$('.header .login-speechballoon .error').hide();
	$('.loggedIn').hide();
	
	$('.login-speechballoon .content').animate({width:'330px',  height:'121px'}, {duration:500});
	$('.login-speechballoon').animate({width:'370px', right:'35px'}, {duration:500});
	$('.login-speechballoon .arrow').animate({width:'370px'}, {duration:500});
	
	setTimeout(function(){
		$('.form').fadeIn();	
		$('.login-speechballoon .content').css('height','auto');
	}, 510)
}

