/* Définition des comportements javascript du site */
/* Activer les effets sur les formulaires  */
function setCSSBehaviours() {
	$$('.themes select').addEvent('change', function() { this.form.submit(); });
	$$('a.back, a.retour').addEvent('click', function(e) { history.back(); new Event(e).stop(); });
	$$('#nav-contenu .print').addEvent('click', function(e) { window.print(); new Event(e).stop(); });

	$$('.accordion').each(function (accordion) {
		var drags = accordion.getElements('.drag');
		var contents = accordion.getElements('.content');
		var options = { opacity: true };
		if (accordion.hasClass('hideall')) {
			options.alwaysHide = true;
			options.show = -1;
		}
		drags.each( function(e) { e.setStyle('cursor', 'pointer'); } );
		var acc = new Accordion(drags, contents, options, accordion);
	});
	editable_textareas = $$('div.html textarea');
	if (editable_textareas.length > 0) {
		var mooed = new MooEditable(editable_textareas);
	}
	var myAccordion = new Accordion($('accordion'), 'h3.drag', '.content', {
		opacity: false,
		display: -1,
		alwaysHide: true
	});
	if ($('mini_rappel')) {
		$('mini_rappel').addEvent('click', function(ev) {
			var i = new Element('input', { 'name': 'tel', 'value': 'Votre numéro de téléphone' }).setStyles({'width': '150px', 'margin': '10px 0'});
			i.addEvent('focus', function() { this.value = ''; });
			var b = new Element('input', { 'type' : 'submit', 'value': 'Ok' }).setStyles({'margin': '10px 0'});
			b.addEvent('click', function(ev) {
				var i = this.getPrevious();
				var tel = i.getValue();
				if (tel == 'Votre numéro de téléphone') { i.select(); }
				else if (tel != '') {
					var myAjax = new Ajax('fr/mini-callback?tpl=callback_mini&callback-submit=1&tel='+tel, {'method': 'get', 'update':'mini_rappel'}).request();
				} else {
					i.value = 'Votre numéro de téléphone';
				}
				new Event(ev).stop();
			});
			var p = new Element('p').setProperty('id', 'mini_rappel').adopt(i, b);
			this.replaceWith(p);
			new Event(ev).stop();
		});
	}
}

window.addEvent('domready', setCSSBehaviours);
window.addEvent('load', function() {
	$$('#contenu img').each (function (img) {
		if (img.hasClass('noauto')) return;
		var legende = '';
		// Vérifier si on n'est pas dans un <div> gauche droite ou centre
		done = false;
		$$('div.gauche, div.droite, div.centre').each( function(div) {
			if (!done) if (div.hasChild(img)) { done = true; }
		});
		if (done) return;
		// Mettre le ALT de l'image en légende dessous
		if (!img.hasClass('nolegend')) {
			var text = img.getProperty('alt');
			if (text) { legende = new Element('span').setText(text); }
		}
		// Détecter les zooms
		a = img.getParent();
		if (a.getTag() == 'a') {
			if (!a.getProperty('rel') && a.href.match(/(jpg|png|gif)$/)) {
				a.setProperties({ 'rel': 'lightbox[]', 'title': img.getProperty('alt') });
			}
		} else { a = false; }
		// Mettre un conteneur div autour des images alignées
		var iclass, align;
		if (!img.getProperty('align') && img.getProperty('class')) {
			iclass = img.getProperty('class');
			img.removeProperty('class');
			img.removeProperty('align');
		} else if (img.getProperty('align')) {
			align = img.getProperty('align');
			if (align == 'left') { iclass = 'gauche'; }
			else if (align == 'right') { iclass = 'droite'; }
			else { iclass = 'centre'; }
			img.removeProperty('align');
		} else iclass = "centre";
		div = new Element('div', { 'class': iclass });
		if (legende) legende.injectInside(div);
		if (a != false) {
			// On injecte le lien
			a.clone().injectTop(div);
			a.replaceWith(div);
		} else {
			// Pas de lien, on injecte l'image
			img.clone().injectTop(div);
			img.replaceWith(div);
		}
	});
	Lightbox.init();

	// Mettre la taille des div droite & gauche à celle de l'image...
	$$('div.gauche, div.droite, div.centre').each( function(div) {
		// Trouver l'image
		var img = $E('img', div);
		if (img) {
			img.addEvent('load', function(e) { if (this.width > 0) { div.setStyle('width', this.width+'px'); } });
			div.setStyle('text-align', 'center');
		}
	} );
	// Vérification des liens
	var uri = location.protocol+'//'+location.host;
	$$('#preview a[href]').each( function(a) {
		var ext, infos, flength;
		if (a.hasClass('noauto')) return;
		if (a.getElement('img')) return;
		if (a.href.search(/(pdf|doc|docx|rtf|xls|xlsx|xla|ppt|pps|zip|rar|jpg|png|gif)$/) != -1) {
			ext = RegExp.$1;
			infos = ext.toUpperCase();
			a.addClass('icone fichier '+ext.substring(0,2));
			flength = '';
			new XHR({ method: 'HEAD',
				async: true,
				onSuccess: function() {
					if (flength = this.transport.getResponseHeader('Content-Length')) {
						infos += ', '+(flength/1024).round()+' Ko';
					}
					new Element('span').setText(' ('+infos+')').injectInside(a);
					a.setProperty('title', a.title+' ('+infos+')');
				},
				onFailure: function() {
					a.addClass('broken');
					new Element('span').setText(' ('+infos+')').injectInside(a);
					a.setProperty('title', a.title+' ('+infos+')');
				}
			}).send(a.href);
		} else if (a.href.indexOf(uri) != 0 && a.href.indexOf('http') == 0) {
			a.addClass('icone url');
			if (!a.getProperty('target')) a.setProperty('target', '_blank');
		}
	});
});


