/*******************************************************
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																	| default
---------------------------------------------------------------------------------------------------------------
*parentEleID      | The ID of the element containing the children to randomize/flip through.	| NONE REQUIRED
childElementType  | The element type of the children.											| div
isRand            | If the script should randomly pick the first child to show.					| true
defaultItemIndex  | If isRand is false you can set the first item you would like to show.		| 1
**transition      | Type of transition between objects. Options are: none, fade.				| none
**duration        | Transition duration (seconds).												| 0.5
**Min             | Transition min. Note case.													| 0.0
**Max             | Transition max. Note case.													| 1.0

* - Required parameter.
** - Require scriptaculous js library.
	
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);
	this.transition = param_default("transition", "none");
	this.duration = param_default("duration", 0.5);
	this.Min = param_default("Min", 0.0);
	this.Max = param_default("Max", 1.0);
	
	if(this.parentEleID == null){
		alert("New contentSelector object could not be created. You must specify the parentEleID variable.");
		return;
	}
	
	this.firstLoad = true;
	this.numItems = 0;
	this.currentItemIndex = 0;
	this.currentItemObj;
	this.targetParent = document.getElementById(this.parentEleID);
	this.Rand = contentSelector.Rand;
	this.nextItem = contentSelector.nextItem;
	this.previousItem = contentSelector.previousItem;
	
	for(i=0; i<this.targetParent.childNodes.length; i++){
		if(this.targetParent.childNodes[i].nodeName.toUpperCase() == this.childElementType.toUpperCase()){
			this.numItems++;
			if(this.targetParent.childNodes[i].id == ""){
				this.targetParent.childNodes[i].id = "CS_"+this.parentEleID+"_"+this.numItems;
			}
			this.targetParent.childNodes[i].style.display = "none";
		}
	}
	
	if(this.isRand){
		this.Rand();
	}else{
		this.nextItem(this.defaultItemIndex);
	}
}

contentSelector.Rand = function(){
	var randNum = Math.ceil(Math.random() * this.numItems);
	this.currentItemIndex = randNum-1;
	
	this.nextItem();
}

contentSelector.nextItem = function(index){
	var index = (typeof index == "undefined")?this.currentItemIndex:index-1;
	var nextNum = (index >= this.numItems)?1:index+1;
	this.currentItemIndex = 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.currentItemIndex){
				var nextItemObj = this.targetParent.childNodes[i];
			}
		}
	}
	
	if(this.firstLoad){
		nextItemObj.style.display = "block";
		this.firstLoad = false;
	}else{
		if(this.transition == "none"){
			this.currentItemObj.style.display = "none";
			nextItemObj.style.display = "block";
		}
		else if(this.transition == "fade"){
			var currentItemId = this.currentItemObj.id;
			var stoMin = this.Min;
			var stoMax = this.Max;
			var stoDuration = this.duration;
			
			new Effect.Opacity(this.currentItemObj.id, { from: this.Max, to: this.Min, duration: this.duration });
			
			setTimeout(function(){
				$(nextItemObj.id).setOpacity(0.0);
				$(currentItemId).style.display = "none";
				$(nextItemObj.id).style.display = "block";
				new Effect.Opacity(nextItemObj.id, { from: stoMin, to: stoMax, duration: stoDuration });
			}, this.duration*1000);
		}
	}
	
	this.currentItemObj = nextItemObj;
}

contentSelector.previousItem = function(index){
	var index = (typeof index == "undefined")?this.currentItemIndex:index-1;
	var prevNum = (index <= 1)?this.numItems:index-1;
	this.currentItemIndex = prevNum;
	
	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.currentItemIndex){
				var previousItemObj = this.targetParent.childNodes[i];
			}
		}
	}
	
	if(this.firstLoad){
		previousItemObj.style.display = "block";
		this.firstLoad = false;
	}else{
		if(this.transition == "none"){
			this.currentItemObj.style.display = "none";
			previousItemObj.style.display = "block";
		}
		else if(this.transition == "fade"){
			var currentItemId = this.currentItemObj.id;
			var stoMin = this.Min;
			var stoMax = this.Max;
			var stoDuration = this.duration;
			
			new Effect.Opacity(this.currentItemObj.id, { from: this.Max, to: this.Min, duration: this.duration });
			
			setTimeout(function(){
				$(previousItemObj.id).setOpacity(0.0);
				$(currentItemId).style.display = "none";
				$(previousItemObj.id).style.display = "block";
				new Effect.Opacity(previousItemObj.id, { from: stoMin, to: stoMax, duration: stoDuration });
			}, this.duration*1000);
		}
	}
	
	this.currentItemObj = previousItemObj;
}