$(document).ready(function() {

  var ReloadPage = function() {
    document.location.href = document.location.href + (document.location.href.indexOf('?')!=-1? '&' : '?') + '_nocache=' + (new Date()).getTime();
  }
  var Loading = false;
  var Loaded = false;
  var ShowLogin = function(){
    $('#TB_login').find('.msg_note, .msg_error').remove();
//    $('#TB_login form').prepend('<div class="msg_error">Kui logida sisse esimest korda pärast arenduste jõustumist on soovitav probleemide vältimiseks tühjendada brauseri vahemälu ja kustutada küpsised (<a href="http://www.google.com/support/youtube/bin/answer.py?answer=105801&ln=et" target="blank">hea abitekst</a>).</div>');
    if ($('#TB_window').length) {
      tb_close(function(){
        tb_show('', '#TB_inline?&height=350&width=380&inlineId=TB_login');
      });
    } else {
      tb_show('', '#TB_inline?&height=350&width=380&inlineId=TB_login');
    }
  };
  $('#show_login_form').click(function(e){
    e.preventDefault();
    if (!Loaded && !Loading) {
      $.loadingIndicator.init(true, e);
      $.ajax({
        url: 'ajax.php?controller=auth&action=load&_nocache=' + (new Date()).getTime(),
        success: function(html){
          html = $(html).appendTo('body');
          setTimeout(function(){
            $('#TB_login form').jsonform({
              service : 'json.php?controller=auth&action=jlogin&_nocache=' + (new Date()).getTime(),
              resetOnSuccess : false,
              response : function(status, r){
                if (status == 'success') {
                  if (r.persons) {
                    var s = $('#select_person_for_login select').html('');
                    $.each(r.persons, function(){
                      s.append('<option value="' + this.value + '">' + this.label + '</option>');
                    })
                    $('#select_person_for_login [name=expire]').val($('#TB_window select[name=expire]').val());
                    tb_remove(function(){
                      tb_show('', '#TB_inline?&height=150&width=250&inlineId=select_person_for_login');
                    });
                  } else {
                    setCookie('user', r.cookie_value, r.cookie_time);
                    ReloadPage();
                  }
                
                }
              }
            });
            $('#select_person_for_login form').jsonform({
              service : 'json.php?controller=auth&action=jlogin&_nocache=' + (new Date()).getTime(),
              response : function(status, r){
                if (status == 'success') {
                  setCookie('user', r.cookie_value, r.cookie_time);
                  tb_remove();
                  ReloadPage();
                }
              }
            });
            $('#TB_forgottpassword form').jsonform({
              service : 'json.php?controller=auth&action=forgottpassword&_nocache=' + (new Date()).getTime(),
              response : function(status, r){
                if (status == 'success') {
                  tb_remove(function(){
                    tb_show('', '#TB_inline?&height=300&width=380&inlineId=TB_login');
                  });
                }
              }
            });
            $('#TB_login a.cancel, #TB_forgottpassword a.cancel').click(function(e){
              e.preventDefault();
              tb_remove();
            });
            $('#TB_login a.forgottpassword').click(function(e){
              e.preventDefault();
              tb_remove(function(){
                tb_show('', '#TB_inline?&height=250&width=380&inlineId=TB_forgottpassword');
              });
            });
            $('#TB_login a.idcard').click(function(e){
              $(this).closest('form').find('[name=username],[name=password]').val('');
            });
            ShowLogin();
            $.loadingIndicator.stop();
            Loaded = true;
            Loading = false;
          }, 100);
        }
      });
    } else if (Loaded) {
      ShowLogin();
    }
  })

 
});

