calling one method in 2 ways

calling one method in 2 ways

Hey,

I've been working on a plug-in which detects weather an element is within the users current view-pane.

The problem is that I want to use it like this:
  1. $('#randomSelector').isOnScreen();
and like this 
  1. $.isOnScreen({left:200, top:200, width:200, height:200});
but I can't figure out how to write the plugin so the method is defined only one, i've got around it so far by defining it twice.

code:
  1. (function($) {
  2. jQuery.extend({
  3. isOnScreen: function(box, container) {
  4. //ensure numbers come in as intgers (not strings) and remove 'px' is it's there
  5. for(var i in box){box[i] = parseFloat(box[i])};
  6. for(var i in container){container[i] = parseFloat(container[i])};
  7. if(!container){
  8. container = {
  9. left: $(window).scrollLeft(), 
  10. top: $(window).scrollTop(), 
  11. width: $(window).width(), 
  12. height: $(window).height()
  13. }
  14. }
  15. if( box.left+box.width-container.left > 0 && 
  16. box.left < container.width+container.left && 
  17. box.top+box.height-container.top > 0 &&
  18. box.top < container.height+container.top
  19. ) return true;
  20. return false;
  21. }
  22. })


  23. jQuery.fn.isOnScreen = function (container) {
  24. for(var i in container){container[i] = parseFloat(container[i])};
  25. if(!container){
  26. container = {
  27. left: $(window).scrollLeft(), 
  28. top: $(window).scrollTop(), 
  29. width: $(window).width(), 
  30. height: $(window).height()
  31. }
  32. }
  33. if( $(this).offset().left+$(this).width()-container.left > 0 && 
  34. $(this).offset().left < container.width+container.left && 
  35. $(this).offset().top+$(this).height()-container.top > 0 &&
  36. $(this).offset().top < container.height+container.top
  37. ) return true;
  38. return false;
  39. }
  40. })(jQuery);
  41.