/*
**	Project:	Horizontal Accordion (hAccodrion)
**	Version:	v1.0
**	Author:		Collin Klopfenstein
**				collink@kulerwerks.com

**  modified version by trio-group
*/
( function ( ) {
	$.fn.hAccordion = function ( settings ) {
		var options = $.extend( {
			section: 'section',
			duration: 750,
			easing: 'swing',
			callback: null,
            selected: null
		}, settings );
		
		var height = 0;
		var width = $(this).width( );
		var labelWidth = 140;
		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 ) {
			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;
                        $(this).addClass( 'active' );
					} else {
                        $(this).removeClass( 'active' );
                    }
				} );
			}
		};
		
		$(children).each( function ( i ) {
			$(this).css( {
				position: 'absolute',
				left: i === 0 ? 0 : ( free + ( i * labelWidth ) ),
				width: free + labelWidth
			} );
			$(this).children( 'h1' ).css( 'cursor', 'pointer' );
			$(this).children( '.content' ).css( {
				width: free - labelWidth - ( $('.section .content').css( 'padding-left' ).replace( /px/, '' ) * 2 )
			} );
			
			$(this).children( ).each( function ( ) {
				height = height > $(this).height( ) ? height : $(this).height( );
			} );

            $(this).mouseenter( function ( ) {
				activate( $(this) );
			} );
		} );
        
        if ( options.selected ) {
            $( function ( ) {
                activate( $(options.selected) );
            } );
        }

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