

/* functions/lewisandclark */

/* Lewis and Clark Effects */
/* Requires jQuery 1.3+ and jQuery UI Effects */

$(function() { // DOM Ready
	$('#query').inlineLabel('Search Lewis & Clark','inline_label');
	var equalize = function() { // define the function that equalizes columns
		$('.equal.columns').each(function() {
			var columns = $(this).children('.column').css('padding-bottom',$(this).is('.bordered') ? 10 : 0);
			var height = $(this).height();
			columns.each(function() {
				if(!$.browser.msie) $(this).css('padding-bottom',(height-$(this).height())+'px');
			});
		});
	};
	equalize(); // and do it
	if($('body').hasClass('institutional')||$('body').is('#homepage')) { // if on institutional pages or a school homepage
		$('#schools>li>a').click(function() { // initialize school nav
			var school = $(this).parent();
			if(school.is('.active')) {
				return; // pass clicks on to the school homepages if the menu is open
				school.parent().stop().animate({height:'30px'},function() {
					school.removeClass('active');
				});
				return false;
			}
			school.addClass('active')
				.siblings()
					.removeClass('active')
				.end().parent()
					.stop().animate({height:'60px'});
			return false;
		});
		if($('body').is('#homepage') && typeof $().jparallax=='function') { // Portlandscape parallax requires jQuery jparallax plugin
 			$('#portlandscape').jparallax({mouseport:$('#portlandscape'),yparallax:false});
		}
		$('<img src="/site/images/navigation/nav_portals.gif"/>').load(function() { // if the portals image loads
			$('#portals').addClass('imagereplacement'); // enable image replacement on them
		});	
	}
	$('<img src="/site/images/navigation/nav_metanav.gif"/>').load(function() { // if the metanav image loads
		$('#metanav').addClass('imagereplacement'); // enable image replacement on it
	});
	$('#metanav_schools').hover(function() {
		$(this).addClass('active');
		$('<ul id="metanav_schools_dropdown"><li id="metanav_schools_cas"><a href="/college/">College of Arts &amp; Sciences<span></span></a><li id="metanav_schools_law"><a href="/law/">Law School<span></span></a></li><li id="metanav_schools_grad"><a href="/graduate/">Graduate School of Education &amp; Counseling<span></span></a></li></ul>')
			.appendTo(this);			
	},function() {
		$(this).removeClass('active').find('#metanav_schools_dropdown').remove();
	});
	// Search box
	var query = $('#search #query'), // the query box
		queryLast='', // the last query
		queryTimer, // the query interval reference
		queryEnabled=2,
		queryResults = $('<ul id="results"/>').appendTo('#search'),
		queryGet = function(value) { // ajax for new results
			if(value.length<3) {
				queryResults.hide();
			} else if(value!=queryLast && queryEnabled > 0) { // if the query isn't the same as the last
				$.ajax({
					type: 'GET',
					url: 'http://search.lclark.edu/pages.json?search='+value,
					dataType: 'jsonp',
					timeout: 2500,
					success: function(data) { // query for the JSON results
						if(data.length) { queryResults.empty().show(); }
						else { queryResults.empty().hide(); }
						$.each(data,function() {
							queryResults.append('<li><a href="'+this.url+'">'+this.title+'</a></li>')
						});
					},
					error: function(XMLHttpRequest, textStatus, errorThrown){
						if (textStatus == 'timeout') { queryEnabled--; }
					}
				});
				queryLast=value; // and set this as the last query
			}
		};
	query.attr('autocomplete','off').focus(function() { // when focusing on the query box
		queryTimer = setInterval(function() { queryGet(query.val()); },500); // set up a re-query timer
		queryGet(query.val()); // and do it instantly
	}).blur(function() { // on blur
		setTimeout(function() { queryResults.hide() },300); // hide the results
		clearInterval(queryTimer); // clear the timer
	}).keydown(function(e) { // capture special keys on keydown
		switch(e.keyCode) {
			case 38: // up arrow
				e.preventDefault(); // prevent default
				var selected = queryResults.find('.selected'); // find the selected item
				selected.removeClass('selected'); // deselect it
				if(selected.prev().length) selected.prev().addClass('selected'); // and select the previous
				else if(!selected.length) queryResults.find('li:last-child').addClass('selected'); // or the last
				break;
			case 40: // down arrow
				e.preventDefault(); // prevent default
				var selected = queryResults.find('.selected'); // find the selected item
				selected.removeClass('selected'); // deselect it
				if(selected.next().length) selected.next().addClass('selected'); // and select the next
				else if(!selected.length) queryResults.children().eq(0).addClass('selected'); // or the first
				break;
			case 13: // enter/return
				var selected = queryResults.find('.selected'); // find the selected item
				if(selected.length) { // if there is one
					e.preventDefault(); // prevent the default
					window.location = selected.find('a').attr('href'); // and go to that result
				}
				break;
		}
	});
	// "Jump" navs
	$('select.jumpto').change(function() { if($(this).val()) window.location = $(this).val(); });
	// Departments dropdown
	$('div#dropdown>a').click(function() {
	if($('#dropdown_list').is(':visible')) {
			$('#dropdown_list').slideUp();
			$('#whiteout').remove();
		} else {
			$('#dropdown_list').slideDown();
			$('body').prepend('<div id="whiteout" style="z-index:400;position:fixed;width:100%;height:100%;"/>');
			$('#whiteout').click(function() { $('div#dropdown>a').click(); });
		}
		return false;
	});
});

$.fn.extend({
	inlineLabel: function(text,style) {
		if(typeof style !='string') { // if no style is specified (including empty strings)
			style ='lw_inline_label'; // the default CSS class for placeholder text	
		}
		this.blur(function() { // onblur
			var val = $.trim($(this).val());
			if(!val||val==text) { // if this input has no contents or the contents are identical to the placeholder text
				$(this).addClass(style) // add the inline_label class
					.val(text) // set the appropriate text
					.one('focus',function() { // and, on the first focus
						$(this).val('') // remove that text
							.removeClass(style); // and the inline_label class
					});
			}
		}).blur(); // and do all this right now
		return this; // return original element for chaining
	}
});
