//-- COMING UP SCROLLING FEATURE --

window.addEvent('load', function(){

displayTime = 15000; //the amount of time the full column of events stays onscreen before clearing

//var slideSize = $('slider').getSize();

// First determine the size of the main content area
var mcSize = $('maincontent').getSize();



//Deternmine if we are on the homepage, because the sizing function below does not apply if we are
if(!$('home')){

	//Determine if we are on a subpage by checking for the existence of an h3 tag inside of the rightdouble div
	if ($('rightdouble').getElement('h3')) {
	
	//if so, we need to subtract the height of the h3 element from the maincontent height
	//in order to properly size the scoller
	//If not, the height of the scroller should just be equal to the height of the main content
	
	var h3Size = $('rightdouble').getElement('h3').getSize();
	
		h3Height = h3Size.y;

	
	} else {
	
		h3Height = 0;
	}

} else {

	h3Height = 0;

}

//Now check for the presence of an h4 tag inside the scrolling column
//so we can include that value into our out-of-bounds calculations

if ($('leftsingle').getElement('h4')) {

	var h4Size = $('leftsingle').getElement('h4').getSize();
	
		h4Height = h4Size.y;
	
	} else {
	
		h4Height = 0;
	}


	mcHeight = mcSize.y - h3Height ;


//slideHeight = slideSize.size.y;


//set the height of the left column appropriately
$('leftsingle').setStyle('height',mcHeight + 'px');



//Resets the position all of the list items to off the bottom of the enclosing element
function resetPosition () {


	arrayPos =0;
	slidePoint = 0;
	i=0;
	prevPos = 0;

	cuEvents = $$('#slider dl.cuItem');
	
	cuEvents.each(function(element) {
	
		element.setStyle('top', mcHeight+'px');
	
	});


}


//This function is the main loop function using a periodical

function startLoop() {
period=250;
loop = comingUp.periodical(period,comingUp);
}


comingUp = function () {
	
		
		element = cuEvents[i];

		elSize = $(element).getSize();
		elHeight = elSize.y;

		//before we slide in the next element we need to check that it won't
		//push off the bottom of its enclosing element
		// you'll notice we need to subtract out the height of the h4 --if present-- to accurately calculate when
		//an item is pushed past the bottom of the column
	
		if ((slidePoint + elHeight) < mcHeight - h4Height) 
		
		
		{
			slideDivIn();
			
			i++;
		
		} else { 
		

		//if it does, it means we've reached the bottom of the column 
		//so slide all of them off the screen and reset the position
	
			$clear(loop);
	
			currentPos = i;
			
			slideDivsOut();
	
			prevPos = i;
		
		}

	

	//check to see if we've reached the end of the events listing Array
	//if so, slide the remaining items off the screen and then
	//reset everything to start the loop over again

	if (i >= cuEvents.length) {
	
		$clear(loop);
		
		currentPos = cuEvents.length;
	
		slideDivsOut();
	
		resetPosition.delay(displayTime + 500, resetPosition);

	} 
	
}	
	


//Slide the next element in to the 'slidepoint' which is a incrementing total of the height of
//each element that has been previously slid in.

function slideDivIn () {

		elSize = $(element).getSize();
		elHeight = elSize.y;
	
		var slideDiv = new Fx.Morph(element, {duration:550, transition:Fx.Transitions.Quart.easeOut, wait:false});
							 
		slideParam=slidePoint+"px";				
		slideDiv.start ({'top':slideParam});
	
		slidePoint += elHeight;

}


//We slide the columnfull of elements all off the top the screen
//by setting their 'top' parameter.  We know how many are on the screen
//from the 'prevPos' var that is incremented in the main loop

function slideDivsOut() {
	
	count=prevPos;
	
	
	do {
	
		exitElement = cuEvents[count];
		var slideOut = new Fx.Morph(exitElement, {duration:400, transition:Fx.Transitions.Quart.easeIn, wait:false});
		
		slideOut.start.delay (displayTime, slideOut, {'top':'-400px'});
		
		count++;
			
	} while (count < currentPos) 

	slidePoint = 0;
	
	startLoop.delay(displayTime + 500, startLoop);
	
	

}

if ($$('#slider dl.cuItem').length >=2) {
	resetPosition();
	startLoop.delay(1500, startLoop);
}

}); //close the load handler
