set_hlink = function(comp, type, url) {
  hcard_url_path = url;
  var form = $("hcard_form");
  $('subscribe_checkbox').hide();
  $('hcard_loader').update('');
  $('url_label').update(url)
  if(comp == "") {
    $('hcard_submit').value = "Get profile";
    $('profile_url_label').update("Add the full url to your profile");
    
    $('hcard_form').down("div.why").update("This will populate your new profile with the info publicly available from any site that supports hCard.  <a href=\"http://microformats.org/wiki/hCard\">What's hCard?</a>")
  } else {
    $('hcard_submit').value = "Get " + comp + " profile";
    $('profile_url_label').update("Add your "+ comp + " " + type);
    
    $('hcard_form').down("div.why").update("This will populate your new profile with the info publicly available from " + comp + ".")
  }
  if(form.visible()) {
    Effect.Appear("hcard_form", {
      duration: 0.5, 
      from: 0.1, 
      afterFinish: function() {
        $('profile_url').focus();
      }
    });
  } else {
    Effect.BlindDown("hcard_form", {
      duration: 0.5, 
      afterFinish: function() {
        $('profile_url').focus();
      }
    });
  }
}

toggle_sync = function(cb_el) {
  $('sync_profile_data').value = $F(cb_el) != null ? "true" : "false";
  $('sync_url').value = $F('sync_profile_data') == "true" ? $('url_label').innerHTML + $('profile_url').value : '';
}

clean_data = function(val) {
  return val.gsub(/\t|\n/, ' ')
}

hCardImport = Class.create();

hCardImport.prototype = {
  initialize: function() {
    this.profile_url = $('url_label').innerHTML + $('profile_url').value;
    this.status_message = $('hcard_loader');
    this.get_hcard_profile();
  },
  
  get_hcard_profile: function() {
    this.update_status("One moment while we look for your profile...");
    new Ajax.Request('/hcards', {
      asynchronous:true, 
      evalScripts:true,
      parameters: {url: this.profile_url},
      onSuccess: function(response) { 
        json = eval( "(" + response.responseText +")" );          
        this.update_status("Sweet! We found it!");
        $('subscribe_url').update(this.profile_url);
        $('sync_profile_data').value = "false";
        $('sync_url').value = "";
        $('sync_cb').checked = false;
        $('subscribe_checkbox').show();
        this.populate_registration_form(json);
        new Effect.Highlight('hcard_loader', {startcolor:'#FFFF99', endcolor:'#f8f8f8'});
      }.bind(this),
      onFailure: function(request) {
        this.update_status("Snap! Something's wrong here: " + request.responseText);
        $('subscribe_checkbox').hide();
      }.bind(this)
    });
  },
  
  update_status: function(message) {
    this.status_message.update(message);
  },
  
  populate_registration_form: function(hcard) {
    // Populate existing fields
    $('user_nick').value = hcard['nickname'] ? clean_data(hcard['nickname']) : '';
    photo = null;
    
    if(hcard['logo']) {
      photo = hcard['logo']
    } else if(hcard['photo']) {
      photo = hcard['photo']
    } else {
      photo = 'http://2.static.getsatisfaction.com/images/user_gray.png';
    }
    
    $('current_user_avatar').src = photo;
    
    // Create new fields for:
    this.add_field("real_name", clean_data(hcard['fn']), "Full Name");
    this.add_field("tagline", hcard['note'], "Tagline");
    this.add_field("personal_url", hcard['url'], "Website URL");
    this.add_field("avatar_url", photo, "User Icon URL");
    loc = hcard['adr'] ? hcard['adr'].locality : "";
    this.add_field("location", loc, "Location");
  },
  
  add_field: function(field_name, value, label) {
    existing = $$('input.hidden_' + field_name).first();
    if(existing) {
      this.remove_field(existing); 
    }
    if(value && value != undefined) {
      name_attribute = "user["+field_name+"]";
      this.insert_field($('inputs'), 'hidden_' + field_name, name_attribute, value, label);
    }
  },

  remove_field: function(element) {
    element.up("ul").removeChild(element.up("li"));
  },

  insert_field: function(form, field_class, field_name, value, label) {
    new Insertion.Bottom(
      form, 
      '<li class="text_row"><span class="t_label"><label for="' + field_name + '">'+ label +':</label></span><span class="t_input"><input type="text" class="' + field_class + '" name="' + field_name + '"  value="' + value + '"></span></li>'
    );
  }
}