﻿
$( document ).ready( function(){
		new MenuSlide( $("#SecondaryMenuTop") );
} )

function MenuSlide( node )
{
	this.node = node;
	this.id = null;
	
	this.currentContent = 0;
	
	this.buttonBefore = null;
	this.buttonNext = null;
	this.presentationPanel = null;
	
	this.Height = 30;
	this.Width = 950;
	this.SlideDelta = 0;
	
	this.contents = "";
	this.aContentsWidth = new Array();
	this.nb_contents = 0;
	
	//Permet de savoir si l'action de Slide est en cours
	this.isSliding = false;
	
	this.init(  );
}

MenuSlide.prototype.init = function( )
{
	var _this = this;
	this.id = $( this.node ).attr( "id" );
	
	this.Width  = $( _this.node ).find( "div.ContainerSlide" ).width();
	
	this.contents = $( _this.node ).find( "ul.SecondaryMenuTop" )[ 0 ];
	
	var iIndexActif = 0;
	var iMarginLeft = 0;
	$( this.contents ).find( "LI" ).each( function(  ){ 
		_this.aContentsWidth[ _this.aContentsWidth.length ] = iMarginLeft;
		iMarginLeft += $(this).width() + 16 + 42;
		
		if( $( this ).find( "a.Active" ).length > 0 )	
		{
			_this.currentContent = iIndexActif;
		}
		iIndexActif++;
	} )
	
	//pour définir jusqu'à combien de lien on slide
	_this.nb_contents = _this.aContentsWidth.length;
	var iTotalWidth = 0;
	for( var i=_this.aContentsWidth.length-1; i > 0; i-- )
	{
		iTotalWidth += _this.aContentsWidth[ i ] - _this.aContentsWidth[ i-1 ];
		if( iTotalWidth < _this.Width )
		{
			this.nb_contents = i+1;
		}
	}
	var iTotalWidth = 0;
	for( var i=0; i<_this.aContentsWidth.length; i++ )
	{
		iTotalWidth += _this.aContentsWidth[ i ];
	}
	if( iTotalWidth < this.Width )	this.nb_contents = 0;
	
	
	
	if( _this.currentContent > this.nb_contents-1 )	_this.currentContent = this.nb_contents-1;
	
	
	
	/*ACTIONS SUR BUTTONS*/
	this.buttonBefore = $( _this.node ).find( "img.ButtonScrollBefore" )[ 0 ];
	this.buttonNext = $( _this.node ).find( "img.ButtonScrollNext" )[ 0 ];
	
	$( this.buttonBefore ).click( function(){ _this.ScrollBefore() } );
	$( this.buttonNext ).click( function(){ _this.ScrollNext() } );	
	
	
	//Placement au chargement
	var iMarginLeft = _this.aContentsWidth[ _this.currentContent ];
	
	var oListeContents = $( _this.node ).find( "ul" )[ 0 ];
	$( oListeContents ).css( "margin-left", -iMarginLeft );
	
	this.MAJ_Navigation(  );
}


MenuSlide.prototype.ActiveButtons = function(  )
{
	var _this = this;
	
	
	//alert( _this.currentContent );
	if( _this.currentContent <= 0 )						$( this.buttonBefore ).hide();
	else												$( this.buttonBefore ).show();
	
	if( _this.currentContent >= _this.nb_contents-1 )	$( this.buttonNext ).hide();
	else												$( this.buttonNext ).show();	
	
}

MenuSlide.prototype.MAJ_Navigation = function(  )
{
	var _this = this;

	
	this.ActiveButtons();
}

MenuSlide.prototype.ScrollBefore = function(  )
{	
	var _this = this;
	
	if( !_this.isSliding )
	{
		var oListeContents = $( _this.node ).find( "ul" )[ 0 ];
		iMarginLeft =  -( _this.aContentsWidth[ _this.currentContent-1 ] );
		
		_this.isSliding = true;
		$( oListeContents ).animate(
			{ marginLeft : iMarginLeft }, 500 , function(  ){ 
				_this.currentContent = _this.currentContent-1;
				_this.MAJ_Navigation();
				_this.isSliding = false;
			}
		);	
	}
}
MenuSlide.prototype.ScrollNext = function(  )
{	
	var _this = this;
	
	if( !_this.isSliding )
	{	
		var oListeContents = $( _this.node ).find( "ul" )[ 0 ];
		iMarginLeft = -( _this.aContentsWidth[ _this.currentContent+1 ] );
		
		_this.isSliding = true;
		$( oListeContents ).animate(
			{ marginLeft : iMarginLeft }, 500 , function(  ){ 
				_this.currentContent = _this.currentContent+1;
				_this.MAJ_Navigation();
				_this.isSliding = false;
			}
		);	
	}
}









