(function($){
	$.uploader = function($el, $input, options){
			
			$input.change(function(e){			
				id = new Date().getTime();					
				$(this).hide();		
				$el.find('.error_fileupload_block').remove();
				newfileupload = $(this).clone(true).appendTo($(this).parent());				
				newfileupload.val('');							
				newFrame = createUploadFrame(id, $el);			
				newForm = createUploadForm(id, this);

				newForm.attr('action', options.send_url);
				
				newForm.attr('target', $(newFrame).attr('name'));
				newForm.submit();				

				files_count = $el.find('.fileblock:visible').length;
				if(files_count < options.limit || options.limit == 0) {
					newfileupload.show();				
				} 
							
			});		

			function createUploadFrame(id, $el){
				var $bar = $('<div id="progress'+ id +'" class="fileblock">Loading...</div>');	
				$bar.prependTo($el); 
								
				var frame = $('<iframe name="uploadFrame' + id + '" id="uploadFrame' + id + '" rel="' + id + '" />');

				frame.load(function(){
					frame = document.getElementById($(this).attr('id'));
					if(frame.contentWindow)
						frame_document = frame.contentWindow;
					else if(frame.contentDocument)
						frame_document = frame.contentDocument;

					var content = frame_document.document.body.innerHTML;
					
					if(content) {
						eval( "data = " + content + ';');	
					
						if (data.result == 100) {
							if (data.type == 'img') {
								$bar.html('<a href="' + data.href + '" target="_blank" class="upload_link"><img src="' + data.filethumb + '"  class="upload_img" /></a><input type="hidden" name="' + options.name + '" value="' + data.filename + '" /><img  class="upload_delete" rel="' + data.filename + '" src="' + options.delete_img + '" />');
							} else {
								$bar.html('<a href="' +
								data.href +
								'" target="_blank"  class="upload_link"><img src="' +
								options.file_img +
								'" /></a>' +
								'<img class="upload_delete" src="' +
								options.delete_img +
								'" rel="' +
								data.filename +
								'" /><br/>' +
								'<a href="' +
								data.href +
								'" target="_blank"  class="upload_link filetxt">' +
								data.filename.substring(0, 5) +
								'...</a>' +
								'<input type="hidden" name="' +
								options.name +
								'" value="' +
								encodeURIComponent(data.filename) +
								'" />');
							}
							$bar.find('a.upload_link').lightBox({
								type: data.upload_link_type
							});
							
							$el.find('.emptyUploadField').remove();
							
						} else {
							$bar.html(data.message);
							$bar.addClass('error_fileupload_block');
							$el.find('input').show();
						}
					}
				});								
				$(frame).css({position: 'absolute', top: -4200, left: -4200}).appendTo('body');				
				
								
				return frame;

			}		

			 function createUploadForm(id, fileElement){
				var $form = $('<form action="" method="POST" enctype="multipart/form-data"><input type="hidden" name="APC_UPLOAD_PROGRESS" value="' + id + '" /></form>');	
				$(fileElement).appendTo($form);
				$form.css({position: 'absolute', top: -4200, left: -4200}).appendTo('body');				
				return $form;
			 }

			 
			 $el.click(function(e) {
			 	var $t = $(e.target);
				if($t.is('img.upload_delete')) {
					var $el = $(this);
					$.get(options.delete_url, { filename: $t.attr('rel')},
					  function(data){
						if (!data.result) 
							alert('File not deleted');
						else {
							$t.parent().find('a.upload_link').unbind('click');
							$t.parent().parent().find('input').show();
							$t.parent().remove();
							if($el.find('.fileblock').length==0) {
								var $bar = $('<div class="emptyUploadField"><input type="hidden" name="' + options.name +'" value="" /></div>');	
								$bar.appendTo($el);															
							}
							
						}
					}, 'json'
					);									
				}
			 });
			 
			 function UploadlinkHandlerImage() {
				$(this).lightbox();					
			 }
			 
			 function UploadlinkHandlerFile() {
				$(this).lightbox({type: 'html'});
			 }			 
			 
			 
	};
	
	 $.fn.uploader = function(options){				

		var defaults = {
			send_url: '/files/upload/',
			delete_img: BASE_URL + 'files/images/del_ic.gif',
			delete_url: '/files/delete/',
			limit: 0,
			file_img: BASE_URL + 'files/images/file_pop.gif',
			files_to_show: 4,
			filelist: [],
			oneFile: false
		};
		var opts = jQuery.extend(defaults, options);
		var filelist = opts.filelist;
		$(this).each(function(){ 

			var file = '';					
			for(i=0; i < filelist.length; i++) {			
				var id = new Date().getTime();
				var $bar = $('<div id="progress'+ id +'" class="fileblock">Loading...</div>');	
				$bar.prependTo(this);
								
					if(filelist[i].type == 'img') {
						$bar.html('<a href="' + filelist[i].href + '" target="_blank" class="upload_link"><img src="' + filelist[i].filethumb + '"  class="upload_img" /></a><input type="hidden" name="' + options.name + '" value="' + filelist[i].file + '" /><img  class="upload_delete" rel="' + filelist[i].file + '" src="' + options.delete_img + '" />');
					} else {
						$bar.html('<a href="' +
						filelist[i].href +
						'" target="_blank"  class="upload_link"><img src="' +
						opts.file_img +
						'" /></a>' +
						'<img class="upload_delete" src="' +
						opts.delete_img +
						'" rel="' +
						filelist[i].file +
						'" /><br/>' +
						'<a href="' +
						filelist[i].href +
						'" target="_blank"  class="upload_link filetxt">' +
						filelist[i].file.substring(0, 5) +
						'...</a>' +
						'<input type="hidden" name="' +
						options.name +
						'" value="' +
						encodeURIComponent(filelist[i].file) +
						'" />');
					}
					
				$bar.find('a.upload_link').lightBox({
					type: filelist[i].upload_link_type
				});
					
					
			}

			
			var $input = $('<input type="file" name="uploadedfile" class="uploadfile" />');
			
			if(options.limit!==0 && i>= options.limit) $input.hide();
			
			$input.appendTo(this);
			$.uploader($(this), $input, opts);
		});
	 }

})(jQuery);
