function init_inject_thumb()
{		
		var form = $('ugc_submission');
		var urlform = form.getProperty('action');
		
		var datas = new Hash({'get_postThumbs':true}).toQueryString();

		new Request.HTML( {url:urlform,data: datas,method: 'post',update:'postThumbsContainer',onComplete:function(){
			
				var  imgs = [];
				$$('#postThumbs li').each(function(el,i){
		
				imgs[i] = el.getProperty('rel');
				
				new Element('a').inject(el,'bottom').adopt(el.getChildren('img')).setProperty('title','Use in editor').addClass('addEditor').addEvent('click',function(e){ tinyMCE.activeEditor.execCommand('mceInsertContent', true, '<img src="'+imgs[i]+'"/>');  });
				new Element('a').inject(el,'top').set('html','<span>x</span>').setProperty('title','Delete this media').addClass('RemoveMedia').addEvent('click',
					function(e){
			  
									if(confirm('Do you really want to delete this picture ?'))
										new Request.HTML({url:urlform, data: new Hash({'deleteThumb':el.getProperty('id')}).toQueryString(),method: 'post',onComplete:function(){ el.destroy() } }).send();	
			  
			  					});
			 
		});

			}}).send();

}

window.addEvent("domready",function(){
	
	if(!$('ugc_submission'))
		return;
	
$('soumettre').addEvent('click',function(e){
	
	new Event(e).stop();
	
	var webservice = this.getProperty('href');
	var tpl_previsualisation = $('previsualiser').getProperty('href');
		
	var myhash = new Hash({'ajax':true,'ugc_submission':true,'sub_content':tinyMCE.get('sub_content').getContent(),'titre':$('titre').get('value'),'category':$('category').get('value')});
	var query = myhash.toQueryString();
	
	new Request.HTML({url:webservice, data: query,method: 'post',update:$('submit_errors'),evalScripts:'true',onComplete:function(Obj){ 
		
		if($('submit_errors').getChildren().hasClass('clrz_errors') != "true")
			window.location.href = '/mon-compte/';
		
	}}).send();
});
	
$('previsualiser').addEvent('click',function(e){

	new Event(e).stop();
		
	var webservice = $('soumettre').getProperty('href');
	var tpl_previsualisation = this.getProperty('href');
		
	var myhash = new Hash({'ajax':true,'save':true,'ugc_submission':true,'sub_content':tinyMCE.get('sub_content').getContent(),'titre':$('titre').get('value'),'category':$('category').get('value')});
	var query = myhash.toQueryString();
	
	new Request.HTML({url:webservice, data: query,method: 'post',update:$('submit_errors'),evalScripts:'true',onComplete:function(Obj){ 
				
		if($('submit_errors').getChildren().hasClass('clrz_errors') != "true" )
			window.location.href = tpl_previsualisation;
		
	}}).send();
});

window.addEvent('load', function() {
 	
	if(!$('ugc_submission'))
		return;
	
	init_inject_thumb();
 
 	var form = $('ugc_submission');
	var urlform = form.getProperty('action');
	
	var swiffy = new FancyUpload2($('up-status'), $('up-list'), {
		url: urlform,
		fieldName: 'Filedata',
		path: '/wp-content/themes/silences/libraries/js/fancy/Swiff.Uploader.swf',
		limitSize: 2 * 1024 * 1024,
		onLoad: function() {
			
		},
		onAllSelect: function() {
			
			swiffy.upload();
		},
		
		onError:function(file,error)
		{
				
			var Obj ={"errors":[{"item":"File error, image size must be 500px width at least."}]};
			new ThrowErrors({'Obj':Obj,'success':'','container':$('submit_errors')});
		},
		
		onComplete: function(){ /*init_inject_thumb()*/ },
		onAllComplete: function(){ init_inject_thumb() },
		
		debug: true, 
		target: 'upload'
	});

	/**
	 * Various interactions
	 */
 
	$('upload').addEvent('click', function() {
		/**
		 * Doesn't work anymore with Flash 10: swiffy.browse();
		 * FancyUpload moves the Flash movie as overlay over the link.
		 * (see opeion "target" above)
		 */
		swiffy.browse();
		return false;
		}); 
	});
});

