/******************************
**	
**	News Story Rotator
**	filename: rotate-stories.js
**	v. 1.0
**	author: brian dichiara - http://www.briandichiara.com
**	created: 2008-04-16 16:47
**	last updated: 2008-04-18 11:00
**	
**	purpose: to rotate news stories on home page of website using a fade in/out effect.
**	notes: designed to work along with jQuery
**	
******************************/

var wait = 5000;


var num = 0;
var rotator;
var on = true;
var playing = false;
var object = 'news_feed';

var playButton = "<strong>&gt;</strong>";
var pauseButton = "<strong>||</strong>";

function rotateStories(){
	if(news_items.length > 0){ // make sure there are news items
		if($("#"+object).html() == ""){
			$("#"+object).html(news_items[num]); // change text
		}
		rotator = setTimeout("changeStory(news_items[num])", wait);
	}
}

jQuery.fn.fadeIn = function(speed, callback) { 
    return this.animate({opacity: 'show'}, speed, function() { 
        if (jQuery.browser.msie)  
            this.style.removeAttribute('filter');  
        if (typeof callback == 'function')  
            callback();  
    }); 
}; 
 
jQuery.fn.fadeOut = function(speed, callback) { 
    return this.animate({opacity: 'hide'}, speed, function() { 
        if (jQuery.browser.msie)  
            this.style.removeAttribute('filter');  
        if (typeof callback == 'function')  
            callback();  
    }); 
}; 
 
jQuery.fn.fadeTo = function(speed,to,callback) { 
    return this.animate({opacity: to}, speed, function() { 
        if (to == 1 && jQuery.browser.msie)  
            this.style.removeAttribute('filter');  
        if (typeof callback == 'function')  
            callback();  
    }); 
}; 

function changeStory(str){
	for(var i=0; i < news_items.length; i++){
		if($("#"+object).html() != str && i == num){
			playing = true;
			
			/* // turn off fade due to problems in IE6
			$("#"+object).queue(function(){
					$(this).fadeTo("normal",0); // fade out
					$(this).dequeue();
				} // end function
			); // end object queue  */
			
			$("#"+object).queue(function(){
					$(this).html(str); // change text
					$(this).dequeue();
				} // end function
			); // end object queue
			
			/* // turn off fade due to problems in IE6 
			$("#"+object).queue(function(){
					$(this).fadeTo("normal",1); // fade in
					$(this).dequeue();
				} // end function
			); // end object queue	*/
			
			i = news_items.length + 5;
		} // end if
	} // end for loop
	num = ((num + 1) < news_items.length) ? (num + 1) : 0;
	playing = false;
	
	if(on){
		rotateStories();
	}
} // end function

function browseNews(dir){
	clearTimeout(rotator);
	if(!playing){
		if(dir == "previous"){
			if(num == 0){
				num = news_items.length - 2;
			} else {
				num = ((num - 2) < 0) ? (news_items.length - 1) : (num - 2);
			}
		}
		// play newly set num
		var newStr = news_items[num];
		changeStory(newStr);
	}
	return false;
}

function playPause(){
	clearTimeout(rotator);
	if(on){
		on = false;
		// change pause button to play button
		$("#play-pause span").html(playButton);
		$("#play-pause span").removeClass();
		$("#play-pause span").addClass("play");
	} else {
		on = true;
		// play current num
		var newStr = news_items[num];
		changeStory(newStr);
		// change play button to pause button.
		$("#play-pause span").html(pauseButton);
		$("#play-pause span").removeClass();
		$("#play-pause span").addClass("pause");
	}
	return false;
}

$(document).ready(function(){
		rotateStories();
	} // end function
); // end ready
