(function($) {
 
   $.fn.accordion = function(settings) {
     var config = {'expandSingle': true};
 
     if (settings) $.extend(config, settings);
 		//alert("hello");
     this.children("h2").each(function() {
        //hover handling   
		$(this).hover(
				function(){
					if ($(this).hasClass("collapsed")) {
						$(this).addClass("header-hover");
					}
				},
				function(){
					if ($(this).hasClass("collapsed")) {
						 $(this).removeClass("header-hover");
					}	
				});
		
		//click handling
		$(this).click(
			function() {
				
				var cb_height = $("#contentBackground").height();
				//alert("cb_height:" + cb_height);
				var a = 0;
				if (config.expandSingle) {
					a = $(this).siblings("h2.expanded").next("div.client-container").height();
				}
				var new_height = cb_height + $(this).next("div.client-container").height() - a;
				//alert("new_height:"+new_height);
				    //expand this element
					if ($(this).hasClass("collapsed")) {
						//make the vsl para inactive
						$("#contentLower p.vsl").css("color","#636363");
						
						$(this).removeClass("collapsed");
						$(this).removeClass("header-hover");
						$(this).addClass("expanded");
						$(this).next("div.client-container").slideDown(800);
						$("#contentBackground").animate({"height": new_height + "px"},800);
					}
				
				//collapse other expanded elements
				if (($(this).siblings("h2.expanded").length > 0) && config.expandSingle) {
					//alert("hello");
					$(this).siblings("h2.expanded").next("div.client-container").slideUp(800);
					$(this).siblings("h2.expanded").addClass("collapsed");
					$(this).siblings("h2.expanded").removeClass("header-hover");
					$(this).siblings("h2.expanded").removeClass("expanded");
				}
				
			}
			);
		
		//click on close button handling
		$(this).find("img").click(function(evt) {
			var cb_height = $("#contentBackground").height();
			var new_height = cb_height - $(this).parents("h2.expanded").next("div.client-container").height();
			$(this).parents("h2.expanded").next("div.client-container").slideUp(800);
			$("#contentBackground").animate({"height": new_height + "px"},800);
			$(this).parents("h2.expanded").removeClass("expanded").addClass("collapsed");
			evt.stopPropagation();
		});
     });
 
     return this;
 
   };
 
 })(jQuery);
