/*
 * Base JavaScript Files to handle common tasks
 */

/* prepend article tools to clickability */
$(document).ready(function() {
	$('#article .clickability ul').append('<li class="clickabilitySave"><a href="#" onclick="return(ST());" onMouseOver="return(STMouseOver());" onMouseOut="return(STMouseOut());">Save</a></li><li class="clickabilityEmail"><a href="#" onclick="return(ET());" onMouseOver="return(ETMouseOver());" onMouseOut="return(ETMouseOut());">Email</a></li><li class="clickabilityPrint"><a href="#" onclick="return(PT());" onMouseOver="return(PTMouseOver());" onMouseOut="return(PTMouseOut());">Print</a></li><li class="rss"><a href="http://feeds.feedburner.com/HomecareMostRecent">RSS</a></li>'); 
});

/* add _blank to any link with class="popup" */
$(document).ready(function() { 
	$('a.popup').click(function() {
		this.target = "_blank";
	});
});

/* fix IE6's inability to have :hover on anything other than an anchor tag */
$(document).ready(function() { 
    $("#topNav li").bind("mouseenter mouseleave", function(){
        $(this).toggleClass("over");
    });
});


/*====
 * rotator
 *================================================ */

var headline_count;
var headline_interval;
var old_headline = 0;
var current_headline = 0;

$(document).ready(function(){
	$("div#topStories").removeClass('noscript'); 
	$("div#topStories").addClass("active");
	headline_count = $("div.topStoryItem").size();
	$("div.topStoryItem:eq("+current_headline+")").css('display','block');

	headline_interval = setInterval(headline_rotate,6000); //time in milliseconds

/* The old numbers based version */
/*
  $("div#topStories").append('<div id="topStoryButtons"><ul></ul></div>');
	$('div#topStories .topStoryItem h2').each(function(index) {
		var $sectionLabel = $(this).remove().text();
		$("div#topStoryButtons ul").append('<li class="' + $sectionLabel + '">' + $sectionLabel + '</li>');
		$("#topStoryButtons li:eq(0)").addClass('activeControls'); 
	});
	$("div#topStories").append('</ul></div>');
*/
 $("div#topStories").append('<div id="topStoryButtons"><a href="#" class="prev"><span>Prev</span></a><a href="#" class="next"><span>Next</span></a></div>');

	$("#topStoryButtons li:eq(0)").click(function() { 
		selected_headline = 0;
		$("div.topStoryItem:eq(" + current_headline + ")").hide();
		$("#topStoryButtons li:eq(" + current_headline + ")").removeClass('activeControls'); 
		$("#topStoryButtons li:eq(0)").addClass('activeControls'); 
		$("div.topStoryItem:eq(0)").show(); 
		current_headline = selected_headline;
		clearInterval(headline_interval);
		return false;
	});
	$("#topStoryButtons li:eq(1)").click(function() { 
		selected_headline = 1;
		$("div.topStoryItem:eq(" + current_headline + ")").hide();
		$("#topStoryButtons li:eq(" + current_headline + ")").removeClass('activeControls'); 
		$("#topStoryButtons li:eq(1)").addClass('activeControls'); 
		$("div.topStoryItem:eq(1)").show(); 
		current_headline = selected_headline;
		clearInterval(headline_interval);
		return false;
	});
	$("#topStoryButtons li:eq(2)").click(function() { 
		selected_headline = 2;
		$("div.topStoryItem:eq(" + current_headline + ")").hide();
		$("#topStoryButtons li:eq(" + current_headline + ")").removeClass('activeControls'); 
		$("#topStoryButtons li:eq(2)").addClass('activeControls'); 
		$("div.topStoryItem:eq(2)").show(); 
		current_headline = selected_headline;
		clearInterval(headline_interval);
		return false;
	});

	$("#topStories a.next").click(function(e) { 
    e.preventDefault();
		clearInterval(headline_interval);
    current_headline = (old_headline + 1) % headline_count; 
    $("div.topStoryItem:eq(" + old_headline + ")").hide();
    $("div.topStoryItem:eq(" + current_headline + ")").show(); 
    old_headline = current_headline;
		current_headline = selected_headline;
  });

	$("#topStories a.prev").click(function(e) { 
    e.preventDefault();
		clearInterval(headline_interval);
    current_headline = (old_headline % headline_count) - 1; 
  	if ( current_headline < 0 ) { 
      current_headline = headline_count -1;
  	} 
    $("div.topStoryItem:eq(" + old_headline + ")").hide();
    $("div.topStoryItem:eq(" + current_headline + ")").show(); 
    old_headline = current_headline;
		current_headline = selected_headline;
  });

});

function headline_rotate() {
	current_headline = (old_headline + 1) % headline_count; 
	$("div.topStoryItem:eq(" + old_headline + ")").hide();
	$("#topStoryButtons li:eq(" + old_headline + ")").removeClass('activeControls'); 
	$("#topStoryButtons li:eq(" + current_headline + ")").addClass('activeControls'); 
	$("div.topStoryItem:eq(" + current_headline + ")").show(); 
	old_headline = current_headline;
}

