
(function($){  
 $.fn.extend({   
	 //plugin name - animatemenu  
	 accordion: function(options) {  

		 //Settings list and the default values  
		 // ADD CLASSTYLES, TOGGLESPEED, etc to make it as dynamic as possible
		 
		var defaults = {  
			itemsAmountVisible: 5,
			toggleSpeed: 'slow',  
			showAllSpeed: 'fast',  
			showRecentSpeed: 'fast',
			showButtons: 'true'
		 };  
		   
		 var options = $.extend(defaults, options);  
	  
		 return this.each(function() {  
			var param =options;  
			   
			// assign current element to variable, in this case is UL element  
			var obj = $(this);                
			
			// Get all LI in the UL  
			// var items = $("li", obj);  
			
			// show buttons if param showButtons = true
			if (param.showButtons == 'true'){
				$("li.accordion_button_container",obj).css("display","block");
			}
			
			// set first title as active
			$("> li:first > a[rel*=title]",obj).toggleClass('title_active');
			
			// hide message_body after the first one
			$("> li > .content:gt(0)",obj).hide();

			// hide message li after the 3th (index starts at 0) but show last li because these contain the buttons
			// use as number 999 if you want to show everything
			if (param.itemsAmountVisible != 999){
				$("li:gt(" + parseInt(param.itemsAmountVisible -1) + ")",obj).not("li.accordion_button_container").hide();
			}
			else{
					$(".show_all",obj).css("display","none");
					$(".show_recent",obj).css("display","none");
			}
			  			  
			// toggle message_body
			$("a[rel*=title]",obj).click(function(){
				$(this).next(".content").slideToggle(param.toggleSpeed)
				$(this).toggleClass('title_active'); 
				$(this).parent().parent().find('> li > .collapse_all').css("display","block");
				return false;
			});
					
			// collapse all messages buttonclick
			$(".collapse_all",obj).click(function(){									  
				$(this).parent().parent().find('> li > .content').slideUp(param.toggleSpeed);
				$(this).parent().parent().find('> li > a[rel*=title]').removeClass('title');
				$(this).parent().parent().find('> li > a[rel*=title]').removeClass('title_active');
				$(this).parent().parent().find('> li > a[rel*=title]').removeClass('title_hover_ie6');
				$(this).parent().parent().find('> li > a[rel*=title]').addClass('title');
				//$(this).parent().parent().find('> li > a[rel*=title]').toggleClass('title');
				$(this).hide();
			});
		
			//show all messages
			$(".show_all",obj).click(function(){
				$(this).hide() //$(obj).find(".show_all").hide()
				$(obj).find(".show_recent").show()
				$(this).parent().parent().find('> li').slideDown(param.showAllSpeed)			   			
				return false;
			});
			
			//show recent x messages only
			$(".show_recent",obj).click(function(){
				$(this).hide()
				$(obj).find(".show_all").show()
				$(this).parent().parent().find('>li:gt(' + parseInt(param.itemsAmountVisible -1) + ')').not("li.accordion_button_container").slideUp(param.showRecentSpeed)
				return false;
			});
		
			// ie6 hover fix			
			$("a[rel*=title]",obj).mouseover( function() { $(this).addClass("title_hover_ie6"); } );
			$("a[rel*=title]",obj).mouseout( function() { $(this).removeClass("title_hover_ie6"); } );
				
		 });  
	 }  
 });  
})(jQuery); 

