/* 
Simple JQuery menu.
HTML structure to use:

Copyright 2007-2010 by Marco van Hylckama Vlieg

web: http://www.i-marco.nl/weblog/
email: marco@i-marco.nl

Free to use any way you like.
*/


jQuery.fn.initMenu = function() {
    return this.each(function(){
		var linksArray = document.getElementById("subnav").getElementsByTagName("a");
		var path = window.location.toString();
		var page = path.substring(path.lastIndexOf("/"));
		var botoxCount = 0;
		
		for(var i in linksArray) {
			try {
				var link = linksArray[i];
				var str = link.toString();
				str = str.substring(str.lastIndexOf("/"));
				if(str == page) {
					if(str == "/botox-cosmetic") botoxCount++;
					if(botoxCount == 1) continue;
					else {
						if(!link.parentNode.parentNode.id) link.parentNode.parentNode.parentNode.className = 'expand';
						link.className = 'selected';
						break;
					}
				}
			} catch(err) { alert(err.message); }
		}
		
        var theMenu = $(this).get(0);
        $('.acitem', this).hide();
        $('li.expand > .acitem', this).show();
        $('li.expand > .acitem', this).prev().addClass('active');
        $('li a', this).click(
            function(e) {
                e.stopImmediatePropagation();
                var theElement = $(this).next();
                var parent = this.parentNode.parentNode;
                if($(parent).hasClass('noaccordion')) {
                    if(theElement[0] === undefined) {
                        window.location.href = this.href;
                    }
                    $(theElement).slideToggle('normal', function() {
                        if ($(this).is(':visible')) {
                            $(this).prev().addClass('active');
                        }
                        else {
                            $(this).prev().removeClass('active');
                        }    
                    });
                    return false;
                }
                else {
                    if(theElement.hasClass('acitem') && theElement.is(':visible')) {
                        if($(parent).hasClass('collapsible')) {
                            $('.acitem:visible', parent).first().slideUp('normal', 
                            function() {
                                $(this).prev().removeClass('active');
                            }
                        );
                        return false;  
                    }
                    return false;
                }
                if(theElement.hasClass('acitem') && !theElement.is(':visible')) {         
                    $('.acitem:visible', parent).first().slideUp('normal', function() {
                        $(this).prev().removeClass('active');
                    });
                    theElement.slideDown('normal', function() {
                        $(this).prev().addClass('active');
                    });
                    return false;
                }
            }
        }
    );
});
};

$(document).ready(function() {$('.menu').initMenu();});
