// ==UserScript==
// @name          LJ-Celsius Integration
// @namespace     http://hacks.atrus.org/greasemonkey/
// @description   Adds fields to describe your relationship with users as you add them
// @include       http://www.livejournal.com/friends/add.bml?user=*
// ==/UserScript==

// LJ-Celsius Integration
// 
// Copyright (C) 2005 Nikolas Coukouma
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

function onAddFriendClick() {
  // extract the current username from the cookie
  var varName = "ljsession="
  var cookie = document.cookie;
  var start = cookie.indexOf( varName ) + varName.length;
  start = cookie.indexOf( ":", start )+1;
  var end = cookie.indexOf( ":", start );
  var fromUser = cookie.substring( start, end );
  //extract the user being added from the URL
  var loc = window.location.toString();
  var toUser = loc.substr( loc.indexOf( "=" )+1 );
  
  // submit info to celsius
  var celsiusURL = "http://splorg.org:8080/people/tobin/projects/livejournal/celsius.cgi?A=" +
                   fromUser + "&B=" + toUser;
  var celsiusStr = "action=update" +
                   "&A=" + escape( fromUser ) + 
                   "&B=" + escpae( toUser ) + 
                   "&via=" + escape( document.getElementById( "introBy" ).value ) +
                   "&story=" + escape( document.getElementById( "howMet" ).value );
  // it barfs without this header ...
  var headers = [new Object()];
  headers[0]["Content-Type"] = "application/x-www-form-urlencoded";
  GM_xmlhttpRequest({
    method: "POST",
    url: celsiusURL,
    onload: function(t){addForm.submit()},
    onerror: function(){alert( "FYI, an error ocurred while saving to Celsius .." ); addForm.submit()},
    headers: headers,
    data: celsiusStr });
  alert( "This will take a second or two .." );
  return false;
}

(function(){
    
  // insert celsius form fields
  var hidField = document.getElementsByName( "user" )[0];
  addForm = hidField.parentNode;
  
  var introByTxt = document.createTextNode( "Introduced via (user/community): " );
  var howMetTxt = document.createTextNode( "You can write a little story about how you met this person here: " );
  var introBy = document.createElement( "input" );
  introBy.type = "text";
  introBy.id = "introBy";
  // CSS from the XColibur theme
  introBy.style.setProperty( "background-image", "url(http://stat.livejournal.com/img/userinfo.gif)", "" )
  introBy.style.setProperty( "background-repeat", "no-repeat", "" )
  introBy.style.setProperty( "background-position", "0px 1px", "" );
  introBy.style.setProperty( "padding-left", "18px", "" );
  introBy.style.setProperty( "color", "#00C", "" );
  introBy.style.setProperty( "font-weight", "bold", "" );
  var howMet = document.createElement( "textarea" );
  howMet.id = "howMet";
  howMet.rows = 5;
  howMet.cols = 80;
  
  addForm.insertBefore( introByTxt, hidField );
  addForm.insertBefore( introBy, hidField );
  addForm.insertBefore( document.createElement( "br" ), hidField );
  addForm.insertBefore( howMetTxt, hidField );
  addForm.insertBefore( document.createElement( "br" ), hidField );
  addForm.insertBefore( howMet, hidField );
  addForm.insertBefore( document.createElement( "br" ), hidField );
  
  // register event handler on button
  document.getElementsByTagName( "center" )[0].firstChild.onclick = onAddFriendClick;
  
})();