/**
 *  WizardTabs Plugin 0.1
 *  
 *  Copyright 2011,
 *  Author: Bruno Felix, White Road Software   
 *  Licenced under the MIT license.
 *  http://www.opensource.org/licenses/mit-license.php
 *   
 *  Depends:
 *    jquery.ui.tabs
 *    jquery.ui.button   
 *  */

(function( $ ){
    /**
     * The plugin already provides some sensible defaults that enable the user to use it without
     * any kind of configuration.
     * */
    var defaults = {
        prevLabel: "indietro",
        nextLabel: "avanti",
        submitLabel: "fine",
        defaultPrevIcon: "ui-icon-circle-arrow-w",
        defaultNextIcon: "ui-icon-circle-arrow-e",
        defaultSubmitIcon: "ui-icon-circle-check",
        endActionEnabled: true,
        submitAction: function(){ alert("Please remember to set the submit action."); }
    };

    var opts = {};

    var methods = {
        /**
         * The init is the only public method that this plugin exposes. Its options require the following fields:
         * submitAction - the action to be executed when the submit button is clicked.
         * */
        init : function( options ){
            var $baseElem = this;
            $.extend(opts, defaults, options);
            
            return this.each(function(){
                if(opts.height !== undefined){
                    $baseElem.height(opts.height);
                }else{
                    $baseElem.height(_getMaxHeight($baseElem));
                }
                var links = [];
                $baseElem.tabs();
                $baseElem.find("ul li a").each(function() {
                    links.push($(this));
                });

                var $prev = null;
                var length = links.length;
                for (var i = 0; i < length; i++) {
                    var $baseDiv = $(links[i].attr("href"));
                    $baseDiv.append("<div style='position: absolute; bottom: 10px; width: 95%; text-align:center; margin:auto;'><hr/><div name='btnContainer' style='float:right;text-align:right; padding:10px;'></div></div>");
                    var $container = $(links[i].attr("href") + " " + "[name=btnContainer]");
                    if (i > 0) {
                        _generatePrevBtn($prev, $container);
                    }

                    if (i < length - 1) {
                        var $next = links[i + 1];
                        _generateNextBtn($next, $container);
                    }

                    if(opts.endActionEnabled){
                        if (i == length - 1) {
                            _generateSubmit($container);
                        }
                    }
                    $prev = links[i];
                    $container.parent().append("<div style='clear: both;'></div>");
                }
            });
        }
    };

    $.fn.wizardTabs = function( method) {
        if ( typeof method === 'object' || ! method ) {
            return methods.init.apply( this, arguments );
        } else {
            $.error( 'Method ' +  method + ' does not exist on jQuery.wizardTabs' );
        }
    };

    function _generateNextBtn($next, $container){
        _generateButton($container , opts.defaultNextIcon, opts.nextLabel, function(){ $next.trigger("click"); });
    }

    function _generatePrevBtn($prev, $container){
        _generateButton($container , opts.defaultPrevIcon, opts.prevLabel, function(){ $prev.trigger("click"); });
    }

    function _generateSubmit($container){
        _generateButton($container , opts.defaultSubmitIcon, opts.submitLabel, opts.submitAction);
    }

    function _generateButton($container, iconClass, text, action){
        var genId = "wizardBtn" + Math.floor(Math.random()*2048);
        var $button = $("<div id='"+genId+"'>"+text+"</div>");
        $button.button({icons:{primary: iconClass}}).click(function(){action();});
        $container.append($button);
    }

    function _getMaxHeight($elem){
        var max = 0;
        $elem.children("div").each(function(){
            var h = $(this).height();
            max = h > max ? h : max;
        });

        return max + 150;
    }

})( jQuery );


