// JavaScript Document
//
// ==================================================================
//
// JAVASCRIPT
// Stephane Chanay-Savoyen
// http://www.chanaysavoyen.com
//
// ==================================================================

(function(){
	var 
	// initialize expanding menu
	expandingMenu_init=function(id,num){
	
		var speed = 1000;
	
		/*
		if(String(num)=="null" || String(num)=="undefined"){
			for(n=0;n<$("#"+id).children("ul").length;n++){
				expandingMenu(id,n)
			}
		
		}else{*/
		
			// hide expandingMenu li
			$('#'+id+' li').each(function(){
				var s = $(this).attr("class");
				if( !s || s.indexOf("section-title")==-1 )
				{ $(this).css({display:"none"}); }
			});
			// be sure to active current
			$('#'+id+' li a').each(function(){
				if( document.location.href==this.href ){
					$(this).parent().addClass("active");
				}
			})
			// create expandingMenu elements
			$('#'+id+' ul').addClass('subMenu');
			//
			$('<ul id="navigation"></ul>').appendTo($("#"+id));
			$('#'+id+' > ul:not(#navigation)').each( function(index) {
				if(typeof n=="undefined"){n=0;}else{n+=1;}
				$("<li"+" class='toggleSubMenu'"+
	                "><a class='section-title' href='#'>"+
	                $('li.section-title:eq('+n+')').html()+
	                "</a><ul class='"+$(this).attr('class')+"'>"+
	                $(this).html()+"</ul></li>")
				.appendTo($("#navigation"));
			}); 
	        $("#"+id+' > ul:not(#navigation)').remove();
	        $("#"+id+' li.section-title').remove();
			//elt=$("li.active")[0]; elt.parentNode.attributes['class'].value="subMenu open_at_load";
	        //elt.parentNode.parentNode.attributes['class'].value="toggleSubMenu open"; 
			$("#"+id+" li.active").eq(0)
			.parent().addClass("open_at_load")
			.parent().addClass("open");
			//
	        // on corrige les styles #expandingMenu #navigation li:first-child
	        // dont les selectors marchent pas sous ie6
	        $("#"+id+" #navigation li a").eq(0)
			.css({backgroundImage:'none'});
	        //
	 		// On cache les sous-menus
	        // sauf celui qui porte la classe "open_at_load" :
	        $("#"+id+" #navigation ul.subMenu:not('.open_at_load')").hide();
	        // On selectionne tous les items de liste portant la classe "toggleSubMenu"
    		//
			// show expandingMenu li
			$("#expandingMenu li")
			.css({display:"list-item"});
			//
	        // et on remplace l'element span qu'ils contiennent par un lien :
	        $("#"+id+" li.toggleSubMenu span").each( function () {
	            // On stocke le contenu du span :
	            var TexteSpan = $(this).text();
				$(this).replaceWith('<a href="" title="Afficher le sous-menu">' + TexteSpan + '</a>') ;
	        } ) ;
    		//
	        // On modifie l'evenement "click" sur les liens dans les items de liste
	        // qui portent la classe "toggleSubMenu" :
	        $("li.toggleSubMenu > a").click( function () {
	            // remove click border
				this.blur();
				// Si le sous-menu etait deja ouvert, on le referme :
	            if ($(this).next("ul.subMenu:visible").length != 0) {
	                $(this).next("ul.subMenu").slideUp("normal", function () { $(this).parent().removeClass("open") } );
	            }
	            // Si le sous-menu est cache, on ferme les autres et on l'affiche :
	            else {
	                //$("ul.subMenu").slideUp("normal", function () { $(this).parent().removeClass("open") } );
					$("ul.subMenu").each(function(index) {
						//if( $(this).parent().hasClass('open')==true){
						if($(this).parent().attr('class')){
	                        if($(this).parent().attr('class').indexOf('open')>=0){
	                            $(this).slideUp("normal").parent().removeClass("open");
	                            //$(this).slideUp("normal");
	                        }
						}
					});
	                //$(this).next("ul.subMenu").slideDown("normal", function () { $(this).parent().addClass("open") } );
					$(this).parent().addClass("open");
					$(this).next("ul.subMenu").slideDown("normal");
	            }
	            // On empêche le navigateur de suivre le lien :
	            return false;
            
	        });
		//}
	}
	,
	// Lets run
	run=function(){
		
		// Create constructor
		var expandingMenu=expandingMenu||function(){};

		// Add to constructor
		expandingMenu.prototype.init=expandingMenu_init;

		// Add to public container for sharing (if needed...)
		// IE bug with __proto__ so used constructor.prototype instead
		window._fun_.prototype.expandingMenu=expandingMenu; // function // YES
		//window._fun_.prototype.expandingMenu=new expandingMenu(); // object // NO

		// Initialize new
		// var n=new expandingMenu(); n.init();
	
	}
	;
	var w=window; // shortcut

	// Make public container accessible
	w._fun_=w._fun_||function(){};

	// Create private instance
	var fun=new w._fun_();

	// autorun
	run();

})();
