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

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

    //pass the options variable to the function
    accordion: function(options) {
        
		var defaults = {
			accordion: 'true',
			speed: 300,
			closedSign: '[+]',
			openedSign: '[-]'
		};

		// 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
 				//$(this).find("a:first").append("<span>"+ opts.closedSign +"</span>");
 				
 				//avoid jumping to the top of the page when the href is an #
// 				if($(this).find("a:first-child").attr('href') == "#"){
// 		  			$(this).click(function(){return false;});
// 		  		}
 			}
                            
 		});

 		//open active level
 		$this.find("li.current-menu-item").each(function() {
 			$(this).parent("ul").slideDown(opts.speed);                    
 			$(this).children("ul").slideDown(opts.speed);
                        return false;
 		});
 		$this.find("li.current-menu-ancestor").each(function() {
                        if($(this).find("a").attr('href') == "http://www.cantinaroeno.com/agriturismo-e-locanda"){
                            return false;
                        }                                           
 			$(this).parent("ul").slideDown(opts.speed);                    
 			$(this).children("ul").slideDown(opts.speed);
 		});
 		$this.find("li.menu-item-44").each(function() {
                        if((location.href).match(/news=/)){
                        $(this).addClass('current-menu-item');    
 			$(this).parent("ul").slideDown(opts.speed);                    
 			$(this).children("ul").slideDown(opts.speed);
                        }
 		});
 		$this.find("li.menu-item-45").each(function() {
                        if((location.href).match(/stampa=/)){
                        $(this).addClass('current-menu-item');    
 			$(this).parent("ul").slideDown(opts.speed);                    
 			$(this).children("ul").slideDown(opts.speed);
                        }
 		});
 		$this.find("li.menu-item-46").each(function() {
                        if((location.href).match(/press-release=/)){
                        $(this).addClass('current-menu-item');    
 			$(this).parent("ul").slideDown(opts.speed);                    
 			$(this).children("ul").slideDown(opts.speed);
                        }
 		});
 		$this.find("li.menu-item-48").each(function() {
                        if((location.href).match(/evento/)){
                        $(this).addClass('current-menu-item');    
 			$(this).parent("ul").slideDown(opts.speed);                    
 			$(this).children("ul").slideDown(opts.speed);
                        }
 		});
 		$this.find("li.menu-item-49").each(function() {
                        if((location.href).match(/video=/)){
                        $(this).addClass('current-menu-item');    
 			$(this).parent("ul").slideDown(opts.speed);                    
 			$(this).children("ul").slideDown(opts.speed);
                        }
 		});
 		$this.find("li.menu-item-5").each(function() {
                        if((location.href).match(/selezioni/)){                                                    
 			$(this).parent("ul").slideDown(opts.speed);                    
 			$(this).children("ul").slideDown(opts.speed);
                        $this.find("li.menu-item-8").each(function() {                            
                        $(this).addClass('current-menu-item');    
                        $(this).parent("ul").slideDown(opts.speed);                                            
 			$(this).children("ul").slideDown(opts.speed); 
                        });
                        }
 		}); 
 		$this.find("li.menu-item-5").each(function() {
                        if((location.href).match(/vignieti/)){                                                    
 			$(this).parent("ul").slideDown(opts.speed);                    
 			$(this).children("ul").slideDown(opts.speed);
                        $this.find("li.menu-item-13").each(function() {
                        $(this).addClass('current-menu-item');    
                        $(this).parent("ul").slideDown(opts.speed);                                            
 			$(this).children("ul").slideDown(opts.speed); 
                        });
                        }
 		});     
 		$this.find("li.menu-item-5").each(function() {
                        if((location.href).match(/spumanti-e-grappe/)){                                                    
 			$(this).parent("ul").slideDown(opts.speed);                    
 			$(this).children("ul").slideDown(opts.speed);
                        $this.find("li.menu-item-28").each(function() {
                        $(this).addClass('current-menu-item');    
 			$(this).parent("ul").slideDown(opts.speed);                                            
 			$(this).children("ul").slideDown(opts.speed); 
                        });
                        }
 		});     
  		$this.find("li a").click(function() {
                     	if($(this).attr('href') == "#"){
                            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:first").html(opts.closedSign);
                                                                            });

                                                                    }
                                                            }
                                                    });
                                            }
                                    }
                                    if($(this).parent().find("ul:first").is(":visible")){
                                            $(this).parent().find("ul:first").slideUp(opts.speed, function(){
                                                    $(this).parent("li").find("span:first").delay(opts.speed).html(opts.closedSign);
                                            });


                                    }else{
                                            $(this).parent().find("ul:first").slideDown(opts.speed, function(){
                                                    $(this).parent("li").find("span:first").delay(opts.speed).html(opts.openedSign);
                                            });
                                    }
                            }
                        }
  		});
    }
});
})(jQuery);
