jQuery.fn.section=function(opt){
  
	var open = false;
	if(opt==null)var opt={};
  
   if(opt.func == 'init'){
	   this.each(function(){
		    if($(this).data('init')!=null && $(this).data('init'))return;
		    $(this).data('init',true);
	   		$(this).addClass('section');
	   		
	   		/* set TOP */
		    var icon = $('<div class="expander ui-icon ui-helper-clearfix"  ></div>');
		    
		    var text = $(this).children('.top').html();
		    var color = $(this).children('.top').css('color');
		    
		    
		    $(this).children('.top').prepend(icon);
		    
		    if(opt.topStyle!=null)$(this).children('.top').css(opt.topStyle);
		    
		    if($(this).children('.top').attr('class').indexOf('ui-widget-header')==-1){
		    	$(this).children('.top').attr('class', 'ui-widget-header ' + $(this).children('.top').attr('class'));
		    }
		    
		    $(this).children('.top').addClass('ui-corner-top');
		    icon.attr('title','massimizza');
		    
		    if(!$(this).children('.body').is(':visible') && $(this).is(':visible') ){
		        $(this).children('.top').addClass('ui-corner-all');
		        icon.addClass('ui-icon-circle-triangle-s');    
		    }else{
		        icon.addClass('ui-icon-circle-triangle-n');
		        icon.attr('title','minimizza');
		    } 
		    
		    
		    
		    if(opt.bodyStyle!=null)$(this).children('.body').css(opt.bodyStyle);
		    $(this).children('.body').addClass('ui-corner-bottom');
		    
		    
		    
		    icon.css('top', ($(this).children('.top').height()/2) - (icon.css('padding-top') + 2));
		    
		    
	   });

   }
  
  if(opt.func == 'slideUp'){
	  this.each(function(){
		  if($(this).hasClass("noSlide"))$(this).parent().children('.body').hide();
		  else $(this).parent().children('.body').slideUp(50);

		  
		  $(this).parent().height('auto');
		  
		  $(this).addClass('ui-corner-all');
		  $(this).children('.expander').attr('title','massimizza');
		  $(this).children('.expander').removeClass('ui-icon-circle-triangle-n').addClass('ui-icon-circle-triangle-s');
		  
		  //evento associato
		  var event = $(this).parent().attr('close');
		  if(event != null)
			  	eval(  event.replace("$(this)","$(this).parents('.section:first')") );
	  });

  }
  
  if(opt.func == 'slideDown'){
	  this.each(function(){
		  if($(this).hasClass("noSlide"))$(this).parent().children('.body').show();
		  else $(this).parent().children('.body').slideDown(50);
		  $(this).removeClass('ui-corner-all');
		  $(this).children('.expander').attr('title','minimizza');
		  $(this).children('.expander').removeClass('ui-icon-circle-triangle-s').addClass('ui-icon-circle-triangle-n');
		  
		  
		  $(this).parent().height('auto');
		 //evento associato
		  var event = $(this).parent().attr('open');
		  if(event != null)
			  	eval(  event.replace("$(this)","$(this).parents('.section:first')") );
		  
		  
	  });
  }
   
  if(opt.func == 'slide'){
	  this.each(function(){
	      if($(this).parent().children('.body').is(':visible'))$(this).section({func : 'slideUp'});
	      else $(this).section({func : 'slideDown'});
	  });
  }
 
  if(opt.func == null){
	  this.each(function(){

		  if($(this).data('init')!=null && $(this).data('init'))return;
		  $(this).section({func : 'init'});
		  if($(this).hasClass('locked'))return;
		  if($(this).hasClass('hover')){
			  $(this).hover( function(){ $(this).children('.top').section({func : 'slideDown'})}, function(){$(this).children('.top').section({func : 'slideUp'})});
			  return;
		  }

		  if($(this).hasClass('draggable')){
			  $(this).children('.top').css('cursor','move');
		  }
		  
		  $(this).children('.top').children('.expander').click(function(){
			  	$(this).parent().section({func : 'slide'}); 
		  });
		 
	  });
  }
  
};

