/**
 * 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,
	_jBtnCreateAccount2:null,
	/**
	 * jQuery reference to the login button
	 * @type {jQuery}
	 */
	_jBtnLogin: null,
	
	/**
	 * jQuery reference to the view profile button
	 * @type {jQuery}
	 */
	_jBtnViewMyActivity: null,
	
	/**
	 * jQuery reference to the login zone
	 * @type {jQuery}
	 */
	_jZone: null,
	
	/**
	 * jQuery reference to the 'My Calendar' button
	 * @type {jQuery}
	 */
	_jMyCalendar: null,
	
	/**
	 * Initialize the login
	 */
	init: function() {
		console.log('VCMLogin.init()');
		
		this._login = $('#loginForm').length == 1;
		this._jZone = $('#zoneLogin');
		this._jMyCalendar = $('#myCalendar');
		
		if (this._login) {
			this._jLoginForm = $('#loginForm');
			this._jLnkForgotPassword = $('#lnkForgotPassword');
			this._jBtnCreateAccount = $('#btnCreateAccount');
			this._jBtnCreateAccount2 = $ ('#btnCreateAccount2');
			this._jBtnLogin = $('#btnLogin');
		} else {
			this._jBtnViewMyActivity = $('#btnViewMyActivity');
		}
		
		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._jBtnCreateAccount2=null;
			this._jBtnLogin = null;
		} else {
			this._jBtnViewMyActivity = null;
		}
		
		this._login = null;
		this._jZone = null;
		this._jMyCalendar = 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._jBtnCreateAccount2.click(function() {
				var inscriptionModal = new VCMInscriptionModal(me._page);
				inscriptionModal.init();
				return false;
			});
			this._jBtnLogin.click(function() {
				me._doLogin();
				return false;
			});

			//Submit form on Enter keypress
			$("#loginForm").keypress(function(e) {
				if(e.keyCode == 13) {
					me._doLogin();
				}
			});
			
		} else {
			this._jBtnViewMyActivity.click(function() {
				me.goToProfile();
				return false;
			});
		}
		this._jMyCalendar.click($.proxy(this, 'onMyCalendarClick'));
	},
	
	/**
	 * Remove events
	 */
	removeEvents: function() {
		console.log('VCMLogin.removeEvents()');
		if (this._login) {
			this._jLnkForgotPassword.unbind('click');
			this._jBtnCreateAccount.unbind('click');
			this._jBtnCreateAccount.unbind('click');
			this._jBtnLogin.unbind('click');
		} else {
			this._jBtnViewMyActivity.unbind('click');
		}
		this._jMyCalendar.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();
	},
	
	onMyCalendarClick: function(evt) {
		console.log('VCMLogin.onMyCalendarClick()');		
		this.toggleLogin();
		// Cancel default
		return false;
	},
	
	toggleLogin: function() {
		var btnHeight = this._jMyCalendar.outerHeight(),
			curHeight = this._jZone.innerHeight(),
			difHeight = $(this._login ? '#loginForm' : '#loggedActionBox').outerHeight(true);
		console.log(btnHeight, curHeight, difHeight);
		if (curHeight > btnHeight) {
			this._jZone.animate({height:btnHeight}, 500);
		} else {
			this._jZone.animate({height:btnHeight + difHeight}, 500);
		}
	}
};


