
var kos_pyro_UploadField;

(function ($) {
	if ( kos_pyro_UploadField != undefined ) return;
	$.pyro.include( '/class/kos/pyro/UploadField/www/swfupload.js' );
	kos_pyro_UploadField = $.pyro.extendClass( $.pyro.Widget, function () {
		$.pyro.Widget.apply( this, arguments );
		this.pyroWidgetName = "UploadField";
	}, {
		widgetName: 'UploadField',
		widgetClassName: 'kos_pyro_UploadField',
		ui: {},
		init: function () {
			var self = this;
			var o = this.options;
			$.pyro.Widget.prototype.init.apply( this, arguments );
			this.element.addClass( 'pyro-UploadField' );
			this.element.data( 'initialValue', this.element.val() );
			this.element.css( 'display', 'none' );

			this.ui.container.addClass( 'pyro-UploadField-container');
			this.ui.buttons = $( '<span class="pyro-UploadField-buttons" style="position:relative;margin-top:2px" />' )
				.appendTo( this.ui.container );
			this.ui.buttonBrowse =
				$('<div style="width:140px;height:19px;padding:2px;background:#bbb">Envoyer votre fichier</div>')
					.appendTo( this.ui.buttons );
			this.ui.buttonBrowse.jsupload( {
				name: 'Filedata', action: '/back-upload.php',
				params: { id: 0, type: this.options.targetFolder.substr(1) },
				onSubmit: function() {
					$('<img class="spinner" src="/images/spinner.gif" />').appendTo( $('#ext-toolbar') )
						.css( { position: 'absolute', left: '-2px', top: '2px' } );
					//$('#progress1').text('Uploading file...');
					//console.log('Uploading');
				},
				onComplete: function(data) {
					$('.spinner', $('#ext-toolbar') ).remove();
					if ( data.substr(0,3) != 'OK:' ) {
						 window.alert(data);
						 return;
					}
					var fileName = data.substr(3);
					self.ui.selectBox.append( '<option value="' + fileName + '">' + fileName + '</option>' );
					self.ui.selectBox.val(fileName);
					self.element.val(fileName);
					self.refreshInterface();
				}
			} );
			this.showList();
		},
		allowUpload: function ( val ) {
			this.options.allowUpload = val ? true : false;
			this.refreshInterface();
		},
		showList: function ( val ) {
			val = this.options.showList ? true : false;
			$('.pyro-UploadImageField-list', this.ui.container ).remove();
			delete this.ui.selectBox;
			if ( val ) {
				this.ui.selectBox = $('<select class="pyro-UploadImageField-list" size="1" />')
					.insertBefore( this.element );
				var i;
				var sel = this.ui.selectBox[0];
				for ( i=0; i!=this.options.listFiles.length; i++ ) {
					sel.options[ sel.options.length ] = new Option( this.options.listFiles[i], this.options.listFiles[i] );
				}
				if ( this.options.allowUpload ) {
					sel.options[ sel.options.length ] = new Option( "Envoyer un fichier...", "" );
				}
				var self = this;
				this.ui.selectBox.val( this.element.val() )
					.change( function () {
						self.element.val( this.value );
						self.refreshInterface();
					} );
				if ( !this.element.val().length && this.ui.selectBox.val().length ) {
					this.element.val( this.ui.selectBox.val() );					
				}
				//this.element.css( 'display', 'none' );
			}
			this.refreshInterface();
		},
		refreshInterface: function () {
			var o = this.options;
			//if (this.ui.selectBox) window.alert(this.ui.selectBox.val().length  );
			this.ui.buttons.css( 'display',
				o.allowUpload && ( !o.showList || ( this.ui.selectBox && !this.ui.selectBox.val().length ) ) ? '' : 'none' );
			/*this.element.css( 'display',
				!o.showList || ( o.allowUpload && this.ui.selectBox && !this.ui.selectBox.val().length ) ? '' : 'none' );*/
			this.ui.buttons.css( 'display', this.element.attr( 'name' ) != 'avatar' ? 'block' : 'none' );
			this.element.css( 'display', 'none' );
		},
		
		destroy: function () {
			return $.pyro.Widget.prototype.destroy.apply( this, arguments );
		},
		clearQueue: function () {
			this.element.val( this.element.data( 'initialValue' ) );
		}
	} );
	$.pyro.registerWidget( 'UploadField', 'kos_pyro_UploadField' );
	$.extend( $.pyro.UploadField, {
		defaults: {
			upload_url: '/class/kos/pyro/UploadField/www/upload.php',
			post_params: {},
			use_query_string: true, 
			file_size_limit: '2MB',
			file_upload_limit: 0,
			file_queue_limit: 1,
			targetFolder: '',
			allowUpload: true,
			showQueueDetails: true,
			showList: true,
			listFiles: []
		}
	} );
} )( jQuery );
 