/*
 * jQuery Expander plugin
 * Version 0.4  (12/09/2008)
 * @requires jQuery v1.1.1+
 *
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */


(function($j) {

  $j.fn.expander = function(options) {

    var opts = $j.extend({}, $j.fn.expander.defaults, options);
    var delayedCollapse;
    return this.each(function() {
      var $jthis = $j(this);
      var o = $j.meta ? $j.extend({}, opts, $jthis.data()) : opts;
     	var cleanedTag, startTags, endTags;	
     	var allText = $jthis.html();
     	var startText = allText.slice(0, o.slicePoint).replace(/\w+$j/,'');
     	startTags = startText.match(/<\w[^>]*>/g);
   	  if (startTags) {startText = allText.slice(0,o.slicePoint + startTags.join('').length).replace(/\w+$j/,'');}
   	  
     	if (startText.lastIndexOf('<') > startText.lastIndexOf('>') ) {
     	  startText = startText.slice(0,startText.lastIndexOf('<'));
     	}
     	var endText = allText.slice(startText.length);    	  
     	// create necessary expand/collapse elements if they don't already exist
   	  if (!$j('span.details', this).length) {
        // end script if text length isn't long enough.
       	if ( endText.replace(/\s+$j/,'').split(' ').length < o.widow ) { return; }
       	// otherwise, continue...    
       	if (endText.indexOf('</') > -1) {
         	endTags = endText.match(/<(\/)?[^>]*>/g);
          for (var i=0; i < endTags.length; i++) {

            if (endTags[i].indexOf('</') > -1) {
              var startTag, startTagExists = false;
              for (var j=0; j < i; j++) {
                startTag = endTags[j].slice(0, endTags[j].indexOf(' ')).replace(/(\w)$j/,'$j1>');
                if (startTag == rSlash(endTags[i])) {
                  startTagExists = true;
                }
              }              
              if (!startTagExists) {
                startText = startText + endTags[i];
                var matched = false;
                for (var s=startTags.length - 1; s >= 0; s--) {
                  if (startTags[s].slice(0, startTags[s].indexOf(' ')).replace(/(\w)$j/,'$j1>') == rSlash(endTags[i]) 
                  && matched == false) {
                    cleanedTag = cleanedTag ? startTags[s] + cleanedTag : startTags[s];
                    matched = true;
                  }
                };
              }
            }
          }

          endText = cleanedTag && cleanedTag + endText || endText;
        }
     	  $jthis.html([
     		startText,
     		'<span class="read-more">',
     		o.expandPrefix,
       		'<a href="#">',
       		  o.expandText,
       		'</a>',
        '</span>',
     		'<span class="details">',
     		  endText,
     		'</span>'
     		].join('')
     	  );
      }
      var $jthisDetails = $j('span.details', this),
        $jreadMore = $j('span.read-more', this);
   	  $jthisDetails.hide();
 	    $jreadMore.find('a').click(function() {
 	      $jreadMore.hide();

 	      if (o.expandEffect === 'show' && !o.expandSpeed) {
          o.beforeExpand($jthis);
 	        $jthisDetails.show();
          o.afterExpand($jthis);
          delayCollapse(o, $jthisDetails);
 	      } else {
          o.beforeExpand($jthis);
 	        $jthisDetails[o.expandEffect](o.expandSpeed, function() {
            $jthisDetails.css({zoom: ''});
            o.afterExpand($jthis);
            delayCollapse(o, $jthisDetails);
 	        });
 	      }
        return false;
 	    });
      if (o.userCollapse) {
        $jthis
        .find('span.details').append('<span class="re-collapse">' + o.userCollapsePrefix + '<a href="#">' + o.userCollapseText + '</a></span>');
        $jthis.find('span.re-collapse a').click(function() {

          clearTimeout(delayedCollapse);
          var $jdetailsCollapsed = $j(this).parents('span.details');
          reCollapse($jdetailsCollapsed);
          o.onCollapse($jthis, true);
          return false;
        });
      }
    });
    function reCollapse(el) {
       el.hide()
        .prev('span.read-more').show();
    }
    function delayCollapse(option, $jcollapseEl) {
      if (option.collapseTimer) {
        delayedCollapse = setTimeout(function() {  
          reCollapse($jcollapseEl);
          option.onCollapse($jcollapseEl.parent(), false);
          },
          option.collapseTimer
        );
      }
    }
    function rSlash(rString) {
      return rString.replace(/\//,'');
    }    
  };
    // plugin defaults
  $j.fn.expander.defaults = {
    slicePoint:       200,  // the number of characters at which the contents will be sliced into two parts. 
                            // Note: any tag names in the HTML that appear inside the sliced element before 
                            // the slicePoint will be counted along with the text characters.
    widow:            4,  // a threshold of sorts for whether to initially hide/collapse part of the element's contents. 
                          // If after slicing the contents in two there are fewer words in the second part than 
                          // the value set by widow, we won't bother hiding/collapsing anything.
    expandText:       '<br/>Read more...', // text displayed in a link instead of the hidden part of the element. 
                                      // clicking this will expand/show the hidden/collapsed text
    expandPrefix:     '&hellip; ',
    collapseTimer:    0, // number of milliseconds after text has been expanded at which to collapse the text again
    expandEffect:     'fadeIn',
    expandSpeed:      '',   // speed in milliseconds of the animation effect for expanding the text
    userCollapse:     true, // allow the user to re-collapse the expanded text.
    userCollapseText: '<br/>Read Less...',  // text to use for the link to re-collapse the text
    userCollapsePrefix: ' ',
    beforeExpand: function($jthisEl) {},
    afterExpand: function($jthisEl) {},
    onCollapse: function($jthisEl, byUser) {}
  };
})(jQuery);

$j(document).ready(function() {

  // simple example, using all default options
  $j('div.expandable p').expander();
  
  // *** OR ***
  
  // override some default options
  $j('div.expandable p').expander({
    slicePoint:       80,  // default is 100
    expandText:         '', // default is 'read more...'
    collapseTimer:    5000, // re-collapses after 5 seconds; default is 0, so no re-collapsing
    userCollapseText: ''  // default is '[collapse expanded text]'
  });
  
});
