dojo.require('actijscore.parser');

dojo.provide('frontpage.carousel');

dojo.declare('frontpage.carousel', null, 
{
    domNode: null,
    domNext: null,
    domPrevious: null,
    domNewsUL: [],
    arrNewsLI: [],
    arrPaging: [],
    arrHandler: [],
    
    index: 0,	    // index de la première news affichée
    focus: 0,	    // index de la news avec le focus
    perPage: 0,	    // nombre de news affichées sur une même page
    newsWidth: 0,   // largeur en pixel d'une news'
    
    txtOn: '',
    txtOut: '',
    
    constructor: function( params, srcNodeRef )
    {
	this.domNewsUL = [];
	this.arrNewsLI = [];
	this.arrPaging = [];
	this.arrHandler = [];
    
	this.perPage = params.perPage;
	this.newsWidth = params.newsWidth;
	this.domNode = srcNodeRef;
	this.domPrevious = dojo.query( 'a.control.previous', this.domNode )[0];
	this.domNext = dojo.query( 'a.control.next', this.domNode )[0];
	this.domNewsUL = dojo.query( 'div.carousel div.newsMask ul:first-child', this.domNode )[0];
	
	if( this.domNewsUL != null )
	{
	    this.arrNewsLI = dojo.query( 'li', this.domNewsUL );
	    this.arrPaging = dojo.query( 'div.pagination ul li', this.domNode );
	    this.txtOn = dojo.attr( dojo.query( '.jsTxtOn', this.domNode )[0], 'value' );
	    this.txtOut = dojo.attr( dojo.query( '.jsTxtOut', this.domNode )[0], 'value' );

	    this.clearHref();
	    this.connect();
	    this.updateButtons();
	    this.updatePaging();
	    this.updateAria();
	}
    },
    
    clearHref: function()
    {
	dojo.attr( this.domNext, 'href', '#news' );
	dojo.attr( this.domPrevious, 'href', '#news' );
	
	this.arrPaging.forEach( dojo.hitch( this, function( domPaging )
	{
	    dojo.attr( domPaging, 'href', '#news' );
	} ) );
    },
    
    connect: function()
    {
	this.arrHandler.push( dojo.connect( this.domNext, 'click', this,    function( e )
									    { 
										this.nextNews();
										dojo.stopEvent( e );
									    } ) );
	
	this.arrHandler.push( dojo.connect( this.domPrevious, 'click', this,	function( e )
										{ 
										    this.previousNews();
										    dojo.stopEvent( e );
										} ) );
	
	this.arrPaging.forEach( dojo.hitch( this, function( domPaging, i )
	{
	    this.arrHandler.push( dojo.connect( domPaging, 'click', this,   function( e )
									    {
										if( i < this.arrNewsLI.length - this.perPage + 1 )
										{
										    this.setNews( i );
										}
										else
										{
										    if( this.arrNewsLI.length > this.perPage - 1 )
										    {	
											this.setNews( this.arrNewsLI.length - this.perPage );
										    }
										    else
										    {
											this.setNews( 0 );
										    }
										}
										dojo.stopEvent( e );
									    } ) );
	} ) );
	
	this.arrNewsLI.forEach( dojo.hitch( this, function( domNewsLI, i )
	{
		this.arrHandler.push( dojo.connect( domNewsLI, 'keypress', this, this.hubKeyBoardEvent ) );
		
		this.arrHandler.push( dojo.connect( domNewsLI, 'focus', this,	function( )
										{
										    this.focus = i;
										} ) );
	} ) );
    },

    hubKeyBoardEvent: function( e )
    {
	switch( e.charOrCode )
	{
	    case dojo.keys.LEFT_ARROW:  this.previousNews();
					dojo.stopEvent( e );
					break;

	    case dojo.keys.RIGHT_ARROW: this.nextNews();
					dojo.stopEvent( e );
					break;
					
	    case dojo.keys.UP_ARROW:    this.previousNews();
					dojo.stopEvent( e );
					break;
					
	    case dojo.keys.DOWN_ARROW:  this.nextNews();
					dojo.stopEvent( e );
					break;
	}
    },
    
    disconnect: function()
    {
	var h;
	while ( ( h = this.arrHandler.pop() ) != undefined)
	{
		dojo.disconnect( h );
	}
    },
    
    previousNews: function()
    {
	if( this.focus > 0 )
	{
	    this.focus--;
	    this.arrNewsLI[this.focus].focus();
	}
	
	if( this.index > 0 )
	{
	    this.setNews( this.index - 1 );
	}
    },
    
    nextNews: function()
    {
	if( this.focus < this.arrNewsLI.length - 1 )
	{
	    this.focus++;
	    this.arrNewsLI[this.focus].focus();
	}
	    
	if( this.index + this.perPage < this.arrNewsLI.length )
	{
	    this.setNews( this.index + 1 );
	}	
    },
    
    setNews: function( i )
    {	
	this.slide( this.index - i );
	this.index = i;
	this.updateButtons();
	this.updatePaging();
	this.updateAria();
    },
    
    slide: function( n )
    {
	var node = dojo.attr( this.domNewsUL, 'id' ); // marche uniquement avec l'id ?
	var cur = dojo.style( this.domNewsUL, 'left' );
       
	dojo.animateProperty( {node: node,
				properties: {left:  cur + n*this.newsWidth},
				beforeBegin: dojo.hitch( this, this.disconnect ),
				onEnd:  dojo.hitch( this, this.connect )
				}).play();
    },
    
    updateButtons: function()
    {
    	if(dojo.isIE == 7){
    		dojo.removeClass( this.domNext, 'ie7disabled' );
    		dojo.removeClass( this.domPrevious, 'ie7disabled' );
		}
		dojo.removeAttr( this.domNext, 'disabled' );
		dojo.removeAttr( this.domPrevious, 'disabled' );
	
	if( this.index <= 0 )
	{
		if(dojo.isIE == 7){
			dojo.addClass( this.domPrevious, 'ie7disabled');
		}
		dojo.attr( this.domPrevious, 'disabled', 'disabled' );
	}

	if( this.index >= this.arrNewsLI.length - this.perPage )
	{
		if(dojo.isIE == 7){
			dojo.addClass( this.domNext, 'ie7disabled');
		}
		dojo.attr( this.domNext, 'disabled', 'disabled' );
	}
    },
    
    updatePaging: function()
    {
	this.arrPaging.forEach( dojo.hitch( this, function( domPaging, i )
	{
	    dojo.empty( domPaging );
	    
	    if( ( i - this.index < this.perPage ) && ( i - this.index >= 0 ) )
	    {
		dojo.create( 'p', {innerHTML: '<span class="notxt">' + this.txtOn + ' </span>' + ( i + 1 )} ,  domPaging );
	    }
	    else
	    {
		dojo.create( 'a', {href: '#page' + i, innerHTML: '<span class="notxt">' + this.txtOut + ' </span>' + ( i + 1 )}, domPaging );
	    }
	    
	} ) ) ;
    },
    
    updateAria: function()
    {
	this.arrNewsLI.forEach( dojo.hitch( this, function( domNewsLI, i )
	{
	    if( ( i - this.index < this.perPage ) && ( i - this.index >= 0 ) )
	    {
		dojo.attr( domNewsLI, {tabindex: '0', 'aria-selected': 'true'} );
	    }
	    else
	    {
		dojo.attr( domNewsLI, {tabindex: '-1', 'aria-selected': 'false'} );
	    }
	    
	} ) ) ;
    },
    
    destroy: function()
    {
	this.disconnect();
    }
});

actiJSCoreParser.addDefinition( '.jsTabPanel', frontpage.carousel, {perPage : 3, newsWidth : 160} );
