/* Définition des comportements javascript du site */
/* Activer les effets sur les formulaires  */
function setCSSBehaviours() {
	$$('input.addsubmit').each(function(input) {
		var src = 'images/bt_ok.gif';
		if (Browser.Engine.trident) {
			// IE
			input.outerHTML = input.outerHTML.replace(/type=submit/i, 'type="image" src="'+src+'" align="absmiddle"');
		} else {
			input.set({
				'type': 'image',
				'src': src
			});
		}
		input.setStyles({'border': 0, 'vertical-align': 'middle' });
	});
	$$('select#departement, .themes select').addEvent('change', function() { this.form.submit(); });
	$$('a.back, a.retour').addEvent('click', function(e) {
		if (document.referrer.indexOf(location.protocol+'//'+location.hostname) == 0) {
			history.back(); new Event(e).stop();
		}
	});
	$$('#nav-contenu .print').addEvent('click', function(e) { window.print(); new Event(e).stop(); });
	$$('#coldroite input').addEvent('focus', function(e) { this.select(); });
	// Séparateurs dans le texte
	var separateur = new Element('div', {'class': 'separateur'});
	$$('hr').each( function(hr) { separateur.clone().replaces(hr); } );
	$$('#catalogue li[class^=f1-OCC]').setStyle('display', 'none');
	$$('#catalogue ul').each(function(ul) {
		var c = 0;
		ul.getChildren('li[class^=f2]').each(function(li) {
			c++;
			if ((c % 6) == 0) {
				new Element('li').addClass('separator').injectBefore(li);
			}
			var child = li.getChildren();
			if (child && child.length == 1) {
				// Si on n'a pas de sousmenu, ajouter un div qui ferme le menu
				new Element('div').set({'class': 'single'}).injectInside(li);
			}
		});
	})
}

function start_ajax_request(elem) {
	if (elem == null) elem = $('preview');
	elem.set('opacity', 0.6).setStyle('cursor', 'wait');
}
function end_ajax_request(elem) {
	if (elem == null) elem = $('preview');
	elem.set('opacity', 1).setStyle('cursor', 'default');
}
window.addEvent('domready', setCSSBehaviours);
window.addEvent('load', function() {
	$$('#contenu img').each (function (img) {
		if (img.hasClass('noauto')) return;
		if (img.src.indexOf('INTERVAL') != -1) return;
		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 TITLE de l'image en légende dessous
		if (img.get('title') && !img.hasClass('nolegend')) {
			legende = new Element('span').set('text', img.get('title'));
		}
		// Détecter les zooms
		a = img.getParent();
		if (a.get('tag') == 'a') {
			if (!a.getProperty('rel') && a.href.match(/(jpg|png|gif)$/i)) {
				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);
			div.replaces(a);
		} else {
			// Pas de lien, on injecte l'image
			img.clone().injectTop(div);
			div.replaces(img);
		}
	});
	if (typeof Slimbox != 'undefined') {
		Slimbox.scanPage();
	}

	// 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 = div.getElement('img');
		if (img) {
			img.addEvent('load', function(e) { if (this.width > 0) { div.setStyle('width', this.width+'px'); } });
			div.setStyle('text-align', 'center');
		}
	} );
});

window.addEvent('domready', function() {
	// 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|rtf|xls|xla|ppt|pps|zip|rar|jpg|png|gif)$/i) != -1) {
			ext = RegExp.$1;
			infos = ext.toUpperCase();
			a.addClass('icone fichier '+ext);
			flength = '';
			new Request({
				method: 'HEAD',
				emulation: false,
				url: a.href,
				async: true,
				onSuccess: function() {
					if (flength = this.getHeader('Content-Length')) {
						infos += ', '+(flength/1024).round()+' Ko';
					}
					new Element('span').set('text', ' ('+infos+')').injectInside(a);
					a.setProperty('title', a.title+' ('+infos+')');
				},
				onFailure: function() {
					a.addClass('broken');
					new Element('span').set('text', ' ('+infos+')').injectInside(a);
					a.setProperty('title', a.title+' ('+infos+')');
				}
			}).send();
		} else if (a.href.indexOf(uri) != 0 && a.href.indexOf('http') == 0) {
			//a.addClass('icone url');
			if (!a.getProperty('target')) a.setProperty('target', '_blank');
		} else if (a.href.indexOf('mailto:') == 0) {
			a.addClass('icone email');
		}
	});
});

function minipopup(html, style, noclose) {
	if ($('minipopup')) { $('minipopup').dispose(); }
	if ($('overlay')) { $('overlay').dispose(); }
	var div = new Element('div', { 'id': 'minipopup', 'style': "position:absolute;z-index: 9999;visibility:hidden;width:450px;min-height:400px;border:1px solid #000;background:#FFF;top:0;padding:5px;"+style });
	if (!noclose) html = '<div style="text-align:right"><a href="#" class="close"><b>Fermer X</b></a></div><br style="clear: both">'+html;
	div.set('html', html);
	div.injectInside(document.getElement('body'));
	// Overlay
	var overlay = new Element('div', { 'id': 'overlay', 'style': 'position: absolute; z-index: 9998; left: 0; right: 0; width: 100%; background-color: #000; cursor: pointer;', 'opacity': 0.6 });
	overlay.injectInside(document.getElement('body'));
	function repos() {
		var w = window.getSize();
		var d = div.getSize();
		var s = window.getScroll();
		overlay.setStyles({'top': s.y+'px','height':w.y+'px'});
		var left = (w.x/2-d.x/2+s.x)+'px';
		var top = (w.y/2-d.y/2+s.y)+'px';
		div.setStyles({ 'visibility': 'visible', 'left': left });
		div.setStyle('top', top);
	}
	window.addEvents({'scroll': repos, 'resize': repos });
	repos();
	var cleanup = function() { div.dispose(); overlay.dispose(); window.removeEvent('scroll', repos);window.removeEvent('resize', repos); return false; }
	div.getElements('.close').addEvent('click', cleanup);
	div.getElements('label').addEvent('click', cleanup);
	overlay.addEvent('click', cleanup);

}

function addtpl(url, tpl) {
	if (url.indexOf('tpl=') != -1) return url.replace(/tpl=[^&]+/, 'tpl='+tpl);
	if (url.indexOf('?') == -1) return url+'?tpl='+tpl;
	else return url+'&tpl='+tpl;
}

if (typeof console != "object") {
	var console = {
		'log':function(){}
	};
}

