/*
 * jQuery Sectionate
 * 
 * Copyright (c) 2009 John Bloomfield
 *
 * http://www.bouncingorange.com
 *
 */
(function($) { 
    $.fn.extend({ 
        sectionate: function() { 
		
			return this.each(function() {
									  
    			var $this = $(this);
				var c = $this.children();
				var s = $('<div class="section"></div>'); s = $(s);
				
				for(i=0; i<c.length; i++) {
					if((c[i].tagName == "P") && (i == 0)) {
						// do nothing
					} else {
						// detect H3 elements
						if(c[i].tagName == "H3") {
							s.insertBefore(c[i]);
							s = $('<div class="section"></div>');
						} else {
							s.append(c[i]);
						}
					}
				}
				$this.append(s);
				
				// make the first H3 open by default.
				//$this.children('h3:first').toggleClass('open');
				//$this.children('h3:first').next('.section').css('display','block');
				
				// detect the click on H3 in texte
				$this.children('h3').click(function(){
					if($(this).hasClass('open')) {
						$('.section').slideUp();
						$(this).toggleClass('open');
					} else {
						$this.children('h3').removeClass('open');
						$(this).toggleClass('open');
						$('.section').slideUp();
						$(this).next('.section').slideDown();
					}
					return false;
				});
				
			});
			
        }
    }); 
})(jQuery); 
