/*
/****************************************************************
/*          EVCUpload V1.0 By Esteban Vaquero Cruz
/*     [ esteban.xfce@gmail.com | www.drawcoders.com ]
/*      - Agradecimientos a WHK por la idea -
/*
/****************************************************************
*/

( function($) {
$.fn.EVCUpload = function( opciones ){
  var imagen, input, subiendo = false;
  var opciones = $.extend(true,{
      nombre: 'InputSA',
      formulario : 'subirF',
      action : '#',
      antesde : function(){},
      alterminar : function( datos ){},
      remover : false
  },opciones);
  imagen = $(this);
  imagen.mouseenter(function(){
  input  = '<form method="POST" '
           +'enctype="multipart/form-data" '
           +'action="'+opciones.action+'" '
           +'id="'+opciones.formulario+'" '
           +'name="'+opciones.formulario+'">'
           +'<input type="file" '
           +'name="'+opciones.nombre
           +'" title="'+imagen.attr('title')+'" /></form>';
    var $iframe = $('<iframe id="Xs42a_TMP" '
                   +'scrolling="no" '
                   +'style="display:none"/>');
    if($('#Xs42a_TMP').length == 0)
    $iframe.appendTo("body");
    var datos = $iframe[0].contentWindow.document;
    datos.open();
    datos.write(input);
    datos.close();
    $iframe.css({
      display: 'block',
      position : "absolute",
      opacity: 0,
      top:  imagen.offset().top +'px',
      left: imagen.offset().left +'px',
      height:imagen.height(),
      width: imagen.width(),
      border: 'transparent 0px solid',
      overflow: 'hidden'
    });
    $iframe.contents().mousemove(function(e){
      $(this).find("input").css({
        position : "absolute",
        top:  (e.pageY - 5) +'px',
        left: (e.pageX - 175 ) +'px'
      });
    });
    $iframe.contents().find("input").change(function(){
      subiendo = true;
      opciones.antesde.call(this);
      $iframe.contents().find("form").submit();
      $iframe.load(function(){
        opciones.alterminar.call(this,$iframe.contents().text());
        subiendo = false;
        $iframe.remove();
      });
    });
    $iframe.mouseleave(function(){
      if(!subiendo &&  opciones.remover )
      $iframe.remove();
    });
  });

};
})(jQuery);