/* Middlebury JS effects */
/* by White Whale Web Services */

$.easing.def = 'easeInOutQuad'; // set the default easing

$(function() { // on DOM ready
	$('#search_query').inlineLabel();
	// Tabs
	$('#tabs li a').click(function() {
		$(this).parent().addClass('active').siblings().removeClass('active');
		$($(this).attr('href')).show().siblings('.tab').hide();
		return false;
	}).eq(0).click(); // and click the first one
	// Links with images
	$('#content a:has(img),#carousel a:has(img)').addClass('noborder');
	// Dropdowns
	var dropdowns = $('.dropdown');
	dropdowns.hover(function() {
		var self = $(this);
		dropdowns.removeClass('active');
		self.addClass('active');
		clearTimeout(self.data('timer'));
	},function() {
		var self = $(this);
		self.data('timer',setTimeout(function() {
			self.removeClass('active')
		},500));
	});
	// Quickaccess (on gateway pages)
	if($('.quickaccess').length) { // if quickaccess and quickaccess.js are present
		$.ajaxSetup({cache:true});
		$.getScript('/scripts/quickaccess.js',function() {
			$('input.quickaccess').quickaccess({selector:'.qa_links a',sort:true,maxResults:10});
		});
	}
	// Footer positioning and panel
	var footerPanel = $('#footer_panel');
	$(window).resize(nudgeFooter).resize(); // attach nudge footer and do it now
	$('#footer #quick a').click(function() {
		var windowHeight = $(window).height(),
			li = $(this).parent();
		if(!footerPanel.is(':visible')) {
			li.addClass('active');
			var fromTop = $(this).offset().top;
			footerPanel.slideDown(1000);	
			$('html,body').animate({scrollTop:(fromTop+330-windowHeight)+'px'},1100);
		} else {
			li.siblings().removeClass('active');
			if(li.is('.active')) {
				footerPanel.slideUp(1000,function() {
					li.removeClass('active');
				});
				$('html,body').animate({scrollTop:($('body').height()-windowHeight-300)+'px'},900);
			} else {
				li.addClass('active');
			}	
		}
		return false;
	});
});

function nudgeFooter() {
	var footer = $('#footer');
		windowHeight = $(window).height(), // get the window height
		bodyHeight = footer.css('margin-top',30).offset().top+30; // the bottom of the footer is the bottom of the body; ignore the footer panel	
	if(bodyHeight<windowHeight) {
		footer.css('margin-top',Math.max(30,windowHeight-bodyHeight+30)+'px');
	}
}

$.fn.extend({ // add jQuery plugins
	inlineLabel: function(style,text) { // places labels inside of text inputs
		if(typeof style !='string') { // if no style is specified (including empty strings)
			style ='inline_label'; // the default CSS class for placeholder text	
		}
		text = text || $('label[for='+this.attr('id')+']').hide().text(); // if text is not specified, use the label text
		var self = this,
			blur = function() { // a blur function that doesn't fire the browser blur event
				var val = $.trim(self.val());
				if(!val||val==text) { // if this input has no contents or the contents are identical to the placeholder text
					self.addClass(style) // add the inline_label class
						.val(text) // set the appropriate text
						.one('focus',function() { // and, on the first focus
							self.val('') // remove that text
								.removeClass(style); // and the inline_label class
						});
				}
			};
		self.blur(blur); // on blur, fire the blur function
		blur(); // and fire it now (without firing the browser blur event)
		return this; // return original element for chaining
	}
});
