// Modified version of jQuery Wizard (http://github.com/jgorset/jquery.wizard)

(function($){
    
    jQuery.fn.wizard = function(options){
        
        return this.each(function(){
            var self = this
            
            // Default options
            options                         = options || {}
            options.element                 = options.element || 'article'
            options.initial_step            = options.initial_step || 1
            options.labels                  = options.labels || {}
            options.labels.next             = options.labels.next || 'Next'
            options.labels.previous         = options.labels.previous || 'Previous'
            options.animations              = options.animations || {}
            options.animations.next         = options.animations.next || animate
            options.animations.previous     = options.animations.previous || animate
            
            var $elements = $(this).children(options.element)
            var $current_element = $elements.eq(options.initial_step - 1)
            
            function initialize(){
               
                $(self).append(
                    $('<input />', {
                        'type': 'button',
                        'value': options.labels.previous,
                        'id': 'previous',
                        'click': previous
                    })
					/*,
                    $('<input />', {
                        'type': 'button',
                        'value': options.labels.next,
                        'id': 'next',
                        'click': next
                    })*/
					/*,
                    $('<div></div>', {
						'id': 'steps' 
					})
					*/
				
                )
				$(self).append(
						$('<input />', {
                        'type': 'button',
                        'value': options.labels.next,
                        'id': 'next',
                        'click': next
                    })
					)
				$(self).append('<div id="steps"></div>')
					
                $(self).find('#steps').html("")
                $elements.each(function(index, element){
                    
                    $(self).find('#steps').append(
                        $('<div />', {
                            'class': 'step',
                            'id': 'step_' + (index + 1)
                        })
                    )
                    
                })
               
                $elements.not($current_element).hide()
                
                synchronize_navigation()
                synchronize_steps()
           
			}
            
            function go(target_step){
                $target_element = $elements.eq(target_step - 1)
                
                if($target_element.index() < $current_element.index()){
                    options.animations.previous($current_element, $target_element)
                }else{
                    options.animations.next($current_element, $target_element)
                }
                
                $current_element = $target_element
                
                synchronize_navigation()
                synchronize_steps()
            }
            
            function previous(){
                $previous_element = $current_element.prev(options.element)
                
                if($previous_element){
                    options.animations.previous($current_element, $previous_element)
                    $current_element = $previous_element
                }

                synchronize_navigation()
                synchronize_steps()
            }
            
            function next(){
                $next_element = $current_element.next(options.element)
                
                if($next_element){
                    options.animations.next($current_element, $next_element)
                    $current_element = $next_element
                }

                synchronize_navigation()
                synchronize_steps()
            }
            
            function animate(old_element, new_element){
                old_element.hide()
                new_element.show()
            }
            
            function synchronize_navigation(){
                $next_element = $current_element.next(options.element)
                $previous_element = $current_element.prev(options.element)
                
                if($previous_element.length){
                    $(self).find('#previous').removeAttr('disabled')
                }else{
                    $(self).find('#previous').attr('disabled', 'true')
                }
                
                if($next_element.length){
                    $(self).find('#next').removeAttr('disabled')
                }else{
                    $(self).find('#next').attr('disabled', 'true')
                }
            
            }
            
            function synchronize_steps(){
                $(self).find('#steps .step.current').removeClass('current')
                $(self).find('#steps .step:eq(' + $elements.index($current_element) + ')').addClass('current')
            }
            
            initialize()

        })
    }
})(jQuery)
