/*
 * Treeview 1.4 - jQuery plugin to hide and show branches of a tree
 * 
 * http://bassistance.de/jquery-plugins/jquery-plugin-treeview/
 * http://docs.jquery.com/Plugins/Treeview
 *
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 *
 * Revision: $Id: jquery.treeview.js 4684 2008-02-07 19:08:06Z joern.zaefferer $
 *
 */

;(function($) {

    $.extend($.fn, {
        swapClass: function(c1, c2) {
            var c1Elements = this.filter('.' + c1);
            this.filter('.' + c2).removeClass(c2).addClass(c1);
            c1Elements.removeClass(c1).addClass(c2);
            var className = $(this).attr("class");
            if (className.search(CLASSES.collapsableHitarea) != -1) 
                $(this).attr({src:CLASSES.minusgifurl , title: CLASSES.einklappen, alt: CLASSES.Einklappsymbol});
            else if (className.search(CLASSES.expandableHitarea) != -1) 
                $(this).attr({src: CLASSES.plusgifurl, title: CLASSES.ausklappen, alt: CLASSES.Ausklappsymbol});
            return this;
        },
        replaceClass: function(c1, c2) {
            return this.filter('.' + c1).removeClass(c1).addClass(c2).end();
        },
        hoverClass: function(className) {
            className = className || "hover";
            return this.hover(function() {
                $(this).addClass(className);
            }, function() {
                $(this).removeClass(className);
            });
        },
        heightToggle: function(animated, callback) {
            animated ?
                this.animate({ height: "toggle" }, animated, callback) :
                this.each(function(){
                jQuery(this)[ jQuery(this).is(":hidden") ? "show" : "hide" ]();
                if(callback)
                    callback.apply(this, arguments);
            });
        },
        prepareBranches: function(settings) {
            if (!settings.prerendered) {
                // mark last tree items
                this.filter(":last-child:not(ul)").addClass(CLASSES.last);
                // collapse whole tree, or only those marked as closed, anyway except those marked as open
                this.filter((settings.collapsed ? "" : "." + CLASSES.closed) + ":not(." + CLASSES.open + ")").find(">ul").hide();
            }
            // return all items with sublists
            return this.filter(":has(>ul)");
        },
        applyClasses: function(settings, toggler) {
            this.filter(":has(>ul):not(:has(>a))").find(">span").click(function(event) {
                toggler.apply($(this).next());
            }).add( $("a", this) ).hoverClass();
            
            if (!settings.prerendered) {
                // handle closed ones first
                this.filter(":has(>ul:hidden)")
                .addClass(CLASSES.expandable)
                .replaceClass(CLASSES.last, CLASSES.lastExpandable);
                        
                // handle open ones
                this.not(":has(>ul:hidden)")
                .addClass(CLASSES.collapsable)
                .replaceClass(CLASSES.last, CLASSES.lastCollapsable);
                        
                // create hitarea
                this.prepend("<img class=\"" + CLASSES.hitarea + "\"/>").find("img." + CLASSES.hitarea).each(function() {
                    var classes = "";
                    $.each($(this).parent().attr("class").split(" "), function() {
                        classes += this + "-hitarea ";
                    });
                    $(this).addClass( classes );
                    if (classes.search(CLASSES.expandableHitarea) != -1) 
                        $(this).attr({src: CLASSES.plusgifurl, title: CLASSES.ausklappen, alt: CLASSES.Ausklappsymbol});
                    else if (classes.search(CLASSES.collapsableHitarea) != -1) 
                        $(this).attr({src: CLASSES.minusgifurl, title: CLASSES.einklappen, alt: CLASSES.Einklappsymbol});
                });
            }
            
            // apply event to hitarea
            this.find("img." + CLASSES.hitarea).click( toggler );
 
        },
        treeview: function(settings) {
            
            settings = $.extend({
                cookieId: "treeview"
            }, settings);
            
            if (settings.add) {
                return this.trigger("add", [settings.add]);
            }
            
            if ( settings.toggle ) {
                var callback = settings.toggle;
                settings.toggle = function() {
                    return callback.apply($(this).parent()[0], arguments);
                };
            }
        
            // handle toggle event
            function toggler() {
                toggleIt ($(this));
            }
            function toggleIt (elem) {
                var node = elem.parent();
                var cn = elem.attr ("class");
                var className = node.attr("class");
                if (elem[0].tagName != "IMG") {
                    if (className.search (/collapsable/) != -1) {
                        var cb = node.find (":checkbox");
                        if (cb.length > 0) {
                            var id = $(cb[0]).attr("id").split("_");
                            // Uncomment the following line to collapse a group when
                            // the groups checkbox is clicked to uncheck
                            // if ($('#'+id[0]+'_'+id[1]).attr("checked") == true)
                                return true; 
                        }
                    }
                }
                if (    elem[0].tagName != "IMG"
                     && elem[0].tagName != "SPAN"
                     && className.search(/closed/)     >= 0
                     && className.search(/expandable/) >= 0
                     && $(node[0]).find(".checkbox").attr("checked") != true
                ) {
                }
                else {
                    node
                    // swap classes for hitarea
                    .find(">.hitarea")
                    .swapClass( CLASSES.collapsableHitarea, CLASSES.expandableHitarea )
                    .swapClass( CLASSES.lastCollapsableHitarea, CLASSES.lastExpandableHitarea )
                    .end()
                    // swap classes for parent li
                    .swapClass( CLASSES.collapsable, CLASSES.expandable )
                    .swapClass( CLASSES.lastCollapsable, CLASSES.lastExpandable )
                    // find child lists
                    .find( ">ul" )
                    // toggle them
                    .heightToggle( settings.animated, settings.toggle );
                }
                return true;
            }
            // add treeview class to activate styles
            this.addClass("treeview");
            
            // prepare branches and find all tree items with child lists
            var branches = this.find("li").prepareBranches(settings);

            branches.applyClasses(settings, toggler);

            function checkBranch(bId) {
                var checked = true;
                $('.'+bId).each (function(){
                    if ($(this).attr("checked") == null || $(this).attr("checked") == "" || $(this).attr("checked") == false)
                        checked = false;
                }) ;
                if (checked == true)
                    $('#'+bId).attr("checked", checked);
                else 
                    $('#'+bId).attr("checked", false);
                return checked;
            }
            // handle toggle checkbox event
            function togglecb() {
                var id = $(this).attr("id");
                var aid = id.split ("_");
                var checked=  $(this).attr("checked");
                var parentid = id;
                if (aid.length > 2) { /* Untergruppe */
                    checked = checkBranch (aid[0]+"_"+aid[1]);
                }
                else { /* Hauptgruppe */
                    $('.'+parentid).each (function(){
                        if (checked == true)
                            $(this).attr("checked", checked);
                        else 
                            $(this).attr("checked", "");
                    }) ;
                }
        
            }
            function showCheckedBranches (cb) {
                var branches = new Array;
                cb.each (function () {
                    if ($(this).attr("checked") == true) {
                        var aid = $(this).attr("id").split ("_");
                        if (-1 == jQuery.inArray (aid[0]+"_"+aid[1], branches)) {
                            branches.push (aid[0]+"_"+aid[1]);
                            checkBranch (aid[0]+"_"+aid[1]);
                        }
                    }
                });
                $(branches).each(function () {
                    var id = this;
                    if ( ! $('#'+this).attr("checked") == true) {
                        var elem = $('#'+this).parent();//.filter(":first-child");
                        toggleIt (elem);
                    }
                });
            }

            // apply event to checkbox
            var cb= this.find("input." + CLASSES.checkbox);
            $(cb).click(togglecb);
            showCheckedBranches (cb);

            return this.bind("add", function(event, branches) {
                $(branches).prev()
                .removeClass(CLASSES.last)
                .removeClass(CLASSES.lastCollapsable)
                .removeClass(CLASSES.lastExpandable)
                .find(">.hitarea")
                .removeClass(CLASSES.lastCollapsableHitarea)
                .removeClass(CLASSES.lastExpandableHitarea);
                $(branches).find("li").andSelf().prepareBranches(settings).applyClasses(settings, toggler);
            });
        }
    });
    
    // classes used by the plugin
    // need to be styled via external stylesheet, see first example
    var CLASSES = $.fn.treeview.classes = {
        open: "open",
        closed: "closed",
        expandable: "expandable",
        expandableHitarea: "expandable-hitarea",
        lastExpandableHitarea: "lastExpandable-hitarea",
        collapsable: "collapsable",
        collapsableHitarea: "collapsable-hitarea",
        lastCollapsableHitarea: "lastCollapsable-hitarea",
        lastCollapsable: "lastCollapsable",
        lastExpandable: "lastExpandable",
        last: "last",
        hitarea: "hitarea",
        checkbox: "checkbox",
        ausklappen: "ausklappen",
        einklappen: "einklappen",
        Ausklappsymbol: "Ausklappsymbol",
        Einklappsymbol: "Einklappsymbol",
        plusgifurl: "/img/plus.gif",
        minusgifurl: "/img/minus.gif"
    };
    
    // provide backwards compability
    $.fn.Treeview = $.fn.treeview;
    
})(jQuery);

/**
 *  Klasse fuer erweiterten Suchfelder
 *****************************************************************************/
function cExtendedFieldset () {
    var m_it = this;
    var m_xDiv = null; 
    var state = "none";
    this.initialize = function (oElem) {
        m_xDiv = oElem;
        if ( ! m_it.checkFields() ) {
            $(m_xDiv).show();
            $('#QUERY_alAL_label').css('width', '');
            $('<button id="btclick" type="button" style="margin-right:1em;">Weniger Optionen</button>').insertBefore('#btsubmit');
        }
        else {
            $(m_xDiv).hide ();
            $('#QUERY_alAL_label').css('width', 'auto');
            $('<button id="btclick" type="button" style="margin-right:1em;">Mehr Optionen</button>').insertBefore('#btsubmit');
        }
        $('#btclick').click(m_it.toggleDisplay);
    },
    this.toggleDisplay = function () {
        if ( state == "inline" ) {
            if ( ! m_it.checkFields() ) 
                return false;
            state = "none";
        }
        else {
            state = "inline";
        }
        $('#btclick').empty()
        if ( state == "inline" ) {
            $(m_xDiv).slideDown();
            $('#QUERY_alAL_label').css('width', '');
            $('#btclick').text('Weniger Optionen');
        }
        else {
            $(m_xDiv).slideUp();
            $('#QUERY_alAL_label').css('width', 'auto');
            $('#btclick').text('Mehr Optionen');
        }
        return true;
    },
    this.checkFields = function () {
        var modified = 0;
        $('div.extended > * > input:text, div.extended > * > select').each(function(){
            var id = $(this).attr("id");
            var val = $(this).attr("value");
            if ($(this).attr("name") == "COUNT") {
                var n = Number(val);
                if (n != 10) 
                    ++modified;
            }
            else if ($(this).attr("name") == "GWTIMEOUT") {
                var n = Number(val);
                if (n != 30) 
                    ++modified;
            }
            else {
                if (val != null) {
                    val = jQuery.trim(val);
                    if (val.length)
                        ++modified;
                }
            }
        });
        return !modified;
    }
    return false;
}


/**
 *  Nachfolgend alles eintragen, was beim Laden einer Seite geschehen soll!
 *****************************************************************************/
jQuery(document).ready(
function() {
    $("#browser" ).treeview({ 'animated': 'linear', 'toggle': function(){if(jQuery.browser.msie)jQuery(this).find('ul').css('zoom','1')} });
    $("#browser1").treeview({ 'animated': 'linear', 'toggle': function(){if(jQuery.browser.msie)jQuery(this).find('ul').css('zoom','1')} });
    $("#browser2").treeview({ 'animated': 'linear', 'toggle': function(){if(jQuery.browser.msie)jQuery(this).find('ul').css('zoom','1')} });
    if (!SearchMaskMoreOptions) {
        var xFieldSet = new cExtendedFieldset ().initialize ($('.extended'));
    }
}
);
