﻿///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// ShortenURLs by Mark Hansen aka stonedonkey
//
// Details: http://www.stonedonkey.com 
//
// License: Commons Attribution-Noncommercial-Share Alike 3.0  License
//          http://creativecommons.org/licenses/by-nc-sa/3.0/
//
// Usage:
//	iLength:				the length you wish to shorten your URLs to
//	sAppend:				the text you wish to append at the end of the visible URL. This will be 
//							appended to your URL at the length you specified so the URL will be 
//							iLength + the length of
//							your append string.			
//	bCheckAllowShortAttrib: if set to true only hrefs with the attribute allowshort="true" will be
//							shortened.
//	sLinkContainer:			only shorten links within this container.  Default is the entire document
// 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 function ShortenURLs(iLength,sAppend,bCheckAllowShortAttrib,sLinkContainer) {
  	
 	// get urls
 	if (sLinkContainer) {
 		if (document.getElementById(sLinkContainer)) {
 			var el = document.getElementById(sLinkContainer);
 			var hrefs = eval( el.getElementsByTagName('a') );
 		}
 		else
 			alert('Could not find document element named \"' + sLinkContainer + '\"');
 	}
 	else
 		var hrefs = document.getElementsByTagName('a');
     
    // loop through all the URLs and shorten then by the length sent in
	if (hrefs)											// were matching hrefs found?
	for(i=0;i<hrefs.length;i++){  
		if (hrefs[i].innerHTML && hrefs[i].href)		// does it have link text, and is it a href?
		    if ( hrefs[i].innerHTML.length > iLength)   // is the length of the href text longer than the value we want?
		        if ( (bCheckAllowShortAttrib==true && hrefs[i].getAttribute("allowshort")=="true") || (bCheckAllowShortAttrib == false)) // check attrib switch
		              hrefs[i].innerHTML = hrefs[i].innerHTML.substring(0,iLength) + sAppend // do replace
    }
 }


