//-- Odiogo Listen Button - Additional Javascript File
//-- This is to be included in the main remote javascript file
//-- v1.12
//-- 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;
}

// Basic "Add Event on Load". From: http://simonwillison.net/2004/May/26/addLoadEvent/
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
}

// 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);
		}
	}
}

/* 	Replacement for window.onload (there is no reason to use window.onload to wait to do DOM traversal)
	By Dean Edwards/Matthias Miller/John Resig -- now implemented in jQuery
*/

// Function to be executed when the DOM is ready
function odiogo_DOMinit() {
	// 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 (_odiogotimer) clearInterval(_odiogotimer);
	// do our stuff
	odiogo_parse_document();
};

/* for Mozilla/Opera9 */
if (document.addEventListener) {
	document.addEventListener("DOMContentLoaded", odiogo_DOMinit, false);
}

/* for Internet Explorer */
/*@cc_on @*/
/*@if (@_win32)
	document.write("<scr" + "ipt id='__ie_odiogonload' defer=true " + "src=//:><\/script>");
	var script = document.getElementById("__ie_odiogonload");
	script.onreadystatechange = function() {
		if (this.readyState == "complete") {
			odiogo_DOMinit(); // call the onload handler
		}
	};
/*@end @*/

/* for Safari */
if (/WebKit/i.test(navigator.userAgent)) { // sniff
	var _odiogotimer = setInterval(function() {
		if (/loaded|complete/.test(document.readyState)) {
			odiogo_DOMinit(); // call the onload handler
		}
	}, 10);
}

/* Fallback for other browsers */
addLoadEvent(odiogo_DOMinit);
