/* (c) Jean Luc Biellmann - Groupe Ressources - 2010  */

var _FormPict = {
	n : 0, // number of pictures
	_Status : new Status('formPictStatus'),
	init : function (cn,thumbPath,id,n) {
		if ($('popupPict'))
			return;
		if (!cn.length)
			return alert('Pas de destination associée pour les images...');
		if (!thumbPath.length)
			return alert('Pas de répertoire associé pour les images miniatures...');
		if (!id.length)
			return alert('Pas de nom wikini associé pour l\'image...');
		n = parseInt(n);
		if (n<1)
			return alert('Nombre d\'images incorrect...');
		_FormPict.cn = cn;
		_FormPict.thumbPath = thumbPath;
		_FormPict.id = id;
		_FormPict.n = n;
		_FormPict.load();
	},
	load : function () {
		var div = E('div',{'id':'popupPict'}).update('\
		<div id="popupPictBg"></div>\
		<div id="popupPictContent">\
			<form id="formPict" action="index.php" method="post" enctype="multipart/form-data" target="iframe1">\
				<fieldset>\
					<legend>'+_FormPict.cn+'...</legend>\
					<input type="hidden" name="m" value="formpict" />\
					<input type="hidden" name="c" value="'+_FormPict.cn+'" />\
					<input type="hidden" name="f" value="rec" />\
					<input type="hidden" name="pname" value="'+_FormPict.id+'" />\
					<div id="formPictThumbs"></div><br style="clear:both"/>\
					<div id="formPictStatus" class="debug"></div>\
					<div class="auto">\
						<input type="file" name="p1"/><br/>\
						<input type="file" name="p2"/><br/>\
						<input type="file" name="p3"/><br/>\
						<br/>\
						<input type="submit" value="Valider"/>\
						<input type="button" value="Annuler" onclick="$(\'popupPict\').remove()"/>\
						<div id="formPictStatus"></div>\
					</div>\
				</fieldset>\
			</form>\
			<iframe name="iframe1" id="iframe1" style="visibility:show;width:100%;height:20px;border:1px solid black;visibility:hidden"></iframe>\
		</div>\
		');
		A(document.body,div);
		_FormPict.fill();
	},
	onLoad : function (e,i,img) {
		var div = E('div').update('<p>Image&nbsp;'+i+'&nbsp;:</p><p><img class="thumbs shadow" src="" id="p'+i+'" alt="vide"/></p><p><input type="checkbox" name="del[]" id="del'+i+'" value=""/> Effacer ?</p>');
		A($('formPictThumbs'),div);
		$('p'+i).src = img.src;
		$('del'+i).value = _FormPict.id+i;
	},
	onError : function (e,i) {
	},
	resize : function () {
		_Window.center($('popupPictContent'));
	},
	fill : function () {
		var now = new Date();
		for (var i=1;i<=_FormPict.n;i++) {
			var img = E('img');
			Event.observe(img, 'error', _FormPict.onError.bindAsEventListener(_FormPict,i));
			Event.observe(img, 'load', _FormPict.onLoad.bindAsEventListener(_FormPict,i,img));
			img.src = _FormPict.thumbPath+_FormPict.id+i+'.jpg?'+now.getTime();
		}
	},
	slot : function (json) { // return after rec...
		if (json.warn) {
			_FormPict._Status.warn(json.warn);
			setTimeout('_FormPict._Status.reset',2000);
		}
		if (json.rec) {
			$('formPictThumbs').update('');
			_FormPict._Status.info('Images mises à jour.');
			_FormPict.fill();
		}
	}
};


