
var NUpload = new Class ({
    'version': '0.0.1',
    Implements: [ Options ],

    getOptions: function() {
	return {
	};
    },

    /* ******************************************************************** */

    initialize: function(options){
	this.setOptions(this.getOptions(), options);
    },

    /* ******************************************************************** */

    isWebKit: function(){
	return RegExp(" AppleWebKit/").test(navigator.userAgent);
    },

    remove: function(theVar) {
	var theParent = theVar.parentNode;
	theParent.removeChild(theVar);
    },

    /* ******************************************************************** */
    Upload: function (form, url_action, id_element, html_show_loading, html_error_http) {

	var detectWebKit = this.isWebKit();

	form = typeof(form)=="string" ? $(form) : form;
	var erro="";

	if(form==null || typeof(form)=="undefined"){
	    erro += "The form of 1st parameter does not exists.\n";
	}
	else if(form.nodeName.toLowerCase()!="form") {
	    erro += "The form of 1st parameter its not a form.\n";
	}

	if( $(id_element) == null ) {
	    erro += "The element of 3rd parameter does not exists.\n";
	}

	if(erro.length>0){
	    alert("Error in call ajaxUpload:\n" + erro);
	    return;
	}

	var iframe = document.createElement("iframe");
	iframe.setAttribute("id","ajax-temp");
	iframe.setAttribute("name","ajax-temp");
	iframe.setAttribute("width","0");
	iframe.setAttribute("height","0");
	iframe.setAttribute("border","0");
	iframe.setAttribute("style","width: 0; height: 0; border: none;");
	form.parentNode.appendChild(iframe);
	window.frames['ajax-temp'].name="ajax-temp";

	var doUpload = function(){
		removeEvent($('ajax-temp'),"load", doUpload);
		var cross = "javascript: ";
		cross += "window.parent.$('"+id_element+"').innerHTML = document.body.innerHTML; void(0);";
		$(id_element).innerHTML = html_error_http;
		$('ajax-temp').src = cross;
		
		if ( detectWebKit ) {
        	    this.remove( $('ajax-temp') );
    		} else {
        	    setTimeout( function() { 
        		    var obj = $('ajax-temp');
			    var theParent = obj.parentNode;
			    theParent.removeChild(obj);
        		}, 250 );
    		}
	}.bind(this);

	$('ajax-temp').addEvent("load", doUpload);

	form.setAttribute("target","ajax-temp");
	form.setAttribute("action",url_action);
	form.setAttribute("method","post");
	form.setAttribute("enctype","multipart/form-data");
	form.setAttribute("encoding","multipart/form-data");

	if( html_show_loading.length > 0 ) {
    	    $(id_element).innerHTML = html_show_loading;
	}

	form.submit();
	
    },

    /* ******************************************************************** */

    foo: function() {}
});


