function wipAjaxHttpAjaxSuccess (t, statusDiv) {

}
function wipAjaxHttpAjaxError (t) {
  alert('Error ' + t.status + ' -- ' + t.statusText);
  //alert('you can\'t user this pseudo (ndr : test version).');
  //$(imgPseudo).src = '/champNonValide.gif';
}

function wipAjaxHttpAjaxError404 (t) {
  alert('Error ' + t.status + ' -- ' + t.statusText);
  //alert('you can\'t user this pseudo (ndr : test version).');
  //$(imgPseudo).src = '/champNonValide.gif';
}


var toggle_db = new Hash();

function wipAjax (queryString, htmlDiv, page, method, effect, toggle) {
  var url = page;
  var status = $(htmlDiv + 'status');
  if (status) {
    status.style.display = 'inline';
  }

  if (toggle) {
    var div_toggle = $(htmlDiv + toggle);
    if ( toggle_db.get(htmlDiv + toggle) ) {
      toggle_db.unset(htmlDiv + toggle);
      if (div_toggle) div_toggle.removeClassName('active');
      new Effect.SlideUp(htmlDiv, { duration: 0.5 } );
      return;

    } else {
      toggle_db.each( function(pair) {
        if ( $('debug') ) $('debug').insert({ bottom : (pair.key + ' = "' + pair.value + '"') });
	var _div_toggle = $(pair.key);
        if (_div_toggle) {
          _div_toggle.removeClassName('active');
          //new Effect.SlideUp(pair.value, { duration: 0.5 } );
	}
	toggle_db.unset(pair.key);
      });
      toggle_db.set(htmlDiv + toggle, htmlDiv);
      if (div_toggle) div_toggle.addClassName('active');
    }
  }


  if (method == 'post') {
    new Ajax.Updater(
                     htmlDiv, url, {
                                    onComplete:function(){
                                                          if (effect) {
							    if (effect == 'slidedown') {
							      new Effect.SlideDown(htmlDiv, { duration: 0.5 } );
							    } else {
							      htmlDiv.show();
                                                              new Effect.Highlight(htmlDiv, { duration: 0.5 } );
							    }
                                                          }
                                                         },
                                    asynchronous: true,
                                    evalScripts: true,
                                    method: 'post',
                                    parameters: '',
                                    postBody: queryString,
                                    on404: wipAjaxHttpAjaxError404,
                                    onSuccess: wipAjaxHttpAjaxSuccess,
                                    onFailure: wipAjaxHttpAjaxError
                                   }
                       );

  } else {
    new Ajax.Updater(
                     htmlDiv, url + '?' + queryString, {
                                                        onComplete:function(){
                                                                              if (effect) {
							                        if (effect == 'slidedown') {
							                          new Effect.SlideDown(htmlDiv, { duration: 0.5 } );
							                        } else {
										  htmlDiv.show();
                                                                                  new Effect.Highlight(htmlDiv, { duration: 0.5 } );
							                        }
                                                                              }
                                                                             },
                                                        asynchronous: true,
                                                        evalScripts: true,
                                                        method: 'get',
                                                        on404: wipAjaxHttpAjaxError404,
                                                        onSuccess: wipAjaxHttpAjaxSuccess,
                                                        onFailure: wipAjaxHttpAjaxError
                                                       }
                    );
  }

}


function update_user_box(div, page, query_string) {
  // alert(div);
  if (div && page) {
    wipAjax(query_string, div, page);
  } else {
    var user_box = document.getElementById("wip_person_fb");
    // add in some XFBML. note that we set useyou=false so it doesn't display "you"
    user_box.innerHTML = "<span>" +
         "<fb:profile-pic uid=loggedinuser facebook-logo=true></fb:profile-pic>" +"<fb:name uid=loggedinuser useyou=false></fb:name>. You are signed in with your Facebook account." +
         "</span>";
    // because this is XFBML, we need to tell Facebook to re-process the document
    FB.XFBML.Host.parseDomTree();
  }
}

function init_user_box() {
  // because this is XFBML, we need to tell Facebook to re-process the document
  FB.XFBML.Host.parseDomTree();
}


/**
* - @element: element to fire event on
* - eventName: name of event to fire (only MouseEvents and HTMLEvents interfaces are supported)
* - options: optional object to fine-tune event properties - pointerX, pointerY, ctrlKey, etc.
*
* $('foo').simulate('click'); // => fires "click" event on an element with id=foo
*
**/
(function(){
  
  var eventMatchers = {
    'HTMLEvents': /^(?:load|unload|abort|error|select|change|submit|reset|focus|blur|resize|scroll)$/,
    'MouseEvents': /^(?:click|mouse(?:down|up|over|move|out))$/
  }
  var defaultOptions = {
		      pointerX: 0,
		      pointerY: 0,
		      button: 0,
		      ctrlKey: false,
		      altKey: false,
		      shiftKey: false,
		      metaKey: false,
		      bubbles: true,
		      cancelable: true
  }
  
    Event.simulate = function(element, eventName) {
    var options = Object.extend(defaultOptions, arguments[2] || { });
    var oEvent, eventType = null;
    
    element = $(element);
    
  for (var name in eventMatchers) {
    if (eventMatchers[name].test(eventName)) { eventType = name; break; }
  }

    if (!eventType)
      throw new SyntaxError('Only HTMLEvents and MouseEvents interfaces are supported');

  if (document.createEvent) {
      oEvent = document.createEvent(eventType);
      if (eventType == 'HTMLEvents') {
        oEvent.initEvent(eventName, options.bubbles, options.cancelable);
      }
      else {
        oEvent.initMouseEvent(eventName, options.bubbles, options.cancelable, document.defaultView,
          options.button, options.pointerX, options.pointerY, options.pointerX, options.pointerY,
          options.ctrlKey, options.altKey, options.shiftKey, options.metaKey, options.button, element);
      }
      element.dispatchEvent(oEvent);
    }
    else {
      options.clientX = options.pointerX;
      options.clientY = options.pointerY;
      oEvent = Object.extend(document.createEventObject(), options);
      element.fireEvent('on' + eventName, oEvent);
    }
    return element;
}
  
  Element.addMethods({ simulate: Event.simulate });
})()




