/**
 * Created by Pau Sanchez Campello for CodigoManso.com
 *
 * Use it for anything you want to, even commercial applications.
 * This software comes with NO WARRANTY
 */

/**
 * $('whatever').jSimulateClick (xPos, yPos, 'round');
 *
 * The third parameter could be ommited
 */
jQuery.fn.jSimulateClick = function (fromX, fromY)
{
  var round = (arguments[2] == 'round');
  var w = $(this).width(),
      h = $(this).height(),
      pos = $(this).offset();

  var extra = '';
  if (round) {
    extra = 'border-radius: 50%; -moz-border-radius: 50%; -webkit-border-radius: 150px; -ms-border-radius: 50%;';  

    // make it round
    w = (w+h)/2;
    h = w;

    pos.top  = fromY - h/2;
    pos.left = fromX - w/2;
  }
  
  $('<div style="position: absolute; width: 1px; height: 1px; border: 3px solid black;' + extra + '"></div>')
    .appendTo('body')
    .css ({ 'top' : fromY-1, 'left' : fromX-1}).show()
    .animate ({ 
      'width' : w,
      'height' : h,
      'top' : pos.top,
      'left' : pos.left,
      'opacity' : 0,
      'border-width' : 1
    },
    250,
    'swing',
    function() { $(this).remove(); }
  );
}
