/*
 * jQuery UI Multilevel Accordion v.1.1
 *
 * Copyright (c) 2011 Pieter Pareit
 *
 * http://www.scriptbreaker.com
 *
 */

//plugin definition
(function($){
    $.fn.extend({

    //pass the options variable to the function
    accordionNavi: function(options) {
       
        var defaults = {
            accordion: 'true',
            speed: 300,
            closedSign: "<span class='controls smartphone-plus'></span>",
            openedSign: "<span class='controls smartphone-minus'></span>",
        };
        // Extend our default options with those provided.
        var opts = $.extend(defaults, options);
        //Assign current element to variable, in this case is UL element
        var $this = $(this);
        //add a mark [+] to a multilevel menu

        $this.find("li").each(function() {
            if($(this).find("ul").size() != 0){
                //add the multilevel sign next to the link
        	    var title = $(this).find("a:first").attr('title');
        	    if($(this).find("ul").is(":visible")) {
        	    	$(this).find("a:first").after(""+ opts.openedSign +"");
        	    } else {
        	    	$(this).find("a:first").after(""+ opts.closedSign +"");
        	    }
                
                //$(this).find("a:first").next().html('+');
                $(this).find("a:first").next().attr('title', opts.closedTitle + ' ' + title);
                //avoid jumping to the top of the page when the href is an #
                if($(this).find("a:first").attr('href') == "#"){
                        $(this).find("a:first").click(function(){return false;});
                }
            }
        });
        //open active level
        /*$this.find("li a.selected").each(function() {
                $(this).parents("ul").slideDown(opts.speed);
                $(this).parents("ul").parent("li").find("span.controls:first").html(opts.openedSign);
        });*/
        $this.find("li span.controls").click(function(event) {
            if($(this).parent().find("ul").size() != 0){
                if(opts.accordion){
                    //Do nothing when the list is open
                    if(!$(this).parent().find("ul").is(':visible')){
                        parents = $(this).parent().parents("ul");
                        visible = $this.find("ul:visible");
                        visible.each(function(visibleIndex){
                            var close = true;
                            parents.each(function(parentIndex){
                                    if(parents[parentIndex] == visible[visibleIndex]){
                                            close = false;
                                            return false;
                                    }
                            });
                            if(close){
                                if($(this).parent().find("ul") != visible[visibleIndex]){
                                        $(visible[visibleIndex]).slideUp(opts.speed, function(){
                                                $(this).parent("li").find("span.controls:first").removeClass('smartphone-plus');
                                                $(this).parent("li").find("span.controls:first").addClass('smartphone-minus');
                                                //$(this).find("a:first").addClass('selected');
                                        });
                                       
                                }
                            }
                        });
                    }
                }
                if($(this).parent().find("ul:first").is(":visible")){
                    $(this).parent().find("ul:first").slideUp(opts.speed, function(){
                		var obj = $(this).parent("li").find("a:first");
                        $(this).parent("li").find("span.controls:first").removeClass('smartphone-minus');
                        $(this).parent("li").find("span.controls:first").addClass('smartphone-plus');
                        //$(this).parent("li").find("span.controls:first").html('+');
                        $(this).parent("li").find("span.controls:first").attr('title', opts.closedTitle + ' ' + obj.attr('title'));
                    });
                }else{
                    $(this).parent().find("ul:first").slideDown(opts.speed, function(){
                        var obj = $(this).parent("li").find("a:first");
                        $(this).parent("li").find("span.controls:first").removeClass('smartphone-plus');
                        $(this).parent("li").find("span.controls:first").addClass('smartphone-minus');
                        //$(this).parent("li").find("span.controls:first").html('-');
                        $(this).parent("li").find("span.controls:first").attr('title', opts.openedTitle + ' ' + obj.attr('title'));
                    });
                }
            }
        });
    }
});
})(jQuery)