/* login.js */
function doSignIn (redirect,submitForm) {
  var loadingMsg = SiteVars.modules.phrasePack.loading;
  
  
  
  
  var content = jQuery("<div class='lightbox'><a href='#' class='close'>close</a><p class='loading'>" + loadingMsg + "</p></div>");
  
  if ($current_loader != null) {
    $current_loader.abort(); 
    $current_loader = null;
  }
  
  
  
  var params = [base_booking_engine_qs, '&submitForm=' + submitForm + '&post_signin_redirect=', encodeURIComponent(redirect)].join('');
 
  
  
  content.lightbox_me({
    centered: false,
    destroyOnClose: true,
    onLoad: function() { 
      $current_loader = cajax.get(SiteVars.path_to_top + '/ajax/signin', params, function(data) {
        hideLoading();
        content.append(data);
      });
    },
    onClose: function() {
      if ($current_loader != null) {
        $current_loader.abort(); 
        $current_loader = null;
      }
    }
  });
} //}}}

//{{{ Account creation lightbox
function doRegister () {
  
  
  
  var loadingMsg = SiteVars.modules.phrasePack.loading;
  var content = jQuery("<div class='lightbox'><a href='#' class='close'>close</a><p class='loading'>" + loadingMsg + "</p></div>");
  
  if ($current_loader != null) {
    $current_loader.abort();
    $current_loader = null;
  }
  
  content.lightbox_me({
    centered: false,
    closeClick: false,
    destroyOnClose: true,
    onLoad: function() { 
      $current_loader = cajax.get(SiteVars.path_to_top + '/ajax/create-account', base_booking_engine_qs, function(data) {
        hideLoading();
        content.append(data);
        
        doRegisterValidation();
        
        
      });
    },
    onClose:function() {
      if ($current_loader != null) {
        $current_loader.abort(); 
        $current_loader = null;
      }
    }
  });
} //}}}

function switchCountry() {
    var country = $j('#country option:selected').eq(0).val();
    if (country == undefined) country = $j('#chosen_country option:selected').val();
   
    
    if( country == '10' ) { // canada
     $j('#fieldlist_chosen_province').show();
     $j('#sel-prov-item').show();
     $j('#sel-state-item').hide();
     $j('#sel-au-item').hide();
     $j('#sel-uk-item').hide();
    } else if ( country == '60'  ) { // USA
     $j('#fieldlist_chosen_province').show();
     $j('#sel-prov-item').hide();
     $j('#sel-state-item').show();
     $j('#sel-au-item').hide();
     $j('#sel-uk-item').hide();
     
    } else if (country == '2') { // australia
      
     $j('#fieldlist_chosen_province').show();
     $j('#sel-prov-item').hide();
     $j('#sel-state-item').hide();
     $j('#sel-au-item').show();
     $j('#sel-uk-item').hide();
      
    } else if (country == '59') {
     $j('#fieldlist_chosen_province').show();
     $j('#sel-prov-item').hide();
     $j('#sel-state-item').hide();
     $j('#sel-au-item').hide();
     $j('#sel-uk-item').show(); 
    } else {
     $j('#fieldlist_chosen_province').hide();
     $j('#sel-prov-item').hide();
     $j('#sel-state-item').hide();
     $j('#sel-au-item').hide();
     $j('#sel-uk-item').hide();      
    }
}

function initCountry() {

   $j('#sel-state-item').hide();
   $j('#sel-prov-item').hide();
   $j('#sel-au-item').hide();
   $j('#sel-uk-item').hide();
   $j('#fieldlist_chosen_province').hide();
   
   switchCountry();

   $j('#country').change(function() {
     switchCountry();
   });
   
   $j('#chosen_country').change(function() {
     switchCountry();
   });
   
   $j('.prov-item').change(function() {
       var prov = $j(this).find('option:selected').val();
       $j('#province').val(prov);
   });
 
}

function doRegisterValidation() {
  
  
  $j('.create-account-form select').selectmenu({transferClasses: true, width: 200});
  $j('#sel-prov-item').show();
  $j('#sel-state-item').hide();  
        
  initCountry();
}


jQuery(document).ready(function() {
  //{{{ Sign-in link
  jQuery(document).delegate('#signin-link', 'click', function() {
      
    // if we are in HTTP mode, move over to HTTPS
    var url = $j.url(); 
    if (url.attr('protocol') == 'http') {
      var url = 'https://' + url.attr('host') + url.attr('path') +  url.attr('query') + '#/signin-login';
      document.location.replace(url);
      return false;
    }
      
    doSignIn(window.location.href,false);
    return false;
  }); 
  
  
  
  //}}}
  
  //{{{ Sign-up link
  jQuery(document).delegate('#create-acct-link', 'click', function() {
    var url = $j.url(); 
    if (url.attr('protocol') == 'http') {
      var url = 'https://' + url.attr('host') + url.attr('path') +  url.attr('query') + '#/signin-register';
      document.location.replace(url);
      return false;
    }     
      
      
    doRegister();
    return false;
  });    
});



