/* 
	*** Javascript Random Inline content by folder w/o AJAX or XML requests ***

	Example of the content on page:
		<div id="profile"></div>
		<div id="bin" style="DISPLAY: none;">***BIN_ID:{3FE11C84-CF88-476C-BF56-FD2B02BD3F60}***</div>
	
	For the above demo, below are the global variable to change:
		binContainerId = 'bin';
		displayId = 'profile';
		itemClass = 'ProfileItem';
		itemTagName = 'div';
*/

var binContainerId = 'bin';			// THIS IS NOT THE BIN ID, this is the Id of the div tag that holds the bin on the page
var displayId = 'profile';			// Id of element that will hold the random item
var itemClass = 'ProfileItem';  	// This is the class on the tag surrounding each element in the content library folder
var itemTagName = 'div';			// tag holding each item in the content folder
var cI = 0;							// Starting item. 0 is the beginning
/* 
var speed = 5000;					// Speed... 2000 = 2 seconds

*/
addLoadListener(pickAndDisplayItem);

var items;

function pickAndDisplayItem() {
	items = parseBin();
	cI = Math.floor(Math.random()*(items.length)); // Random code... comment out if you like
	display(cI);
	//setInterval(timer,speed);
}
/* 
function timer(){
	if(cI == (items.length-1)) {
		cI = 0;
	} else {
		cI++;
	}
	display(cI);
}
*/
function display(i) {
	document.getElementById(displayId).innerHTML = items[i].innerHTML;
}

function parseBin() {
	var profileNode = document.getElementById(binContainerId);
	var rawBin = profileNode.innerHTML;
	rawBin.replace("\'","'");
	// logic to split up content listing in bin
	items = getElementsByClassName(document, itemTagName, itemClass);
	return items;
}

function getElementsByClassName(oElm, strTagName, strClassName){
    var arrElements = (strTagName == "*" && document.all)? document.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++){
        oElement = arrElements[i];
        if(oRegExp.test(oElement.className)){
            arrReturnElements.push(oElement);
        }
    }
    return (arrReturnElements)
}

function addLoadListener(fn)
{
	if (typeof window.addEventListener != 'undefined')
	{
		window.addEventListener('load', fn, false);
	}
 	else if (typeof document.addEventListener != 'undefined')
 	{
   		document.addEventListener('load', fn, false);
	}
 	else if (typeof window.attachEvent != 'undefined')
 	{
   		window.attachEvent('onload', fn);
 	}
 	else
 	{
   		var oldfn = window.onload;
  	 	if (typeof window.onload != 'function')
   		{
   		  window.onload = fn;
   		}
   		else
   		{
     		window.onload = function()
     	{
		oldfn();
		fn();
     };
   }
 }
}