/**
 * jQuery.accordion - html5 accordion 
 * Written by Dominique Feyer (dominique DOT feyer AT reelpeek DOT net)
 * Date: 2010/07/26
 *
 * @author Dominique Feyer
 * @version 1.0.0
 *
 **/
new function($) {
    $.fn.accordion = function(settings) {
        settings = settings || {};

        var $overClassName = settings.overClassName || 500;

		var $thisSection = this;
		var $children = this.children('article');
		
		function slideUpAll($section) {
			$section.children('article').each(function(){
				var $thisSection = $(this).children('section');
				var $thisArticle = $(this).closest('article');
				if ($thisSection.is(':visible')) {
					$thisSection.stop(true, true).slideUp(300);
					$thisArticle.removeClass('active');
				}
			});
		}

		var $firstChildren = true;
		
		$children.each(function(){
			var $thisChildren = $(this);
			var $headerLink = $thisChildren.find('h2 a, h3 a');
			var $section = $thisChildren.children('section');
			
			if (!$firstChildren) {
				$section.addClass("hide");
				$thisChildren.removeClass("active");
			}
			$firstChildren = false;
			
			// if ($headerLink.attr('href') == '#') {
				$headerLink.bind('click', function(){
					var $thisSection = $(this).closest('article').children('section');
					var $thisArticle = $thisSection.closest('article');
					var $parentSection = $thisSection.parent().closest('section');
					
					if (!$thisSection.is(':visible')) {
						slideUpAll($parentSection);
						$thisSection.stop(true, true).slideDown(300);
						$thisArticle.addClass('active');
					} else {
						slideUpAll($parentSection);
					}
				});
			// }
			
		});
		
		$thisSection.fadeIn(800);
    };
}(jQuery);