function logged_in(){
  return $.cookie('loggin') != null;
}

function login(xml){
  current_user = new User(xml);
  $.cookie('loggin', current_user.id);
  load_projects();
  get_menues();
  $('#logout .username').html(current_user.username);
  $('#login').hide();
  $('#logout').show();
  $('#projects').show();
  if(current_song){$('#tracks a.download_cd').show();}
  // $('.download_track').show();
}

function logout(){
  $.cookie('loggin', null);
  current_user = new User($());
  current_project = new Project($());
  
  load_project();
  load_projects();
  
  $('#logout').hide();
  $('#login').show();
  $('#projects').hide('blind', {direction:'vertical'}, 250);
  get_menues();
  
  $('#tracks a.download_cd').hide();
  // $('.download_track').hide();
}

// ==================
// = Document Ready =
// ==================
$(function(){
  $('#login_form form').validate();
  $('.error').hide();
  if(logged_in()){
    $.ajax({
      url:"/import/projects.php?action=login&UserLabel=guest&SessionId=1271449272395&PW=%2D1&len=0&UN=%2D1", 
      beforeSend:function(){loading_cursor('show')},
      success:function(response){
        if($(response).find('error:first').attr('num') == '0'){
          login($(response));
          get_menues();
        }else{
          alert($(response).find('error:first').attr('msg'));
          $.cookie('loggin', null);
        };
      },
      complete:function(){loading_cursor('hide')}
    });
  }
  
  $('.logout').live('click', function(){
    me = $(this);
    $.ajax({
      url: '/import/projects.php?action=logout',
      type: 'get',
      beforeSend: function(){loading_cursor('show');},
      success: function(response){logout()},
      error: function(response){
        alert("We're sorry but something went wrong.");
      },
      complete: function(){loading_cursor('hide')}
    });
    return false;
  });
  
  
 
//<script type="text/javascript" src="http://dev.jquery.com/view/trunk/plugins/validate/jquery.validate.js"></script>
    /*validate();
    
    function validate()
    {   
        
         formTarget = "checkIt";

         //can we submit it? this will tell us
         $("#login_form input[type=submit]").click( submitHandler );

          //add classes to form in order to value check
         $("#login_form form input").addClass(""+ formTarget +"");

          // on blur... check this value
         $("."+ formTarget +"").blur(function(){
              if( this.value == '' ){
                  $(this).removeClass('hasValue').addClass('invalid');
              }
              else{
                  $(this).addClass('hasValue');
              }
         });

         // on focus.. check this value
         $("."+ formTarget +"").focus(function(){
              // if focused, and was declared invalid, lets remove the invalid class
              if ( $(this).is(".invalid") ){
                $(this).removeClass("invalid");
              }
              $(this).removeClass('hasValue');
          });
    }*/

    

    /*function submitHandler( event )
    {
      if ( $(".invalid").length || $(""+ formTarget +"").length )
      {
        event.preventDefault();
      }
    }*/
      $('#login_form form').submit(function(){
    
            $('#login_form form').validate(); // validation plug-in
            
            if ( $('#login_form form').valid() )
            {
              var me = $(this);
              $.ajax({
                url: $(this).attr('action'),
                data: $(this).serialize(),
                beforeSend: function(){
                  me.find('.loading').fadeIn();
                  loading_cursor('show');
                },
                success: function(response){
                  me.find('.loading').fadeOut();
                  if($(response).find('error:first').attr('num') == '0'){
                    login($(response));
                    login_overlay.close();
                    me.find('input[type=password]').val('');
                  }else{
                    alert($(response).find('error:first').attr('msg'));
                  };
                },
                error: function(){alert('We are sorry, but something went wrong.')},
                complete: function(){loading_cursor('hide')}
              });
            }
            return false;
      });
});

// ==========
// = Models =
// ==========
function User(xml){
  this.id = xml.find('login:first').attr('uid');
  this.username = xml.find('login:first un').text();
  this.sales_rep = new SalesRep(xml.find('sr:first'));
  this.projects = new Array;
  this.projects_xml = $();
}

function SalesRep(xml){
  this.name = xml.find('name:first').text();
  this.phone = xml.find('phone:first').text();
  this.fax = xml.find('fax:first').text();
  this.email = xml.find('email:first').text();
}