/**
 * @author Rytis
 */
( 	function ($) {
		
		$.fn.raTabbedForm = function ( options ) {
			
			var opts = {
				
				formTabClass 				: 'formTab',
				
				formWizardNavigationClass	: 'wizardNavigation',
				
				wizardLeftClass				: 'wizardNavLeft',
				
				wizardMiddleClass			: 'wizardNavMiddle',
				
				wizardRightClass			: 'wizardNavRight',
				
				activeWizardCellClass		: 'active',
				
				currentIndex				: 0,
				
				backButtonClass				: 'wizardBack',
				
				nextButtonClass				: 'wizardNext'
				
			}
			
			$.extend( opts, options );
			
			var thisForm = this;
			
			var openedTab = null;
			
			var tabs = $( '.' + opts.formTabClass, this)
			
			var tabsCount = tabs.length;
			
			opts.currentIndex = Math.min( Math.max( opts.currentIndex, 0 ), tabsCount - 1 );
			
			var wizard = $( '.' + opts.formWizardNavigationClass + ' tr', this)
			
			wizard.empty();
			
			for ( var i = 0; i < tabsCount; i++ ) {
				if ( i == 0 ) {
					wizard.append( '<td class=\'' + opts.wizardLeftClass +  ( i == opts.currentIndex ? ( ' ' + opts.activeWizardCellClass ) : '' ) + '\'>' + (i+1) + '</td>' );
					
				} else if ( i == tabsCount - 1 ) {
					wizard.append( '<td class=\'' + opts.wizardRightClass +  ( i == opts.currentIndex ? ( ' ' + opts.activeWizardCellClass ) : '' ) + '\'>' + (i+1) + '</td>' )
				} else {
					wizard.append( '<td class=\'' + opts.wizardMiddleClass +  ( i == opts.currentIndex ? ( ' ' + opts.activeWizardCellClass ) : '' ) + '\'>' + (i+1) + '</td>' )
				}
				
				if ( i == opts.currentIndex ) {
					openedTab = $(tabs.get(i));
					openedTab.removeClass( 'closed' );
				} else {
					$(tabs.get(i)).addClass( 'closed' );
				}
				
				
			}
			
			$( '.' + opts.nextButtonClass, openedTab ).click(
			
				function () {
					$(thisForm).raTabbedForm( { currentIndex : ++opts.currentIndex } );
				}
				
			)
			
			$( '.' + opts.backButtonClass, openedTab ).click(
			
				function () {
					$(thisForm).raTabbedForm( { currentIndex : --opts.currentIndex } );
				}
				
			)
			
		}
		
		return this;
		
	}
)(jQuery)

