$(document).ready(function() {
	
	//Buttons that trigger the popup and are in the navigation lsit
	$(".signin_popup_button, .signup_popup_button, .recoverpass_popup_button, .activate_account_popup_button").click(function(e) {
		$(this).doPopup({
			button_type: $(this).attr("id")
		});
		//this is needed so it wont fire the closePopup function when we click the body
		e.stopPropagation();
	});
	
	//Buttons that can be found when the popup is activate
	//and are on the right side of the popup and slide left and right the pages
	$(".switch_popup_signup_button, .switch_popup_signin_button, .switch_popup_recover_pass_button, .switch_popup_new_activation_code_button, .switch_popup_resent_activation_code_button").click(function(){
		$(this).doPopup({
			button_type: $(this).attr("id"),
			slide: 1
		});
	});
	
	//signs the user in
	$("#signin_submit").click(function(){
		//get username
		var username = $("#username_input_signin").val();
		//get user password
		var password = $("#password_input_signin").val();
		//params that we will sent to the ajax request
		var params = {
			'method' : 'signin',
			'username' : username,
			'password' : password
		};
		//submit the ajax request
		$(this).submitAjax({
			params: params,
			button: $(this).attr("id"),
			correctMessage: false
		});
	});
	
	//sign up the user
	$("#signup_submit").click(function(){
		//get each variable
		var username = $("#username_input_signup").val();
		var firstname = $("#firstname_input_signup").val();
		var lastname = $("#lastname_input_signup").val();
		var password1 = $("#password_input_signup").val();
		var password2 = $("#retype_password_input_signup").val();
		var email = $("#email_input_signup").val();
		var gender = $("#gender_input_signup option:selected").attr("name");
		var activate_os = $("#activate_onsignup").attr("checked");
		//set the params needed for the request
		var params = {
			'method' : 'signup',
			'username' : username,
			'firstname' : firstname,
			'lastname' : lastname,
			'password1' : password1,
			'password2' : password2,
			'email' : email,
			'gender' : gender,
			'activate_os' : activate_os
		};
		//submit the request
		$(this).submitAjax({
			params: params,
			button: $(this).attr("id"),
			correctMessage: false
		});
	});
	
	//update user settings for the user page
	$("#update_user_settings").click(function(){
		//get the variables needed
		var username = $("#user_settings_username").val();
		var firstname = $("#user_settings_firstname").val();
		var lastname = $("#user_settings_lastname").val();
		var current_password = $("#user_settings_current_password").val();
		var password1 = $("#user_settings_password").val();
		var password2 = $("#user_settings_retype_password").val();
		var email = $("#user_settings_email").val();
		var gender = $("#user_settings_gender option:selected").attr("name");
		//params to proceed the ajax request
		var params = {
			'method' : 'update_settings',
			'username' : username,
			'firstname' : firstname,
			'lastname' : lastname,
			'current_password' : current_password,
			'password1' : password1,
			'password2' : password2,
			'email' : email,
			'gender' : gender
		};
		//submit the request
		$(this).submitAjax({
			params: params,
			button: $(this).attr("id"),
			correctMessage: false
		});
	});
	
	//update user settings as admin
	$(".admin_update_user_settings").click(function(){
		//get the variables needed
		var username = $(this).parents(".user_profile_details").find("#user_settings_username").val();
		var username_ue = $(this).parents(".user_profile_details").find("#u_un_ue").val();
		var firstname = $(this).parents(".user_profile_details").find("#user_settings_firstname").val();
		var lastname = $(this).parents(".user_profile_details").find("#user_settings_lastname").val();
		var password1 = $(this).parents(".user_profile_details").find("#user_settings_password").val();
		var password2 = $(this).parents(".user_profile_details").find("#user_settings_retype_password").val();
		var email = $(this).parents(".user_profile_details").find("#user_settings_email").val();
		var email_ue = $(this).parents(".user_profile_details").find("#u_em_ue").val();
		var gender = $(this).parents(".user_profile_details").find("#user_settings_gender option:selected").attr("name");
		//params to proceed the ajax request
		var params = {
			'method' : 'admin_update_settings',
			'username' : username,
			'username_ue' : username_ue,
			'firstname' : firstname,
			'lastname' : lastname,
			'password1' : password1,
			'password2' : password2,
			'email' : email,
			'email_ue' : email_ue,
			'gender' : gender
		};
		//submit the request
		$(this).submitAjax({
			params: params,
			button: $(this).attr("class"),
			correctMessage: true
		});
	});
	
	//recover password button
	$("#recoverpass_submit").click(function(){
		var email = $("#email_input_recpass").val();
		var params = {
			'method' : 'recover_password',
			'email' : email
		};
		$(this).submitAjax({
			params: params,
			button: $(this).attr("id"),
			correctMessage: false
		});
	});
	
	//resent activation code
	$("#resent_activation_code_submit").click(function(){
		var email = $("#email_input_resent_activation_code").val();
		var params = {
			'method' : 'resent_activation_code',
			'email' : email
		};
		$(this).submitAjax({
			params: params,
			button: $(this).attr("id"),
			correctMessage: false
		});
	});
	
	//activate account
	$("#activateaccount_submit").click(function(){
		var email = $("#email_input_activateaccount").val();
		var code = $("#code_input_activateaccount").val();
		var params = {
			'method' : 'activate_account',
			'email' : email,
			'code' : code
		};
		$(this).submitAjax({
			params: params,
			button: $(this).attr("id"),
			correctMessage: false
		});
	});
	$(".view_account_container").hover(
		function(){
			$(".account_body_actions", this).show();
		},
		function(){
			$(".account_body_actions", this).hide();
		}
	);
	$(".account_edit").click(function(){
		if ($(this).parent().parent().next().is(":visible")){
			$(this).parent().parent().next().hide();
		}else{
			$(".view_account_edit_container").hide();
			$(".message_error").html('').hide();
			$(this).parent().parent().next().show(); 
		}
		return false;
	});
});
