/****************************************
* University of Dayton
* JavaScript effects for News pages
****************************************/
var selectedRadio = "nothing";


/*-----------------------------------------------------------------------------------------------
 * Change Log: 
 * oscar (2/19/07) - added filterByInTheNews related vars and code to setupNewFilterBox
 *---------------------------------------------------------------------------------------------*/

// ---------------------------------------------------------------------------
// Extend moo.fx Fx.Height effect to support setting on/off instead of just
// toggling
// ---------------------------------------------------------------------------
Object.extend(Fx.Height.prototype, {
	toggleOn: function() {
		this.custom(0, this.element.scrollHeight);
	},

	toggleOff: function() {
		this.custom(this.element.offsetHeight, 0);
	}
});

// Show/hide dropdown menu options for News Archive
// page based on whether the News or Publications
// radio button is selected
// ==================================================

function setupNewFilterBox() {
	// check for filter box
	if($('filter-box')) {
                $('jsForm').style.display = "block";
		newsRadio = $('filterByNews');
		pubsRadio = $('filterByPublications');
                inTheNewsRadio = $('filterByInTheNews'); //changelog.oscar (2/19/07)

		newsMenus = $('newsFilters');
		pubsMenus = $('publicationsFilters');
                inTheNewsMenus = $('inTheNewsFilters'); //changelog.oscar (2/19/07)
		
		toggleNews = new Fx.Height(newsMenus, {duration: 350});
		togglePubs = new Fx.Height(pubsMenus, {duration: 350});
                toggleInTheNews = new Fx.Height(inTheNewsMenus, {duration: 350}); //changelog.oscar (2/19/07)
		
		if(!newsRadio.checked) {
			toggleNews.hide();
		}
		if(!pubsRadio.checked) {
			togglePubs.hide();
		}

                if(!inTheNewsRadio.checked) { //changelog.oscar (2/19/07)
                        toggleInTheNews.hide();
                }

                function getRadioHistory() {
                    var val = true; 

                    if( newsRadio.checked) {
                           if(selectedRadio == "OnlineNews") val = false;

                           selectedRadio = "OnlineNews";
                    }
   
                    if( pubsRadio.checked) {
                           if(selectedRadio == "Publications") val = false;

                           selectedRadio = "Publications";
                    }
   
                    if( inTheNewsRadio.checked) {
                           if(selectedRadio == "InTheNews") val = false;

                           selectedRadio = "InTheNews";
                    }
                    
                    return val;
                }
	
		function clickNews() {
                       if( getRadioHistory() ){
                          toggleNews.toggleOn();
			  togglePubs.toggleOff();
			  toggleInTheNews.toggleOff(); //changelog.oscar (2/19/07)
                       }
		}
		
		function clickPubs() { 
                       if( getRadioHistory() ){
			  togglePubs.toggleOn();
			  toggleNews.toggleOff();
			  toggleInTheNews.toggleOff(); //changelog.oscar (2/19/07)
                       }
		}
		
		function clickInTheNews() { //changelog.oscar (2/19/07)
                       if( getRadioHistory() ){
			  toggleInTheNews.toggleOn();
			  togglePubs.toggleOff(); 
			  toggleNews.toggleOff();
                       }
		}

		Event.observe(newsRadio, "click", function() { clickNews();	}, false);
		Event.observe(pubsRadio, "click", function() { clickPubs();	}, false); 
		Event.observe(inTheNewsRadio, "click", function() { clickInTheNews();	}, false); //changelog.oscar (2/19/07)
	}
}

// Setup a slideshow of news features
//  - specify fade speed (in milliseconds) below
//  - specify slide duration (in seconds) below
// ==================================================



// Set it up when the page loads
function setupNewsFeatures() {
	var fadeSpeed = 1000;	// set the speed of the fade transition effect (milliseconds)
	var duration = 6;		// set the duration of each slide (seconds)
	// check for features
	if(newsFeaturesContainer = $('news-features')) {
		if(newsFeatures = document.getElementsByClassName('news-feature', newsFeaturesContainer)) {
			numFeatures = newsFeatures.length;	// count features
			
			// only do the slideshow if there is more than one feature
			if(numFeatures > 1) {
				transitions = new Array(numFeatures);	// keep each fade effect in an array
				current = 0;				// keep track of the feature being displayed
				transitioning = false;			// keep track of whether the slide is changing

				// create a fade effect for each feature
				for(i=0; i<numFeatures; i++) {
					feature = $('news-feature-'+(i+1));
					feature.style.display = 'block';	// all but the first feature need to be unhidden
					transitions[i] = new Fx.Opacity(feature, { duration: fadeSpeed, onStart: function() { transitioning = true; }, onComplete: function() { transitioning = false; } }); // create the fade
					
					// hide all but the first feature
					if(i != 0) {
						transitions[i].hide();
					}
				}				

				// create the timer to automatically advance the slideshow
				timer = new PeriodicalExecuter(nextFeature, duration);
				
				// show the slideshow controls (hidden at first)
				$('news-features-controls').style.display = 'block';
				setCurrentIndicator();
				
				// set up the play/pause toggle link
				Event.observe($('news-features-play'), 'click', togglePlaying, false);
				
				// set up the manual next link
				Event.observe($('news-features-next'), 'click', manualNext, false);
			}
		}
	}
}

// Advance the slideshow
function nextFeature() {
	// fade out the displayed feature
	transitions[current].toggle();
	
	// pick the next feature (go back to 0 if at the end, other wise increment)
	if(current == (numFeatures-1)) {
		current = 0;
	}
	else {
		current++;
	}
	
	// turn on the new feature
	transitions[current].toggle();
	
	// update current indicator
	setCurrentIndicator();
}

// Update the current slide indicator
// - If paused is set to true, add (Paused) at the end
function setCurrentIndicator(paused) {
	$('news-features-current').innerHTML = (current+1)+' of '+numFeatures+(paused ? (' (paused)') : '');
}

// Toggle playing of the slideshow
function togglePlaying(e) {
	toggleLink = $('news-features-play');

	// playing -> paused
	if(timer.currentlyExecuting == false) {
		toggleLink.innerHTML = 'Play'	// Change link text to "Play"
		setCurrentIndicator(true);		// add (Paused) to current indicator
	}
	
	// paused -> playing
	else {
		toggleLink.innerHTML = 'Pause';	// Change link text to "Pause"
		setCurrentIndicator();				// remove (Paused) from current indicator
	}

	// toggle playing
	timer.currentlyExecuting = !timer.currentlyExecuting;

	if(e) Event.stop(e);
}

// Manually advance to the next feature by clicking the "Next >>" link
function manualNext(e) {
	if(e) Event.stop(e);
	
	// if the slideshow is playing, pause it
	if(timer.currentlyExecuting == false) {
		togglePlaying();
	}
	
	if(!transitioning) { // don't advance if we're already fading
		// advance to the next feature (and indicate paused status)
		nextFeature();
		setCurrentIndicator(true);
	}
}

Event.observe(window, 'unload', function() {duration = 0, timer = null}, false);

Event.observe(window, 'load', function() {
	setupNewsFeatures();
	setupNewFilterBox();
}, false);

