/*

	jQuery accordion plugin
	Copyright (c) 2010 Gael Jaunin

	For support and tutorials visit
	http://www.xuserver.net/

	License: GNU Lesser General Public License (LGPL) 
	at http://opensource.org/licenses/lgpl-2.1.php

	This plugin is free software;  you can redistribute it  and/or  modify  it 
	under the terms of the GNU Lesser General Public License as  published  by 
	the Free Software Foundation;  either version 2.1 of the License,  or  (at 
	your option) any later version.
	This software is distributed in the hope  that  it  will  be  useful,  but 
	WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
	or FITNESS FOR A PARTICULAR PURPOSE.  See the  GNU Lesser  General  Public 
	License for more details.
	You should have received a copy of  the  GNU Lesser General Public License 
	along with this library;  if not,  write to the  Free Software Foundation, 
	Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

	THIS COMMENT AND COPYRIGHT NOTICE MUST BE RETAINED IN THE CODE AS IS FOR LEGAL USE

*/

(function($){
  $.fn.extend({
    
    jxAccordion:function (options){
      var obj = $(this);        
      defaults = {
        header :'h3',
        icons : true,
        buttonClass:'jxAccordionMenu',
        onOpen:function(accordion, child){},
        onExpend:function(accordion, child){},
        onClose:function(accordion, child){},
        onCollapse:function(accordion, child){}
        
      };
      var options = $.extend(defaults, options);
      var setIcons = function(o){
        if(o.css('display') != 'none'){
          $('span.jxAccordionIcon',o.prev() ).removeClass('ui-icon-triangle-1-e').addClass('ui-icon-triangle-1-s');
          o.prev().removeClass('ui-corner-all').addClass('ui-corner-top');
        }else{
          $('span.jxAccordionIcon',o.prev() ).removeClass('ui-icon-triangle-1-s').addClass('ui-icon-triangle-1-e');
          o.prev().removeClass('ui-corner-top').addClass('ui-corner-all').css('margin-bottom','2px');
        };
      };
      
      if(options.icons){
        $(options.header, this).prepend('<span class="jxAccordionIcon jx-inline ui-icon " />');
      };
      
      $(options.header, this).each(function(){
    	 $(this).next().addClass("jxAccordionWindow"); 
      });
      
      $(options.header, this)
      .addClass('ui-accordion-header ui-helper-reset ui-state-default ui-corner-top')
      .hover(function(){$(this).addClass('ui-state-hover'); } ,function(){$(this).removeClass('ui-state-hover');} )
      .css('padding','3px').css('cursor','pointer')
      .click(function() {
        $(this).css('margin-bottom','0px').next().slideToggle('fast', function(){ 
          if(options.icons){
            setIcons ( $(this) );
          };
          if($(this).is(':visible')){
        	  options.onOpen(obj,$(this));
        	  if($(".jxAccordionWindow:hidden", obj ).length <1 ){
        		  options.onExpend(obj,$(this));
        	  }
          }else{
        	  options.onClose(obj,$(this));
        	  if($(".jxAccordionWindow:visible", obj ).length <1 ){
        		  options.onCollapse(obj,$(this));
        	  }
        	  
          }
          
          
        });
        return false;
      }).css('margin-bottom','0px');
      
      $(options.header, this)
        .next()
        .css('padding','5px').css('margin-bottom','2px').css('border-top','0px')
        .addClass('ui-widget ui-widget-content ui-corner-bottom')
        .hide();
      
      $('a.'+options.buttonClass,this).css('margin-bottom','2px')
        .css('display','block')
        .css('outline','none')
        .button();
            
      setIcons ( $(options.header, this).next() );
      var i=0;
      
      $(options.header, this).each(function(){
    	  var a = $(this);
    	  a._open=function (){
        	  a.next().css("color","blue");
          };
      })
      
      $(options.header, this).each(function(){
        i++;
        if($(this).attr('open') == ''){
          $(this).css('margin-bottom','0px').next().hide();
        }else if(i==1 || $(this).attr('open') == 'open'){
          $(this).css('margin-bottom','0px').removeClass('ui-corner-all').addClass('ui-corner-top').next().show();
        };
      });
      
    }
    
	}); 
})(jQuery);

