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

$(document).ready(function(){
	headline_count = $("div.story").size();
	$("a#next").click(function() {headline_rotate();});
	$(".story:gt(0)").hide();
//  $(".story:eq(" + current_headline + ")").show();

// preload "loading" image
	jQuery("<img>").attr("src","private/dev/images/loading.gif");
});

// function that targets event calendar links to load within their div; 
// this function runs once when the calendar is initially loaded, and 
// again whenever new calendar content is loaded in (to retarget links)
function fixlinks() {
	$("#calendar .remote a").click(function() {
		$("#calendar").html('<p align="center"><img src="private/dev/images/loading.gif" alt="loading" id="loading" /></p>');
		$("#calendar").load(this.href,fixlinks); return false;
	});
	$("#calendar .remote a").href = "#events";
}

$("#calendar").ready(fixlinks);

function headline_rotate() {
  current_headline = (old_headline + 1) % headline_count; //remainder will always equal old_headline until it reaches headline_count - at which point it becomes zero. clock arithmetic
  $("div.story:eq(" + old_headline + ")").fadeOut("slow",function(){$("div.story:eq(" + current_headline + ")").fadeIn("slow");});
  old_headline = current_headline;
}

$.fn.ajaxSubmit = function(e) {
	/* Change a form's submission type to ajax */
	this.submit(function(){
		var params = {};

		$(this).find("select").each(function() {
			params[this.name] = this.options[this.selectedIndex].text;
		});

		$(this)
		.find("input[@checked], input[@type='text'], input[@type='hidden'], input[@type='password'], input[@type='submit'], textarea")
		.filter(":enabled")
		.each(function() {
			params[ this.name || this.id || this.parentNode.name || this.parentNode.id ] = this.value;
		});
		$("#TB_window").fadeOut("fast");
                $("#TB_load").css({display:"block"});

		$("#TB_ajaxContent").load(this.getAttribute("action") + "&call=ajax",params,function() {
			$("#TB_load").css({display:"none"}); 
			$("#TB_window").fadeIn("slow");;
			$("#requestform").ajaxSubmit();
                        if(TB_TYPE=='classroom_change' || TB_TYPE=='classroom') {
                            clrinputs(); selectinvalid(); setcollapse();
                        }
		});
		return false;
	});
	
	return this;
}
