//Copyright EQAL, Inc. 2008

U.User = $extend({
	set : function(props) {
		$extend(this, props);
	},
	showLogin : function() {
		//new U.Request().send(U.Request.Manager.getConfig('getLoginHtml'), this._showLogin.bind(this));
		new U.Request().send(U.Request.Manager.getConfig('getLoginForm'), this._showLogin.bind(this));
	},
	_showLogin : function(html) {
		this._showInTaskbar(html);
		new U.Form('login-form');
		window.location = '#u-login'; //temporary till redesign
	},
	showLoginSuccess : function(html) {
		//redirect to html instead of show in task bar
		//this._showInTaskbar(html);
		window.location = html;
	},
	showSignUp : function() {
		//new U.Request().send(U.Request.Manager.getConfig('getSignUpHtml'), this._showSignUp.bind(this));
		new U.Request().send(U.Request.Manager.getConfig('getSignUpForm'), this._showSignUp.bind(this));
		window.location = '#u-login'; //temporary till redesign
	},
	_showSignUp : function(html) {
		this._showInTaskbar(html);
		this.signUp = new U.User.SignUp();
	},
	showSignUpSuccess : function(html) {
		this._showInTaskbar(html);
	},
	showLostPwd : function() {
		new U.Request().send(U.Request.Manager.getConfig('getLostPwdHtml'), this._showLostPwd.bind(this));
	},
	_showLostPwd : function(html) {
		this._showInTaskbar(html);
		new U.User.LostPassword();
	},
	showLostPasswordSuccess : function() {
		$log('about to request')
		new U.Request().send(U.Request.Manager.getConfig('getLostPwdSuccessHtml'), this._showInTaskbar.bind(this));
	},
	showProfile : function(user_id) {
		var config = U.Request.Manager.getConfig('getProfile', {userid:user_id});
		new U.Request().send(config, this._showProfile.bind(this));
	},
	_showProfile : function(html) {
		this._showInTaskbar(html);
	},
	showRequests : function(user_id) {
		var config = U.Request.Manager.getConfig('getRequests', {userid:user_id});
		new U.Request().send(config, this._showRequests.bind(this));
	},
	_showRequests : function(html) {
		this._showInTaskbar(html);
	},
	showNotifications : function(user_id) {
		var config = U.Request.Manager.getConfig('getNotifications', {userid:user_id});
		new U.Request().send(config, this._showNotifications.bind(this));
	},
	_showNotifications : function(html) {
		this._showInTaskbar(html);
	},
	showMessages : function(user_id) {
		var config = U.Request.Manager.getConfig('getMessages', {userid:user_id});
		new U.Request().send(config, this._showMessages.bind(this));
	},
	_showMessages : function(html) {
		this._showInTaskbar(html);
	},
	_showInTaskbar : function(html) {
		$usocial.loadHtml(html).maximize();
	},
	doAction : function(options) {
		$log(options.action);
		this.fireEvent(options.action, options.message);
	}
}, new Events);
U.User.SignUp = new Class({
	initialize : function() {
		this.form = new U.Form('signup-form', {doSubmit:false}).addEvent('submit', this._send.bind(this));
		$log('got here')
		this.email = U.UI.GetTextBox('signup-form-input-email');
		this.password = U.UI.GetTextBox('signup-form-input-password');
		this.month = $('signup-form-option-birthday-month');
		this.day = $('signup-form-option-birthday-day');
		this.year = $('signup-form-option-birthday-year');
		//this.form_name = $('form_name');
		
		//U.User.addEvent('signUpOk', this._signUpOk.bind(this)).addEvent('signUpFailed', this._signUpFailed.bind(this));
	},
	_signUpOk : function() {
		$log('calling : showSignUpSuccess')
		U.User.showSignUpSuccess();
	},
	_signUpFailed : function() {
		// handle errors here;
	},
	_send : function() {
		var config = U.Request.Manager.getFormConfig('signup-form');

		new U.Request().send(config, this._sendResponse.bind(this));
	},
	_sendResponse : function(responseText) {
		$log('we got the response');
		//hack for testing js remove
		//responseText = responseText.replace(" = ",":");
		eval('var r = '+responseText.getTagContent('eval'));
		if (r.success) {
			U.User.showSignUpSuccess(responseText.getTagContent('html'));
		} else {
			this.form.showErrors(r.errors);
		}
	}
});
U.User.Login = new Class({
	initialize : function() {
		this.form = new U.Form('login-form', {doSubmit:false}).addEvent('submit', this._send.bind(this));
		this.email = U.UI.GetTextBox('login-form-input-email');
		this.password = U.UI.GetTextBox('login-form-input-password');
		//this.form_name = $('form_name');
		
		//U.User.addEvent('signUpOk', this._signUpOk.bind(this)).addEvent('signUpFailed', this._signUpFailed.bind(this));
	},
	_signUpOk : function() {
		$log('calling : showLoginSuccess')
		U.User.showLoginSuccess();
	},
	_signUpFailed : function() {
		// handle errors here;
	},
	_send : function() {
		var config = U.Request.Manager.getFormConfig('login-form');

		new U.Request().send(config, this._sendResponse.bind(this));
	},
	_sendResponse : function(responseText) {
		$log('we got the response');
		eval('var r = '+responseText.getTagContent('eval'));
		if (r.success) {
			U.User.showLoginSuccess(responseText.getTagContent('html'));
		} else {
			this.form.showErrors(r.errors);
		}
	}
});
U.User.LostPassword = new Class({
	initialize : function() {
		this.form = new U.Form('lostpw-form', {doSubmit:false}).addEvent('submit', this._send.bind(this));
		this.email = U.UI.GetTextBox('lostpw-form-email');
		$log('got here')
		U.User.addEvent('lostPasswordOk', this._lostPasswordOk.bind(this))
	},//
	_lostPasswordOk : function() {
		$log('calling : showSignUpSuccess')
		U.User.showLostPasswordSuccess();
	},
	_send : function() {
		var config = U.Request.Manager.getConfig('lostpw');
		config.data.email = this.email.getValue();
		new U.Request().send(config, this._sendResponse.bind(this));
	},
	_sendResponse : function(responseText) {
		eval(responseText.getTagContent('eval'));
	}
});
U.Request.Manager.addConfig('getLoginForm', '/login/ajax_view');
U.Request.Manager.addConfig('getLoginSuccessForm', '/login/login');
U.Request.Manager.addConfig('getSignUpForm', '/join_eqal/ajax_view');
U.Request.Manager.addConfig('getSignUpSuccessForm', '/join_eqal/create');
U.Request.Manager.addConfig('getLostpwForm', '/lostpw/ajax_view');
U.Request.Manager.addConfig('getProfile', '/user_profile/ajax_view/#userid');
U.Request.Manager.addConfig('getRequests', '/pending_requests/ajax_list/#userid');
U.Request.Manager.addConfig('getNotifications', '/notifications/ajax_list/#userid');
U.Request.Manager.addConfig('getMessages', '/messages/ajax_list#userid');

