/*
 * Plugin dodaje rysowanie pask�w post�pu
 */
;
(function($) {

    $.fn.progressBarCompletion = function() {
        return $(this).data("percentage");
    }
    $.fn.progressBar = function(options) {
        options = jQuery.extend({
            emptyBarClass: "emptyBar",
            fullBarClass: "fullBar",
            txt: "Kompletno\u015b\u0107 ",
            txtClass: "progressBarText",
            percentClass: "progressBarPercent",
            findClass:"progressBar",
            maxWidth:160,
            widthMargin:4,
            mode:"start"
        }, options);
        //-------------------------//
        return this.each(function() {
            var $this = $(this);
            //http://krakow.coreteam.pl:9090/redmine/issues/show/371

            var all = 1;//$this.parent().find("input[type=checkbox]").length;
            var checked = $this.parent().find("input[type=checkbox][checked]:not(#queryForm_searchQuery_sendAuto,#queryForm_searchQuery_sendLocation,.countAll)").length > 0 ? 1 : 0;
            checked+=$this.parent().find("input[type=checkbox][checked].countAll").length ;

            var selects = $this.parent().find("select");
            for (var i = 1; i < selects.length; i++) {//uwaga
                if (($(selects[i]).val() == -1) || ($(selects[i]).val().indexOf("---") != -1)) {
                    ;//empty
                } else {
                    checked = checked + 1;
                }
                all = all + 1;
            }

            var input = $this.parent().find("input[type=text]");
            for (var i = 0; i < input.length; i++) {
                if (($(input[i]).val() == "")) {
                    ;//empty
                } else {
                    checked = checked + 1;
                }
                all = all + 1;
            }

            checked += $this.parent().find("#queryForm_searchQuery_sendAuto:checked").length
            all += $this.parent().find("#queryForm_searchQuery_sendAuto").length;
            checked += $this.parent().find("#queryForm_searchQuery_sendLocation:checked").length
            all += $this.parent().find("#queryForm_searchQuery_sendLocation").length;
            var p = parseInt((checked * 100) / all);
            $this.data("percentage",p);
            var fullSize = parseInt(((options.maxWidth - 2 * options.widthMargin) * p ) / 100 + options.widthMargin);

            var divs = "<div class='" + options.emptyBarClass + "'><div class='" + options.fullBarClass + "' style='width:" + fullSize + "px'></div></div>";
            $this.html("<div class='" + options.txtClass + "'>" + options.txt + "</div>"
                    + "<div class='" + options.percentClass + "'>" + p + "%</div>"
                    + "" + divs + "");
            var updateFunction = function() {
                    var $this = $(this);
                    while ($this.find(".progressBar").length == 0) {
                        $this = $this.parent();
                    }
                    var update = $this.find(".progressBar");
                    update.progressBar({
                        mode:"reload"
                    });
                };
            if (options.mode == "start") {
                $this.parent().find("input[type=checkbox], select").click(updateFunction);
                $this.parent().find("input[type=text]").keyup(updateFunction);
            }
        });
    };
})(jQuery);


