// Site Featured Selector
// Loops through images provided and opens associated url when image is clicked on

wsd_FeaturedRotators = [];

function wsd_StartFeaturedRotator(name, containerid, prefix, delay, overlayid){
	wsd_FeaturedRotators[name] = new wsd_FeaturedRotator(name, containerid, prefix, delay, overlayid);
}

// define the selector
// name is a name, so more than one can be active on a page
// containerid is id of images container div or table cell
// prefix is the prefix of the div ids sequence of divs to be extracted ie. cdiv
//		a div sequence can be anything that starts with the prefix and ends with '-identifier'
//		where the identifier is numeric and unique and no other hyphens are in the prefix
//		Example sequence contentdiv-1, contentdiv-2 would be a numerical sequence.
//		The prefix must also be unique inside the container element.
// delay is the delay in milliseconds between fades
// overlayid is the overlay container to assign the 
// The container must be a div or table cell that contains the divs which contain the links and images
// This makes it so that content of divs is swapped, making this more universal

// Usage:
/*
	Usually on page load add
	wsd_StartFeaturedRotator(name, containerid, prefix, delay);

	This will create the rotator and start it.
	To stop from a link or something use:
	wsd_FeaturedRotators[name].stop();

	To display a specific div use:
	wsd_FeaturedRotators[name].show(ind);
	where name is the name of the rotator you creaded and ind is the index number to show.
	Note: The index number is an array index starting from 0 and ending 1 less than the number of swap divs

	Here is a sample implementation and controls

	<html>
<head>
<title>blah</title>
<script type="text/javascript" src="{$def_path}js/prototype.js"></script>
<script type="text/javascript" src="{$def_path}js/scriptaculous.js"></script>
<script type="text/javascript" src="{$def_path}js/wsd_featuredrotator.js"></script>

<style type="text/css">
{literal}
div#rotateContainer {
	position: relative;
	width: 200px;
	height: 80px;
	border: 1px solid black;
}
#links {
	position: relative;
	top: 40;
}

.lnkimg {
	position: absolute;
	top: 0;
	width: 100px;
	height: 20px;
	border: 1px solid blue;
}
{/literal}
</style>
</head>

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-3782916-1");
pageTracker._initData();
pageTracker._trackPageview();
</script>



<body style="font-family: Arial" onload="wsd_StartFeaturedRotator('rot1', 'rotateContainer', 'swap', 3000)">

<div id="rotateContainer">
	<div id="swap-1" class="lnkimg"><a href="ln1">Hello</a></div>
	<div id="swap-2" class="lnkimg" style="display: none;"><a href="ln2">There</a></div>
	<div id="swap-3" class="lnkimg" style="display: none;"><a href="ln3">All u</a></div>
	<div id="links">
		<a href="#" onClick="return wsd_FeaturedRotators['rot1'].show(0)">1</a> |
		<a href="#" onClick="return wsd_FeaturedRotators['rot1'].show(1)">2</a> |
		<a href="#" onClick="return wsd_FeaturedRotators['rot1'].show(2)">3</a> |
		<a href="#" onmouseover="wsd_FeaturedRotators['rot1'].stop()" onmouseout="wsd_FeaturedRotators['rot1'].start()">Pause</a>


	</div>
</div>

</body>
</html>

*/

function wsd_FeaturedRotator(name, containerid, prefix, delay, overlayid){
	this.name = name;
	this.containerid = containerid;
	this.overlayid = overlayid;
	this.delay = delay;
	this.prefix = prefix;
	this.swapdivs = [];
//	this.swapurls = [];
	this.current = 0;
	this.timer = 0;

	var container = $(containerid);
	// build array of divs to swap, and urls to swap
	var descendants = container.descendants();

	for(var i = 0; i < descendants.length; i++){
		var id = descendants[i].id;
		var parts = id.split('-');
		if(parts[0] == this.prefix){
			this.swapdivs[this.swapdivs.length] = id;
		}		
		
//		var obj2 = descendants[i];
//		var descs = obj2.descendants();
//		for(var j = 0; j < descs.length; j++) {
//			var aid = descs[j].id;
//			var hrefparts = aid.split('-');
//			if(hrefparts[0] == 'swaplink') {
//				var myurl = descs[j].href;
//				this.swapurls[this.swapurls.length] = myurl;
//			}	
//		}

	}

	this.start = function(){
		clearTimeout(this.timer);
		this.timer = setTimeout("wsd_FeaturedRotators['"+this.name+"'].next()", this.delay);
		return false;
	};

	this.stop = function(){
		clearTimeout(this.timer);
		return false;
	};

	this.next = function(){
		var current = this.current;
		var next = this.current + 1;
		if(next >= this.swapdivs.length)
			next = 0;
		Effect.Fade(this.swapdivs[current]);
		Effect.Appear(this.swapdivs[next]);
		this.current = next;
		setAltTitle(this.current + 1);
		this.start();
		return false;
	};

	this.prev = function(){
		var current = this.current;
		var prev = this.current - 1;
		if(prev < 0)
			 prev = this.swapdivs.length - 1;
		Effect.Fade(this.swapdivs[current]);
		Effect.Appear(this.swapdivs[prev]);
		this.current = prev;
		setAltTitle(this.current + 1);
		this.start();
		return false;
	};

	this.show = function(ind){
		if(ind < 0 || ind > this.swapdivs.length){
			alert("Specified index out of range: "+ind);
		} else {
			this.stop();
			if(this.current != ind){
				Effect.Fade(this.swapdivs[this.current]);
				Effect.Appear(this.swapdivs[ind]);
				this.current = ind;
			}
			this.start();
		}
		return false;
	}

	this.start();
}