var referer = null;
$(function(){
	$('#top-login-button').bind('showLoginForm', function(){
		var form = $(this).closest('.login').find('.login-form');
		
		$(this).addClass('active');
		form.show();
		
		$(document).bind('click.hideLoginForm', function(){
			$('#top-login-button').trigger('hideLoginForm');
		});
	}).bind('hideLoginForm', function(){
		var form = $(this).closest('.login').find('.login-form');
		
		$(this).removeClass('active');
		form.hide();
		
		$(document).unbind('click.hideLoginForm');
	}).bind('click', function(event){
		event.preventDefault();
		event.stopPropagation();
		$(this).trigger('showLoginForm');
	});
	
	$('.login-form').find('form').formHints();
	
	$('.login-form').bind('click', function(event){
		event.stopImmediatePropagation();
		$(this).find('form')./*formHints().*/validate({
			errorPlacement: function(error, element) {
			 	if($(element).next('span.error').length == 0){
			 		$(element).after('<span class="error">'+$(error).html()+'</span>');
			 	}
			},
			highlight: function(element, errorClass){
				$(element).addClass(errorClass).next('span.error').show();
			},
			unhighlight: function(element, errorClass){
				$(element).removeClass(errorClass).next('span.error').hide();
			},
			rules:{
				email: {requiredWithFormHint: true, email: true},
				password: {requiredWithFormHint: true}
			},
			messages:{
				email: {requiredWithFormHint: 'Podaj adres email',
						email: 'Adres email jest niepoprawny'},
				password: {requiredWithFormHint: 'Podaj hasło'}
			}
		});
	});
	
	
	$('.facebook-login').click(function(event)
			{
		FB.login(function(response) {
			   if (response.authResponse) {
					//Uzyskano dostęp
				     $.ajax({
						async: false,
						type: 'post',
						dataType: 'json',
						url: '/kandydat/logowanie/socialMediaLogin',
						data: {'service': 'facebook', 'data': {'accessToken': response.authResponse.accessToken, 'userId': response.authResponse.userID}},
						success: function(data){
							if(data.status == 'ok')
								{
									window.location.href = '/candidate/profile2';
								}
							else
								{
									ShowDialog({
										title: 'Logowanie',
										html: data.msg,
										icon: DialogIcons.ERROR
									});
								}
							},
						error: function(){
							ShowDialog({
								title: 'Logowanie',
								html: 'Wystąpił nieoczekiwany błąd. Prosimy spróbować ponownie.',
								icon: DialogIcons.ERROR
							});
							}
			     });
			   }
			   else{
				   //Odmówiono dostępu
			   } 
			 }, {scope: 'email,publish_stream,user_work_history,user_education_history,friends_work_history,friends_education_history'});
		});
	
	
	
	$('.google-login').click(function(event){
		var url = 'https://accounts.google.com/o/oauth2/auth?';
			url +=  'client_id=415744080325.apps.googleusercontent.com&';
			url +=  'redirect_uri=http://www.absolvent.pl/googleAuth&';
			url +=  'scope=https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email&';
			url +=  'response_type=token';
		
			
			var popUpWindow = window.open(url,"_blank", 'height=500, width=400');
			
			var fun = function(){
					if(popUpWindow.closed)
					{
						window.clearInterval(id);
						if(!$.cookie('accessToken')) return;
						$.ajax({
							url: '/kandydat/logowanie/socialMediaLogin',
							type: 'post',
							dataType: 'json',
							data: {'service': 'google', 'data': {'accessToken': $.cookie('accessToken')}},
							success: function(data){
								if(data.status == 'ok')
									{
										window.location.href = '/candidate/profile2';
									}
								else
									{
										ShowDialog({
											title: 'Logowanie',
											html: data.msg,
											icon: DialogIcons.ERROR
										});
									}
								},
							error: function(){
								ShowDialog({
									title: 'Logowanie',
									html: 'Wystąpił nieoczekiwany błąd. Prosimy spróbować ponownie.',
									icon: DialogIcons.ERROR
								});
							}
							});
						return;
					}
				};
			
			var id = window.setInterval(fun, 100);
		});
	
	
	$('#google-register').click(function(event){
		var url = 'https://accounts.google.com/o/oauth2/auth?';
			url +=  'client_id=415744080325.apps.googleusercontent.com&';
			url +=  'redirect_uri=http://www.absolvent.pl/googleAuth&';
			url +=  'scope=https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email&';
			url +=  'response_type=token';
		
			
			var popUpWindow = window.open(url,"_blank", 'height=500, width=400');
			
			var fun = function(){
					if(popUpWindow.closed)
					{
						window.clearInterval(id);
						if(!$.cookie('accessToken')) return;
						$.ajax({
							url: '/kandydat/rejestracja/socialMediaRegistration',
							type: 'post',
							dataType: 'json',
							data: {'service': 'google', 'data': {'accessToken': $.cookie('accessToken')}},
							success: function(data){
								if(data.status == 'ok')
									{
										if(referer)
											window.location.href = referer;
										else 
											window.location.href = '/candidate/profile2';
									}
								else
									{
										ShowDialog({
											title: 'Rejstracja',
											html: data.msg,
											icon: DialogIcons.ERROR
										});
									}
								},
							error: function(){
								ShowDialog({
									title: 'Rejstracja',
									html: 'Wystąpił nieoczekiwany błąd. Prosimy spróbować ponownie.',
									icon: DialogIcons.ERROR
								});
							}
							});
						return;
					}
				};
			
			var id = window.setInterval(fun, 100);
			
				
		});
				
			
	$('#facebook-register').click(function(event)
			{
		FB.login(function(response) {
			   if (response.authResponse) {
					//Uzyskano dostęp
					 $.ajax({
						async: false,
						type: 'post',
						dataType: 'json',
						url: '/kandydat/rejestracja/socialMediaRegistration',
						data: {'service': 'facebook', 'data': {'accessToken': response.authResponse.accessToken, 'userId': response.authResponse.userID}},
						success: function(data){
							if(data.status == 'ok')
								{
									if(referer)
										window.location.href = referer;
									else 
										window.location.href = '/candidate/profile2';
								}
							else
								{
									ShowDialog({
										title: 'Rejstracja',
										html: data.msg,
										icon: DialogIcons.ERROR
									});
								}
							},
						error: function(){
							ShowDialog({
								title: 'Rejstracja',
								html: 'Wystąpił nieoczekiwany błąd. Prosimy spróbować ponownie.',
								icon: DialogIcons.ERROR
							});
							}
				 });
			   }
			   else{
				   //Odmówiono dostępu
			   } 
			 }, {scope: 'email,publish_stream,user_work_history,user_education_history,friends_work_history,friends_education_history'});
		});
});
