// --- funkce zjisti, jak je natom uzivatel a nastavi akce na ruzna tlacitka
function checkUserStatus() {

  $.post( 'user_status.php', processUser)
  $( '#login' ).click( showLoginExpandedForm )
  $( '.hide_login_expanded' ).click( hideLoginExpandedForm )
  $( '.try_to_login' ).click( tryLogin )
  $( '[name=user_login_form]' ).submit( loginFormSubmit )
  $( '.try_to_logout' ).click( tryLogout )

}

// --- funkce zjisti, zda je uzivatel prihlasen, podle toho zobrazi potrebny formular
function processUser( data ) {

  if( data == 'notloged' ) {
    // --- uzivatel neni prihlasen
    // --- zobrazim skryty prihlasovaci formular
    showForm( 'login' )
  } else {
    // --- uzivatel je prihlasen, musim zobrazit formular o prihlaseni a naplnit jej daty
    showForm( 'loged' )
    $( '#loged_user_name' ).html( data )
  }

}

function showLoginExpandedForm() { showForm( 'login_expanded' ); return false; }
function hideLoginExpandedForm() { clearLoginExpandedForm(); showForm( 'login' ); return false; }
function clearLoginExpandedForm() { $( '#login_expanded input' ).val(''); }

// --- funkce se pokusi o prihlaseni
function tryLogin() {

  loginFormSubmit()

  return false
}

// --- funkce se pokusi uzivatele odhlasit
function tryLogout() {
  $.get( 'user_login.php', {logout: 'true'}, function( data ) {

    if( data == 'OK' ) {
      showForm( 'login' )
      clearLoginExpandedForm()
    }

  } )
  return false
}

function loginFormSubmit() {

  $.post( 'user_login.php', {nick: $( '#login_expanded input[name=nick]' ).val(), pass: bs.encode($( '#login_expanded input[name=pass]' ).val())}, function( data ) {

    if( data == 'OK' ) {
      processUser( $( '#login_expanded input[name=nick]' ).val() );
    }

  } )
  return false
}

// --- funkce zobrazi pozadovany formular (ostatni skryje)
function showForm( formname ) {

  var login_forms = new Array( 'login', 'login_expanded', 'loged')
  for( var i = 0; i < login_forms.length; i++ ) {
    $( '#'+login_forms[i] ).css( 'display', (login_forms[i]==formname ? 'block' : 'none') )
  }
}

var bs = {

    // private property
    _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

    // public method for encoding
    encode : function (input) {
        var output = "";
        var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
        var i = 0;

        input = bs._utf8_encode(input);

        while (i < input.length) {

            chr1 = input.charCodeAt(i++);
            chr2 = input.charCodeAt(i++);
            chr3 = input.charCodeAt(i++);

            enc1 = chr1 >> 2;
            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
            enc4 = chr3 & 63;

            if (isNaN(chr2)) {
                enc3 = enc4 = 64;
            } else if (isNaN(chr3)) {
                enc4 = 64;
            }

            output = output +
            this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
            this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

        }

        return output;
    },

    // private method for UTF-8 encoding
    _utf8_encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    }
}