// b3rtShowHide 1.0
// Copyright 2011, H. poort
// This software may not be used without a valid license

function b3rtShowHide(){this.classNames='b3rtShowHide';this.tagNames='*';this.showText='&nbsp;Read more &gt;&gt;&gt;';this.hideText='&nbsp;Hide &lt;&lt;&lt;';this.placeHolderMaxWords=8;this.eventHandlers=[];this.sourceElements=[];this.groupedElements=[];this.linksShow=[];this.linksHide=[];this.placeHolderElements=[]};b3rtShowHide.prototype.setClassNames=function(a){if(((typeof a)=='string')&&(a!=''))this.classNames=a};b3rtShowHide.prototype.setTagNames=function(a){if(((typeof a)=='string')&&(a!=''))this.tagNames=a};b3rtShowHide.prototype.setShowText=function(a){if(((typeof a)=='string')&&(a!=''))this.showText=a};b3rtShowHide.prototype.setHideText=function(a){if(((typeof a)=='string')&&(a!=''))this.hideText=a};b3rtShowHide.prototype.setPlaceHolderMaxWords=function(a){if(((typeof a)=='number')&&(a>0))this.placeHolderMaxWords=a};b3rtShowHide.prototype.initialize=function(a){if((typeof a)=='undefined')a=0;if(!document.getElementById||!document.createElement||!document.getElementsByTagName)return 0;var b=false;if(!document.body)b=true;if(b){if(a>60000)return;var c=this;window.setTimeout(function(){if(c){c.initialize(a+100);c=null}},100);return 1}this.sourceElements=this.getElementsByClassName(this.classNames,this.tagNames,window.document);if(this.sourceElements.length>0){for(var i=0;i<this.sourceElements.length;i++)this.sourceElements[i].style.display='none';this.createGroups()}};b3rtShowHide.prototype.createGroups=function(){var a=0;var b=0;while(a<this.sourceElements.length){this.groupedElements[b]=[];this.groupedElements[b].push(this.sourceElements[a]);var c=this.sourceElements[a].nextSibling;var d=[];while(c&&((a+1)<this.sourceElements.length)){if(c.nodeType==1){var e=c.nodeName.toLowerCase();if((e!='br')&&(e!='hr')){if(c==this.sourceElements[a+1]){for(var i=0;i<d.length;i++){d[i].style.display='none';this.groupedElements[b].push(d[i])}this.groupedElements[b].push(this.sourceElements[++a])}else break}d.push(c)}c=c.nextSibling}b++;a++}this.createElements()};b3rtShowHide.prototype.createElements=function(){for(var i=0;i<this.groupedElements.length;i++){if(this.groupedElements[i].length==0)continue;var a=this.groupedElements[i][0];var b=this.groupedElements[i][this.groupedElements[i].length-1];var c=window.document.createElement('a');c.className='b3rtShowHideLinkShow';c.id='b3rtShowHideLinkShow_'+i;c.href='#';c.innerHTML=this.showText;var d=window.document.createElement('a');d.className='b3rtShowHideLinkHide';d.id='b3rtShowHideLinkHide_'+i;d.href='#';d.innerHTML=this.hideText;var e=window.document.createElement(a.nodeName.toLowerCase());e.className='b3rtShowHidePlaceHolder';e.style.display='';var f=new RegExp('<[^>]+>','gi');var g=a.innerHTML.replace(f,'');var f=new RegExp('(?:[^\\s.!?,;:]*[\\s.!?,;:]*){0,'+(this.placeHolderMaxWords-1)+'}[^\\s.!?,;:]*','i');var h=g.match(f);if(h&&(h.length>0))g=h[0];else g=g.substr(0,(this.placeHolderMaxWords*8));e.innerHTML=g+'...&nbsp;';var j=b.parentNode;if(!j)j=window.document.body;var k=b.nextSibling;if(k){j.insertBefore(e,k)}else{j.appendChild(e)}e.appendChild(c);b.appendChild(d);this.linksShow[i]=c;this.linksHide[i]=d;this.placeHolderElements[i]=e}this.setupEvents()};b3rtShowHide.prototype.setupEvents=function(){var d=this;this.eventHandlers['windowOnUnload']=function(a){var b=(a?a:window.event);return d.destroy(b)};this.eventHandlers['linkShowOnClick']=function(a){var b=(a?a:window.event);var c=(b.srcElement?b.srcElement:b.target);return d.showElement(c,b)};this.eventHandlers['linkHideOnClick']=function(a){var b=(a?a:window.event);var c=(b.srcElement?b.srcElement:b.target);return d.hideElement(c,b)};this.addEventHandler(window,'unload',this.eventHandlers['windowOnUnload']);for(var i=0;i<this.linksHide.length;i++)this.addEventHandler(this.linksShow[i],'click',this.eventHandlers['linkShowOnClick']);for(var i=0;i<this.linksHide.length;i++)this.addEventHandler(this.linksHide[i],'click',this.eventHandlers['linkHideOnClick'])};b3rtShowHide.prototype.showElement=function(a,b){if((typeof b.preventDefault)=='function')b.preventDefault();if((typeof b.stopPropagation)=='function')b.stopPropagation();if((typeof b.cancelBubble)=='boolean')b.cancelBubble=true;var c=-1;var d=new RegExp('\\d+$');var e=a.id.match(d);if(e&&(e.length>0))c=window.parseInt(e[0]);else return false;if(isNaN(c))return false;for(var i=0;i<this.groupedElements[c].length;i++)this.groupedElements[c][i].style.display='';this.linksShow[c].style.display='none';this.linksHide[c].style.display='';this.placeHolderElements[c].style.display='none';return false};b3rtShowHide.prototype.hideElement=function(a,b){if((typeof b.preventDefault)=='function')b.preventDefault();if((typeof b.stopPropagation)=='function')b.stopPropagation();if((typeof b.cancelBubble)=='boolean')b.cancelBubble=true;var c=-1;var d=new RegExp('\\d+$');var e=a.id.match(d);if(e&&(e.length>0))c=window.parseInt(e[0]);else return false;if(isNaN(c))return false;for(var i=0;i<this.groupedElements[c].length;i++)this.groupedElements[c][i].style.display='none';this.linksShow[c].style.display='';this.linksHide[c].style.display='none';this.placeHolderElements[c].style.display='';return false};b3rtShowHide.prototype.destroy=function(){for(var i=0;i<this.linksHide.length;i++)this.removeEventHandler(this.linksShow[i],'click',this.eventHandlers['linkShowOnClick']);for(var i=0;i<this.linksHide.length;i++)this.removeEventHandler(this.linksHide[i],'click',this.eventHandlers['linkHideOnClick']);this.removeEventHandler(window,'unload',this.eventHandlers['windowOnUnload']);this.eventHandlers=null;this.sourceElements=null;this.groupedElements=null;this.linksShow=null;this.linksHide=null;this.placeHolderElements=null};b3rtShowHide.prototype.addEventHandler=function(a,b,c,d){var e=((typeof d)=='undefined'?false:d);if((typeof a.addEventListener)!='undefined')a.addEventListener(b,c,e);else if((typeof a.attachEvent)!='undefined')a.attachEvent('on'+b,c);else a['on'+b]=c};b3rtShowHide.prototype.removeEventHandler=function(a,b,c,d){var e=((typeof d)=='undefined'?false:d);if((typeof a.removeEventListener)!='undefined')a.removeEventListener(b,c,e);else if((typeof a.detachEvent)!='undefined')a.detachEvent('on'+b,c);else a['on'+b]=null};b3rtShowHide.prototype.getElementsByClassName=function(a,b,c){var d=[];var e=(((typeof b)!='undefined')?b:'*');var f=(((typeof c)!='undefined')?c:window.document);var g=a.split(' ');var h=e.split(' ');if((h.length==0)||(g.length==0))return d;var k='';for(var i=0;i<g.length;i++){k+='(?:(?:^|\\s)'+g[i]+'(?:\\s|$))';if((i+1)!=g.length)k+='|'}var l=new RegExp(k,'i');for(var i=0;i<h.length;i++){var m=f.getElementsByTagName(h[i]);for(var j=0;j<m.length;j++){if((m[j].className!='')&&(l.test(m[j].className)))d[d.length]=m[j]}}return d};
