var TellAFriendClass = new Class(
{
  sendLocation: document.location,

  initialize: function ()
  {
    window.addEvent('domready', this.boot.bind( this ) );
  },

  boot: function ()
  {
    //set events
    document.getElements( '.tellafriendButton' ).addEvent( 'click', this.showForm.bind( this ) );
  },

  showForm: function( event )
  {
    event = new Event( event );
    event.preventDefault();

    var target = $(event.target);
    if ( target.rel ) this.sendLocation = target.rel;

    SqueezeBox.initialize({
      size: {x: 520, y: 420},
      sizeLoading: {x: 220, y: 120}
    });
    SqueezeBox.open( "/service/html/Tellafriend/display.html" );

  },

  close: function() {
    SqueezeBox.close();
  },
  
  sendForm: function()
  {
	  $( 'url' ).value = this.sendLocation;
    var jsonRequest = new Request.JSON({url: "/service/json/Tellafriend/send.html", method: 'post', onSuccess: function( result ) {
      if( result.data.error ) {
        for ( var item in result.data.error )
        	if ( $( 'tellafriend[' + result.data.error[item] + ']' ) )
        		$( 'tellafriend[' + result.data.error[item] + ']' ).className = "tellafriend invalid";
      } else if ( result.success ) {
   	    $( "tellafriendForm" ).setStyle( "display", "none" );
        $( "tellafriendSuccess" ).setStyle( "display", "block" );
      }
    }}).post( $( 'tellafriendForm' ) );
  }
}
);

var tellafriend = new TellAFriendClass();
