/*  

SiteUtils js library
@author: giuseppe roccazzella
@firstedit: 28.11.2007

*/

var SiteUtils = {
  Version: '0.0.1',


/* 
	method provides easy fix for dummy '#' anchors (making page jump on top after click);
	
	scan document body for <a href="#" ...> tags; 
	- if anchor already has a name, href attribute will be set as '#[name]';
	- if anchor has no name yet, random unique name is generated and attached to href attribute

	TODO:
		* provide input param for starting element (so far is document.body)

*/

    fixDummyAnchors: function() {
	    $$('a[href="#"]').each(function(s) {
		  var onclick = s.readAttribute('onclick');
		  if (onclick != null && onclick != 'javascript:;') {
		  	s.href = onclick.strip();
		  	s.onclick = '';
		  }
		  else 
		  	if (s.readAttribute('name') != null) 
		  		s.href = '#' + s.readAttribute('name');
		  	else {
		  		var random_id = Math.round(10000 * Math.random());
		  		while ($A($$("a[name=\"" + random_id + "\"]")).length > 0) 
		  			random_id = Math.round(10000 * Math.random());
		  		
		  		s.name = random_id;
		  		s.href = '#' + random_id;
		  	}
	    });
    }
}

FastInit.addOnLoad(SiteUtils.fixDummyAnchors);
