/* Key People Plugin */

(function($)
{
	$.fn.extend(
		{
			keyPeople : function( options )
			{
				/*
				 * Set up default options
				*/

				var defaults = {

					// Offsets

					top_offset : 34, // base top offset
					left_offset : 150, // default item box popup left offset
					left_offset_2 : 225, // second item box popup left offset
					left_offset_3 : 300, // third item box popup left offset

					// Element Targeting

					plus_minus : 'div.plus-minus', // plus minus base class
					open : 'open-widget', // open widget class
					close : 'close-widget', // closed widget class
					person : 'current_person', // current person
					box : 'div.box', // popup box class

					// Speeds

					fade_in : 'slow', // box fade in speed
					fade_out : 'fast' // box fade out speed
				}

				/*
				 * Extend your options
				*/

				var options = $.extend( defaults, options )

				/*
				 * Public Functions
				*/

				function resetPeople( object, options )
				{
					var o = options

					object.each(

						function()
						{
							// Set all Plus-Minus to default
							$(this).children( o.plus_minus ).removeClass( o.close ).addClass( o.open )

							// Fade out all boxes
							$(this).parent().children( o.box ).fadeOut( o.fade_out )

							// Remove all people from being the current selected one
							$(this).removeClass( o.person )
						}

					)
				}

				/*
				 * Set up variables to make it easier
				*/

				var all_items = this
				var o = options

				/*
				 * Loop through any multiple's of this element
				*/

				return this.each(

					function()
					{
						var obj = $(this)
						
						obj.bind(

							'click',

							function()
							{

								// Define the Plus Minus changing div to $this var
								var $plus_minus = $(this).children( o.plus_minus )
								var $box = $(this).parent().children(o.box)
								
								// Person is closed
								if ( $plus_minus.attr( 'class' ) == 'plus-minus ' + o.open )
								{
									// Reset all the people to default states
									resetPeople( all_items, o )

									// Set up basic vars to hold elements
									$(this).addClass( o.person )
									$plus_minus.addClass( o.close )

									// get the height of the box that is opening
									var box_height = $box.height()
									var top_offset = ( box_height + o.top_offset ) * -1

									// Get the location of the person in the row ( 1, 2 or 3 )
									var item_number = $(this).attr( 'rel' )

									// Change the left offset of the box to prevent from resizing the browser
									var left_offset = o.left_offset

									if ( 3 == item_number ) left_offset = o.left_offset_3
									else if ( 2 == item_number ) left_offset = o.left_offset_2

									// Convert to negative number to move left
									left_offset = left_offset * -1

									// Add css to box and then fade in
									$box.css( 'left', left_offset ).css( 'top', top_offset + 'px' ).fadeIn( o.fade_in )


								}
								else // Anything else
								{
									resetPeople( all_items, o )
								}

								// don't follow the link in the href attribute
								return false
							}

						)

					}
				)

			}
		}
	)

}
)(jQuery)


