//this script provides folding functionality for long texts

//folds or unfolds content by showing or hiding relevant elements
//the ids in this function are generated by the foldAllContent
//so this functionality is quite generic
function foldUnfoldContent (id, foldContent)
{
	//the link which opens the fold
	var ahref = document.getElementById("a"+id);
	//the brief text generated via regex from the long/original
	var brief = document.getElementById("_brief"+id);
	//the holder containing the original text, cleaned from ||| which indicates what the brief part is
	var hidden = document.getElementById("_hiddenSpan"+id);    
    
    ahref.style.display= (foldContent) ? '' : 'none';
    brief.style.display= (foldContent) ? '' : 'none';
    
    hidden.style.display= (foldContent) ? 'none' : '';
}

//check if the url contains an anchor
//used for initial page load, to unfold relevant anchor
function unfoldAnchor()
{
	var url = window.location.href;
	var regexAnchor = /#(\w+)/gim;		
	var regexFoldID = /javascript:foldUnfoldContent\(\"(\d+)\"/gi;		
	var matchesAnchor = regexAnchor.exec(url);	
	
	//if no anchor in url end here
	if(matchesAnchor==null) return;	
	
	var name = matchesAnchor[1];
	var anchor = document.getElementsByName(name);
	
	//if none found end here
	if(anchor==null || anchor.length<1) return;
	
	//get the unique id generated by foldAllContent
	var matchesFoldID = regexFoldID.exec(anchor[0].parentNode.innerHTML);	
	
	//make another try a level of nodes up
	if(matchesFoldID==null)
	{
		matchesFoldID = regexFoldID.exec(anchor[0].parentNode.parentNode.innerHTML);
	}
	
	//make another try a level of nodes up
	if(matchesFoldID==null)
	{
		matchesFoldID = regexFoldID.exec(anchor[0].parentNode.parentNode.parentNode.innerHTML);
	}
	
	//if none found end here
	if(matchesFoldID==null)
	{
		return;
	}
	
	//unfold content
	foldUnfoldContent(matchesFoldID[1],false);		
}

//gets all content wraped in <span> tag for exmaple
//then process only those with the foldID parameter
function foldAllContent (tagName, foldID)
{
	var foldObjects = document.getElementsByTagName(tagName);    
    var originalInnerHTML;
    var hiddenSpanID;
    var hiddenSpan;
    var sb;
	
    //gets text either until ||| or first line and next 15-20 words from next line
    var regexBrief = /(?:^.+?(?:$\r?\n?|\r?\n?))((?:\s+|^)\S+){15,20}/gim;
    var regexBriefDiv = /.+?(?=\|\|\|)/gim;
	var regexBriefClosingTag = /(\|\|\|\s*)(<\/[^>]+>)/gim;
	var regexLastClosingTag = /<\/[^>]+>\s*$/gim;
	var regexBriefDiv_StripHTML = /<[^>]+>\s*\|\|\|\s*<\/[^>]+>/gim;
	var regexNewLine = /(?:\r?\n)/gim;	
    
    //regex to get the anchor within the content
	//used to remove the name attr. from the brief part, since it's a duplicate from the 
	//original content anchor
	var regexName = /name=(\"([^\"]+)\"|\w+)/gim;    
    
    //loop all elements by tag name and then process only those with the foldID parameter
	for(i=0;i<foldObjects.length;++i)
    {
        if(foldObjects[i].id == foldID)
        {
            //save original html
			originalInnerHTML = foldObjects[i].innerHTML;
			//trim new lines - not relevant for html
			originalInnerHTML = originalInnerHTML.replace(regexNewLine,"");
			//trim html tags from |||
			originalInnerHTML = originalInnerHTML.replace(regexBriefDiv_StripHTML,"|||");			
			
			//move the ||| devider after the closing html tag
			originalInnerHTML = originalInnerHTML.replace(regexBriefClosingTag,"$2$1");						
			
            
            //set a new stringbuilder
			sb = '';    			
            
            //a temp var to hold a match for the brief part if any
			var m;
			var closingTag;
            if(m = originalInnerHTML.match(regexBriefDiv))
            {
                sb += m;
				//save closing tag to futher close the brief
				closingTag = sb.match(regexLastClosingTag);
				//strip it from the end for now
				sb = sb.replace(regexLastClosingTag,'');				
            }
			else
			{
				continue;
			}            
            
            //build the brief part - wrap it a <span> to be able to hide it after
			sb = '<span id="_brief'+i+'" >' + sb;                        
            sb = sb.replace(regexName,"");            
            //replace single quotes to preven js error
			sb = sb.replace("'","\'");			
            
            sb += ' ';
            
            //unfold
			sb += '<a title="read more..." class="aFold" id="a'+i+'" href=javascript:foldUnfoldContent("';
            sb += i;
            sb += '",false);>'
            sb += '...+';
            sb += '</a>';			
			//close here the last sb tag
			sb += (closingTag!=null) ? closingTag : '';
            sb += ' ';
			sb += '</span>';			
			
			hiddenSpanID = "_hiddenSpan" + i;            
            
            //here is the original text, wrapped in a span to be hidden, with the ||| brief divider removed (if any)
			sb += '<span style="display:none;" id='+hiddenSpanID+' >';
            //replace the |||
            originalInnerHTML = originalInnerHTML.replace(/\|\|\|\s*/,'');
            sb += originalInnerHTML.replace("'","\'"); 
            
            //sb+= "<br/>";
            
            //fold
			sb += ' <a title="fold content" class="aFold" href=javascript:foldUnfoldContent("';
            sb += i;
            sb += '",true);>'
            sb += '...--';
            sb += '</a>';
            
            sb += '</span>';
            
            foldObjects[i].innerHTML = sb;  
        }
    }
}

