/* Javascript effects for Lake Forest College */
/* (requires jQuery 1.4 and jQuery UI easing) */
/* by White Whale Web Services */

$.easing.def = 'easeInOutSine';
var whiteout,blackout;

$.fn.extend({
	whenloaded:function(callback) { // when all images have loaded
		var loadable = 'img,object,video,iframe', // selector for elements with the load event
			elements = this.find(loadable), // get the list of loadable elements
			loaded = 0; // and the number that have loaded so far
		if(this.is(loadable)) elements.add(this); // add the current element if its loadable
		elements.each(function() { // get each element
			if(this.complete) loaded++; // if it's already loaded, increment the counter
			else {
				$(this).load(function() { // otherwise, attach a load event
					if(++loaded==elements.length) callback.apply(this); // that increments the counter and checks if its time to run the callback
				});
			}
		});
		if(elements.length==loaded) callback.apply(this); // if the elements are all loaded, apply the callback immediately	
		return this; // return the original element for chaining
	}
});

(function($) {
$(function() {
	whiteout = $('<div id="whiteout"/>').appendTo('body');
	blackout = $('<div id="blackout"/>').appendTo('body');
	// Tabbed panels
	$('#panels #tabs a').click(function() {
		$(this).parent().addClass('active').siblings().removeClass('active'); // add the active class to this item, strip it from siblings
		$($(this).attr('href')).show().siblings('.panel').hide(); // show the appropriate panel and hide the siblings
		return false; // and cancel the original click
	}).eq(0).click(); // and click the first one right now
	
	// Quick links
	var quicklinks_timer,
		quicklinks_open = $('#quicklinks_open'),
		quicklinks = quicklinks_open.siblings('#quicklinks'),
		quicklinks_height = quicklinks.show().height();
	quicklinks.height(0);
	quicklinks_open.click(function() { return false; })
		.parent().hover(function() {
			clearTimeout(quicklinks_timer);
			quicklinks_timer = setTimeout(function() {
				quicklinks_open.addClass('active');
				quicklinks.stop().animate({height:quicklinks_height},300);
			},(quicklinks_open.is('active') ? 0 : 300));
		},function() {
			clearTimeout(quicklinks_timer);
			quicklinks_timer = setTimeout(function() {
				quicklinks.stop().animate({height:0},300,function() {
					quicklinks_open.removeClass('active');	
				});
			},500);
		});
	// Search placeholder
	$('#search_query').placeholder();
	// Pull down page if it's too short
	if(!$('body').is('#homepage')) $('#page').css('min-height',$('#sidebar_left').outerHeight(true)+30);
	// Department switchers
	var switcher_list = $('.switcher_list');
	$('.switcher_open').click(function() {
		$(this).siblings('.switcher_list').show(600);
		whiteout.show().one('click',function() { whiteout.hide(); switcher_list.hide(600); });
		switcher_list.click(function(e) { if(!$(e.target).is('a')) whiteout.click(); });
		return false;
	});
	// Accordions
	$('.accordion').each(function() {
		$(this).accordion({active:Math.round(Math.random()*$(this).children(':header').length)});
	});
	// Quickaccess
	$('#search_query').quickaccess({links:[{selector:'.quicklinks a',title:'Quick Links:'},{selector:'.offices a',title:'Offices:'},{selector:'.careerfinder a',title:'Careers:'},{selector:'.services a',title:'Services:'},{selector:'.money a',title:'Your Money:'},{selector:'.life a',title:'Your Life:'},{selector:'.academics a',title:'Academics:'},{selector:'.career a',title:'Your Career:'}],sort:true,removeDuplicates:true,forceSelect:false});
	$('#quickquery').quickaccess({links:[{selector:'.quicklinks a',title:'Quick Links:'},{selector:'.offices a',title:'Offices:'},{selector:'.careerfinder a',title:'Careers:'},{selector:'.services a',title:'Services:'},{selector:'.money a',title:'Your Money:'},{selector:'.life a',title:'Your Life:'},{selector:'.academics a',title:'Academics:'},{selector:'.career a',title:'Your Career:'}],sort:true,removeDuplicates:true});
	// Photo galleries in titlebar
	if(window.titlebar_gallery) {
		var gallery_thumbs = $('#gallery_thumbs'),
			gallery = $('#gallery'),
			thumbs = $('<ul/>').appendTo(gallery_thumbs),
			pointer = $('<div id="gallery_pointer"/>').appendTo(gallery_thumbs);
		if($('#titlebar').index()<gallery.index()) gallery_thumbs.addClass('pointer_bottom');
		$.getJSON('?livewhale=ajax&function=getGalleriesJSON&width=746&height=215&crop=true&thumb_width=100&thumb_height=50&max=3&thumb_crop=true&id='+titlebar_gallery,function(data) {
			$.each(data.photos,function() {
				var thumb = $('<a href="'+this.file+'"><img src="'+this.thumb+'" alt="Gallery photo" width="100" height="50"/></a>').data('caption',this.caption);
				if(this.credit) thumb.data('credit',this.credit);
				thumbs.append($('<li/>').append(thumb));
			});
			thumbs.whenloaded(function() {
				thumbs.find('li').css('background-color','#fff').find('img').each(function(index) {
					$(this).hover(function() {
						$(this).stop().fadeTo(200,0.6);
					},function() {
						$(this).stop().fadeTo(200,1);
					}).delay(500+(index*100)).fadeTo(200,0.6).fadeTo(200,1);
				});
			});
			pointer.css('left',gallery_thumbs.width());
			thumbs.find('a').click(function() {
				var thumb = $(this);
				if(gallery.is(':hidden')) { // on the first reveal
					gallery.height(0).show();
					gallery.animate({height:215,marginBottom:15},1000);
					setTimeout(function() { pointer.animate({height:17,left:thumb.position().left+35},1000); },500);
				}
				pointer.stop().delay(gallery.height() || 500).animate({height:17,left:thumb.position().left+35},500);				
				gallery.children().stop().unbind('load');
				$('<img/>').load(function() {
					$(this).parent().fadeTo(500,1,function() {
						$(this).siblings().remove();
					});
				}).attr('alt','Gallery photo').attr('src',thumb.attr('href')).appendTo($('<div class="gallery_image"><div class="gallery_caption"><span>'+thumb.data('caption')+'</span></div>'+(thumb.data('credit') ? '<div class="gallery_credit">'+thumb.data('credit')+'</div>' : '')+'</div>').css('opacity',0).appendTo(gallery));
				return false;
			});
			gallery_thumbs.parent().css('padding-right',gallery_thumbs.width()+5);
			if($('body').hasClass('topphoto')) {
				thumbs.find('a:first').click();
			}
		});
	}
});

$.fn.extend({ // add plugins
	placeholder:function(options) { // bootstrap HTML5 placeholder attributes for browsers that don’t support it
		options = options || {};
		var s = {
			style:options.style || 'lw_placeholder', // the class to add when the element is operating as a placeholder
			clear:options.clear || false // the elements which, when clicked, should wipe the placeholder
		};
		return this.each(function() { // with each matched element
			var self = $(this);
			if (this.placeholder && 'placeholder' in document.createElement(this.tagName)) return; // if the browser supports placeholders for this element, abort
			if(self.data('placeholder')) { // if a placeholder has already been set on this element
				return; // abort to avoid double-binding
			}
			self.data('placeholder',true); // flag this element as having a placeholder, so we'll never double-bind
			var placeholder = self.attr('placeholder'),
				clear = function() { // to clear the placeholder
					if(self.val()==placeholder) { // if the text is the placeholder
						self.removeClass(s.style).val(''); // blank the text and remove the placeholder style
					}
				};
			self.focus(clear)
				.blur(function() {
					var val = self.val();
					if(!val||val==placeholder) { // if there’s no text, or the text is the placeholder
						self.addClass(s.style).val(placeholder); // set the text to the placeholder and add the style
					}
				}).blur(); // and do it now
			self.parents('form').submit(clear);
			$(s.clear).click(clear);
		});
	}
});

})(jQuery);
