Event.onReady(function() {
  login_links = $$("a.user-link");
  login_links.each(function(link){
    link.onclick = get_form;
  });
  analytics();
  //mapload();
  if(/profile/.test(document.location)){
      profileDragDrop();
  }
});

function profileDragDrop(){

    dds = $$("dl#dragleft dd a");
    dds.each(function(value){
            value.onclick = function(){
                     new Ajax.Request('/user/baldur/remove_bucket',
                         {post: 'post',
                          postBody: 'bucket_id='+value.parentNode.id
                                   //function(){console.log(value.parentNode.id);return false;
                         });
                     return false;
                                   }
    });

    urlbar = document.location.toString().split("/");
    user_name = urlbar[urlbar.length-2];

    Sortable.create('dragright',{tag: 'dd',dropOnEmpty:false,  constraint:false});
  Droppables.add('dragleft',{
    onDrop:function(element){
              //extract user with regex
              new Ajax.Request('/user/'+user_name+'/add_bucket',{
                      method: 'post',
                      postBody: 'user='+user_name+'&bucket_id='+element.id
              });
    }
  });
}


function analytics(){
  if(location.hostname == "grubsnitch.com" || location.hostname == "www.grubsnitch.com"){
  _uacct = "UA-565491-1";
  urchinTracker();
  }
}

function get_form(){
  //alert(this.href);
  var link = this.href.split("/");
  var form_type = link[link.length-1]
  options = { method: 'post',
    parameters: 'ajax=1',
    onSuccess: function(t) {
        slip_in_login_form(t.responseText,form_type);
    },
    // Handle 404
    on404: function(t) {
        alert('Error 404: location "' + t.statusText + '" was not found.');
    },
    // Handle other errors
    onFailure: function(t) {
        alert('Error ' + t.status + ' -- ' + t.statusText);
    }
  }
  new Ajax.Request("/form_bits/"+form_type, options)
  return false;
}


function slip_in_login_form(form, type){
  $('slipin-form').innerHTML = form;
  new Effect.toggle('slipin-form', 'slide', {duration: 0.2});
  if($(type+'-button').innerHTML == type){
     Element.update (type+'-button','close');
  } else {
     Element.update(type+'-button', type);
  }
  return false;
}

