// Clean LeMonde Portfolio
// version 0.3 BETA!
// 2005-12-12
// Copyright (c) 2005, Loic d'Anterroches
// Released under the GPL license
// http://www.gnu.org/copyleft/gpl.html
//
// --------------------------------------------------------------------
//
// This is a Greasemonkey user script.
//
// To install, you need Greasemonkey: http://greasemonkey.mozdev.org/
// Then restart Firefox and revisit this script.
// Under Tools, there will be a new menu item to "Install User Script".
// Accept the default configuration and install.
//
// To uninstall, go to Tools/Manage User Scripts,
// select "Le Monde Portfolio", and click Uninstall.
//
// --------------------------------------------------------------------
//
// ==UserScript==
// @name          Le Monde Portfolio
// @namespace     http://xhtml.net/
// @description   Remove the flash applet for the portfolio and display images
// @include       http://www.lemonde.fr/web/portfolio/*
// @include       http://lemonde.fr/web/portfolio/*
// ==/UserScript==


var theFlash;

function replaceFlash(infos)
{
	var theFlash = document.getElementById('portfolio');
	var newHTML = '<div style="margin: 5px auto 5px auto; ' +
		'margin-bottom: 5px; ' +
		'font-size: small; ">';
	for (var i=0; i<infos.length; i++) {
		newHTML += '<div style="margin: 5px auto 5px auto; ' +
		'border: 2px solid #bcbcbc; margin-bottom: 5px; ' +
		'font-size: small; text-align: center;">';
		newHTML += '<p style="text-align: center;"><strong>'+infos[i]['title']+'</strong></p>';
		if (infos[i]['url'].length > 0) {
			newHTML += '<a href="'+infos[i]['url']+'">';
		}
		newHTML += '<img border="0" src="'+infos[i]['src']+'" alt=" " />';
		if (infos[i]['url'].length > 0) {
			newHTML += '</a>';
		}
		newHTML += '<p style="text-align: left; padding: 1em;">'+infos[i]['legende'];
		if (infos[i]['url'].length > 0) {
			newHTML += ' <a href="'+infos[i]['url']+'">En savoir plus...</a>';
		}
		newHTML += '</p></div>';
	}
	newHTML += '</div>';

	theFlash.innerHTML = newHTML
	theFlash.style.visibility = 'visible';
}


function parseXmlResponse(response)
{
	if (response.status != '200') {
		GM_log('XML not found Status returned: '+response.status+' ' + response.statusText);
		return;
	}
	var parser = new DOMParser();
	var xmlDoc = parser.parseFromString(response.responseText, "application/xml");
	var slides = xmlDoc.getElementsByTagName('slide');
        var img_list = [];
	var title, legende, big_img;
        for (var i = 0; i < slides.length; i++) {
		title = slides[i].getElementsByTagName('titre')[0].textContent;
		legende = slides[i].getElementsByTagName('legende')[0].textContent;
		big_img = slides[i].getElementsByTagName('img_grande')[0].getAttribute('src');
		savoir_plus = slides[i].getElementsByTagName('savoirplus')[0].getAttribute('url');
		img_list[i] = {
			'title': title,
			'legende': legende,
			'src': big_img,
			'url': savoir_plus,
		}
        }
	replaceFlash(img_list);
}

//http://medias.lemonde.fr/mmpub/edt/pf_jour/xml/20051212/portfolio_720409.xml

//http://xhtml.net/Le Monde Portfolio: http://medias.lemonde.fr/mmpub/edt/pf_jour/xml/20051213/portfolio_720797.xml

theFlash = document.getElementById('portfolio');
if (theFlash) {
	theFlash.style.visibility = 'hidden';
	var allParams, thisParam;
	allParams = document.evaluate(
		'//param[@name]',
		document,
		null,
		XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
		null);
	for (var i = 0; i < allParams.snapshotLength; i++) {
		thisParam = allParams.snapshotItem(i);
		// do something with thisLink
		if (thisParam.getAttribute('name') == 'movie') {
			var theUrl = thisParam.getAttribute('value');
			var index = theUrl.indexOf("source=");
			theUrl = theUrl.substring(index+7);
			while (theUrl.indexOf("%3A") > -1)
				theUrl = theUrl.replace('%3A', ':');
			while (theUrl.indexOf("%2F") > -1)
				theUrl = theUrl.replace('%2F', '/');
			GM_log(theUrl);
			GM_xmlhttpRequest({
				method: 'GET',
				url: theUrl,
				headers: {
					'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey',
					'Accept': 'application/atom+xml,application/xml,text/xml',
				},
				onload: parseXmlResponse,
			});
		}
	}
}
