//Class Importateur
var Importateur = Class.create({
	
	initialize: function(reload,origine,id){
		this.reload    = reload;
		this.dimension = new Array();
		this.ajax      = new Object();
		this.att       = new Object();
		this.origine   = origine;
		this.id        = id;
		this.indice    = 0;
		this.w         = 0;
		this.h         = 0;
		$$$I           = this;
		//eval('alert("' + origine + '")');
	},
    
	open: function(page,id,entite){
		var sup = '';
		
		if($F(this.origine) == 0){
			alert('Vous devez choisir un type de publicité');
			return(false);
		}
		
		var w = parseInt(this.dimension[$F(this.origine)][0],10) + 150;
		if(w<330) w=380;
		var h = parseInt(this.dimension[$F(this.origine)][1],10) + 100;
		$$$I.w = this.dimension[$F(this.origine)][0];
		$$$I.h = this.dimension[$F(this.origine)][1];
		if($('f_uti_id') != undefined && $F('f_uti_id') != ''){
			sup = "&f_uti_id=" + $F('f_uti_id');
		}
		var url  = page + "?type="+$F(this.origine) + "&i=" + id + "&w=" + this.dimension[$F(this.origine)][0];
		    url += "&h=" + this.dimension[$F(this.origine)][1] +"&entite=" + entite + sup;
		
		
		$$$I.fen = window.open(url,"fen_importer","menubar=no, status=no, scrollbars=no, menubar=no, width=" + w + ", height=" + h);
	},
	
	openFile: function(id,entite,param){
		if($('f_uti_id') != undefined && $F('f_uti_id') != ''){
			sup = "&f_uti_id=" + $F('f_uti_id');
		} else {
			sup = '';
		}
		if($('f_uti_type_document0') != undefined){
			if(_F_.valueOf('f_uti_type_document','radio') == ''){
				alert('Veuillez sélectionner un type de document.');
				return(false);
			} else {
				sup += "&f_type_document=" + _F_.valueOf('f_uti_type_document','radio');
			}
		}
		if(param != '') sup += '&' + param;
		var url  = "module/Importateur/importer_file.php?id=" + id + '&entite=' + entite + sup;
		
		
		$$$I.fen = window.open(url,"fen_importer","menubar=no, status=no, scrollbars=no, menubar=no, width=320, height=80");
	},
	
	upload: function(valeur,id){
		$(id).value = valeur;
	},
	
	push: function(data){
		this.dimension[data[0]] = new Array(data[1],data[2]);
	},
	
	checkFile: function(fichier){
		$('nom_fichier').value = fichier;
		return(true);
	},
	
	valider: function(file,type,identifiant){
		var url = "module/Importateur/valider.php";
		var pars = "file=" + file + "&type=" + type/* + "&uti_id=" + $F('f_uti_id')*/;
		if($('f_uti_id') != undefined) pars += "&uti_id=" + $F('f_uti_id');
		if($F('engine') == 'pubImageAdmin') pars += '&engine=pubadmin';
		
		$$$I.ajax = new Ajax.Request(url,{
											method: 'post',
											parameters: pars,
											onComplete: function(r){
															var rep     = r.responseText;
															var arrRep  = rep.split(';');
															var typeRep = arrRep[0];
															var id      = arrRep[1];
															var width   = arrRep[2];
															var height  = arrRep[3];
															var lecteur  = arrRep[4];
															window.$$$I.fen.close();
															if($$$I.reload){
																window.location.reload();
															} else {
																if(typeRep == 'photo_editeur'){
																	$$$I.insertImage(file);
																} else {
																	if($(identifiant + '_td_screen') != undefined){
																		$$$I.displayUniqueImage(id, file, typeRep,identifiant,width,height,lecteur);
																	} else {
																		$$$I.insertLigne(file,id,typeRep,identifiant,width,height);
																	}
																}
																/*
																
																switch(typeRep){
																case 'photo':
																	$$$I.insertLigne(file,id,typeRep,identifiant);
																	break;
																case 'pubImage':
																	$$$I.insertLigne(file,id,typeRep,identifiant);
																	break;
																case 'photoAsso':
																	$$$I.insertLigne(file,id,typeRep,identifiant);
																	break;
																case 'pubImageAdmin':
																	$$$I.displayUniqueImage(id, file, typeRep,identifiant);
																	break;	
																case 'page':
																	$$$I.insertImage(file);
																	break;
																default:
																	$$$I.displayUniqueImage(id, file, typeRep,identifiant);
																	break;
																}
																*/
																
															}
															
														}
									
									});
	},
	
	displayUniqueImage: function(id,file,typeRep,identifiant,w,h,lecteur){
		//alert("id = " + id + "\nfile = " + file + "\ntypeRep = " + typeRep);
		if(id != ""){
			if(lecteur == 'img'){
				//tmp_id = this.id;
				//$(tmp_id).value = id;
				//$('td_screen').setStyle({width: $$$I.w + 'px', height: $$$I.h + 'px'});
				$(identifiant).value = id;
				//$(identifiant + '_td_screen').morph('width: ' + $$$I.w + 'px; height: ' + $$$I.h + 'px');
				var tmp = function(){
						   //$(identifiant + '_img_screen').src = 'module/Importateur/uploaded/' + file;
				           //$(identifiant + '_img_screen').setStyle({width:  $$$I.w + 'px', height: $$$I.h + 'px'});
				           $(identifiant + '_td_screen').setStyle({background: 'none',width: w + 'px',height: h + 'px'});
				           var image_c = '<img src="module/Importateur/uploaded/' + file + '" width="' + w + 
				           '" height="' + h + '" />';  
				           $(identifiant + '_td_screen').update(image_c);
				           //$(identifiant + '_img_screen').setStyle({width: w + 'px',height: h + 'px'});
				          };
				tmp.delay(1);
			} else {
				$(identifiant).value = id;
				var contenu = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" '+
				'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" ' +
				'width="' + w + '" height="' + h + '" id="anim" align="middle">' +
				'<param name="allowScriptAccess" value="sameDomain" />' +
				'<param name="allowFullScreen" value="false" />' +
				'<param name="WMODE" value="transparent" />' +
				'<param name="movie" value="module/Importateur/uploaded/' + file + '" /><param name="quality" value="high" />'+
				'<param name="bgcolor" value="#ffffff" />' +
				'<embed WMODE="transparent" src="module/Importateur/uploaded/' + file + '" quality="high" bgcolor="#ffffff" width="' + w + '" height="' + h + '" name="anim" '+
				'align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" '+
				'type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />'+
				'</object>';
				
				$(identifiant + '_td_screen').update(contenu);
			}
		}
	},
	
	insertImage: function(file){
		var image = '<img src="module/Importateur/uploaded/' + file + '" />';
		tinyMCE.activeEditor.execCommand('mceInsertContent', false, image);
	},
	
	insertLigne: function(file,id,type,identifiant,w,h){

		var newId = $$('input.' + identifiant + 'forjs').length;
		var chm = "module/Importateur/uploaded/";
		var contenu = '<img src="' + chm + file + '" width="' + w + '" height="' + h + '" id="scr_' + identifiant + '_' + newId + '"> ' +
                      '<input type="hidden" value="' + id + '" id="img_' + identifiant + '_' + newId + '" /> ' +
                      '<input type="button" class="' + identifiant + 'forjs" value="Supprimer cette image" ' +
                      ' id="rem_' + identifiant + '_' + newId + '" /><br />';
		
		$('insertion_' + identifiant).insert({top: contenu});
		_F_.add(new FormItem(new Array("img_" + identifiant + '_' + newId,"hidden","",id,"")));
		$('nb_img_'+identifiant).value = $$('input.' + identifiant + 'forjs').length;
		$('scr_' + identifiant + '_' + newId).onclick = function(){$$$I.showImage(newId,identifiant);};
		$('rem_' + identifiant + '_' + newId).onclick = function(){$$$I.removeImg(newId,identifiant);};
		
		// Test pour savoir si on a atteint le nombre max d'image
		if($$('input.' + identifiant + 'forjs').length == $('nb_max_' + identifiant).value){
			$('inserer_' + identifiant).remove();
		}
 	},
	
	removeImg: function(newId,identifiant){
		att = new Attente();
		att.show();
		var url  = "module/Importateur/remove.php";
		var pars = "identifiant=" + $('img_' + identifiant + '_' + newId).value;
		$$$I.ajax = new Ajax.Request(url,{
			method: 'post',
			parameters: pars,
			onComplete: function(r){
							var rep = r.responseText;
							if(rep != ''){
								alert(rep);
							} else {
								$('scr_' + identifiant + '_' + newId).setStyle({display: 'none'});
								$('img_' + identifiant + '_' + newId).value = 0;
								$('rem_' + identifiant + '_' + newId).setStyle({display: 'none'});
							}

							att.hide();
						}
		});
		
	},
	
	showImage: function(indice,id){
		$$$I.att = new Attente();
		$$$I.att.show();
		var contenu = '<div id="A_screen"></div>';
		$('A_loader').update(contenu);
		$('A_loader').setStyle({width :'400px'});
		var pos_y = ((window.screen.height + $$$I.att.getScrollPosition())/2 - 150);
		$$$I.indice = indice;
		$$$I.id = id;
		$$$I.displayImage.delay(1);
	},
	
	displayImage: function(){
		$('A_screen').setStyle({background : 'url(' + $('scr_' + $$$I.id + '_' + $$$I.indice).src + ') no-repeat center center'});
		$('A_background').onclick = function(){$$$I.hideImage()};
		$('A_container').onclick = function(){$$$I.hideImage()};
	},
	
	hideImage: function(){
		if($('A_screen')) $('A_screen').remove();
		this.att.hide();
	},
	
	supprimer: function(file){
		var url = "module/Importateur/supprimer.php";
		var pars = "file=" + file;
		
		$$$I.ajax = new Ajax.Request(url,{
											method: 'post',
											parameters: pars,
											onComplete: function(r){
															window.$$$I.fen.close();
														}
									
									});
	}
});

var Selecteur = Class.create({
	
	initialize: function(selecteur,ecran){
		$$$S           = this;
		$$$S.selecteur = selecteur;
		$$$S.ecran     = ecran;
		$$$S.liste     = new Array();
	},
	
	push: function(data){
		$$$S.liste = data;
	},
	
	changeType: function(data){
		if($$$S.liste.length > 0){
			if(data == 0){
				tableau = $$$S.liste;
			} else {
				tableau = $$$S.liste.select(function(e){return e.img_type == data;});
			}
			$$$S.displayImage();
			$$$S.displaySelect(tableau,$('f_img_id').value);
		}
	},
	
	displaySelect: function(tableau,selected){
		var tmp = "<option value='0'> </option>";
		for(var i=0; i<tableau.length; i++){
			tmp += "<option value='" + tableau[i].data + "'>" + tableau[i].label + "</option>";
		}
		$($$$S.selecteur).update(tmp);
	},
	
	displayImage: function(image){
		if(image != "" && image != undefined){
			$($$$S.ecran).innerHTML = '<img src="module/Importateur/uploaded/' + image +'" />';
		} else {
			$($$$S.ecran).innerHTML = '<img src="images/leurre.gif" />';
		}
	}
});
