dojo.require('actijscore.parser');
dojo.require("dojo.fx.easing"); 
dojo.require("dojox.fx.scroll");

dojo.provide('smoothscrolling');
dojo.declare('smoothscroller', null, 
{
    domNode: null,
    
    constructor: function( params, srcNodeRef )
    {
	this.domNode = srcNodeRef;
	
	// CONNECTION SI LIEN INTERNE A LA PAGE
	var href = this.domNode.href;
	var pathname = this.domNode.pathname;
	var search = this.domNode.search;

	if( ( href && href.indexOf( '#' ) != -1)
	&&( ( pathname == location.pathname) 
	||( '/' + pathname == location.pathname ) ) 
	&&( search == location.search ) )
	{
	    dojo.connect( this.domNode, 'click', null, this.onClick );
	}
    },
    
    onClick: function(e)
    {
	var target = this;
	dojo.stopEvent(e);
	
	// REMONTER DANS LE A SI CLIQUE DANS LE INNERHTML
	if( target.nodeName.toLowerCase() != 'a')
	{
	    target = target.parentNode;
	}
	
	// VERIFIER QUE C'EST UN A
	if( this.nodeName.toLowerCase() != 'a')
	{
	    return;
	}
	
	// DESTINATION DU LIEN
	var anchor = target.hash.substr(1);
	var destination = dojo.query( 'a[name="' + anchor + '"]' )[0];
	if( !destination ) 
	{
	    destination = dojo.byId( anchor );
	}
	if( !destination )
	{
	    return;
	}
	
	// ANIMATION
	var animation = dojox.fx.smoothScroll({	node: destination,
						win: window, 
						duration: 500, 
						easing: dojo.fx.easing.easeOut 
						} );
	animation.play();
    }
    
});

actiJSCoreParser.addDefinition( '.jsScroll', smoothscroller, {} );
