﻿/*
 * 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($) {

  $.fn.expander = function(options) {

    var opts = $.extend({}, $.fn.expander.defaults, options);
    var delayedCollapse;
    return this.each(function() {
      var $this = $(this);
      var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
     	var cleanedTag, startTags, endTags;	
     	var allText = $this.html();
     	
     	// jwg 2010-04-10 - adjust slicepoint forward to allow for leading anchors
			var didadjust = false;
     	var slicept = o.slicePoint;
     	var x = allText.indexOf('<p class="entry-content'); // detect standard noticelist content start 
     	var hiy = x;
     	var startcontent = hiy; // jwg 2010-06-15
     	var sliceinc = 0;	// jwg 2010-05-03
     	if (x > -1) {
     		x = allText.indexOf('>', x);
     		x += 1; // bypass opening <p..
				var y = 0;	// scan past leading anchor referrents
				var t = 0; // DEBUG
				while ((allText.substr(x,3) == '<a ') || 
							 (allText.substr(x,4) == ' <a ') || 
							 (allText.substr(x,9) == '&nbsp;<a ') || 
							 (allText.substr(x+1,9) == '&nbsp;<a ') ||
							 (allText.substr(x,6) == 'RT <a ') || 
							 (allText.substr(x+1,6) == 'RT <a ') ||
							 (allText.substr(x,12) == 'RT &nbsp;<a ') || 
							 (allText.substr(x+1,12) == 'RT &nbsp;<a ')
							 ) {
					t++; 					// jwg 2010-05-03
					y = allText.indexOf('</a>',x);
					if (y > -1) { 
						x = y+4;
						hiy = x;
						while ((x < allText.length) && 
									 ((allText.substr(x,1) == ' ') || 
									  (allText.substr(x,1) == "\n") || 
									  (allText.substr(x,1) == ",") ||
									  (allText.substr(x,1) == ":"))) { 
							x++; hiy=x; 
						} 
					} else {
						break;
					}
				}
				if (hiy > startcontent) slicept += (hiy-startcontent); // jwg 2010-06-15
				if (t>0) {
					slicept += t*15; // little fudge factor -- was 20
					//sliceinc = t*50; // jwg 2010-05-03
				}
     	}
     	// jwg 2010-06-08 -- replaced by slicept assignment above on 2010-06-15
			//var tempx = allText.substr(hiy).replace(/<[^>]+>/gi, '');
			//if (tempx.length < slicept+sliceinc) return; // retain as much text (after recipient list) as normal twitter
	    //if (slicept < o.slicePoint+sliceinc) slicept = o.slicePoint+sliceinc;
	    
     	var startText = allText.slice(0, slicept).replace(/\w+$/,''); // jwg - use adjusted slicept
     	startTags = startText.match(/<\w[^>]*>/g); 
   	  
     	if (startText.lastIndexOf('<') > startText.lastIndexOf('>') ) {
     	  startText = startText.slice(0,startText.lastIndexOf('<'));
     	}
     	
     	// jwg 2010-05-06 -- try to avoid really short lines when slicept occurs shortly after a <br/> or \n 
     	x = startText.lastIndexOf('<br');
     	if (x < 0) x = startText.lastIndexOf("\n");
     	if (x > -1) {    		
     		var temp = startText.substr(x).replace(/<[^>]+>/gi, '');
     		var words = temp.split(' ');
     		if ((temp.length < 90) || (words.length < 5)) {
     			startText = startText.substr(0,x);	
     		} 
    	}

			// jwg 2010-06-19
			var temp2 = startText.replace(/<[^>]+>/gi, '');
			if (temp2.length < 90) return; // not collapse if very short first part

     	var endText = allText.slice(startText.length);    	  
     	
     	// jwg 2010-05-06 -- try to avoid splice if only a small amount remains
     	var endtemp = endText.replace(/<[^>]+>/gi, '');
     	var endwords = endtemp.split(' ');
     	if ((endtemp.length < 90) || (endwords.length < 20)) return;
     	
     	
     	if (endText.length < 80) return; 								// jwg 2010-05-03
     	var i = endText.indexOf('<input '); 						// ..
     	if ((i > -1) && (i < 80)) return; 							// ..

			// jwg 2010-06-28 - avoid breaking in middle of <object >...</object>
			var atxt = allText.toLowerCase();
			var stxt = startText.toLowerCase();
  		x = atxt.indexOf('<object');
			if (x == -1) x = atxt.indexOf('&lt;object');
			if ((x > -1) && (x <= stxt.length)) {
				y = stxt.indexOf('</object>',x+8);
				if (y == -1) {
					y = stxt.indexOf('&lt;/object&gt;',x+8);
				}
				if (y == -1) {
					var inc = 9;
					y = atxt.indexOf('</object>',x+8);
					if (y == -1) {
						y = atxt.indexOf('&lt;/object&gt;',x+8);
						inc = 15;
					}
					if (y > -1) {
						startText = allText.substr(0,y+inc);
						endText = allText.slice(startText.length);
					}
				}
			}

     	// create necessary expand/collapse elements if they don't already exist
   	  if (!$('span.details', this).length) {
        // end script if text length isn't long enough.
       	if ( endText.replace(/\s+$/,'').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)$/,'$1>');
                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)$/,'$1>') == rSlash(endTags[i]) 
                  && matched == false) {
                    cleanedTag = cleanedTag ? startTags[s] + cleanedTag : startTags[s];
                    matched = true;
                  }
                };
              }
            }
          }

          endText = cleanedTag && cleanedTag + endText || endText;
        }

     	  $this.html([
     		startText,
     		'<span class="read-more">',
     		o.expandPrefix,
       		'<a href="#">',
       		  o.expandText,
       		'</a>',
        '</span>',
     		'<span class="details">',
     		  endText,
     		'</span>'
     		].join('')
     	  );
      }
      var $thisDetails = $('span.details', this),
        $readMore = $('span.read-more', this);
   	  $thisDetails.hide();
 	    $readMore.find('a').click(function() {
 	      $readMore.hide();

 	      if (o.expandEffect === 'show' && !o.expandSpeed) {
          o.beforeExpand($this);
 	        $thisDetails.show();
          o.afterExpand($this);
          delayCollapse(o, $thisDetails);
 	      } else {
          o.beforeExpand($this);
 	        $thisDetails[o.expandEffect](o.expandSpeed, function() {
            $thisDetails.css({zoom: ''});
            o.afterExpand($this);
            delayCollapse(o, $thisDetails);
 	        });
 	      }
        return false;
 	    });
      if (o.userCollapse) {
        $this
        .find('span.details').append('<span class="re-collapse">' + o.userCollapsePrefix + '<a href="#">' + o.userCollapseText + '</a></span>');
        $this.find('span.re-collapse a').click(function() {

          clearTimeout(delayedCollapse);
          var $detailsCollapsed = $(this).parents('span.details');
          reCollapse($detailsCollapsed);
          o.onCollapse($this, true);
          return false;
        });
      }
    });
    function reCollapse(el) {
       el.hide()
        .prev('span.read-more').show();
    }
    function delayCollapse(option, $collapseEl) {
      if (option.collapseTimer) {
        delayedCollapse = setTimeout(function() {  
          reCollapse($collapseEl);
          option.onCollapse($collapseEl.parent(), false);
          },
          option.collapseTimer
        );
      }
    }
    function rSlash(rString) {
      return rString.replace(/\//,'');
    }    
  };
    // plugin defaults
  $.fn.expander.defaults = {
    slicePoint:       100,  // 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:       '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: '[collapse expanded text]',  // text to use for the link to re-collapse the text
    userCollapsePrefix: ' ',
    beforeExpand: function($thisEl) {},
    afterExpand: function($thisEl) {},
    onCollapse: function($thisEl, byUser) {}
  };
})(jQuery);
