(function($)
{
    $.fn.uploadFiles = function(settings)
    {
			settings = jQuery.extend({
			templateName: "templateName",
			tableName: "tableName",
			fieldName: "fieldName",
			inputId: "inputId",
			imageId: "imageId",
			inputIdReference: "title",
			fillInput: false,
			fillImage: false,
			controllerajax: "packages/backend/view/default/upload.php",
			hosting: "http://localhost",
		}, settings);

		html="<div id='popupUpload' style='position:absolute; top: 0%;left: 0%;width: 100%;height: 100%; z-index:1001; overflow:auto; background:#000; -moz-opacity: 0.8;opacity:.80;filter: alpha(opacity=80);  '></div>";
		html+="<div id='popupUploadInBox'  style='position:absolute; top: 30%;left: 35%;width: 320px;height: 200px; z-index:1002; overflow:auto; background:#transparent;'>";
		html+="<table border='0' width='320px' height='120px' cellspacing='0' cellpadding='0'>"
		html+="<tr style='background:#333; color:#FFF; border:1px solid #333; width:200px; height:20px;'><td width='250' align='left'>&nbsp;UPLOAD</td><td align='right' ><img height='12' id='popupClose' src='"+settings.hosting+"js/jquery/upload/close.png' alt='[X] '></td></tr>"
		html+="<tr><td colspan='2' align='center' valign='middle' style='background:#FFF; border:1px solid #333; width:200px; height:100px;'>";
		html+="<form name='frmUpload' id='frmUpload' action='' enctype='multipart/form-data' method='POST' >";
		html+="<span id='uploadMessage' style='display:none;'></span>";
		html+="<span class='uploadInputField' id='uploadInputField'>";
			html+="<label>File:</label>";
			html+="<input type='file' name='frmFile' id='frmFile' value='' /><br><br>";
			html+="<img id='loading' src='"+settings.hosting+"js/jquery/upload/loading.gif' style='display:none;' alt='Loading...'><br>";
			html+="<input type='submit' name='submitUpload' id='submitUpload' value='UPLOAD NOW!' />";
		html+="</span>";
		html+="</form>";
		
		html+="</td></tr></table>";
		html+="</div>";
			
		$('body').prepend(html).focus();
		$("#popupUpload").fadeIn('slow'); 
		$("#popupUploadInBox").fadeIn('slow');

	$("#popupClose").click(function () {
      $("#popupUpload").fadeOut('slow'); 
      $("#popupUploadInBox").fadeOut('slow');
   });
   
	$('#frmUpload').submit(function() {
		var url=settings.controllerajax + settings.templateName + "/" + settings.tableName + "/" + settings.fieldName +"/";	
		$("#loading")
		.ajaxStart(function(){
			$(this).show();
		})
		.ajaxComplete(function(){
			$(this).hide();
		});
		
		$.ajaxFileUpload
		(
			{
				url:url,
				secureuri:false,
				fileElementId:'frmFile',
				dataType: 'json',
				success: function (data, status)
				{
					if(typeof(data.error) != 'undefined')
					{
						$('#uploadMessage').show();
						if(data.error != '')
						{
							$('#uploadMessage').html(data.error);
						} else {
							//$('#upload').removeClass("upload");
							$('#uploadMessage').html("OK, file is uploaded!!");
							if(settings.fillInput)
								{
									$("#"+settings.inputId).attr("value",data.msg);
								}
							if(settings.fillImage)
								{
									$("#"+settings.imageId).attr("src",settings.hosting+data.msg);
								}
							$("#popupUpload").fadeOut('slow'); 
							$("#popupUploadInBox").fadeOut('slow');
						}
					}
				},
				error: function (data, status, e)
				{
					alert(e);
				}
			}
		);
		return false;
		});
		
	}
})(jQuery);