/*
**	Project:	Horizontal Accordion (hAccodrion)
**	Version:	v1.0
**	Author:		Collin Klopfenstein
**				collink@kulerwerks.com
*/
( function ( ) {
	$.fn.hAccordion = function ( settings ) {
		var options = $.extend( {
			section: 'section',
			label: 'label',
			duration: 750,
			easing: 'swing',
			callback: null
		}, settings );

/*		
		var height = 0;
		var width = $(this).width();
		var optLabel = '.' + options.label;
		var labelWidth = $(optLabel).width( );
		var optSection = '.' + options.section;
		var children = $(this).children( '.' + options.section );
		var free = width - ( labelWidth * children.length );
*/
		
		var height = 0;
		var width = 880;
		var labelWidth = 34;
		var children = $(this).children( '.section' );
		var free = width - ( labelWidth * children.length );
		
		$('.label').css( 'background-position', '-34px 0' );
		$(this).css( {
			position: 'relative',
			overflow: 'hidden'
		} );
		
		var getPosition = function ( section ) {
			var i = 0, pos = 0;
			$(children).each( function ( ) {
				if ( this == section[0] ) {
					pos = i;
				}
					
				i ++;
			} );
			return pos;
		};
		
		var activate = function ( section ) {
			var pos = getPosition( section );
			$('.label').css( 'background-position', '-34px 0' );
			
			if ( section.length > 0 && !window.accordionExpanding ) {
				window.accordionExpanding = true;
				window.accordionSections = children.length;
				window.sectionsFinished = 0;
				
				var hit = false;
				$(children).each( function ( i ) {
					var tmp = this;
					if ( !hit ) {
						$(this).animate( { left: ( i * labelWidth ) }, options.duration, options.easing, function ( ) { window.sectionsFinished ++; if ( window.sectionsFinished == window.accordionSections ) { window.accordionExpanding = false; } if ( typeof options.callback == 'function' ) { options.callback.call( tmp ); } } );
					} else {
						$(this).animate( { left: ( i * labelWidth ) + $(this).width( ) - labelWidth }, options.duration, options.easing, function ( ) { window.sectionsFinished ++; if ( window.sectionsFinished == window.accordionSections ) { window.accordionExpanding = false; } if ( typeof options.callback == 'function' ) { options.callback.call( tmp ); } } );
					}
					
					if ( this == section[0] ) {
						hit = true;
					}
				} );
			}
			
			$(children).each( function ( ) {
				if ( this == section[0] ) {
					$(this).children( '.label' ).css( 'background-position', '0 0' );
				}
			} );
		};
		
		$(children).each( function ( i ) {
			$(this).css( {
				position: 'absolute',
				left: i === 0 ? 0 : ( free + ( i * labelWidth ) ),
				width: free + labelWidth
			} );
			$(this).children( '.label' ).css( {
				'background-position': i === 0 ? 0 : '-34px',
				'float': 'left',
				'cursor': 'pointer'
			} );
			$(this).children( '.content' ).css( {
				'float': 'left',
				width: free - labelWidth - ( $('.section .content').css( 'padding-left' ).replace( /px/, '' ) * 2 )
			} );
			
			$(this).children( ).each( function ( ) {
				height = height > $(this).height( ) ? height : $(this).height( );
			} );

			$(this).children( '.label' ).mouseover( function ( ) {
				activate( $(this).parent( ) );
			} );

			$(this).children( '.label' ).click( function ( ) {
				activate( $(this).parent( ) );
			} );
		} );

		if ( height > $(this).height( ) ) {
			$(this).css( 'height', height );
		}
	};
} )(jQuery);