/*
**	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',
			content: 'content',
			duration: 750,
			easing: 'swing',
			callback: null,
			animateOnLoad: false
		}, settings );
		
		var height = 0;
		var width = $(this).width( );
		var labelWidth = $('.' + options.label).width( );
		var children = $(this).children( '.' + options.section );
		var free = width - ( labelWidth * children.length );
		
		$(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 ) {
			$(children).stop(false, true);
			var pos = getPosition( section );
			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 ( i ) {
			$(this).css( {
				position: 'absolute',
				left: i === 0 ? 0 : ( free + ( i * labelWidth ) )				
			} );					
			$(this).css('width', free + labelWidth);
			$(this).children( '.' + options.label ).css( 'float', 'left' ).css( 'cursor', 'pointer' );
			$(this).children( '.' + options.content ).css('float', 'left').css('width', free - ( $('.' + options.section + ' .' + options.content).css( 'padding-left' ).replace( /px/, '' ) * 2 ));
			
			$(this).children( ).each( function ( ) {
				height = height > $(this).height( ) ? height : $(this).height( );
			} );

			$(this).children( '.' + options.label ).click( function ( ) {
				activate( $(this).parent( ) );
			} );
			
			$(this).children( '.' + options.label ).mouseover( function ( ) {
				activate( $(this).parent( ) );
			} );
			if (options.animateOnLoad !== false && i > 0){  setTimeout(function(){activate( $(children[i])); },  (i-1) * options.duration); }
		} );

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