//-- Odiogo Listen Button - Additional Javascript File
//-- This is to be included in the main remote javascript file
//-- v1.14
//-- Copyright (c) 2008 Odiogo. All rights reserved.

// Get array of elements by class name. From: http://www.dustindiaz.com/getelementsbyclass/
function odiogo_getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if (node == null)
		node = document;
	if (tag == null)
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}


// Loop through all placeholders and add buttons
function odiogo_parse_document() {
	// Replace the "Subscribe to this podcast" in <div id="odiogo_subscribe_placeholder">
	var odiogo_subscribe = document.getElementById('odiogo_subscribe_placeholder');
	if (odiogo_subscribe) odiogo_subscribe.innerHTML = showOdiogoSubscribeButton_str (_odiogo_directory_name)

	// Get all <div class='odiogo_placeholder'>
	var odiogo_divs = odiogo_getElementsByClass('odiogo_placeholder');

	// Get all divs, add button and the frame
	for (var i = 0; i < odiogo_divs.length; i++) {
		var title = odiogo_divs[i].title;
		var id =  odiogo_divs[i].id;
		var obj = document.getElementById (id);
		if (obj) {
			obj.innerHTML = showOdiogoReadNowButton_str (_odiogo_feed_id, title, id , 290, 55) + '<br/>' + showInitialOdiogoReadNowFrame_str (_odiogo_feed_id, id , 290, 0);
		}
	}
}

/*
 *
 * ContentLoaded.js
 *
 * Author: Diego Perini (diego.perini at gmail.com)
 * Summary: Cross-browser wrapper for DOMContentLoaded
 * Updated: 05/10/2007
 * License: GPL/CC
 * Version: 1.0
 *
 * Notes:
 *
 *  based on code by Dean Edwards and John Resig
 *  http://dean.edwards.name/weblog/2006/06/again/
 *	http://javascript.nwbox.com/ContentLoaded/
 *
 */

// @w	window reference
// @f	function reference
function ContentLoaded (w, fn) {
	var d = w.document,
		u = w.navigator.userAgent.toLowerCase();

	function init(e) {
		if (!arguments.callee.done) {
			arguments.callee.done = true;
			fn(e);
		}
	}

	// konqueror/safari
	if (/khtml|webkit/.test(u)) {

		(function () {
			if (/complete|loaded/.test(d.readyState)) {
				init('poll');
			} else {
				setTimeout(arguments.callee, 10);
			}
		})();

	// internet explorer all versions
	} else if (/msie/.test(u) && !w.opera) {

		(function () {
			try {
				d.documentElement.doScroll('left');
			} catch (e) {
				setTimeout(arguments.callee, 10);
				return;
			}
			init('poll');
		})();
		d.attachEvent('onreadystatechange',
			function (e) {
				if (d.readyState == 'complete') {
					d.detachEvent('on'+e.type, arguments.callee);
					init(e.type);
				}
			}
		);

	// browsers having native DOMContentLoaded
	} else if (d.addEventListener &&
		(/gecko/.test(u) && parseFloat(u.split('rv:')[1]) >= 1.8) ||
		(/opera/.test(u) && parseFloat(u.split('opera ')[1]) > 9)) {

		d.addEventListener('DOMContentLoaded',
			function (e) {
				this.removeEventListener(e.type, arguments.callee, false);
				init(e.type);
			}, false
		);

	// fallback to last resort
	} else {

		// from Simon Willison
		var oldonload = w.onload;
		w.onload = function (e) {
			if (typeof oldonload == 'function') {
				oldonload(e || w.event);
			}
			init((e || w.event).type);
		};

	}
}

ContentLoaded (window, odiogo_parse_document);
