/**
* Condense 0.1 - Condense and expand text heavy elements
* edited by BG 10.02.2010
*
* (c) 2008 Joseph Sillitoe
* Dual licensed under the MIT License (MIT-LICENSE) and GPL License,version 2 (GPL-LICENSE). 
*/

(function($){$.fn.condense=function(options){$.metadata?debug('metadata plugin detected'):debug('metadata plugin not present');var opts=$.extend({},$.fn.condense.defaults,options);return this.each(function(){$this=$(this);var o=$.metadata?$.extend({},opts,$this.metadata()):opts;debug('Condensing ['+$this.text().length+']: '+$this.text());var clone=cloneCondensed($this,o);if(clone){$this.attr('id')?$this.attr('id','condensed_'+$this.attr('id')):false;var controlMore="";var controlLess="";if(o.moreText!='')controlMore=" (<span class='condense_control condense_control_more' style='cursor:pointer;'>"+o.moreText+"</span>)";if(o.lessText!='')controlLess=" (<span class='condense_control condense_control_less' style='cursor:pointer;'>"+o.lessText+"</span>)";clone.append(o.ellipsis+controlMore);$this.after(clone).hide().append(controlLess);$('.condense_control_more',clone).click(function(){debug('moreControl clicked.');triggerExpand($(this),o)});$('.condense_control_less',$this).click(function(){debug('lessControl clicked.');triggerCondense($(this),o)});}});};function cloneCondensed(elem,opts){if($.trim(elem.text()).length<=opts.condensedLength+opts.minTrail){debug('element too short: skipping.');return false;}
var fullbody=$.trim(elem.html());var fulltext=$.trim(elem.text());var delim=opts.delim;var clone=elem.clone();var delta=0;do{var loc=findDelimiterLocation(fullbody,opts.delim,(opts.condensedLength+delta));clone.html($.trim(fullbody.substring(0,(loc+1))));var cloneTextLength=clone.text().length;var cloneHtmlLength=clone.html().length;delta=clone.html().length-cloneTextLength;debug("condensing... [html-length:"+cloneHtmlLength+" text-length:"+cloneTextLength+" delta: "+delta+" break-point: "+loc+"]");}while(clone.text().length<opts.condensedLength)
if((fulltext.length-cloneTextLength)<opts.minTrail){debug('not enough trailing text: skipping.');return false;}
debug('clone condensed. [text-length:'+cloneTextLength+']');return clone;}
function findDelimiterLocation(html,delim,startpos){var foundDelim=false;var loc=startpos;do{var loc=html.indexOf(delim,loc);if(loc<0){debug("No delimiter found.");return html.length;}
foundDelim=true;while(isInsideTag(html,loc)){loc++;foundDelim=false;}}while(!foundDelim)
debug("Delimiter found in html at: "+loc);return loc;}
function isInsideTag(html,loc){return(html.indexOf('>',loc)<html.indexOf('<',loc));}
function triggerCondense(control,opts){debug('Condense Trigger: '+control.html());var orig=control.parent();var condensed=orig.next();condensed.show();var con_w=condensed.width();var con_h=condensed.height();condensed.hide();var orig_w=orig.width();var orig_h=orig.height();orig.animate({height:con_h,width:con_w},opts.lessSpeed,opts.easing,function(){orig.height(orig_h).width(orig_w).hide();condensed.show();});}
function triggerExpand(control,opts){debug('Expand Trigger: '+control.html());var condensed=control.parent();var orig=condensed.prev();orig.show();var orig_w=orig.width();var orig_h=orig.height();orig.width(condensed.width()+"px").height(condensed.height()+"px");condensed.hide();orig.animate({height:orig_h,width:orig_w},opts.moreSpeed,opts.easing);if(condensed.attr('id')){var idAttr=condensed.attr('id');condensed.attr('id','condensed_'+idAttr);orig.attr('id',idAttr);}}
function debug($obj){if(window.console&&window.console.log){window.console.log($obj);}};$.fn.condense.defaults={condensedLength:200,minTrail:20,delim:" ",moreText:"[more]",lessText:"[less]",ellipsis:" ( ... )",moreSpeed:"normal",lessSpeed:"normal",easing:"linear"};})(jQuery);