/* 
 * Custom thickbox class listener that checks to see if the user is logged in before 
 * call thickbox. If the user is already logged in then let the user continue to the URL
 */
$(document).ready(function() {
	
 $('.authenticate').bind('click', function(e){
    if(IsLoggedIn()){
      return true;
    }else{
      //Ensure to close any existing thickboxes
      tb_remove();
      
      // Grab the url and parse the get variables
      urlParams = parseParams(this.href); 
      
      // Reconstruct the URL to accommidate for the redirection
      var url = '&returnEvent=' + urlParams['e'] + '&width=' + 300 + '&height=' + 240 + '&modal=true';
      var paramList = 'returnEvent';
      
      // Get additional params
      for (param in urlParams){
        if(param != 'e' && param != 'width' && param != 'height' && param != 'modal'){
          url = url + '&' + param + '=' + urlParams[param];
          paramList = paramList + ',' + param;
        } 
      }
      url = url + '&paramList=' + paramList;

      // Call the thickbox and pass in the appropriate arguments
      tb_show('Login User', '/?e=isa.account.login' + url, 'login');
      
      return false;
      
    }
  });
});

/*
 * Loading message that will display whenever an AJAX action is fired within the login screen.
 * @msg: Message that explains the action while loading
 */
getLoadingMessage = function(msg){
  return '<a href="##" onclick="tb_remove()" id="close">Close [X]</a><br /><p style="text-align:center; font-weight: bold">' + msg + '...<br /><img src="/images/loadingAnimation.gif" /></p>';
}

/*
 * Returns true if user is logged in, false otherwise based on the user cookie
 */
IsLoggedIn = function(){
  
  var loggedIn = readCookie('loggedin');
  if(loggedIn == null){
    return false;
  }else if(loggedIn == 1){
    return true;
  }
}

/*
 * Performs an AJAX action based on the login form, reset thickbox after ajax call
 * @containerForm: The form container that houses the login
 * @message: message to be displayed when AJAX calls are made
 * @loginCheck: should we check to see if the user is logged in before redirecting them
 */
accountAction = function(containerForm, message, loginCheck){
	var loginForm = $('#' + containerForm);
  var paramList = $('#' + containerForm + ' #paramList');
  var returnEvent = $('#' + containerForm + ' #param_returnEvent');
  
  url = '';
  urlParams = paramList.attr('value').split(',');
  for (param in urlParams){
    if(urlParams[param] != 'returnEvent'){
      currParam = $('#' + containerForm + ' #param_' + urlParams[param]);
      url = url + '&' + urlParams[param] + '=' + currParam.attr('value');
    } 
  }
  
  $('#login_box').html(getLoadingMessage(message));
  
  $.ajax({
   type: loginForm.attr('method'),
   url: loginForm.attr('action'),
   data: loginForm.serialize(),
   success: function(msg){
    if(loginCheck == 1 && IsLoggedIn()){
      document.location = '/?e=' + returnEvent.attr('value') + url;
    }else{
     $('#login_box').html(msg);
     // Catch thickbox calls within the login box
     tb_init('a.thickbox');
    }
   }
 });
}