jQuery(document).ready(function() {
	jQuery('a[rel*=facebox]').facebox() 

	//$("a[href^='http']").attr('target','_blank');
	$('a[rel*=external]').attr('target', '_blank');

  $('a[rel*=inlinehelp]').mouseover(function(event){
    $('#' + this.href.split('#')[1]).show();
  });

  $('a[rel*=inlinehelp]').mouseout(function(event){
    $('#' + this.href.split('#')[1]).hide();
  });

  $('a[rel*=inlinehelp]').click(function(event){
    return false;
  });
  
  $('input[type=text], input[type=password], textarea').focus(function(){
    $(this).addClass('focused');
  });

  $('input[type=text], input[type=password], textarea').blur(function(){
    $(this).removeClass('focused');
  });
  
  // signup page
  var coupon_timeout;
  $('#signup input.couponcode').bind('textchange', function () {
    clearTimeout(coupon_timeout);
    $('#couponLookup').html('');
      var self = this;
      coupon_timeout = setTimeout(function () {
        var value = $(self).val();
        if(value != '') {
          $.ajax({
            url: '/signup/lookup_coupon',
            data: {'code' : value},
            dataType: 'html',
            success:function(data) {
              $('#couponLookup').html(data);
            },
            beforeSend:function() {
              $('#couponLookup').html("<span class='checking'>checking code...</span>")
            }
          });
        } else {
          $('#couponLookup').html('');
        }
    }, 1000);
  });
  
  // signup page
  var domain_timeout;
  $('#signup input.subdomain').bind('textchange', function () {
    clearTimeout(domain_timeout);
    $('#domainLookup').html('');
    $(this).removeClass('taken');
      var self = this;
      domain_timeout = setTimeout(function () {
        var value = $(self).val();
        if(value != '') {
          $.ajax({
            url: '/signup/lookup_domain',
            data: {'domain' : value},
            dataType: 'html',
            success:function(data) {
              $('#domainLookup').html(data);
              if ($('#domainLookup span').hasClass('taken')) {
                $(self).addClass('taken');
              }
            },
            beforeSend:function() {
              $('#domainLookup').html("<span class='checking'>checking your domain's availability... please wait...</span>")
            }
          });
        } else {
          $('#couponLookup').html('');
        }
    }, 1000);
  });
  
  
  $('#signup p.promo-question a').click(function() {
    $(this).parent().hide();
    $('#signup div.promo-form').removeClass('hidden');
    $('#signup div.promo-form input').focus();
    return false;
  });
  
  if($('#signup form')) {
    $('#signup form input#account_name').focus();
  };
  

});

//Send a request header of text/javascript with all AJAX requests
jQuery.ajaxSetup({ 
  'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")} 
});

// Include the authenticity token with all JS requests.

$(document).ajaxSend(function(event, request, settings) {
  if (settings.type == 'GET' || settings.type == 'get' || typeof(AUTHENTICITY_TOKEN) == "undefined") return;
  // settings.data is a serialized string like "foo=bar&baz=boink" (or null)
  settings.data = settings.data || "";
  settings.data += (settings.data ? "&" : "") + "authenticity_token=" + encodeURIComponent(AUTHENTICITY_TOKEN);
});



$(document).bind('reveal.facebox', function() { 

	$('#facebox form').unbind('submit');
	$('#facebox form').submit(function() { 
	  
	  $(this).find('input[type=submit]').attr('value', 'Please wait...');
	  $(this).find('input[type=submit]').attr('disabled', 'disabled');
	  
		data_to_send = $.param($(this).serializeArray());
    request = { data: data_to_send, dataType:'script', type:this.method, url: this.action };
    $.ajax(request);
		return false;
	});

});