
(function($){
    $.fn.buliExpert = function(options) {
        var defaults = {
            selectLabel : 'Spieltag',
            preselectedDay: '1',
            selectedDay: '1',
            obj : null,
            tableArray : null,
            tableHeadline : null
        };
        var options = $.extend(defaults, options);
        var generateDropdown = function(data) {
            var selectOptions = [];
            $(options.tableArray).each(function (i, entry) {
                if (i != 0) {
                    $posEntry = $("td:first", entry).text();
                    if (jQuery.inArray($posEntry, selectOptions) == -1) { 
                        selectOptions.push($posEntry);
                    }
                }
            });
            $("select", options.obj).before('<label style="color:#7AB800; font-weight:bold; margin-right: 10px;">' + options.selectLabel + '</label>');
            for (var i=0; i< selectOptions.length; i++) {
                if (selectOptions[i] != "x") {
                    var tempOption = new Option(selectOptions[i] + ". " + options.selectLabel, selectOptions[i]);
                    $("select", options.obj).get(0).options[i] = tempOption;
                }    
            }
        }
        var showFirst = function(data) {
            $(options.tableArray).hide();
            if(!$.browser.msie) {
                $(options.tableHeadline).css({display:'table-row'});
            } else {
                $(options.tableHeadline).show();
            }

            $("th:first", options.tableHeadline).hide();
            $(options.tableArray).each(function (i, entry) {
                $("td:first", entry).hide();
                if (data == $("td:first", entry).text()) {
                    if(!$.browser.msie) {
                        $(entry).css({display:'table-row'});
                    } else {
                        $(entry).show();
                    }
                }
            });
            $("select option", options.obj).each(function (i) {
                if (($(this).text().split("."))[0] == data) {
                    $(this).attr("selected", "selected");
                }
            });
        }
        return this.each(function() {
            options.obj = $(this);
            options.tableArray = $("div table.tab_tabello tr", this);
            options.tableHeadline = $("div table.tab_tabello tr:first", this);
            options.selectedDay = $(document).getUrlParam("buliPage") || options.preselectedDay;
            generateDropdown();
            (parseInt(options.selectedDay) <= $("select option", options.obj).length) ? showFirst(options.selectedDay) : showFirst("1");
            $("select", options.obj).change(function () {
                var selPosition = ($("option:selected", this).text().split("."))[0];
                $(options.tableArray).hide();
                $(options.tableHeadline).show();
                $(options.tableArray).each(function (i, entry) {
                    $("td:first", entry).hide();
                    if (selPosition == $("td:first", entry).text())
                        $(entry).show();
                });    
            });
    
        });
    };
})(jQuery);
