/*
 * Bubbleup - Spicing up menu jQuery plugin
 *
 * Tranform the images inside the list items like Mac Dock effect
 *
 * Homepage: http://aext.net/2010/04/bubbleup-jquery-plugin/
 *
 * Copryright? I don't have any!
 *
 * First written by Lam Nguyen (yah, it's me)
 * Written again by Jonathan Uhlmann http://jonnitto.ch
 * Thanks to Icehawg (Hey, I don't know anything from him, just a name http://aext.net/2010/02/learn-jquery-first-jquery-plugin-bubbleup/#comment-6777)
 * Rewrittern by Anton Muravyev to suit his own needs.
 *
 */


(function($){
	$.fn.bubbleup = function(options) {
		/*@cc_on
		@if (@_jscript_version <= 5.6)
		  return;
		/*@end @*/
		
		//Extend the default options of plugin
		var opt = $.extend({}, $.fn.bubbleup.defaults, options),tip = null;
		
		return this.each(function(i) {
			var that = this;
			
			this.onload = function() {
				// Lock the li size
				$(that).closest('li').css({
					height: $(that).height(),
					width: $(that).width()
				});
				$(that).css({
					position: 'absolute',
					top: 0,
					left: 0
				});
			};
			
			if ($(this).height() > 0 && $(this).width() > 0) {
				this.onload();
			}
			
			$(this).mouseover(function(){
				if(opt.tooltip) {
					tip = $('<div>' + $(this).attr('alt') + '</div>').css({
						fontFamily: opt.fontFamily,
						color: opt.color, 
						fontSize: opt.fontSize, 
						fontWeight: opt.fontWeight, 
						position: 'absolute', 
						zIndex: 100000
					}).remove().css({top:0,left: 0,visibility:'hidden',display:'block'}).appendTo(document.body);
					var position = $.extend({},$(this).offset(),{width:this.offsetWidth,height:this.offsetHeight}),tipWidth = tip[0].offsetWidth, tipHeight = tip[0].offsetHeight;
					tip.stop().css({
						top: position.top - tipHeight - 26, 
						left: position.left + position.width / 2 - tipWidth / 2,
						visibility: 'visible'
					}).animate({top:'+=16'},opt.tipSpeed); 
				}
				
			}).mouseout(function(){
				
				if(opt.tooltip && tip) {
					tip.remove();
				}

			}).click(function(event) {
				event.preventDefault();
				
				if(opt.tooltip && tip) {
					tip.remove();
				}
				
				$("ul#people-selector li a img").each(function(j,el) {
					var _parent = $(el).closest('li'),
						_h = $(_parent).height(),
						_w = $(_parent).width(),
						_s = (j == i ? opt.scaleFull : opt.scaleBack) / _h,
						_th = _s * _h,
						_tw = _s * _w;
					
					if (_th == _h && _tw == _tw) {
						return;
					}
					
					$(el).stop().animate({
						width: _tw,
						height: _th,
						top: (_h - _th) / 2,
						left: (_w - _tw) / 2
					},opt.scaleSpeed);
					
				});
				var _link = $(this).closest('a').attr('href');
					_link = _link.substr(_link.indexOf('#') + 1);
				
				$('#our-people #content .person:not([id='+_link+']), #our-people #content .description').stop().animate({opacity: 0},'slow','swing',function() {
					$(this).css('display','none');
				});
				
				$('#'+_link).css({display:'block',opacity: 0}).stop().animate({opacity: 1.0},'slow');
			});
		});
	};
	$.fn.bubbleup.defaults = {
		tooltip: true,
		scaleFull: 246,
		scaleBack: 180,
		fontFamily:'Helvetica, Arial, sans-serif',
		color:'#700F33',
		fontSize:14,
		fontWeight:'normal',
		tipSpeed:'fast',
		scaleSpeed:'slow'
	};
})(jQuery);