// add function createStories() to loadJS() in global_vars.js
//set class of banner modules with content to "story"

/*
Elements to style and position:
#multi_content
#controls
#cboxes
#cbox_[#]
(#cbox_[#]) .on
#mc_back
#mc_fwd
#more_link
*/

var container_div = "listcon"; //has the content elements
var module_class = "eventcol"; // class name of elements
var target_div = "text1"; //will recieve content and controls

var news_string = "View More Events";
var news_page = "/page.cfm?p=58"; //url of more news page
var no_news = "There are no items at this time."; //message if there are no stoires

var random = 0; // starts at a random story if set to 1
var delay = 4000; //time between story swap (milliseconds)

var timer;
var timerRunning = 0;
var current_block;
var blocks = new Array();
var lks = new Array();


function createStories(){
	
	document.getElementById(target_div).innerHTML = "<div id=\"multi_content\"></div><div id=\"controls\"></div>";
	
	var controls = "";
	var stories = $j('.'+ module_class);
	var j=0;
	for(i=0;i<stories.length;i++){
			blocks[j] = stories[i].innerHTML;
			controls += "<a href=\"#\" id=\"cbox_"+j+"\" onclick=\"swapMulti(" +j+ ",\'cbox_" +j+ "\'); stopTimer(); return false;\">" + (j+1) + "</a> ";
			j++;
	}
	
	var back_btn = '<a href="#" onclick="swapMulti(\'back\'); startTimer(); return false;" id="mc_back">&lt;</a>';
	var fwd_btn = '<a href="#" onclick="swapMulti(\'forward\'); startTimer(); return false;" id="mc_fwd">&gt;</a>';
	
	if(news_string.length>1 ){
		news_lnk = '<a href="'+ news_page +'" id="more_link">'+news_string+'</a>';
	}else{
		news_lnk = '';
	}
	
	document.getElementById('controls').innerHTML = back_btn + '<span id="cboxes">' + controls + "</span>" + fwd_btn  + news_lnk ;
	
	lks = document.getElementById('controls').getElementsByTagName('a');
	
	if(random){
		rN = Math.round(Math.random() * (blocks.length-1) );
		swapMulti(rN,"cbox_"+rN);
	}else{
		swapMulti(0,'cbox_0');
	}
	
	startTimer();

}

function startTimer(){
	if(!timerRunning && blocks.length > 1){
		timer = setInterval('swapMulti("forward")', delay);
		timerRunning = 1;
	}
}

function stopTimer(){
		window.clearInterval(timer);
		timerRunning = 0;
}

function swapMulti(n,l){

	if( n == 'back' ){
		if(current_block==0){ n=blocks.length-1; l ="cbox_"+(blocks.length-1);
		}else{ n = current_block - 1; l = "cbox_"+n; }

	}else if( n == 'forward'){ if(current_block + 1 == blocks.length){ n=0; l = "cbox_0";
		}else{ n = current_block + 1; l = "cbox_"+n; }
	}
		
	if(blocks[n]){
		
		// fade out			

		$j('#multi_content').animate({opacity:'.1'},200,'',function(){
			$j('#multi_content').html( blocks[n] ).animate({opacity:'1'},150);
		});
						
		for(i=0;i<lks.length;i++){
			if( lks[i].id == l){ lks[i].className = "on";}else{ lks[i].className = "";}
		}
		
		current_block = n;
	}else{
		document.getElementById('multi_content').innerHTML = no_news;
	}
}
