/*********************************************
>>>>>>>> Breaking News application <<<<<<<<<<<
**********************************************/

// Local app variables
var headline_count;     // Total number of headlines
var headline_interval;  // JS Interval object

var current = 0;  // Current headline index
var real = 1;     // Semantic value of current headline index (for output)
var prev = 0;     // Previous headline index


$(document).ready(
 function(){

	// Determine how many headlines there are, if any
	headline_count = $("#Breaking_News > #White_Border > #Headlines > .headline").size();
	
	// Only show box and perform operations if there are headlines
	if( headline_count != 0 ) {
		$("#Breaking_News").show(); 
		
		$("div.headline:eq("+current+")").css('top','7px');
		$("#Breaking_News > #White_Border > #Controls > #Count").html( "1 of " + headline_count );
		
  
  		if( headline_count > 1 ) {
			headline_interval = setInterval(next,5000); //time in milliseconds
			
	
			$('#Headlines').hover(
			 function() {
				clearInterval(headline_interval);
			 },
			 function() {
				headline_interval = setInterval(next,5000); //time in milliseconds
			 }
			);
		} else {
			
			// Hide the previous and next buttons, as well as counter
			$("#Breaking_News > #White_Border > #Controls > #Count").hide();
			$("#Breaking_News > #White_Border > #Controls > #Previous").hide();
			$("#Breaking_News > #White_Border > #Controls > #Next").hide();

			// Reposition the close button
			$("#Breaking_News > #White_Border > #Controls > #Close").css("margin-left","90px");

		}
		
		
		
		/*********************************
		>>>>>>>> Previous button <<<<<<<<<
		**********************************/
		// Click behavior
		$("#Breaking_News > #White_Border > #Controls > #Previous").click(
			function() { 
				clearInterval(headline_interval);
				previous();
				
				headline_interval = setInterval(next,5000);
			}
		);		


		// Mouseover / mouseout behavior
		$("#Breaking_News > #White_Border > #Controls > #Previous").hover(
			function() {
				$(this).attr("src","global/resources/images/_site/breaking/previous_on.jpg");
				$(this).css("cursor","pointer");
			},
			function() {
				$(this).attr("src","global/resources/images/_site/breaking/previous_off.jpg");
			}
		);
		
		
		/*********************************
		>>>>>>>>>> Next button <<<<<<<<<<<
		**********************************/
		// Click behavior
		$("#Breaking_News > #White_Border > #Controls > #Next").click(
			function() {
				clearInterval(headline_interval);
				next();
				
				headline_interval = setInterval(next,5000);
			}
		);		
		
		$("#Breaking_News > #White_Border > #Controls > #Next").hover(
			function() {
				$(this).attr("src","global/resources/images/_site/breaking/next_on.jpg");
				$(this).css("cursor","pointer");
			},
			function() {
				$(this).attr("src","global/resources/images/_site/breaking/next_off.jpg");
			}
		);
		
		
		/********************************
		>>>>>>>>> Close button <<<<<<<<<<
		*********************************/
		// Click behavior
		$("#Breaking_News > #White_Border > #Controls > #Close").click( 
			function() {
				$("#Breaking_News > #White_Border > #Headlines > .headline").each(
					function() {
						$(this).hide();
					}
				);
				
				$("#Breaking_News").css("margin-top","5px");
				$("#Breaking_News").slideUp(400);
			}
		);
		
		// Mouseover / mouseout behavior
		$("#Breaking_News > #White_Border > #Controls > #Close").hover(
			function() {  
				$(this).attr("src","global/resources/images/_site/breaking/close_on.jpg");  
				$(this).css("cursor","pointer");
			},
			function() {  $(this).attr("src","global/resources/images/_site/breaking/close_off.jpg"); }
		);		
		
	}
 }
);


/*******************************************************************
>>>>>>>> 'Next Headline' function <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
********************************************************************
 Cycles to the next headline
********************************************************************/
function next() {
	if( current != headline_count-1 ) {
		prev = current;      // update previous pointer
		current++;           // update current pointer
		real = current + 1;  // update real pointer
	} else {
		prev = headline_count - 1;
		current = 0;
		real = 1;
	}
	

	// Move previous headline up
	$("div.headline:eq(" + prev + ")").animate({top: -50},"slow", function() {
		$(this).css('top','210px');
	});
	

	// Bring up new headline
	$("div.headline:eq(" + current + ")").css("top","210px");
	$("div.headline:eq(" + current + ")").show().animate({top: 7},"slow");  
	

	// Update headline counter 
	$("#Breaking_News > #White_Border > #Controls > #Count").html( "" + real + " of " + headline_count );

}



/*******************************************************************
>>>>>>>> 'Previous Headline' function <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
********************************************************************
 Cycles to the previous headline
********************************************************************/
function previous() {
	// Update the various pointers
	if( current != 0 ) {
		prev = current;      // the current headline is now the previous one
		current--;           // advance current pointer
		real = current + 1;  // update the 'real' pointer
	} else {
		prev = 0; 
		current = headline_count - 1;
		real = headline_count;
	}
		
		
	// Animate the previous headline downward and out of site.
	// - callback function then moves the headline back above application viewing area
	$("div.headline:eq(" + prev + ")").animate({top: 50},"slow", function() {
		$(this).css('top','-210px');
	});

	// Animate the next headline downward
	$("div.headline:eq(" + current + ")").css("top","-210px");  // bug fix, resets positioning if user just switched from next() to previous()
	$("div.headline:eq(" + current + ")").show().animate({top: 7},"slow");  
	
	
	// Update headline counter 
	$("#Breaking_News > #White_Border > #Controls > #Count").html( "" + real + " of " + headline_count );
	
}