/*

	jQuery toggle 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({    
	 jxToggle: function(options){
      var defaults = {
        title:"show ...",
        group:false
		};				
      var options =  $.extend(defaults, options);
      var selection = $(this);
      if(options.group){options.iconstyle='carat';}else{options.iconstyle='triangle';};
      
      return this.each(function() {
        var $obj = $(this);
        if($obj.attr('jxToggle' ) != 'ok' ){
          $obj.attr('jxToggle' , 'ok');
          var jxId =Math.ceil(100000000*Math.random());
          var caption ='';
          if($obj.attr('title')!=""){
            caption = $obj.attr('title');
          }else{
            caption = options.title;
          };
          if($obj.attr('open') =="open"){this.iconstyle = 'arrowreturnthick';}else{this.iconstyle = options.iconstyle;};
          $link = $('<div><span class="jxtoggler" style="cursor:pointer" ><span id="jxtogglericon-'+jxId+'" class="jxtoggleicon jx-inline ui-icon ui-icon-'+this.iconstyle+'-1-e" />'+caption+'</span></div>');
          $obj.before($link);
          $obj.css('display','none');
          $obj.attr('jxtoggleid', jxId);
  
          $('span.jxtoggler',$link).click(function(e){            
            //window.scrollTo(0,  e.pageY)
            if(options.group){
              selection.each(function (){
                if($(this).attr('jxtoggleid') != $obj.attr('jxtoggleid') &&  $(this).attr('open') !='open'){
                  $(this).hide();
                  $('span.jxtoggleicon',$(this).prev()).switchClass('ui-icon-'+this.iconstyle+'-1-s', 'ui-icon-'+this.iconstyle+'-1-e', 0);
                };
              });
            };
            $obj.slideToggle( function(){
              if($obj.css('display')=="none"){
                $('#jxtogglericon-'+jxId).switchClass('ui-icon-'+this.iconstyle+'-1-s', 'ui-icon-'+this.iconstyle+'-1-e', 0);
              }else{
                $('#jxtogglericon-'+jxId).switchClass('ui-icon-'+this.iconstyle+'-1-e', 'ui-icon-'+this.iconstyle+'-1-s', 0);
              };
            });           
          });
          if(options.open || $obj.hasClass('open') || $obj.attr('open') == 'open') {
            $link.children().click();
          };
        };
      }); 
    }

  }); 
})(jQuery);




