$(document).ready(function(){
	
	$('#homepage_photo').innerfade({
		animationtype: 'fade',
		speed: 'slow',
		timeout: 8000,
		type: 'random_start',
		containerheight: '267px'
	});
	
	setClassLinks();
	
	//TABLE SORTER
	//sort by last name in the name field
	var myTextExtraction = function(node){  
		// extract data from markup and return it 
		if(name = $(node).find('span.last_name').html()){
			return name;
		} else {
			return $(node).html();
		}
	}
	$("table").tablesorter({
		textExtraction: myTextExtraction,

        // pass the headers argument and assing a object 
        headers: { 
            // assign the secound column (we start counting zero) 
            0: { 
                // disable it by setting the property sorter to false 
                sorter: false 
            }
        } 
    });
	
	//FACEBOX
	$('a[rel*=facebox]').facebox();	

	//SLIDER FOR ABOUT SLIDE SHOWS
	$('#slide_holder').serialScroll({
		items:'li',
		prev:'img.prev',
		next:'img.next',
		axis:'x',
		offset:0,
		start:0,
		duration:1200,
		force:true,
		step:4,
		stop:true,
		cycle:true,
		jump:true //click on the images to scroll to them
	});

	//set the class for all of the submit buttons on the right side
	$("#right :submit").addClass("submit");

	//TEXT REPLACEMENT FOR CHECK RECIEPT FORM
	$("#check_reciept :text").each(function(){
		if($(this).attr('value')){
			$('span#'+$(this).attr('id')+'_text').html($(this).attr('value')).addClass('filled');
		}
	});	
	$("#check_reciept").find(":text").keyup(function(){
		$('span#'+$(this).attr('id')+'_text').html($(this).attr('value')).addClass('filled');
	});

	
	//AUTOCALCULATE FOR PAYMENT REQUEST
	if($('div.Payment_Request_Form').length){
		$('input#total_expenses, input#remaining_balance, input#final_balance').before('$');
		
		// figure total expenses, remaining balance, and final balance
		$('input[name^="exp_"], input#financial_aid_amount, input#cayf_award_amount').bind("keyup",function(){
			var total = $('input[name^="exp_"]').sum();
			var aid = $('input#financial_aid_amount').parseNumber();
			var cayf_award = $('input#cayf_award_amount').parseNumber();

			$('input#total_expenses').val(total);
			$('input#remaining_balance').val(total - aid);
			$('input#final_balance').val(total - aid - cayf_award);
		});

		$('div.Payment_Request_Form form').bind('submit', function(){
			//quickly enable the auto-totaling fields
			$('input#total_expenses, input#remaining_balance, input#final_balance').removeAttr('disabled');
		});
	}

	//NAV
	$('#large_nav li.top_level').hover(
		function(){
			$(this).children('.sub_level').slideDown('fast');
		},
		function(){
			$(this).children('.sub_level').slideUp('fast');
		}
	);


	//MAILING LIST
	$('#frm_mailing_list input').each(function(){
		$(this).example(function() {
			return $(this).attr('title'); 
		});
	});
	
	$('#frm_mailing_list input.hide').hide();
	$('#frm_mailing_list input').focus(function(){
		$('#mailing_list').stop();
		$('#frm_mailing_list input.hide').show('fast');
	}).blur(function(){
		$('#mailing_list').animate({opacity:1},1500, function(){
			$('#frm_mailing_list input.hide').hide('fast');	
		});
	});

	//form submission
	$('#frm_mailing_list').submit(function(){
		$('#ml_holder').slideUp('fast');
		$('#ml_msg').slideDown('fast');
		frm_values = $(this).serialize();

		$.ajax({
			data:frm_values,
			type:"POST",
			url:"insert_to_mailing_list.php",
			success: function(data, textStatus){
				$('#ml_msg').html(data);				
			},
			error: function (XMLHttpRequest, textStatus, errorThrown){
				$('#ml_msg').html(textStatus+'<br />'+errorThrown);
			}
		});

		return false;
	});

	
	if ($.browser.msie && $.browser.version == 6.0 && $('#left').height()<850){
		$('#left').height(850);
	}

	resizeSides();
});//jquery document ready

function reloadCalendar(url){
	$.get(url, function(data){
	  $("#calendar_area").html(data);
	});
}

function resizeSides(){
	//make the left and right columns the same height
	if(document.getElementById("right")){
		
		var r_height = $("#right").height() + $('#quote').height() + 20;
		var l_height = $("#left").height();
				
		new_height = Math.max(r_height,l_height);
		$("#right").height(new_height);
		$("#left").height(new_height);
		
	}
	//alert("hit");
}

function setClassLinks(){
	//set class for links to external sites and other document types
	$('#main a[href^="http"]').addClass('external').attr('target', '_blank');
	$('#main a[href$=".pdf"]').addClass('pdf').attr('target','_blank');
	$('#main a[href$=".ppt"]').addClass('powerpoint').attr('target','_blank');
	$('#main a[href$=".doc"]').addClass('word').attr('target','_blank');
	$('#main a[href$=".xls"]').addClass('excel').attr('target','_blank');
	$('#main a[href$=".csv"]').addClass('excel').attr('target','_blank');
}

/*-------------------------------------------------------------------- 
 * JQuery Plugin: "EqualHeights"
 * by:	Scott Jehl, Todd Parker, Maggie Costello Wachs (http://www.filamentgroup.com)
 *
 * Copyright (c) 2008 Filament Group
 * Licensed under GPL (http://www.opensource.org/licenses/gpl-license.php)
 *
 * Description: Compares the heights or widths of the top-level children of a provided element 
 		and sets their min-height to the tallest height (or width to widest width). Sets in em units 
 		by default if pxToEm() method is available.
 * Dependencies: jQuery library, pxToEm method	(article: 
		http://www.filamentgroup.com/lab/retaining_scalable_interfaces_with_pixel_to_em_conversion/)							  
 * Usage Example: $(element).equalHeights();
  		Optional: to set min-height in px, pass a true argument: $(element).equalHeights(true);
 * Version: 2.0, 08.01.2008
--------------------------------------------------------------------*/

$.fn.equalHeights = function(px) {
	$(this).each(function(){
		var currentTallest = 0;
		$(this).children().each(function(i){
			if ($(this).height() > currentTallest) { currentTallest = $(this).height(); }
		});
		if (!px || !Number.prototype.pxToEm) currentTallest = currentTallest.pxToEm(); //use ems unless px is specified
		// for ie6, set height since min-height isn't supported
		if ($.browser.msie && $.browser.version == 6.0) { $(this).children().css({'height': currentTallest}); }
		$(this).children().css({'min-height': currentTallest}); 
	});
	return this;
};
