(function($) {
 
 var g_dirty = false;
 
 function main()
 {
  if( $( '#frmEditor' ).length > 0 )
  {
   $( '#frmEditor' ).ajaxForm();
   $( '#frmEditor .btn' ).click( onSubmitEdit );
   $( window ).unload( saveChanges );
   $( '#frmEditor input' ).change( setDirty );
   $( '#frmEditor select' ).change( setDirty );
  }
  else if( $( 'input#user' ).length > 0 )
  {
   $( 'input#user' ).focus();
   if( $( 'input#passDummy' ) && $( 'input#pass' ) )
   {
    $( 'input#userDummy' ).css( 'display', 'block' );
    $( 'input#user' ).css( 'display', 'none' );
    $( 'input#passDummy' ).css( 'display', 'block' );
    $( 'input#pass' ).css( 'display', 'none' );
    $( 'input#userDummy' ).bind( 'focus', onFocusUserDummy );
    $( 'input#passDummy' ).bind( 'focus', onFocusPassDummy );
    $( 'input#user' ).bind( 'blur', onBlurUserDummy );
    $( 'input#pass' ).bind( 'blur', onBlurPassDummy );
   }
  }
  else if( $( 'input#eml1' ).length > 0 )
  {
   if( !$( 'input#eml1' ).attr( 'value' ) )
   {
    $( 'input#eml1' ).focus();
   }
   else if( !$( 'input#eml2' ).attr( 'value' ) )
   {
    $( 'input#eml2' ).focus();
   }
  }
  $( '#athenesProfileEditor form input' ).keypress( onKeyInput );
 }

 function onKeyInput( e )
 {
  // Inhibit enter key in profile editor
  if( e.which && e.which == 13 || e.keyCode && e.keyCode == 13 )
  {
   e.preventDefault();
  }
 }

 function onFocusPassDummy( e )
 {
  
  $( 'input#passDummy' ).css( 'display', 'none' );
  $( 'input#pass' ).css( 'display', 'block' );
  $( 'input#pass' ).focus();
 }

 function onBlurPassDummy()
 {
  if( $( 'input#pass' ).attr( 'value' ) == '' )
  {
   $( 'input#passDummy' ).css( 'display', 'block' );
   $( 'input#pass' ).css( 'display', 'none' );
  }
 }
 
 function onFocusUserDummy( e )
 {
  
  $( 'input#userDummy' ).css( 'display', 'none' );
  $( 'input#user' ).css( 'display', 'block' );
  $( 'input#user' ).focus();
 }

 function onBlurUserDummy()
 {
  if( $( 'input#user' ).attr( 'value' ) == '' )
  {
   $( 'input#userDummy' ).css( 'display', 'block' );
   $( 'input#user' ).css( 'display', 'none' );
  }
 }

 function setDirty()
 {
  g_dirty = true;
 }

 function saveChanges()
 {
  if( g_dirty && confirm( 'Ønsker du å lagre endringene du har gjort før du går videre?' ) )
  {
   var postVars = $( '#frmEditor' ).formSerialize();
   postVars += ( '&cmdSave=1' );
   $.post( 'ajax.php?action=edit', postVars );
  }
 }

 function onSubmitEdit( e )
 {
  var postVars = $( '#frmEditor' ).formSerialize();
  postVars += ( '&' + this.name + '=1' );
  switch( this.name )
  {
   case 'cmdSave':
    $.post( 'ajax.php?part=profile_editor&action=edit', postVars, okSubmitEdit );
    break;
   case 'cmdDeleteProfile':
    if( confirm( 'Dette vil slette din profil for godt.\nØnsker du å fortsette?' ) )
    {
     $.post( 'ajax.php?action=edit', postVars, okSubmitDeletion );
    }
    break;
  }
  return false;
 }

 function okSubmitDeletion( data, status )
 {
  if( status == 'success' )
  {
   alert( 'Brukerprofilen er slettet' );
   window.location = 'index.php?action=logout'; 
  }
  else
  {
   alert( 'En feil har oppstått, endringene er ikke utført. Vi beklager det intrufne.\nVennligst Prøv igjen senere.' );
  }
 }
 
 function okSubmitEdit( data, status )
 {
  if( status == 'success' )
  {
   $( '#profileEditor' ).html( data );
   g_dirty = false;
  }
  else
  {
   alert( 'En feil har oppstått, endringene er ikke lagret. Vi beklager det intrufne.\nVennligst Prøv igjen senere.' );
  }
  $( '#frmEditor .btn' ).click( onSubmitEdit );
 }
 $( document ).ready( main );
})(jQuery);