﻿

var gBANDEAU = null;
var gTIMER_BANDEAU = null;
var g_TIMER_BANDEAU_AUTO_SCROLL = 10000;

$( document ).ready( function(){
	if( document.getElementById( "BandeauHome" ) )
		gBANDEAU = new BandeauSlide( $("#BandeauHome") );
} )



function BandeauStartAutoScroll(  )
{
	if( gBANDEAU.contents.length > 1 )
	{
		if( !gBANDEAU.disabledAutoScroll )
		{
			gBANDEAU.ScrollNext(  );
			gTIMER_BANDEAU = setTimeout( "BandeauStartAutoScroll(  )", g_TIMER_BANDEAU_AUTO_SCROLL );
		}
	}
}
function BandeauStopAutoScroll(  )
{
	clearTimeout( gTIMER_BANDEAU );
	gTIMER_BANDEAU = null;
}	

function BandeauSlide( node )
{
	this.node = node;
	this.id = null;
	
	this.currentContent = 0;
	
	this.buttonBefore = null;
	this.buttonNext = null;
	this.presentationPanel = null;
	
	this.Height = 250;
	this.Width = 1004;
	this.SlideDelta = 0;
	
	this.contents = "";
	this.nb_contents = 0;
	
	//Permet de savoir si l'action de Slide est en cours
	this.isSliding = false;
	
	this.init(  );
}

BandeauSlide.prototype.init = function( )
{
	this.id = $( this.node ).attr( "id" );
	this.loadContents(  );
}

BandeauSlide.prototype.loadContents = function( )
{
	$( this.node ).html( "<table width='100%' height='100%'><tr><td valign='middle' align='center'><img src='images/icones/loading.gif'/></td></tr></table>" );

	
	var _this = this;
	$.ajax({ 
		url: "/web-services/Bandeau/bandeau_contents.aspx", 
		type: "GET",
		cache: false,
		success: function( html ){
			_this.contents = html;
			_this.buildContents(  );
			
			//Survol du bandeau ==> Arrêt de l'auto scrool
			$( _this.node ).mouseover( function(){
				BandeauStopAutoScroll(  );
			} )
			$( _this.node ).mouseout( function(){
					gTIMER_BANDEAU = setTimeout( "BandeauStartAutoScroll(  )", g_TIMER_BANDEAU_AUTO_SCROLL );
			} )							
			
			
			//Lance l'auto scrolling
			gTIMER_BANDEAU = setTimeout( "BandeauStartAutoScroll(  )", g_TIMER_BANDEAU_AUTO_SCROLL );
			
		}
	});	
	
}

BandeauSlide.prototype.buildContents = function( )
{
	
	var _this = this

	this.SlideDelta = this.Width-100;
	this.nb_contents = $(this.contents).find( "li[ bandeau_contenu='1' ]" ).length;	
	
	
	if( this.nb_contents == 0 )
	{
		$( _this.node ).html( "" );
		return;
	}
	
	var sHTML = "";
	sHTML += "<table cellspacing='0' cellpadding='0' width='"+ this.Width +"' style='border:0px solid red;'>";
	

	
	/*SLIDE des logos*/
	sHTML += "<tr height='"+ _this.Height +"'>";
	sHTML += "	<td align='left' width='50'	valign='middle'><img class=\"ButtonScrollBefore\" 	src=\"images/slide-nav-left.gif\" style=\"cursor:pointer;\" /></td>";
	sHTML += "	<td align='center' valign='middle' width='"+ this.SlideDelta +"'>";
	sHTML += "		<div class=\"ContainerSlide\" style=\"display:block;width:"+ this.SlideDelta +"px;height:"+ _this.Height +"px;border:0px solid red;overflow-x:hidden;overflow-y:hidden;\">";
	sHTML += "			<div style=\"width:"+ ( this.SlideDelta*this.nb_contents ) +"px;height:245px;border:0px solid green;margin-left:0px;\">";
	sHTML += 			this.contents;
	sHTML += "			</div>";
	sHTML += "		</div>";
	sHTML += "	</td>";
	sHTML += "	<td align='right' width='50' 	valign='middle'><img class=\"ButtonScrollNext\" 	src=\"images/slide-nav-right.gif\" style=\"cursor:pointer;\" /></td>";
	sHTML += "</tr>";
	
	sHTML += "<tr>";
	sHTML += "	<td colspan='10' align='center'><div class=\"ContainerSlideNavBottom\">";
	for( var i=0; i<this.nb_contents; i++ )
	{
		sHTML += "<img  src='images/slide_etat_off.gif' index='"+ i +"' style='margin-left:3px;margin-right:3px;'/>";
	}
	sHTML += "	</div></td>";
	sHTML += "</tr>";
	
	sHTML += "</table>";
	$( _this.node ).html( sHTML );
	
	$( _this.node ).find( "li[bandeau_contenu='1']" ).find( "h3" ).each( function(){
		if( $(this).next()[0].tagName == "BR" )
		{
			$(this).next().remove();
		}
	} )
	
	
	var i = 0;
	
	
	/*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() } );
	
	
	this.currentContent = 0;
	this.MAJ_Navigation(  );
	
	var index=0;
	$( _this.node ).find( "img.LinkLireSuite" ).each( function(){ 
		if( index != 0 )	$(this).hide();
		index++;
	} );
}


BandeauSlide.prototype.DesActiveEndTexte = function(  )
{
	$( this.node ).find( "li[bandeau_contenu='1']" ).find( "div.endTexte" ).remove();
}

BandeauSlide.prototype.ActiveEndTexte = function( p_iContenuIndex )
{
	var oContenu = $( this.node ).find( "li[bandeau_contenu='1']" )[ p_iContenuIndex ];
	var oImgLireSuite = $(oContenu).find( "img.LinkLireSuite" )[0];	
	var iWidth = $(oContenu).find( "td.Contenu" ).width() + 15;
	var iMarginLeft = $(oContenu).find( "td[ is_image='1' ]" ).width() + 15;	
	
	$( "<div class='endTexte' style='position:absolute;z-index:10;height:45px;width:100px;'></div>" ).insertBefore( oImgLireSuite );
	var oEndTexte = $(oContenu).find( "div.endTexte" )[ 0 ];
	$(oEndTexte).width( iWidth ).css( "margin-top", ( 220 - $(oEndTexte).height() ) + "px" );
	$(oEndTexte).css( "margin-left", iMarginLeft+"px" );
}


BandeauSlide.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();	
	
}

BandeauSlide.prototype.MAJ_Navigation = function(  )
{
	var _this = this;
	
	$( _this.node ).find( "div.ContainerSlideNavBottom" ).find( "img" ).each( function(){
		if( $( this ).attr( "index" )+"" == _this.currentContent )
		{
			$( this ).attr( "src", $( this ).attr( "src" ).replace( "_off.gif", "_on.gif" ) )
		}
		else
			$( this ).attr( "src", $( this ).attr( "src" ).replace( "_on.gif" , "_off.gif" ) )
		
	} );
	
	$( $( $( _this.node ).find( "li.Slide" )[ _this.currentContent ] ).find( "img.LinkLireSuite" ) ).show();
	
	this.ActiveEndTexte(  _this.currentContent );
	this.ActiveButtons();
}

BandeauSlide.prototype.ScrollBefore = function(  )
{	
	var _this = this;
	$( _this.node ).find( "img.LinkLireSuite" ).hide();
	this.DesActiveEndTexte();
	
	if( !_this.isSliding )
	{
		var iContainerWidth = $( _this.node ).find( "div.ContainerSlide" ).width();
		var oListeContents = $( _this.node ).find( "ul" )[ 0 ];
		
		var iMarginLeft = $( oListeContents ).css( "margin-left" );
		if( iMarginLeft.indexOf( "px" ) )	iMarginLeft = parseInt( iMarginLeft.replace( "px", "" ) );
		iMarginLeft += iContainerWidth;
		
		_this.isSliding = true;
		$( oListeContents ).animate(
			{ marginLeft : iMarginLeft }, 500 , function(  ){ 
				_this.currentContent = _this.currentContent-1;
				_this.MAJ_Navigation();
				_this.isSliding = false;
			}
		);	
	}
}
BandeauSlide.prototype.ScrollNext = function(  )
{	
	var _this = this;
	this.DesActiveEndTexte();
	
	
	//Si en bout de chaine ==> On revient sur zle 1er

	if( _this.currentContent == _this.nb_contents-1 )	
	{
		_this.buildContents(  );
		return;
	}
	$( _this.node ).find( "img.LinkLireSuite" ).hide();



	
	if( !_this.isSliding )
	{	
		var iContainerWidth = $( _this.node ).find( "div.ContainerSlide" ).width();
		var oListeContents = $( _this.node ).find( "ul" )[ 0 ];
		
		var iMarginLeft = $( oListeContents ).css( "margin-left" );
		if( iMarginLeft.indexOf( "px" ) )	iMarginLeft = parseInt( iMarginLeft.replace( "px", "" ) );
		iMarginLeft -= iContainerWidth;
		
		_this.isSliding = true;
		$( oListeContents ).animate(
			{ marginLeft : iMarginLeft }, 500 , function(  ){ 
				_this.currentContent = _this.currentContent+1;
				_this.MAJ_Navigation();
				_this.isSliding = false;
			}
		);	
	}
}






function GetImageDimensionRatioSlide( p_iSrcImgWidth, p_iSrcImgHeight, p_iMaxWidth, p_iMaxHeight )
{
	//Si image plus petit que container on renvoie l'image
	if( p_iSrcImgHeight < p_iMaxHeight && p_iSrcImgWidth < p_iMaxWidth )	return  { WIDTH :p_iSrcImgWidth, HEIGHT : p_iSrcImgHeight  };
	
	var iImgHeight = p_iSrcImgHeight;
	var iImgWidth = p_iSrcImgWidth;		
	
	if( iImgWidth == 0 )	iImgWidth = p_iMaxWidth;
	
	var iMaxWidth = p_iMaxWidth;
	var iMaxHeight = p_iMaxHeight;

	var ratio = 1.0;
	//Image paysage
	if( iImgWidth > iImgHeight )
	{
		ratio = iMaxWidth / iImgWidth;
		iImgWidth = iImgWidth * ratio;
		iImgHeight = iImgHeight * ratio;
		
		if( iImgHeight > iMaxHeight )
		{
			ratio = iMaxHeight / iImgHeight;
			iImgHeight = iMaxHeight;
			iImgWidth = iImgWidth * ratio;
		}
	}
	else
	{
		ratio = iMaxHeight / iImgHeight;
		iImgWidth = iImgWidth * ratio;
		iImgHeight = iImgHeight * ratio;
		
		if( iImgWidth > iMaxWidth )
		{
			ratio = iMaxWidth / iImgWidth;
			iImgWidth = iMaxWidth;
			iImgHeight = iImgHeight * ratio;
		}						
	}
	iImgWidth = Math.floor( iImgWidth )
	iImgHeight = Math.floor( iImgHeight )
	return { WIDTH :iImgWidth, HEIGHT : iImgHeight  };
	
}




