(function($){
    $.fn.msTabs = function() {
        return this.each(function(){

            var $that = $(this);

            $that.find(".ms-tabs-titles li").click(function(){

                // if ( $(this).hasClass("selected") ) return false;

                var t = $that.find(".ms-tabs-titles li.selected").removeClass("selected").attr("rel");
                if ( t ) {
                    $that.removeClass(t);
                    $that.find(".ms-tabs-content."+t).fadeOut(200);
                }

                t = $(this).attr("rel");
                $that.addClass(t);
                $(this).addClass("selected");
                $that.find(".ms-tabs-content."+t).slideDown(300);
            });

            var h = 0;
            $.each($(".ms-tabs-content"), function(){
                var v = MS.ie7 ? $(this).innerHeight() : $(this).outerHeight();
                h = Math.max(h,  v );
            })
            $(".ms-tabs-contents,.ms-tabs-content").height(h);

            $(".ms-tabs-titles li:eq(0)").trigger("click");
        });
    }

    $.fn.msNav = function() {

        return this.each(function(){

            var $that = $(this), $_current;

            $("body").click( function(e){
                $.fn.msNav.close($that);
            });

            $(this).find(".menu-content>.close").click(function(e){
                $.fn.msNav.close($that, e);
                return false;
            });

            $(this).find(">li").click(function(e){
                var target = e.target || e.srcElement;
                if ( $(target).parents(".subcontents").length>0 ) {
                    return true;
                }
                if ( $(this).hasClass("selected") ) {
                    $(this).removeClass("selected");
                    $(this).find(".menu-content").fadeOut(100);
                    return false;
                }
                if ( $(this).find(".menu-content").length < 1 ) {
                    return true;
                }

                $.fn.msNav.close($that, $(this));
                $(this).addClass("selected");
                $(this).find(".menu-content").fadeIn(100);
                $(this).find(".subcontents ul").hide();
                // $(this).find(".subcontents ul:first").show();
                e.stopPropagation();
                return false;
            });

            $(this).find("ol>li>a").click(function(e){
                var $p = $(this).parent().parent().next(".subcontents"), i = $(this).parent().index();
                $ul = $p.find("ul").eq(i);
                if ( $ul.children().length<1 ) {
                    window.location.href = this.href;
                    return true;
                }
                $p.find("ul").hide();
                $ul.show();
                e.stopPropagation();
                return false;
            });
            $(this).find("ol, menu-content").click(function(e){
                e.stopPropagation();
                return false;
            });
            
            $(this).find(".subcontents li a").click(function(){
                return true;
            });
        });
    }
    $.fn.msNav.close = function($obj, e) {
        $obj.children("li.selected").removeClass("selected").find(".menu-content").fadeOut(50);
        return false;
    }

    $.fn.msAccordion = function() {

        return this.each(function(){

            var $that = $(this);

            $that.find(".text").hide();

            $that.find("h2").click(function(){
                var $li = $(this).parents("li");
                if ( !$li.hasClass("selected") ) {
                    $that.find("li.selected").removeClass("selected").find(".text").slideUp();
                    var $li = $(this).parents("li");
                    $li.addClass("selected").find(".text").slideDown();
                }
            });
        });
    }
})(jQuery);

