/*******************************************************
Author:			Tim Miller
Date:			7/16/2009
Filename:		contentSelector.js

-Constructor.
-Function to randomly select a child element inside a containing object to display.
-Function to flip through the child elements inside a containing object in order.
						
The "params" is a single object that can have the following properties:

prop. name        | description
-------------------------------------------------------------------------------------------------
*parentEleID      | The ID of the element containing the children to randomize/flip through.
childElementType  | The element type of the children.
isRand            | If the script should randomly pick the first child to show.
defaultItemIndex  | If isRand is false you can set the first item you would like to show.

* - Required parameter.
	
This can be called at any time in the body of the document after the parent element has been closed 
or onload of the window.

Examples:
	var inFocusStories = new contentSelector({
		parentEleID :	"inFocusContainer"
	};
OR
	var imgRandom = new contentSelector({
		parentEleID			:	"inFocusContainer2",
		childElementType	:	"img",
		isRand				:	false,
		defaultItemIndex	:	4
	});
*******************************************************/

function contentSelector(params){
	function param_default(pname, def){
		if(typeof params[pname] == "undefined") params[pname] = def;
		return params[pname];
	}
	
	this.parentEleID = param_default("parentEleID", null);
	this.childElementType = param_default("childElementType", "div");
	this.isRand = param_default("isRand", true);
	this.defaultItemIndex = param_default("defaultItemIndex", 1);
	
	if(this.parentEleID == null){
		alert("New contentSelector object could not be created. You must specify the parentEleID variable.");
		return;
	}
	
	this.numItems = 0;
	this.currentItem = 0;
	this.targetParent = document.getElementById(this.parentEleID);
	this.Rand = contentSelector.Rand;
	this.nextItem = contentSelector.nextItem;
	
	for(i=0; i<this.targetParent.childNodes.length; i++){
		if(this.targetParent.childNodes[i].nodeName.toUpperCase() == this.childElementType.toUpperCase()){
			this.numItems++;
		}
	}
	
	if(this.isRand){
		this.Rand();
	}else{
		this.nextItem(this.defaultItemIndex);
	}
}

contentSelector.Rand = function(){
	var randNum = Math.ceil(Math.random() * this.numItems);
	this.currentItem = randNum-1;
	
	this.nextItem();
}

contentSelector.nextItem = function(index){
	var index = (typeof index == "undefined")?this.currentItem:index-1;
	var nextNum = (index >= this.numItems)?1:index+1;
	this.currentItem = nextNum;
	
	var foundItems = 0;
	for(i=0; i<this.targetParent.childNodes.length; i++){
		if(this.targetParent.childNodes[i].nodeName.toUpperCase() == this.childElementType.toUpperCase()){
			foundItems++;
			if(foundItems == this.currentItem){
				this.targetParent.childNodes[i].style.display = "block";
			}else{
				this.targetParent.childNodes[i].style.display = "none";
			}
		}
	}
}
