/*

	filename:	/js/printReferences.js
	version:	1.2 - 2007-03-06 - mkr

	author:		martin (dot) krause (at) gpm (dot) de 

	depends on : 
				prototype.js (inArray())
	
	1.2 - mkr: added self.preFnSup, fixed [object object] at baseUrl
	1.1 - mkr: optimised speed 
*/

/*!
	@function printReferences();
	@abstract constructor
*/
printReferences = function (){
	
	if (!self) {this.self = this;}
	self.idParent = 'contentMain'; // a's parent element
	self.idFnRoot = 'printFootnotes'; // new div, sourounding the created orderd list of links
	self.cnFnSup = 'printSup'; // new footnotes class (<sup class="cnFnSup">fn</sup>)
	self.cnSkipLink = 'noFn'; // links that should be skipped
	self.preFnSup = 'Link #'; // <sup>-text in front of current number
	self.txtFnH2 = 'Links'; // h2 above footnotes
	self.defaultBaseUrl = ''; // default base url - in case there's no param for create(), a href="/link/ => a href="baseUrl/link/"
};


/*!
	@function printReferences.create();
	@param baseUrl: string containing base URL
	@abstract creates footnotes on every link inside self.idParent, places them at the end of self.idParent (<h2><ol><li><a>)
*/
printReferences.prototype.create = function(baseUrl){
		var baseUrl = (!baseUrl || typeof(baseUrl) != 'string') ? self.defaultBaseUrl : baseUrl;
		var _aryHref = []; // stores hrefs
		var _fn = 1; // counting real footnotes
		
		// get content's anchor-elements 
		var _parent = document.getElementById(self.idParent);
		var _col = _parent.getElementsByTagName('a');

		var _n = _col.length;
		if ( _n == 0 ) { return false; }

		// create new element for footnotes
		var _root =  document.createElement('div');
		_root.id = self.idFnRoot;
		_parent.appendChild(_root);
	
		var _nH2 = document.createElement('h2');
		var _nt = document.createTextNode(self.txtFnH2);
		_nH2.appendChild(_nt)
		_root.appendChild(_nH2)

		var _nOl = document.createElement('ol');

		// cycle anchors
		for (var _n = 0; _n <= (_col.length-1);  _n++)
		{
			// cache current el
			var _el = _col[_n];
			// cache current href
			var _href = _el.getAttribute('href');
			// continue if we're at a skip link or no href is present 
			if (_el.className.strstr(self.cnSkipLink) || !_href ) {continue;}

			// check doublet
			var _aryPos = _aryHref.inArray(_href);
// without Array.inArray()
/*
			var _aryPos = false;
			var _m = _aryHref.length;
			if (_m > 0) 
			{
				do 
				{
					if (_aryHref[_m] === _href) { _aryPos = _m; }
				} 
				while (_m--);
			}
*/
			// new element
			if (_aryPos == false) 
			{
				// store href using current fn# as index
				_aryHref[_fn] = _href;
				
				// create footnote using fn# 
				var _tFn = self.preFnSup+_fn;
				var _nE = printReferences._createFnSup(_tFn,self.cnFnSup);
				_col[_n].appendChild(_nE);
				
				// add base url to internal links 
				var _href = !( _href.strstr('http://') ) ? baseUrl + _href : _href;

				// insert new footnote into fn container (_root)
				var _nLi = document.createElement('li');
				var _nText = document.createTextNode(_href);
				_nLi.appendChild(_nText);
				_nOl.appendChild(_nLi);

				// increase footnote
				_fn++;
			}
			// we have a doublet
			else
			{
				// create footnote using href's aray index
				var _tFn = self.preFnSup+_aryPos;
				var _nE = printReferences._createFnSup(_tFn,self.cnFnSup);
				_col[_n].appendChild(_nE);
			}
			
			// append all the new footnotes
			_root.appendChild(_nOl)

		}
	}
	
	
/*!
	@function printReferences._createFnSup();
	@param 	_t: string containing text 
			_cn: string for sup.className
	@abstract creates and returns <sup>-element, using _t for textNode, _cn as sup.className
*/
printReferences.prototype._createFnSup = function(_t,_cn) {
		if (!_t) { return false; }
		var _nSup = document.createElement('sup');
		if (_cn) { _nSup.className = _cn; }
		_nT = document.createTextNode(_t);
		_nSup.appendChild(_nT);
		return _nSup;
	},
	
printReferences = new printReferences();
evt.add(window,'load',printReferences.create);