/**
 * Create an instance of VCMLogin
 * @classDescription Class for the login
 * @param {VCMPage} page Reference to the VCMPage
 * @return Returns a VCMLogin object
 * @constructor
 */
function VCMLogin(page) {
	this._page = page;
}

VCMLogin.prototype = {
	/**
	 * Reference to the VCMPage
	 * @type {VCMPage}
	 */
	_page: null,
	
	/**
	 * @type {Boolean}
	 */
	_login: false,
	
	/**
	 * jQuery reference to the login form
	 * @type {jQuery}
	 */
	_jLoginForm: null,
	
	/**
	 * jQuery reference to the forgot password link
	 * @type {jQuery}
	 */
	_jLnkForgotPassword: null,
	
	/**
	 * jQuery reference to the create account button
	 * @type {jQuery}
	 */
	_jBtnCreateAccount: null,
	
	/**
	 * jQuery reference to the login button
	 * @type {jQuery}
	 */
	_jBtnLogin: null,
	
	/**
	 * jQuery reference to the view profile button
	 * @type {jQuery}
	 */
	_jBtnViewProfile: null,
	
	/**
	 * jQuery reference to the create account link in the CMS menu
	 * @type {jQuery}
	 */
	_jLnkCreateAccount: null,
	
	/**
	 * jQuery reference to the create account link in the Footer menu
	 * @type {jQuery}
	 */
	_jLnkFooterCreateAccount: null,
	
	/**
	 * Initialize the login
	 */
	init: function() {
		console.log('VCMLogin.init()');
		this._login = $('#loginForm').length == 1;
		if (this._login) {
			this._jLoginForm = $('#loginForm');
			this._jLnkForgotPassword = $('#lnkForgotPassword');
			this._jBtnCreateAccount = $('#btnCreateAccount');
			this._jBtnLogin = $('#btnLogin');
			this._jLnkCreateAccount = $('#lnkCreateAccount');
			this._jLnkFooterCreateAccount = $('#lnkFooterCreateAccount');
		} else {
			this._jBtnViewProfile = $('#btnViewProfile');
		}
		
		this.addEvents();
	},
	
	/**
	 * Destroy the login
	 */
	destroy: function() {
		console.log('VCMLogin.destroy()');
		this.removeEvents();
		
		if (this._login) {
			this._jLoginForm = null;
			this._jLnkForgotPassword = null;
			this._jBtnCreateAccount = null;
			this._jBtnLogin = null;
			this._jLnkCreateAccount = null;
			this._jLnkFooterCreateAccount = null;
		} else {
			this._jBtnViewProfile = null;
		}
	},
	
	/**
	 * Add events
	 */
	addEvents: function() {
		console.log('VCMLogin.addEvents()');
		var me = this;
		if (this._login) {
			this._jLnkForgotPassword.click(function() {
				me._forgotPassword();
				return false;
			});
			this._jBtnCreateAccount.click(function() {
				var inscriptionModal = new VCMInscriptionModal(me._page);
				inscriptionModal.init();
				return false;
			});
			this._jBtnLogin.click(function() {
				me._doLogin();
				return false;
			});
			this._jLnkCreateAccount.click(function() {
				var inscriptionModal = new VCMInscriptionModal(me._page);
				inscriptionModal.init();
				return false;
			});
			this._jLnkFooterCreateAccount.click(function() {
				var inscriptionModal = new VCMInscriptionModal(me._page);
				inscriptionModal.init();
				return false;
			});
		} else {
			this._jBtnViewProfile.click(function() {
				me.goToProfile();
				return false;
			});
		}
	},
	
	/**
	 * Remove events
	 */
	removeEvents: function() {
		console.log('VCMLogin.removeEvents()');
		if (this._login) {
			this._jLnkForgotPassword.unbind('click');
			this._jBtnCreateAccount.unbind('click');
			this._jBtnLogin.unbind('click');
			this._jLnkCreateAccount.unbind('click');
			this._jLnkFooterCreateAccount.unbind('click');
		} else {
			this._jBtnViewProfile.unbind('click');
		}
	},
	
	/**
	 * Do the login
	 */
	_doLogin: function() {
		console.log('VCMLogin._doLogin()');
		var me = this;
		// TODO Send email and password as post data		
		$.getJSON(VCMUtils.prepareCommand({url:'doJSON',command:'login'}), this._jLoginForm.serialize(), function(data) {
			if (data.success) {
				me._page.getContent().load({command:'displayHomePage'}, true);
				me._page.getSideBar().displayProfile();
			} else {
				alert(data.msg);
			}
		});
	},
	
	/**
	 * Forgot password
	 */
	_forgotPassword: function() {
		console.log('VCMLogin._forgotPassword()');
		var modal = this._page.getModal(),
			me = this;
		
		modal.show($('#passwordModal').html());
		
		var btnSendPassword = $('.btnSendPassword'),
			email = $("#popupContent .passwordForm input[name='email']");
		btnSendPassword.click(function() {
			$.getJSON(VCMUtils.prepareCommand({url:'doJSON',command:'retrievePassword',email:email.val()}), function(data) {
				btnSendPassword.unbind('click');
				if (data.success) {
					// Hide the form
					$('#popupContent .passwordForm').hide();
					// Show the sent part
					$('#popupContent .passwordSent').show();
					// Put the return message in the popup
					$('#popupContent .passwordSentTo').html(data.msg);
				} else {
					// Replace content with error message
					$('#popupContent .passwordContent').html(data.msg);
				}
			});
		});
		
		var btnClose = $('.passwordFooter > a.btn');		
		btnClose.click(function() {
			modal.close();
			btnClose.unbind('click');
		});
	},
	
	/**
	 * Show a login reminder
	 */
	showLoginReminder: function() {
		console.log('VCMLogin.showLoginReminder()');
		$('#loginReminderContent').html($('#txtLoginReminder').html());
		$('#loginReminderClose > a.btn').html($('#txtFermer').html());
		
		$('#loginReminder').show();
		$('#loginReminderClose > a.btn').click(function() {
			$('#loginReminder').hide();
			$('#loginReminderClose > a.btn').unbind('click');
		});
	},
	
	/**
	 * Goto the profile
	 */
	goToProfile: function() {
		console.log('VCMLogin.goToProfile()');
		this._page.getContent().showHomePage();
		this._page.getSideBar().displayProfile();
	}
};


