// USAGE EXAMPLE:
//
//<script type="text/javascript" src="http://js.sapo.pt/SAPO/"></script>
//<script type="text/javascript" src="http://js.sapo.pt/SAPO/Utility/Crypto/"></script>
//<script type="text/javascript" src="http://js.sapo.pt/SAPO/Communication/Syndication/"></script>
//<script type="text/javascript">
//var FEED_CMP = 'http://infernogelado.blogs.sapo.pt/data/atom';
//var FEED_CMP_MAX = 5;
//var FEED_CONTENT = 1;
//</script>
//<script type="text/javascript" src="http://blogs.sapo.pt/feedworker.js"></script>

var SHOW_CONTENT = false;
if (typeof FEED_CONTENT != 'undefined') SHOW_CONTENT = true;

var MAX_ITEMS = 10;
if (typeof FEED_CMP_MAX != 'undefined') MAX_ITEMS = FEED_CMP_MAX;

if (typeof FEED_CMP == 'undefined') alert("ERROR: FEED_CMP param not passed!");

var REVERSE_CONTENT = false;
if (typeof FEED_CMP_REVERSE != 'undefined') REVERSE_CONTENT = true;

var bucket = SAPO.Utility.Crypto.md5(FEED_CMP);

document.write('<script language="javascript">\n');

document.write('function doOnLoad' + bucket + '(value)\n');
document.write('{\n');
document.write('    document.getElementById(\'bucket_' + bucket + '\').innerHTML = \'loading...<br />\';\n');
document.write('}\n');

document.write('function doOnComplete' + bucket + '(obj)\n');
document.write('{\n');
document.write('    var campo = document.getElementById(\'bucket_' + bucket + '\');\n');
document.write('    campo.innerHTML = \'\';\n');
document.write('    var title = false;\n');
document.write('    var desc = false;\n');
document.write('    var link = false;\n');
document.write('    var items = obj.rss.channel.item;');
document.write('    if (typeof items.join == "undefined") { items = [items] } else if (items.length > ' + MAX_ITEMS + ') { items = items.slice(0, ' + MAX_ITEMS + '); } ');
if (REVERSE_CONTENT) {
document.write('    items.reverse();');
}
document.write('    for(var i=0; i < items.length; i++) {\n');
document.write('            title = items[i].title;\n');
document.write('            link = items[i].link;\n');
document.write('            if (! title) { title = "Sem t&iacute;tulo"; };\n');
document.write('            campo.innerHTML += \'<a class="feed-title" href="\' + link + \'" target="_blank">\' + title + \'</a><br />\';\n');
if (SHOW_CONTENT) {
    document.write('                desc = items[i].description;\n');
    document.write('                campo.innerHTML += \'<span class="feed-content">\' + desc + \'</span><br />\';\n');
}
document.write('    }\n');
document.write('}\n');

document.write('function doOnTimeout' + bucket + '()\n');
document.write('{\n');
document.write('	document.getElementById(\'bucket_' + bucket + '\').innerHTML += \'Timeout ao ler o feed<br />\';\n');
document.write('}\n');

document.write('<\/script>\n');

document.write('<div class="feed-box" id="bucket_' + bucket + '"></div>\n');
document.write('<script language="javascript">\n');
document.write('var syndicationObj = new SAPO.Communication.Syndication();\n');
document.write('var id_' + bucket + ' = syndicationObj.push(\'http://services.sapo.pt/Transformer/RSSOrAtomToJSON?url=' + FEED_CMP + '\', {onComplete:doOnComplete' + bucket + ', onLoading:doOnLoad' + bucket + ', onTimeout:doOnTimeout' + bucket + '});\n');
document.write('syndicationObj.run(id_' + bucket + ');\n');
document.write('<\/script>\n');

