/**
 * Accordeon - jQuery plugin
 * @version: 1.0 (2011/07/07)
 * @requires jQuery v1.5.2 or later 
 * @author Alexandre Beaulieu
 
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
**/


(function($) {
	
	// Globals
	var opts = new Array();	
	
	$.fn.accordeon = $.fn.accordeon = function(options){
		
		// Intiallisation
		init = function(dl){
		
			//Options
			opts[dl.id] = $.extend({}, $.fn.accordeon.defaults, options); //opts[dl.id].width);
			
			
			switch(opts[dl.id].effect)
			{
				case 'normal':
				    $.normal(dl);
				break;

                case 'fade':
				    $.fade(dl);
				break;

                case 'fademix':
				    $.fademix(dl);
				break;

                case 'bounce':
				    $.bounce(dl);
				break;
				  
				default:
				    $.normal(dl);
			}
			
		};
		
		// Default movement. Open the list in a linear way
		$.normal = function(dl) {

			var l  =  opts[dl.id].speed;             // speed of the animation
			var ac =  opts[dl.id].activeClass;       // Class for active button
			var dc =  opts[dl.id].dropDownClass;     // Class of the dropDown to animate
			var cc =  opts[dl.id].contentClass;      // Class of the content
			
			var sub = '.'+opts[dl.id].dropDownClass; 
			
            $('.'+dl.className).children('.'+cc).hide();
			
			$('.'+dl.className+' .'+dc).click(function() {
			

                    $(this).parent().children('.'+cc+":not(:animated)").siblings('.'+dc).toggleClass(ac);
                    $(this).parent().children('.'+cc+":not(:animated)").slideToggle(l);
            
                    $(this).parent().siblings('.'+dl.className).children('.'+cc).slideUp(l);
                    $(this).parent().siblings('.'+dl.className).children('.'+dc).removeClass(ac);
				
			});
		}

        // Open the list, then fade in. Fade out, then close the list
        $.fade = function(dl) {

			var l  =  opts[dl.id].speed;             // speed of the animation
			var ac =  opts[dl.id].activeClass;       // Class for active button
			var dc =  opts[dl.id].dropDownClass;     // Class of the dropDown to animate
			var cc =  opts[dl.id].contentClass;      // Class of the content
            var aa =  opts[dl.id].activeAnimation;   // active Animation
			
			var sub = '.'+opts[dl.id].dropDownClass; 
			
            $('.'+dl.className).children('.'+cc).hide();
			
			$('.'+dl.className+' .'+dc).click(function() {
			

                    $(this).parent().children('.'+cc+":not(:animated)").siblings('.'+dc).toggleClass(ac);

                    
                    if(aa == 'inout') {
                        if($(this).parent().children('.'+dc).hasClass('open') == true) {
                            $(this).parent().children('.'+cc+":not(:animated)").css({opacity : "0"});
                            $(this).parent().children('.'+cc+":not(:animated)").slideToggle(l, function(){
                                $(this).fadeTo(l, 1);
                            });
                        } else {
                            $(this).parent().children('.'+cc+":not(:animated)").css({opacity : "1"});
                            $(this).parent().children('.'+cc+":not(:animated)").fadeTo(l, 0, function(){
                                $(this).slideToggle(l);
                            });
                        }
                    } else if(aa == 'in') {
                        if($(this).parent().children('.'+dc).hasClass('open') == true) {
                            $(this).parent().children('.'+cc+":not(:animated)").css({opacity : "0"});
                            $(this).parent().children('.'+cc+":not(:animated)").slideToggle(l, function(){
                                $(this).fadeTo(l, 1);
                            });
                        } else {
                            $(this).parent().children('.'+cc+":not(:animated)").css({opacity : "1"});
                            $(this).parent().children('.'+cc+":not(:animated)").slideToggle(l);
                        }
                        
                    } else if(aa == 'out') {
                        if($(this).parent().children('.'+dc).hasClass('open') == true) {
                            $(this).parent().children('.'+cc+":not(:animated)").css({opacity : "1"});
                            $(this).parent().children('.'+cc+":not(:animated)").slideToggle(l);
                        } else {
                            $(this).parent().children('.'+cc+":not(:animated)").css({opacity : "1"});
                            $(this).parent().children('.'+cc+":not(:animated)").fadeTo(l, 0, function(){
                                $(this).slideToggle(l);
                            });
                        }
                        
                    }


                    /*
if($(this).parent().children('.'+dc).hasClass('open') == true) {
                            $(this).parent().children('.'+cc+":not(:animated)").css({opacity : "0"});
                            $(this).parent().children('.'+cc+":not(:animated)").slideToggle(l, function(){
                                $(this).fadeTo(l, 1);
                            });
                    } else {
                            $(this).parent().children('.'+cc+":not(:animated)").css({opacity : "1"});
                                $(this).parent().children('.'+cc+":not(:animated)").fadeTo(l, 0, function(){
                                    $(this).slideToggle(l);
                                });
                    }
*/
                    
                    
            
                    $(this).parent().siblings('.'+dl.className).children('.'+cc).slideUp(l);
                    $(this).parent().siblings('.'+dl.className).children('.'+dc).removeClass(ac);
				
			});
		}

        // Open the list while fading in. Close the list while fading out
        $.fademix = function(dl) {

			var l  =  opts[dl.id].speed;             // speed of the animation
			var ac =  opts[dl.id].activeClass;       // Class for active button
			var dc =  opts[dl.id].dropDownClass;     // Class of the dropDown to animate
			var cc =  opts[dl.id].contentClass;      // Class of the content
            var aa =  opts[dl.id].activeAnimation;   // active Animation
			
			var sub = '.'+opts[dl.id].dropDownClass; 
			
            $('.'+dl.className).children('.'+cc).hide();
			
			$('.'+dl.className+' .'+dc).click(function() {
			

                    $(this).parent().children('.'+cc+":not(:animated)").siblings('.'+dc).toggleClass(ac);


                    if(aa == 'inout') {
                        
                        if($(this).parent().children('.'+dc).hasClass('open') == true) {
                            $(this).parent().children('.'+cc+":not(:animated)").css({opacity : "0"});
                            $(this).parent().children('.'+cc+":not(:animated)").slideToggle({queue: false, duration: l}).fadeTo(l, 1);
                        } else {
                            $(this).parent().children('.'+cc+":not(:animated)").css({opacity : "1"});
                            $(this).parent().children('.'+cc+":not(:animated)").fadeTo({queue:false, duration:l}, 0).slideToggle(l);
                        }  
                    } else if(aa == 'in') {
                        if($(this).parent().children('.'+dc).hasClass('open') == true) {
                            $(this).parent().children('.'+cc+":not(:animated)").css({opacity : "0"});
                            $(this).parent().children('.'+cc+":not(:animated)").slideToggle({queue: false, duration: l}).fadeTo(l, 1);
                        } else {
                            $(this).parent().children('.'+cc+":not(:animated)").css({opacity : "1"});
                            $(this).parent().children('.'+cc+":not(:animated)").slideToggle(l);
                        } 
                    } else if(aa == 'out') {
                        if($(this).parent().children('.'+dc).hasClass('open') == true) {
                            $(this).parent().children('.'+cc+":not(:animated)").css({opacity : "1"});
                            $(this).parent().children('.'+cc+":not(:animated)").slideToggle(l);
                        } else {
                            $(this).parent().children('.'+cc+":not(:animated)").css({opacity : "1"});
                            $(this).parent().children('.'+cc+":not(:animated)").fadeTo({queue:false, duration:l}, 0).slideToggle(l);
                        } 
                    }
                    
                    
            
                    $(this).parent().siblings('.'+dl.className).children('.'+cc).slideUp(l);
                    $(this).parent().siblings('.'+dl.className).children('.'+dc).removeClass(ac);
				
			});
		}
        
        // Makes the bottom bounce when opened
        $.bounce = function(dl) {

			var l  =  opts[dl.id].speed;             // speed of the animation
			var ac =  opts[dl.id].activeClass;       // Class for active button
			var dc =  opts[dl.id].dropDownClass;     // Class of the dropDown to animate
			var cc =  opts[dl.id].contentClass;      // Class of the content
			
			var sub = '.'+opts[dl.id].dropDownClass; 
			
            $('.'+dl.className).children('.'+cc).hide();
			
			$('.'+dl.className+' .'+dc).click(function() {
			

                    $(this).parent().children('.'+cc+":not(:animated)").siblings('.'+dc).toggleClass(ac);

                    
                    if($(this).parent().children('.'+dc).hasClass('open') == true) {
                        $(this).parent().children('.'+cc+":not(:animated)").slideToggle(l, function(){
                            $(this).animate({height: $(this).height()-50}, l/2, function() {
                                $(this).animate({height: $(this).height()+50}, l/2, function() {
                                    $(this).animate({height: $(this).height()-20}, l/2, function() {
                                        $(this).animate({height: $(this).height()+20}, l/2);
                                    });
                                });
                            });
                        });
                    } else {
                        $(this).parent().children('.'+cc+":not(:animated)").slideToggle(l);         
                    }
            
                    $(this).parent().siblings('.'+dl.className).children('.'+cc).slideUp(l);
                    $(this).parent().siblings('.'+dl.className).children('.'+dc).removeClass(ac);
				
			});
		}

		
		this.each (
			function(){ init(this); }
		);	
	};
	
	
	
	// default values
	$.fn.accordeon.defaults = {
		effect: 		'normal', 	      // normal, fade, fademix, bounce
        speed:          500,              // Speed of the animation
		dropDownClass: 	'textslider',     // Class for the dropdown elements
		activeClass:    'open',           // Class active for the button
        contentClass:   'dropDownContent', // Class for the content
        activeAnimation:'inout'           // Set when animation will take place (in, out, inout) Only works for fade and fademix
	};
	
})(jQuery);

/*
---------------------------------------------------------------------------
    HOW TO CALL IT
---------------------------------------------------------------------------
    $('.dropbox').accordeon({
    	effect : 'fademix',
    	speed : 500,
        dropDownClass : "textslider",
        activeClass : "open",
        contentClass : "dropDownContent",
        activeAnimation : "inout"
    });
---------------------------------------------------------------------------
    HTML EXAMPLE
---------------------------------------------------------------------------
<div class="dropbox">
    <p class="textslider">SOME TITLE</p>
    <div class="dropDownContent">
        <ul>
            <li>Some text 1</li>
            <li>Some text 2</li>
            <li>Some text 3</li>
            <li>Some text 4</li>
            <li>Some text 5</li>
        </ul>
    </div>    
</div>
---------------------------------------------------------------------------
    CSS EXAMPLE
---------------------------------------------------------------------------
body div.dropbox p.textslider{
    padding-left:15px;
    color: #4897c6;
    letter-spacing: 1px;
    line-height: 18px;
    font-weight: normal;
    font-family: Arial, Helvetica, Verdana, sans-serif;
    margin-bottom:20px;
    font-size: 14px;
    background-image: url("../medias/images/fleche_liste_accordeon_close.gif");
    background-repeat: no-repeat;
    background-position: 0px 4px;
    cursor: pointer;
}

body div.dropbox p.textslider.open{
    background-image: url("../medias/images/fleche_liste_accordeon_open.gif");
}


body div.dropbox ul{
    margin-bottom:20px;
    width:280px;
    overflow:hidden;
}

body div.dropbox ul li{
    list-style-type: none;
    background-image: url("../medias/images/puce2.gif");
    background-repeat: no-repeat;
    background-position: 20px 7px;
    padding-left:31px;
    color: #4c4d4f;
    letter-spacing: 1px;
    line-height: 18px;
    font-weight: normal;
    font-family: Arial, Helvetica, Verdana, sans-serif;
    margin-bottom:5px;
    font-size: 12px;
}
---------------------------------------------------------------------------
*/
