
// COLLAPSIBLE CONTENT PLUGIN
jQuery.fn.collapseContent = function(options){
	var opts = jQuery.extend({}, jQuery.fn.collapseContent.defaults, options);
	
	collapseThis(); // run collapseThis function
	
	function collapseThis(){  
	
		var sectionGroup = jQuery(".collapseWrap .sectionGroup");
		var sectionItem = jQuery(".collapseWrap .sectionGroup li");
		var triggerAcc = sectionItem.children("h2");
		
		// generate class markup
		jQuery(sectionItem).addClass("sectionItem");
		jQuery(sectionItem).children("h2").addClass("triggerAcc");
		jQuery(sectionItem).children("div").addClass("sectionContent").hide(); // hide all item content
					
		if(opts.showFirst == true){
			sectionGroup.find("li:first").find("div:first").addClass("opened").show(); // show first item content
			sectionGroup.find("h2:first").addClass("highlight"); // toggle plus/minus image
		} 
			
		triggerAcc.click(function() {
		
			var clicked = jQuery(this);
			var content = clicked.siblings(".sectionContent").stop(false, true) ;
			
			function slideAction(){
				clicked.toggleClass("highlight"); // toggle plus/minus image
				content.animate({height: "toggle"}, opts.slideSpeed).toggleClass("opened"); // animate content hide/show
				return false;	
			}; // toggle action
			
			if(opts.showOnlyOne == true){
				if(clicked.hasClass("highlight")){ // if the one you clicked is opened
					slideAction();
				} else { // if not, find the one that is opened, close it, remove highlight class, run slideAction()
					clicked.parent().parent().parent().find(".opened").toggleClass("opened").animate({height: "toggle"}, opts.slideSpeed).parent().find(".highlight").toggleClass("highlight"); // animate content hide/show	
					slideAction();
				}
			} else {			
				slideAction();
			}
		});
	};
	
};

jQuery.fn.collapseContent.defaults = {
	slideSpeed: 250,
	easingAnim: 'swing',
	showFirst: false,
	showOnlyOne: false
};
