/**
 * CORRELYCE: Catalogue Ouvert Régional de Ressources Editoriales pour les LYCéEs
 *
 * Copyright (C) 2007-2009 Région PACA, PASS-TECH
 *
 * Région Provence, Alpes,Côte d'Azur (Région PACA) Direction des lycées
 * 27, rue Jules Guesde, 13001 Marseille (France) 
 * Contact : Eric Mazo <emazo@regionpaca.fr>, tél : +33.4.91.57.50.56
 *
 * Avec la contribution de :
 *      Centre régional de documentation pédagogique d'Aix-Marseille
 *      31, boulevard d'Athènes, 13232 Marseille cedex 1 (France)
 *      Contact : Gérard Puimatto <gerard.puimatto@crdp-aix-marseille.fr>
 *
 * PASS Technologie (PASS-TECH)
 * 26, rue Louis Braille, 75012 Paris (France) 
 * Contact : Nader Boutros <nader.boutros@pass-tech.fr>, tél : +33.1.43.41.42.44
 * 
 * Développé par :
 *      Cédric Torcq <cedric.torcq@pass-tech.fr>
 *      Pierre Dittgen <pierre.dittgen@pass-tech.fr> 
 *      Julien Bellion <julien.bellion@pass-tech.fr> (depuis 2009)
 * 
 * Ce programme est un logiciel libre: vous pouvez le redistribuer et/ou le modifier selon les termes 
 * de la "GNU General Public License", tels que publiés par la "Free Software Foundation"; 
 * soit la version 2 de cette licence ou (à votre choix) toute version ultérieure.
 * 
 * Ce programme est distribué dans l'espoir qu'il sera utile, mais SANS AUCUNE GARANTIE,
 * ni explicite ni implicite; sans même les garanties de commercialisation ou d'adaptation 
 * dans un but spécifique.
 * 
 * Se référer à la "GNU General Public License" pour plus de détails.
 * 
 * Vous devriez avoir reçu une copie de la "GNU General Public License"
 * en même temps que ce programme; sinon, écrivez à la "Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

/**
 * resource display.js
 * several functions to enhance LOM(FR) resource display
 */

/** Global variables */
var currentSection = null;

/** Enhancement of string class */
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };

/* === Motbis automatic enhancements === */
function prettyOneMotBis(content) {
	content = content.trim();

	var motbisRegExp = new RegExp(/^motbis:([^:]*):(.*)$/);
	if (!content.match(motbisRegExp)) {
		return content;
	}

	var m = motbisRegExp.exec(content);
	var id = m[1];
	var term = m[2];

	content = term;
	//if (id != '') {
		//content += '<sub>' + id + '</sub>';
	//}

	return content;
}
function prettyMotbisSpan(spanElt) {
	var content = spanElt.innerHTML;

	var values = $A(content.split(/,/)).collect(prettyOneMotBis);
	spanElt.innerHTML = values.join(', ');
}
function prettyMotbis() {
	$A(document.getElementsByClassName('motbis')).each(prettyMotbisSpan);
}


/* === Sections management === */
function sectionLabel(sectionId, type) {
	type = 'normal';
	switch (sectionId) {
		case 'general':
			if (type == 'normal') { return 'G&eacute;n&eacute;ral'; }
			else { return 'g&eacute;n.'; }
		case 'lifeCycle':
			if (type == 'normal') { return 'Cycle de vie'; }
			else { return 'cyc.'; }
		case 'metaMetadata':
			if (type == 'normal') { return 'M&eacute;ta'; }
			else { return 'm&eacute;t.'; }
		case 'technical':
			if (type == 'normal') { return 'Technique'; }
			else { return 'tec.'; }
		case 'educational':
			if (type == 'normal') { return 'P&eacute;dagogique'; }
			else { return 'p&eacute;d.'; }
		case 'rights':
			if (type == 'normal') { return 'Droits'; }
			else { return 'dro.'; }
		case 'relation':
			if (type == 'normal') { return 'Rel.'; }
			else { return 'rel.'; }
		case 'annotation':
			if (type == 'normal') { return 'Annot.'; }
			else { return 'ann.'; }
		case 'classification':
			if (type == 'normal') { return 'Class.'; }
			else { return 'cla.'; }
		default:
			if (type == 'normal') { return 'foobar'; }
			else { return 'foo.'; }
	}
}

function changeSection(newSection) {
	if (currentSection == newSection) {
		return false;
	}

	$A($('tabs').getElementsByTagName('li')).each(function(elt) {
		var aElts = elt.getElementsByTagName('a');
		if (aElts.length == 0) {
			var strongElt = elt.getElementsByTagName('p')[0];
			elt.innerHTML = '<a href="#' + currentSection
				+ '" onclick="return changeSection(\''
				+ currentSection + '\');"><span>' + sectionLabel(currentSection, 'brief');
				+ '</span></a>';
		} else {
			var aElt = aElts[0];
			var eltSection = computeSectionFromAnchor(aElt.getAttribute('href'));
			if (eltSection == newSection) {
				aElt.innerHTML = sectionLabel(newSection, 'normal');
				elt.innerHTML = '<p><span>' + aElt.innerHTML + '</span></p>';
			}
		}
	});

	Element.hide($('section_' + currentSection));
	Element.show($('section_' + newSection));
	currentSection = newSection;
	return false;
}

function computeSectionFromAnchor(anchor) {
	return anchor.substr(anchor.lastIndexOf('#')+1);
}

function manageSections() {
	$A(document.getElementsByClassName('section')).each(function(elt, index) {
		if (index == 0) {
			currentSection = elt.getAttribute('id').substr(8);
		} else {
			elt.style.display = 'none';
		}
	});
	$A($('tabs').getElementsByTagName('li')).each(function(elt, index) {
		var aElt = elt.getElementsByTagName('a')[0];
		var sectionId = computeSectionFromAnchor(aElt.getAttribute('href'));
		if (index == 0) {
			elt.innerHTML = '<p><span>' + sectionLabel(sectionId, 'normal') + '</span></p>';

		} else {
			aElt.onclick = function() {
				return changeSection(sectionId);
			}
			aElt.innerHTML = '<span>' + sectionLabel(sectionId, 'brief') + '</span>';
		}
	});
}

// Main program
function initDoc() {
	manageSections();
	prettyMotbis();
}

// Ce qui suit permet de lancer la fonction initDoc() dès le chargement du DOM
// (sans attendre celui de toutes les images)
//
// Dean Edwards/Matthias Miller/John Resig
function init() {
    // quit if this function has already been called
    if (arguments.callee.done) return;

    // flag this function so we don't do the same thing twice
    arguments.callee.done = true;

    // kill the timer
    if (_timer) clearInterval(_timer);

    // do stuff
	initDoc();
};

/* for Mozilla/Opera9 */
if (document.addEventListener) {
    document.addEventListener("DOMContentLoaded", init, false);
}

/* for Internet Explorer */
/*@cc_on @*/
/*@if (@_win32)
    document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
    var script = document.getElementById("__ie_onload");
    script.onreadystatechange = function() {
        if (this.readyState == "complete") {
            init(); // call the onload handler
        }
    };
/*@end @*/

/* for Safari */
if (/WebKit/i.test(navigator.userAgent)) { // sniff
    var _timer = setInterval(function() {
        if (/loaded|complete/.test(document.readyState)) {
            init(); // call the onload handler
        }
    }, 10);
}

/* for other browsers */
window.onload = init;


