dojo.provide('image.description');

dojo.declare('image.description', null, {
	arrHandler: [],
	domNode: null,

	constructor: function(params, srcNodeRef) {
		this.arrHandler = [];
		this.domNode = srcNodeRef;
		var handler = dojo.connect(this.domNode, 'click', this, this.onClick);
		this.arrHandler.push(handler);
	},

	destroy: function() {
		var h;
		while ((h = this.arrHandler.pop()) != undefined) {
			dojo.disconnect(h);
		}
	},

	/**
	 * Affiche/masque la description
	 */
	onClick: function(e) {
		var domDesc = this.domNode.nextSibling;
		while (domDesc.nodeType != 1)
		{
			domDesc = domDesc.nextSibling;
		}
		dojo.toggleClass(domDesc, 'notxt');
		dojo.toggleClass(this.domNode, 'show');
		dojo.toggleClass(this.domNode, 'hide');
		dojo.stopEvent(e);
	}
});

/**
 * Créé les liens d'affichage/masquage de la description et masque la description
 */
dojo.addOnLoad(function() {
	dojo.query('.jsImageDescription').forEach(function(item) {
		var domLink = dojo.create('a', {'innerHTML': '<span class="notxt">Afficher/masquer la description</span>', 'href': '#', 'title': 'Afficher/masquer la description', 'class': 'show'}, item, 'before');
		new image.description({}, domLink);
		dojo.addClass(item, 'notxt');
	});
});

/**
 * Redimensionne la div de l'image
 */
dojo.addOnLoad(function() {
	dojo.query('.jsImage').forEach(function(item) {
		dojo.style(item.parentNode.parentNode, 'width', item.width+'px');
	});
});
