/**
 * 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;


