/**
* Action System Installation
*
* @author	  RaphaeL Pralat <raph@plus2.fr>
* @version    CVS: $Id:$
* @uses       http://prototype.conio.net/
* @uses       http://bennolan.com/behaviour/
* @uses       http://script.aculo.us
*/

var						ActionUser = {

	rules :				{
	
	/**
	 * Admin Login
	 *
	 */
	'#adminLogin' : function(el) {
		el.onclick = function() {
			$('formAdminLogin').submit();
		}
	},
	
	/**
	 * User Login
	 *
	 */
	'#userLoginAction' : function(el) {
		el.onclick = function() {
			
			form = $('formLogin').serialize();
			$('buttonConnectionAction').innerHTML = "en cours...";
			new Ajax.Updater('connectionHeader', '/ajax/login', {
				parameters	: form,
				method		: 'post',
				onComplete	: ActionUser.testLogin
			});
		}
	},
	
	/**
	 * User Disconnect
	 *
	 */
	'#userDisconnectAction' : function(el) {
		el.onclick = function() {
			new Ajax.Updater('connectionHeader', '/ajax/disconnect', {
				parameters	: '',
				method		: 'post',
				onComplete	: ActionUser.redirectHome
			});
		}
	},
	
	/**
	 * User Inscription
	 *
	 */
	'#userInscription' : function(el) {
		el.onclick = function() {
			form = $('formInscription').serialize();
			$('formInscriptionSubmit').innerHTML = '<img src="/themes/_main/images/anim/loading.gif">';
			new Ajax.Updater('inscriptionAction', '/ajax/inscription', {
				parameters	: form,
				method		: 'post',
				onComplete	: ActionUser.reload
			});
			
		}
	},
	
	/**
	 * User Forgot Pass
	 *
	 */
	'#actionForgotPass' : function(el) {
		el.onclick = function() {
			form = $('formForgotPass').serialize();
			new Ajax.Updater('forgotPassAction', '/ajax/forgotPass', {
				parameters	: form,
				method		: 'post',
				onComplete	: ActionUser.reload
			});
		}
	},
	
	/**
	 * Change User Information
	 *
	 */
	'.updateUserInformationAction' : function(el) {
		el.onclick = function() {
			
			// Update or Display
			var action = findParamInClass('action', this);
			
			if (action == "update") {
				form = 0;
			} else if (action == "updateSubmit") {
				form = $('userInformationForm').serialize();
			} else {
				form = 0;
			}
			
			new Ajax.Updater('informationAction', '/ajax/updateUserInformation', {
				parameters	: "action="+ action + "&" + form,
				method		: 'post',
				onComplete	: ActionUser.reload
			});
			
		}
	},
	
	/**
	 * User Show Cgu
	 *
	 */
	'.userShowCguAction' : function(el) {
		el.onclick = function() {
			if(($('cgu').style.display == 'block')) {
				$('pub').style.display = 'block';
				$('cgu').style.display = 'none';
			} else {
				$('pub').style.display = 'none';
				$('cgu').style.display = 'block';
			}
		}
	},
	
	/**
	 * User Add Relation
	 *
	 */
	'.ajaxAddUserRelation' : function(el) {
		el.onclick = function() {
			this.innerHTML = 'attente de validation';
			var userId = findParamInClass('userId', this);
			new Ajax.Request('/ajax/addUserRelation', {
				parameters	: 'userId=' + userId,
				method		: 'post',
				onComplete	: ActionUser.reload
			});
		}
	},
	
	/**
	 * User Validate Relation
	 *
	 */
	'.ajaxValidUserRelation' : function(el) {
		el.onclick = function() {
			var relationId = findParamInClass('relationId', this);
			$('relation_' + relationId).style.display = 'none';
			new Ajax.Updater('desktopRelationBox', '/ajax/validUserRelation', {
				parameters	: 'relationId=' + relationId,
				method		: 'post',
				onComplete	: ActionUser.reload
			});
		}
	},
	
	/**
	 * User Delete Relation
	 *
	 */
	'.ajaxDeleteRelation' : function(el) {
		el.onclick = function() {
			var relationId = findParamInClass('relationId', this);
			new Ajax.Updater('desktopRelationBox', '/ajax/deleteUserRelation', {
				parameters	: 'relationId=' + relationId,
				method		: 'post',
				onComplete	: ActionUser.reload
			});
		}
	},
	
	
	/**
	 *
	 * Change User Information
	 *
	 */
	'#ajaxUserInfoEdit' : function(el) {
		el.onclick = function() {
			form = $('formUserInfo').serialize();
			
			new Ajax.Updater('formUserInfo', '/ajax/userInfoEdit', {
				parameters	: form,
				method		: 'post',
				onComplete	: ActionUser.reload
			});
		}
	},
	
	
	
	/**
	 * User Menu
	 *
	 */
	'.userMenuAction' : function(el) {
		el.onmouseover = function() {
			floating = el.getElementsByTagName("div")[0].getElementsByTagName("div")[0];
			floating.style.display = "inline";
			el.getElementsByTagName("img")[0].style.display = "none";
			el.getElementsByTagName("img")[1].style.display = "inline";
		},
		el.onmouseout = function() {
			floating = el.getElementsByTagName("div")[0].getElementsByTagName("div")[0];
			floating.style.display = "none";
			el.getElementsByTagName("img")[1].style.display = "none";
			el.getElementsByTagName("img")[0].style.display = "inline";
		}
	},
	
	'lastbutnotleast': {}
	}, /** rules **/
	
	'reload' :	function(ajax) {
		 Behaviour.apply(ActionUser.rules);
	},
	
	/**
	 * Redirection à la porc quand la connection n'est pas bonne
	 *
	 *
	 */
	'testLogin' :	function(ajax) {
		
		testLogin = $('testLogin');
		Behaviour.apply(ActionUser.rules);
		if (testLogin == null) {
			window.location.href = "/membre/connexion"
		} else {
			document.location.reload();
		}
	},
	
	'redirectHome' :	function(ajax) {
		window.location.href = "/"
	}
}


function findParamInClass(param, el) {
   var regexp = new RegExp(param + '_([A-Za-z0-9/:?&\-\._]+)');
   var mymatch = regexp.exec(el.className);
   if(mymatch) {
       return mymatch[1];
   }
   return false;
}

Behaviour.register(ActionUser.rules);