var prompt_for_email_and_send_vcard = function () {
  var email_address_value,
      my_url,
      business_card_id_value; 
  
  business_card_id_value = jQuery('#business_card_id').val()
  email_address_value = prompt("Please enter your email address");
  my_url = "/business_card_emailer/create";
  
  if (email_address_value != undefined) {
    jQuery.post(my_url, { email_address: email_address_value, business_card_id: business_card_id_value });
  }
};

var vanity_url_check = function (vanity_url, original_vanity_url) {
  var my_url,
      vanity_url_okay;
  
  my_url = "/business_cards/vanity_url_check?vanity_url=" + vanity_url;
  jQuery.getJSON(my_url, function(data) {
    vanity_url_okay = !(data.vanity_url_taken == "true")
    
    if (vanity_url_okay == false && original_vanity_url != data.vanity_url) {
      jQuery('#vanity_url_check').html("Not Okay");
      jQuery('#submit_button').attr('disabled', 'disabled');
    } else {
      jQuery('#vanity_url_check').html("Okay");
      jQuery('#submit_button').removeAttr('disabled');
    }  
  });
};

var vanity_url_explanation = "This is what appears in your HCOMNBC URL, e.g. http://www.hcomnbc.com/johnsmith"
var twitter_message  = "Enter in your full Twitter URL! e.g. http://twitter.com/kellysutton";
var facebook_message = "Enter your Facebook URL. They look like this: fb://profile/10805374. <a href='http://www.facebook.com/topic.php?uid=27817827944&topic=13276'>Help!</a> ";
var phone_message    = "Enter in your 11-digit phone number, e.g. 1-555-555-5555";
var nonblank_space   = "&nbsp;";

jQuery(document).ready(function () {
  var original_vanity_url = jQuery('#id_vanity_url').val();
  var is_iphone = (navigator.userAgent.toLowerCase().indexOf('iphone') > -1);
  
  jQuery('#id_vanity_url').bind('change', function() {
    var attempted_vanity_url;

    attempted_vanity_url = jQuery('#id_vanity_url').val();
    vanity_url_check(attempted_vanity_url, original_vanity_url);
  });
  
  jQuery('#id_vanity_url').bind('focus', function () {
    jQuery('#vanity_url_check').html(vanity_url_explanation);
  });
  
  jQuery('#id_vanity_url').bind('blur', function () {
    jQuery('#vanity_url_check').html(nonblank_space);
  });
  
  jQuery('#id_twitter').bind('blur', function () {
    jQuery('#twitter_explanation').html(nonblank_space);
  });
  
  jQuery('#id_twitter').bind('focus', function () {
    jQuery('#twitter_explanation').html(twitter_message);
  });

  jQuery('#id_facebook').bind('blur', function () {
    jQuery('#facebook_explanation').html(nonblank_space);
  });
  
  jQuery('#id_facebook').bind('focus', function () {
    jQuery('#facebook_explanation').html(facebook_message);
  });

  jQuery('#id_phone_number').bind('blur', function () {
    jQuery('#phone_number_explanation').html(nonblank_space);
  });
  
  jQuery('#id_phone_number').bind('focus', function () {
    jQuery('#phone_number_explanation').html(phone_message);
  });  
  
  if (is_iphone == true) {
    jQuery('#email_vcard_anchor').bind('click', prompt_for_email_and_send_vcard);
  } else {
    jQuery('#email_vcard_anchor').attr('href', "http://www.hcomnbc.com/" + jQuery('#vanity_url').val() + ".vcf");
  }
  
  jQuery('.info').first().addClass('first_info');
});

