$.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;
};

/* end jquery.equalheight.js */

$(document).ready(function() {
	
	if ( $("#contactForm").length > 0 ) {

		$("#contactForm").validate({
			errorPlacement: function(error, element) { 
				if ( element.is(":radio") ) {
					error.appendTo( element.parent().next().next() ); 
				}
	            else if ( element.is(":checkbox") ) {
					error.appendTo ( element.next() ); 
				}
	            else {
					error.prependTo( element.parent().next() ); 
				}
	        }
		});
		
	}
	
	if ( $("#OnlineApplication").length > 0 ) {

		$("#OnlineApplication").validate({
			errorPlacement: function(error, element) { 
				if ( element.is(":radio") ) {
					error.appendTo( element.parent().next().next() ); 
				}
			    else if ( element.is(":checkbox") ){ 
				    error.appendTo ( element.next() ); 
				}
			    else { 
				    error.prependTo( element.parent().next() ); 
			   }
			}
		});

	}
	
	if ( $("#volunteerSignup").length > 0 ) {

		$("#volunteerSignup").validate({
			errorPlacement: function(error, element) { 
				if ( element.is(":radio") ) {
					error.appendTo( element.parent().next().next() ); 
				}
			    else if ( element.is(":checkbox") ){ 
				    error.appendTo ( element.next() ); 
				}
			    else { 
				    error.prependTo( element.parent().next() ); 
			   }
			}
		});

	}
	
	if ( $("#requestMoreInfo").length > 0 ) {

		$("#requestMoreInfo").validate({
			errorPlacement: function(error, element) { 
				if ( element.is(":radio") ) {
			        error.appendTo( element.parent().next().next() ); 
				}
			    else if ( element.is(":checkbox") ) {
			        error.appendTo ( element.next() ); 
				}
			    else { 
				    error.prependTo( element.parent().next() );
			    }
			}
		});
	}
	
	$("#socialNav a").tooltip({
	   showURL: false,
	   top:-10,
	   track: true
	});
	
	$("a.boxy").fancybox({
		'frameWidth': 250, 
		'frameHeight':	400,
		'hideOnContentClick':false
	});
	
	$("a.gallery").fancybox({
		'hideOnContentClick':false
	});	

	var slideActive = true;

	function slideNav(tgt) {
		if ( slideActive === true ) {
		    $(tgt).animate({
		       "height": "toggle", "opacity": "toggle"
		    }, 375, "easeOutQuad", function(){slideActive = true;} );
			slideActive = false;
		}
	}

	/*$("#calendarArchiveContent").css('opacity','0');
	$("#calendarCategoryContent").css('opacity','0');*/
	
	$("#quickLinks a.header").click(function() {
		slideNav("#quickLinksContent");
	});	
	$("#calendarArchiveSelect a.header").click(function() {
		slideNav("#calendarArchiveContent");
		if($('#calendarCategoryContent').css('opacity','1')){
			slideNav("#calendarCategoryContent");
		}
		return false;
	});
	$("#calendarCategorySelect a.header").click(function() {
		slideNav("#calendarCategoryContent");
		if($('#calendarArchiveContent').css('opacity','1')){
			slideNav("#calendarArchiveContent");
		}
		return false;
	});

	$("#calendarMonthSelect select").change(function() {
		window.location = $(this).children('option:selected').val();
	});

	var previousCategory;
	var calCategory;

	function showCategories(catName) {
		if(catName == 'allSchoolEvents'){
			//$('#calendar .calendarItem:not(.'+previousCategory+')').fadeOut('fast');
			$('#calendar .calendarItem').fadeOut('fast');
			//$('#calendar .calDay h5:not(.admissionsEvents,.athleticsEvents,.juniorSchoolEvents,.middleSchoolEvents,.seniorSchoolEvents,.artsEvents)').fadeIn('normal');
			$('#calendar .calDay .calendarItem:not(.isType)').fadeIn('normal');
		} else {
			$('#calendar .calendarItem').fadeOut('fast');
			$('#calendar .'+catName).fadeIn('normal');
		}
		previousCategory = calCategory;
	}

	/*function showItem($o,n) { 
	  setTimeout(function() {
	     $o.fadeIn(220);
	  }, n); 
		return false;
	}*/

	$("#calendarLegend a").click(function() {
		calCategory = $(this).parent().find('span').attr('class');
		if(calCategory != previousCategory){
			showCategories(calCategory);
			/*var delay = 100;
			$('#calendar .'+catName).each(function(){
				var fade = $(this);
				showItem(fade,delay);
				delay += 160;
			});*/
		}
		return false;
	});



	if ( getIE6 === true ) {
		DD_belatedPNG.fix('.png'); // Belated PNG fix by Drew Diller - http://www.dillerdesign.com/experiment/DD_belatedPNG/
		$("#mainNav").superfish({speed:0});
	}
	
	$(".vtabs").tabs().addClass('ui-tabs-vertical ui-helper-clearfix');
	$(".vtabs li").removeClass('ui-corner-top').addClass('ui-corner-left');
	
	/*if ( $("#homeTabs").length > 0 ) {
		$("#homeTabs").tabs({ fx: { opacity: 'toggle' } });	
	};*/
	
	$('#homeTabs').cycle({ 
	    fx: 'fade', 
		speed: 2000,
		timeout: 6000,
		pager:  '#homeTabNav'
	});
	
	$('.galleryInside').cycle({ 
	    fx: 'scrollHorz', 
		speed: 1200,
		timeout: 8000,
		prev: '.left',
		next:   '.right',
		pager:  '#homeTabNav'
	});
	
	$('#homeTabNav a').each(function(i) {
		newID = "spotlightNav" + (i +1);
		$(this).attr("id",newID);
	});
	
	$('#homeTabNav a').click(function() {
		if($(this).hasClass("current")) {}
		else {
			$("#homeTabNav a.current").toggleClass("current");
			$(this).addClass("current");
		}
	});
	
	if ( $("#calloutTabs").length > 0 ) {
		$("#calloutTabs").tabs();	
	}
	
	jQuery.fn.slideFadeToggle = function(speed, easing, callback) {
	  return this.animate({opacity: 'toggle', height: 'toggle'}, speed, easing, callback);  
	};
	
	//$('#historyList dt').click(function() {$(this).next().slideFadeToggle('fast');});
	
	$(".historyList dd").addClass("hidden");
	
	$("ul.studentClubs").addClass("hidden");
	
	$('.historyList dt').click(function() {
		$(".historyList dd").addClass("hidden");
		if ($(this).next().is('.hidden')) {
			$(this).next().removeClass('hidden');
		} else {
			$(this).next().addClass('hidden');
		}
	});
	$('h4.studentClubs').click(function() {$(this).next().slideFadeToggle();});
	
	$('.calendarRow').equalHeights(true);

});

DD_roundies.addRule('.calendarItem', '5px', true);
DD_roundies.addRule('#calendarLegend', '5px', true);