function initUserPanel() {
	if($$('div#userPanel.loginPanel')[0]) {
		if(loginForm = $$('div#userPanel.loginPanel form')[0]) {
			loginForm.onsubmit = function() {
				respondToClick(
          '/includes/userPanel.php',
          'loginPanel',
          Form.serialize(loginForm),
          'Fel användarnamn eller lösenord. <a href="#" onclick="forgotPassword()">Klicka här</a>'
        );
				return false;
			}
      if(createAccountLink = $$('div#userPanel.loginPanel p.loginLeft a')[0]) {
        createAccountLink.onclick = function() {
          respondToClick(
            '/includes/registerPanel.php',
            'accountForm',
            {
              action: 'ajaxRegister'
            }
          );
          return false;
        }
      }
    } else if(logout = $$('div#userPanel.loginPanel a.logoutButton')[0]) {
			logout.onclick = function() {
				respondToClick(
          '/includes/userPanel.php',
          'loginPanel',
          {
            logout: 'true'
          }
        );
				return false;
			}
      changeUser = $$('div#userPanel.loginPanel p.loggedInRight a')[0];
      changeUser.onclick = function() {
        respondToClick(
          '/includes/changeUserSettings.php',
          'accountForm change',
          {
            action: 'changeuser'
          }
        );
        return false;
      }
		}
	} else if($$('div#userPanel.accountForm')[0]) {
    if(accountForm = $$('div#userPanel.accountForm form')[0]) {
      accountForm.onsubmit = function() {
        if($$('div#userPanel.accountForm')[0].hasClassName('change')) {
          respondToClick(
            '/includes/changeUserSettings.php',
            'accountForm change',
            Form.serialize(accountForm),
            'Fel vid ändring!'
          );
        } else if($$('div#userPanel.accountForm')[0].hasClassName('forgotPw')) {
          respondToClick(
            '/includes/forgotpw.php',
            'accountForm forgotPw',
            Form.serialize(accountForm)
          );
        } else {
          respondToClick(
            '/includes/registerPanel.php',
            'accountForm',
            Form.serialize(accountForm),
            'Fel vid registrering!'
          );
        }
        return false;
      }
      cancelButton = $$('div#userPanel.accountForm a.cancelButton')[0];
      cancelButton.onclick = function() {
        respondToClick(
          '/includes/userPanel.php',
          'loginPanel'
        );
        return false;
      }
      if(moreinfoButton = $$('div#userPanel.accountForm ul+p>a')[0]) {
        moreinfoDiv = $$('div#userPanel.accountForm div.hidden')[0];
        moreinfoDiv.hide();
        moreinfoButton.onclick = function() {
          moreinfoDiv.toggle();
          return false;
        }
      }
    }
    if(successButton = $$('div#userPanel.accountForm p.regSuccess a')[0]) {
      successButton.onclick = function() {
        respondToClick(
          '/includes/userPanel.php',
          'loginPanel'
        );
        return false;
      }
    }
  }
  initMemberfunctions();
}

function forgotPassword() {
  var errorP = $$('.errorMsgAjax')[0];
  errorP.dropOut({duration: 0.2, afterFinish: function() {
    errorP.remove();
  }} );
  respondToClick(
    '/includes/forgotpw.php',
    'accountForm forgotPw',
    {
      action: 'getpw'
    }
  );
}

function respondToClick(url, className, params, errorMsg) {
  new Ajax.Request(
    url, {
      onSuccess: function(response) {
        if(response.responseText != 'epicFail') {
          updateUserPanel(response.responseText, className);
        } else if(errorMsg) {
          flashError(errorMsg);
        }
      },
      parameters: params
    }
  );
}

function flashError(errorMsg) {
	if(!($$('.errorMsgAjax')[0])) {
		var myP = new Element('p', { 'class':'errorMsgAjax' }).update(errorMsg);
		var myPCloseButton = new Element('div', { 'class':'errorMsgAjaxClose' });
		myP.insert({'top':myPCloseButton});
		myP.hide();
		$$('.loginPanel')[0].insert({'before':myP});
		
		new Effect.Parallel([
			new Effect.Move(myP, {
				sync: true,
				x: 300,
				y: 130,
				transition: Effect.Transitions.spring
			}),
			new Effect.Appear(myP, { sync: true})
		], {
			duration: 1.3
		});
		myPCloseButton.onclick = function() {
			myP.fade({duration: 0.3, afterFinish: function() {
				myP.remove()
			}} );
		}
	}
}

function updateUserPanel(response,newclass) {
	$('userPanel').fade({duration: 0.25, afterFinish: function()
		{
			$('userPanel').update(response);
      $('userPanel').className = newclass;
			$('userPanel').appear({duration: 0.25});
			initUserPanel();
		}
	});
}