/*
 * 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
 *
 * Copyright (c) 2007 JГ¶rn Zaefferer
 *
 * 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);

            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);
            });
        },
        aheightToggle: function(animated, callback) {

        this.parent().find('li div').hide();
        /*serialize.apply();*/

        /*callback.apply(this, arguments);*/
        /*alert('dfdf');*/
        if (animated ){
            if (this.size() == 1){
                this.animate({
                    height: "show"
                }, animated,callback)
            }else{

                this.filter("ul:first").animate({
                    height: "show"
                }, animated,callback)
                this.filter("ul:last").animate({
                    height: "show"
                }, animated, function() {
                        if ($(this).is(':visible')) {$(this).find('div').show();}
                        if ($.browser.msie && $.browser.version == 6) {

                            $('#tree li span,#tree1 li span').removeClass("color_o");
                            $('#tree li a,#tree1 li a').removeClass("color_o");
                            $('#tree li.collapsable>span,#tree li.collapsable>a,#tree1 li.collapsable>span,#tree1 li.collapsable>a').addClass("color_o");
                            /*$('#pttree li.collapsable > span,#pttree li.collapsable > a').css('font-weight','bold');*/
                        }
                    })

            }
        } else{
            this.each(function(){
                                
                $(this)[ "show" ]();
                                        
                if(callback)
                    callback.apply(this, arguments);

            });
        }
    },
        heightToggle: function(animated, callback) {
      //    callback.apply(this, arguments);
         /* alert(callback)*/
         
          if ($(this).parents('#tree').is('ul')){
            $(this).find('div').hide();
          }
            if (animated){
            
            if (this.size() == 1){
                /*$(this).is('#pt').find('div').show();*/
                this.animate({
                    height: "toggle"
                }, animated,callback)
            }else{

                this.filter("ul:first").animate({
                    height: "toggle"
                }, animated,callback)
                this.filter("ul:last").animate({
                    height: "toggle"
                }, animated, function() {
                        if ($(this).is(':visible')) {$(this).find('div').show();}
                        if ($.browser.msie && $.browser.version == 6) {

                            $('#tree li span,#tree1 li span').removeClass("color_o");
                            $('#tree li a,#tree1 li a').removeClass("color_o");
                            $('#tree li.collapsable>span,#tree li.collapsable>a,#tree1 li.collapsable>span,#tree1 li.collapsable>a').addClass("color_o");
                           /* $('#pttree li.collapsable > span,#pttree li.collapsable > a').css('font-weight','bold');*/
                        }
                    })

            }
//            if (this.is(':visible')) {this.find('div').show();}

          }else{
            this.each(function(){
                jQuery(this)[ jQuery(this).is(":hidden") ? "show" : "hide" ]();
                if(callback)
                    callback.apply(this, arguments);

            });

          }


        },
        heightHide: function(animated, callback) {


            if (animated) {
                this.animate({
                    height: "hide"
                }, animated, callback);
            } else {
                this.hide();
                if (callback)
                    this.each(callback);
            }
        },
        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("<div class=\"" + CLASSES.hitarea + "\"/>").find("div." + CLASSES.hitarea).each(function() {
                    var classes = "";
                    $.each($(this).parent().attr("class").split(" "), function() {
                        classes += this + "-hitarea ";
                    });

                    $(this).addClass( classes );

                });
            }

            // apply event to hitarea
                        
            this.find("div." + CLASSES.hitarea).click( toggler );
            this.find("span").click( toggler );
            this.find("a.a" + 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);
                };
            }

            // factory for treecontroller
            function treeController(tree, control) {
                // factory for click handlers

                function handler(filter) {

                    return function() {
                        // reuse toggle event handler, applying the elements to toggle
                        // start searching for all hitareas
                                                
                        toggler.apply( $("div." + CLASSES.hitarea, tree).filter(function() {
                            // for plain toggle, no filter is provided, otherwise we need to check the parent element
                            return filter ? $(this).parent("." + filter).length : true;
                        }) );
                        return false;
                    };
                }
                // click on first element to collapse tree
                            
                $("a:eq(0)", control).click( handler(CLASSES.collapsable) );
                // click on second to expand tree
                $("a:eq(1)", control).click( handler(CLASSES.expandable) );
                // click on third to toggle tree
                $("a:eq(2)", control).click( handler() );
                if ($.browser.msie && $.browser.version == 6) {
                    $('#tree li.collapsable>span,#tree li.collapsable>a,#tree1 li.collapsable>span,#tree1 li.collapsable>a').addClass("color_o");
                            $('#pttree li.expandable > span,#pttree li.expandable > a').css('font-weight','normal');
                            $('#pttree li.collapsable > span,#pttree li.collapsable > a').css('font-weight','bold');
                }
            }

            // handle toggle event
              

            function toggler() {

			
                // toggle them
                 
                if ($(this).is('a')){
                    if ($(this).parent().find( ">ul" ).is(':hidden')){

                        $(this).parent().find( ">ul" ).aheightToggle( settings.animated, settings.toggle1 );
                        if ( settings.unique ) {

                            $(this).parent()
                            .siblings()
                            // swap classes for hitarea
                            .find(">.hitarea")
                            .replaceClass( CLASSES.collapsableHitarea, CLASSES.expandableHitarea )
                            .replaceClass( CLASSES.lastCollapsableHitarea, CLASSES.lastExpandableHitarea )
                            .end()
                            .replaceClass( CLASSES.collapsable, CLASSES.expandable )
                            .replaceClass( CLASSES.lastCollapsable, CLASSES.lastExpandable )
                            .find( ">ul" )
                            .heightHide( settings.animated, settings.toggle );

                        }
                       
                        return false;
                    }
                }


                if ($(this).is('div') || $(this).is('span')){
                    
                    // swap classes for hitarea

                    // find child lists
//test = $(this).parent().find( "ul:first" )
                    $(this).parent().find( ">ul" ).heightToggle( settings.animated, settings.toggle);

                    if ( settings.unique ) {
                       
                        $(this).parent()
                        .siblings()
                        // swap classes for hitarea
                        .find(">.hitarea")
                        .replaceClass( CLASSES.collapsableHitarea, CLASSES.expandableHitarea )
                        .replaceClass( CLASSES.lastCollapsableHitarea, CLASSES.lastExpandableHitarea )
                        .end()
                        .replaceClass( CLASSES.collapsable, CLASSES.expandable )
                        .replaceClass( CLASSES.lastCollapsable, CLASSES.lastExpandable )
                        .find( ">ul" )
                        .heightHide( settings.animated, settings.toggle,settings.serialize);

                    }
                    
                }
            }

            function serialize() {
                
                function binary(arg) {
                    return arg ? 1 : 0;
                }
                var data = [];
                
                branches.each(function(i, e) {
                    data[i] = $(e).is(":has(ul:first:visible)") ? 1 : 0;
                
                });
              
                $.cookie(settings.cookieId, data.join("") );
            }

            function deserialize() {
                var stored = $.cookie(settings.cookieId);
                if ( stored ) {
                    var data = stored.split("");
                    branches.each(function(i, e) {
                        $(e).find(">ul")[ parseInt(data[i]) ? "show" : "hide" ]();
                    });
                }

            }

            // 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);

            switch(settings.persist) {
                case "cookie":
                    var toggleCallback = settings.toggle;
                    settings.toggle = function() {
                
                        if ($(this).is(':visible')) {$(this).find('div').show();}
                        
                        serialize();
                        if (toggleCallback) {
                            toggleCallback.apply(this, arguments);
                        }
                      
                        $(this)

                        .parent()
                        .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 )


                        if ($.browser.msie && $.browser.version == 6) {

                            $('#tree li span,#tree1 li span').removeClass("color_o");
                            $('#tree li a,#tree1 li a').removeClass("color_o");
                            $('#tree li.collapsable>span,#tree li.collapsable>a,#tree1 li.collapsable>span,#tree1 li.collapsable>a').addClass("color_o");
                            $('#pttree li.expandable > span,#pttree li.expandable > a').css('font-weight','normal');
                            $('#pttree li.collapsable > span,#pttree li.collapsable > a').css('font-weight','bold');
                        /*$('#nav-one li.sfHover li.sfHover li a').css('color','#000000');
					alert($('#nav-one li.sfHover li.sfHover li a').size());*/
                        }
                       




                    };
                    settings.toggle1 = function() {
                        if ($(this).is(':visible')) {$(this).find('div').show();}

                        serialize();
                        if (toggleCallback) {
                            toggleCallback.apply(this, arguments);
                        }

                        $(this)

                        .parent()
                        .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 )


                        if ($.browser.msie && $.browser.version == 6) {

                            $('#tree li span,#tree1 li span').removeClass("color_o");
                            $('#tree li a,#tree1 li a').removeClass("color_o");
                            $('#tree li.collapsable>span,#tree li.collapsable>a,#tree1 li.collapsable>span,#tree1 li.collapsable>a').addClass("color_o");
                            $('#pttree li.expandable > span,#pttree li.expandable > a').css('font-weight','normal');
                            $('#pttree li.collapsable > span,#pttree li.collapsable > a').css('font-weight','bold');
                        /*$('#nav-one li.sfHover li.sfHover li a').css('color','#000000');
					alert($('#nav-one li.sfHover li.sfHover li a').size());*/
                        }
                        //alert($(this).find('a:first-child').size())
                        document.location = $(this).parent().find('a:first ').attr('href');
                        return false;
                        


                    };




                    settings.serialize = function() {
                        serialize();
                    };

                    deserialize();
                    break;
                case "location":
                    var current = this.find("a").filter(function() {
                        return this.href.toLowerCase() == location.href.toLowerCase();
                    });
                    if ( current.length ) {
                        current.addClass("selected").parents("ul, li").add( current.next() ).show();
                    }
                    break;
            }

            branches.applyClasses(settings, toggler);

            // if control option is set, create the treecontroller and show it
            if ( settings.control ) {
                treeController(this, settings.control);
                $(settings.control).show();
            }

            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"
    };

    // provide backwards compability
    $.fn.Treeview = $.fn.treeview;

})(jQuery);
