(function($) { 
$.ajaxFileUpload = function(options) 
{	
	options = jQuery.extend({}, jQuery.ajaxSettings, options);

	var id = new Date().getTime();   
	var form = createUploadForm(id, options.fileElementId,options.url);
	var iframe = createUploadIframe(id);
	var frameId = 'jUploadFrame' + id;
	var formId = 'jUploadForm' + id;		

	if ( options.global && ! jQuery.active++ )
	{
		jQuery.event.trigger( "ajaxStart" );
	} 
	
	var requestDone = false;
	var xml = {}   
	if ( options.global )
		jQuery.event.trigger("ajaxSend", [xml, options]);

	var uploadCallback = function(isTimeout)
		{	
			var iframe = $('#'+frameId)[0];
            try 
			{	
				if(iframe.contentWindow)
				{
					returnData = iframe.contentWindow.document.body?iframe.contentWindow.document.body.innerHTML:null;
				}
				else if(iframe.contentDocument)
				{
					returnData = iframe.contentDocument.document.body?iframe.contentDocument.document.body.innerHTML:null;
				}
            }
			catch(e)
			{	
				jQuery.handleError(options, returnData, null, e);
			}
		
            if ( xml || isTimeout == "timeout") 
			{				
                
				requestDone = true;
                var status;
                try 
				{
                  
				    status = isTimeout != "timeout" ? "success" : "error";
					
					if ( status != "error" )
					{
                      
						var data = returnData;
                         
                        if ( options.success )
                        {
							options.success( data, status );
						}
    					
                        
                        if( options.global )
						{
                            jQuery.event.trigger( "ajaxSuccess", [data, options] );
						}
                    } 
					else
					{
                        jQuery.handleError(options, data, status);
					}
                } 
				catch(e) 
				{

                    status = "error";
                    jQuery.handleError(options, data, status, e);
                }

           
                if( options.global )
					
                    jQuery.event.trigger( "ajaxComplete", [data, options] );

      
                if ( options.global && ! --jQuery.active )
                    jQuery.event.trigger( "ajaxStop" );

             
                if ( options.complete )
                    options.complete(data, status);

                jQuery(iframe).unbind()

               setTimeout(function()
									{	try 
										{
											$(iframe).remove();
											$(form).remove();	
											
										} catch(e) 
										{
											jQuery.handleError(options, data, null, e);
										}									

									}, 100)

                xml = null

            }
        }
	// Timeout checker
	if ( options.timeout > 0 ) 
	{
		setTimeout(function(){
			// Check to see if the request is still happening
			if( !requestDone ) uploadCallback("timeout");
		}, options.timeout);
	}
	
	try 
	{
		var form = $('#' + formId);
		$(form).attr('action', options.url);
		$(form).attr('method', 'POST');
		$(form).attr('target', frameId);
		if(form.encoding)
		{
			form.encoding = 'multipart/form-data';				
		}
		else
		{				
			form.enctype = 'multipart/form-data';
		}			
		$(form).submit();

	}
	catch(e) 
	{			
		jQuery.handleError(options, data, null, e);
	}
	
	if(window.attachEvent){
            document.getElementById(frameId).attachEvent('onload', uploadCallback);
        }
        else{
            document.getElementById(frameId).addEventListener('load', uploadCallback, false);
        } 	
		//$('#'+frameId).bind('load',uploadCallback);
	
	return {abort: function () {}};	
	
	
	function createUploadForm(id, fileElementId, uri)
	{
		//create form	
		var formId = 'jUploadForm' + id;
		var fileId = 'jUploadFile' + id;
		var form = $('<form  action="'+ uri+'" method="POST" name="' + formId + '" id="' + formId + '" enctype="multipart/form-data"></form>');	
		var oldElement = $('#' + fileElementId);
		
		var newElement = $(oldElement).clone();
		$(oldElement).attr('id', fileId);
		$(oldElement).before(newElement);
		$(oldElement).appendTo(form);
		//set attributes
		$(form).css('position', 'absolute');
		$(form).css('top', '-1200px');
		$(form).css('left', '-1200px');
		$(form).appendTo('body');	
		
		return form;
    }
	
	function createUploadIframe(id)
	{
		var frameId = 'jUploadFrame' + id;
		
		var iframe = document.createElement('iframe');
		iframe.id = frameId;
		iframe.name = frameId;
		
		iframe.style.position = 'absolute';
		iframe.style.top = '-1000px';
		iframe.style.left = '-1000px';

	
		document.body.appendChild(iframe);

		return iframe	
	}
	
	
/*	function uploadCallbacks(isTimeout)
	{
		var iframe = $('#'+frameId);
		
		try 
		{
			if(iframe.contentWindow)
			{				
				 xml.responseText = iframe.contentWindow.document.body?iframe.contentWindow.document.body.innerHTML:null;
				 xml.responseXML = iframe.contentWindow.document.XMLDocument?iframe.contentWindow.document.XMLDocument:iframe.contentWindow.document;
				 
			}else if(iframe.contentDocument)
			{
				 xml.responseText = iframe.contentDocument.document.body?iframe.contentDocument.document.body.innerHTML:null;
				 xml.responseXML = iframe.contentDocument.document.XMLDocument?iframe.contentDocument.document.XMLDocument:iframe.contentDocument.document;
			}		
		
		}
		catch(e)
		{					
			jQuery.handleError(options, xml, null, e);
		}
	
		if ( xml || isTimeout == "timeout") 
		{				
			requestDone = true;
			var status;
			try {
				
				status = isTimeout != "timeout" ? "success" : "error";
				// Make sure that the request was successful or notmodified
				if ( status != "error" )
				{
					// process the data (runs the xml through httpData regardless of callback)
					
					eval( "unjson = " + xml );
					var data = unjson
					// If a local callback was specified, fire it and pass it the data
					if ( options.success )
						options.success( data, status );

					// Fire the global callback
					if( options.global )
						jQuery.event.trigger( "ajaxSuccess", [xml, options] );
				} else
					jQuery.handleError(options, xml, status);
			} 
			catch(e) 
			{
				status = "error";
				jQuery.handleError(options, xml, status, e);
			}

			// The request was completed
			if( options.global )
				jQuery.event.trigger( "ajaxComplete", [xml, options] );

			// Handle the global AJAX counter
			if ( options.global && ! --jQuery.active )
				jQuery.event.trigger( "ajaxStop" );

			// Process result
			if ( options.complete )
				options.complete(xml, status);

			$(iframe).unbind()

			setTimeout(function()
								{	try 
									{
										$(iframe).remove();
										$(form).remove();	
										
									} catch(e) 
									{
										jQuery.handleError(options, xml, null, e);
									}									

								}, 100)

			xml = null

		}
	}*/
}
})($);