/*
 * widgets.js
 * requiert jQuery - http://jquery.com
 */

$(document).ready(function() {


	var fallback = document.location;
	var textSize = [ 'smallest', 'small', 'normal', 'large', 'largest' ];

	// init
	$('#docBody').addClass('normal');

	if (!$('#Widgets').is('ul')) {
		$('<ul id="Widgets"></ul>').insertBefore('#navDocument');
	}
	
	// bouton Imprimer
	$('<a id="WPrint" href="'+fallback+'"></a>')
		.appendTo('#Widgets')
		.attr('title',  'Imprimer ce document')
		.wrap('<li></li>')
		.bind('click', function() {
			//previewPrint();
			window.print();
			return false;
		});

	// bouton Réduire taille du texte
	$('<a id="WTextSmaller" href="'+fallback+'"></a>')
		.appendTo('#Widgets')
		.attr('title',  'R\xE9duire la taille du texte')
		.wrap('<li></li>')
		.bind('click', function() {
			return setFontSize(-1);
		});

	// bouton Augmenter taille du texte
	$('<a id="WTextLarger" href="'+fallback+'"></a>')
		.appendTo('#Widgets')
		.attr('title',  'Augmenter la taille du texte')
		.wrap('<li></li>')
		.bind('click', function() {
			return setFontSize(+1);
		});

	if ($.browser.mozilla) {
		$('#Widgets a').css('-moz-outline-style', 'none');
	}
	
	function getTextSize(c) {
		for(var i = textSize.length; i-- && textSize[i] != c;);
		return i;
	}

	function setFontSize(s) {
		var c = $('#docBody').attr('class').replace(/article\s?/, '');
		var k = getTextSize(c);
		var size = textSize[k + s];
		if ((s < 0 && k > 0) || (s > 0 && k < 4)) {
			$('#docBody')
				.removeClass(c).addClass(size)
				//.children('div').removeClass(c).addClass(size);
				.children().removeClass(c).addClass(size);
		}
		return false;
	}
	
	function previewPrint() {
		$('link[@rel*=style]').each(function() {
			this.disabled = true;
			if (this.getAttribute('title') == 'Version imprimable') {
				this.disabled = false;
			}
		});
	}
	
	
	// Intitulé dans le champ de recherche
	var label = 'Votre recherche';
	$('input[@class=label]')
		.val(label)
		.blur(function(e) {
			if (this.value == '') {
				this.value = label;
			}
		})
		.focus(function() {
			if (this.value == label) {
				this.value = '';
			}
		});
	
	
	// Nettoyage des mises en forme locales
	$('img').removeAttr('style');
	$('table').removeAttr('style');
	$('tr').removeAttr('style');
	$('td').removeAttr('style');
	
	// Amelioration des appels de notes
	$('a[@class$=notecall]//span').removeAttr('style');
	$('a[@class$=notecall]').parents('span').removeAttr('style');
	//$('a[@class$=notecall]').prepend('&nbsp;(').append(')');
	
	// Rustines
	$('h2//ul[@class=sidenotes]').remove();
	
});
