// add function createStories() to loadJS() in global_vars.js
// add function addPopup() to show all stories on mouseover (optional)

/*
Elements to style and position:
#multi_content
#controls
#cboxes
#cbox_[#]
(#cbox_[#]) .on
#mc_back
#mc_fwd
#more_link
*/

var container_div = "leftbanner"; //has the content elements
var module_class = "photo"; // class name of elements
var target_div = "photo_rotation"; //will recieve content and controls

var news_string = "View All News";
var news_page = "/news"; //url of more news page
var no_news = "There are no items at this time."; //message if there are no stoires

var random = 1; // starts at a random story if set to 1
var delay = 8000; //time between story swap (milliseconds)

var timer;
var timerRunning = 0;
var current_block;
var blocks = new Array();
var lks = new Array();

function addPopup(){
	var hideNews;

	document.getElementById(target_div).onmouseover = function(){
			clearTimeout(hideNews);
			document.getElementById(container_div).style.display='block';
			document.getElementById(container_div).style.top = (findPosY(document.getElementById(target_div)) - document.getElementById(container_div).scrollHeight) + "px";
			stopTimer();
	}
	document.getElementById(target_div).onmouseout = function(){
		hideNews = setTimeout( function(){ document.getElementById(container_div).style.display='none'; startTimer(); },250 );
	}
	
	document.getElementById(container_div).onmouseover = function(){
		clearTimeout(hideNews);
		document.getElementById(container_div).style.display='block';
		}
	document.getElementById(container_div).onmouseout = function(){
	 	hideNews = setTimeout( function(){ document.getElementById(container_div).style.display='none'; startTimer(); },250 );
	}
}


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"><</a>';
	var fwd_btn = '<a href="#" onclick="swapMulti(\'forward\'); startTimer(); return false;" id="mc_fwd">></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,'',function(){
				//clear alpha filter in IE, and reset styles
				if(isIE){$j('#multi_content2').attr('style','filter:alpha(opacity=100); position:absolute; top:0; left:0;');}
			});
		});
						
		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;
	}
}

