﻿var aItems = new Array( );
var lastIndexItem = 0;
var indexItem = 0;
var iNumImagesLoaded = 0;
var CoverFlowWidthBig = 240;
var CoverFlowWidthMedium = 100;
var CoverFlowWidthSmall = 60;
var CoverFlowMargesWidth = 10;
var CoverFlowIntervalButtonScroll = 1000;
var CoverFlowOpacity = 0.9;

var CoverFlowTotalWidth = 1000;

var CoverFlowWidthInfos = 250;

var CoverFlowAnimation = 0;	// compteur d'anim -> si != 0 on ne peut pas relancer un autre scroll...
var CoverFlowAnimationTimer = 290	// en ms

var aIntervales = new Array( );


function intervalStart( name, callback, interval )
{
	callback.call(this);
	aIntervales[name] = setInterval( callback, interval )
}

function intervalEnd( name )
{
	if ( aIntervales[name] != null ) {
		clearInterval( aIntervales[name] )
		aIntervales[name] = null;
	}
}

function toLeft( )
{
	if ( CoverFlowAnimation > 0 ) return;
	if ( indexItem > 0 ) {
		$('#Infos').hide( );
		if ( indexItem+1 < aItems.length )	toMin( aItems[indexItem+1].get( "img" ), indexItem+1 )
		toMedium( aItems[indexItem].get( "img" ), indexItem )
		toLarge( aItems[indexItem-1].get( "img" ), indexItem-1 )
		if ( indexItem-2 >= 0)	toMedium( aItems[indexItem-2].get( "img" ), indexItem-2 )
		indexItem--;
	}
}
function toRight( )
{
	if ( CoverFlowAnimation > 0 ) return;
	if ( indexItem < aItems.length-1 ) {
		$('#Infos').hide( );
		if ( indexItem+2 < aItems.length ) toMedium( aItems[indexItem+2].get( "img" ), indexItem+2 )
		toLarge( aItems[indexItem+1].get( "img" ), indexItem+1 )
		if ( indexItem >= 0 ) toMedium( aItems[indexItem].get( "img" ), indexItem )
		if ( indexItem-1 >= 0 )	toMin( aItems[indexItem-1].get( "img" ), indexItem-1 )
		indexItem++;
	}
}
function to( index )
{
	$('#Infos').hide( );
	if ( indexItem == index-1 )
	{
		toRight( );
	}
	else if ( indexItem == index-2 )
	{
		if ( indexItem-1 >= 0 ) toMin( aItems[indexItem-1].get( "img" ), indexItem-1)
		toMin( aItems[indexItem].get( "img" ), indexItem )
		
		toMedium( aItems[indexItem+1].get( "img" ), indexItem+1 )
		toLarge( aItems[indexItem+2].get( "img" ), indexItem+2 )
		if ( indexItem+3 < aItems.length ) toMedium( aItems[indexItem+3].get( "img" ), indexItem+3 )
		indexItem+=2;
	}
	else if ( index == indexItem-1 )
	{
		toLeft( );
	}
	else if ( index == indexItem-2 )
	{
		if ( indexItem+1 < aItems.length ) toMin( aItems[indexItem+1].get( "img" ), indexItem+1)
		toMin( aItems[indexItem].get( "img" ), indexItem )
		
		if ( indexItem-3 >= 0 ) toMedium( aItems[indexItem-3].get( "img" ), indexItem-3 )
		toLarge( aItems[indexItem-2].get( "img" ), indexItem-2 )
		toMedium( aItems[indexItem-1].get( "img" ), indexItem-1 )

		indexItem-=2;
	}
	else 
	{	
		if ( indexItem - 1 >= 0 )				toMin( aItems[indexItem-1].get( "img" ), indexItem-1 )
		toMin( aItems[indexItem].get( "img" ), indexItem )
		if ( indexItem + 1 < aItems.length )	toMin( aItems[indexItem+1].get( "img" ), indexItem+1 )

		indexItem = index
		if ( indexItem-1 >= 0 ) toMedium( aItems[indexItem-1].get( "img" ), indexItem-1 )
		toLarge( aItems[indexItem].get( "img" ), indexItem )
		if ( indexItem+1 < aItems.length ) toMedium( aItems[indexItem+1].get( "img" ), indexItem+1 )
	}
}

function BdVo_ChangeBandeauOP( )
{
	
	$("#ctl00_PageBandeauVO").find("img").load( function( ) {
		$("#ctl00_PageBandeauVO").css('visibility','visible');
	} );
	$("#ctl00_PageBandeauVO").find("img").attr('src', '/images/bandeau-operation-vo-2012.jpg' ).css('cursor','pointer').click( function() {
		document.location = "/vehicules-occasions/offres-du-moment";
	});
}

/*
var BdVo_ForceBandeau_MethodCalled = false;
var BdVo_ForceBandeau = setInterval( function( )
{
	if ( $("#ctl00_PageBandeauVO").length && $("#ctl00_PageBandeauVO").find("img").length )
	{
		$("#ctl00_PageBandeauVO").css('visibility','hidden');
		clearInterval( BdVo_ForceBandeau );
		BdVo_ForceBandeau = null;
		
		if ( !BdVo_ForceBandeau_MethodCalled )	BdVo_ChangeBandeauOP();
 	}
}, 150 );
*/

function LoadBandeauSlide( id_vehicules )
{
/*	BdVo_ForceBandeau_MethodCalled = true;
	
	if ( typeof DISABLE_BANDEAU_VO != "undefined" && DISABLE_BANDEAU_VO ) {
		BdVo_ChangeBandeauOP( );
		return;
	}
*/	
	var sUrl = "/web-services/VO/bandeau.aspx?"
//	if ( SiteInfo.PlaqueId != "" )		sUrl += "code=&plaque_id="+SiteInfo.PlaqueId+"&xml=1"
//	else								sUrl += "code="+SiteInfo.CodeDistributeur+"&plaque_id=&xml=1"

	if ( typeof id_vehicules != "undefined" && id_vehicules != "" )
		sUrl += "id_vehicules="+id_vehicules+"&xml=1"
	else
		sUrl += "site_id="+SiteInfo.SiteId+"&xml=1"
	
	$.ajax({
		 type: "GET",
		 url: sUrl,
		 dataType: "xml",
		 cache: false,
		 success: function(xml) {
			var num = 0;
			iNumImagesLoaded = $(xml).find('vehicule').length;

			if ( iNumImagesLoaded <= 0 )	$('#BandeauSlideContainer').hide();
			else							$('#BandeauSlideContainer').show();

			if ( iNumImagesLoaded == 1 )
			{
				$('#BandeauSlideContainer_Left').hide( );
				$('#BandeauSlideContainer_Right').hide( );
			}
			else
			{
				$('#BandeauSlideContainer_Left').show( );
				$('#BandeauSlideContainer_Right').show( );
			}
			
			if ( $(xml).find('vehicule').length <= 0 )
			{
		//		BdVo_ChangeBandeauOP( );
			}
			else
			{
				$("#ctl00_PageBandeauVO").css('visibility','visible');
			
				$(xml).find('vehicule').each(function(){
					var oItem = new jQuery.Hashtable( );
					var aPhotos = $(this).find("photo");
					 
					var foundPhoto = 0;
					for ( var i = 0 ; i < aPhotos.length ; i++ )
					{
						if ( aPhotos[i].firstChild )
							oItem.add( "photo"+((foundPhoto++)+1), aPhotos[i].firstChild.nodeValue );
					}
						
					oItem.add( "modele", $(this).find('modele_complete').text() );
					oItem.add( "prix", $(this).find('prix').text() );
					oItem.add( "kilometrage", $(this).find('kilometrage').text() );
					oItem.add( "tel", $(this).find('distributeurs').find('distributeur').find('telephone').text() );
					oItem.add( "id", $(this).attr('id') );
					oItem.add( "url", $(this).find('url-complete').text());
					
					var oImg = document.createElement( "img" )
					oImg.style.cursor = "pointer"
					$(oImg).attr('index', num++)
					$(oImg).click( function( ) {
						to( parseInt($(this).attr('index')) )
					});
					
					var sEquip = "";
					if ( $(this).find('toit_ouvrant').text() == "1" )
						sEquip += '<div style="float:left;"><img src="/images/occasions/toit_ouvrant.gif" alt="Toit ouvrant" border="0" align="absmiddle" />&#160;</div>'
						
					if ( $(this).find('cuir').text() == "1" )
						sEquip += '<div style="float:left;"><img src="/images/occasions/cuir.gif" alt="Intérieur Cuir" border="0" align="absmiddle" />&#160;</div>'
						
					if ( $(this).find('climatisation').text() == "1" )
						sEquip += '<div style="float:left;"><img src="/images/occasions/clim.gif" alt="Climatisation" border="0" align="absmiddle" />&#160;</div>'
						
					if ( $(this).find('gps').text() == "1" )
						sEquip += '<div style="float:left;"><img src="/images/occasions/gps.gif" alt="GPS" border="0" align="absmiddle" />&#160;</div>'
						
					if ( $(this).find('transmission').text() == "Automatique" )
						sEquip += '<div style="float:left;"><img src="/images/occasions/boite_auto.gif" alt="Boîte automatique" border="0" align="absmiddle" />&#160;</div>'
					oItem.add( "equipements", sEquip );
					
					
					var loadOrDie = function( ) {
						$(this).show(); 
						
						iNumImagesLoaded--; 
						if ( iNumImagesLoaded == 0 )
							LaunchItuneStyle()
					}
					$(oImg).hide().load( loadOrDie ).error( loadOrDie );
					if ( oItem.get("photo1") == null ) 
					{
						oImg.src = '/images/blank.gif';
					}
					else
					{
						oImg.src = oItem.get("photo1");
					}
					//oImg.src ="images/besoin-aide-photo.gif"
					
					oImg.style.height = "auto";
					oImg.style.width = CoverFlowWidthSmall + "px"
					oItem.add( "img", $(oImg));
					
					var oTd = document.createElement( "TD" );
					oTd.setAttribute( "valign", "bottom" );
					oTd.style.cssFloat = "left"
					//oTd.style.height = "150px"
					/*
					oTd.style.border= "2px solid red"
					oTd.style.height = "150px"
					oTd.style.margin = 0;
					oTd.style.padding = 0;
					oImg.style.padding = 0;
					oImg.style.margin = 0;*/

					var oDiv = document.createElement( "SPAN" );
					if ( jQuery.browser.msie ) 
					{
						oTd.appendChild( oImg )
					}
					else
					{
						oDiv.appendChild( oImg )
						oTd.appendChild( oDiv )
					}
					
					$('#iTuneZoneContent').append( oTd );
						
					 aItems[aItems.length] = oItem;
				}); //close each(
				//alert( $('#iTuneZoneContent').parent().parent().html() );
				
				$('#slideLeft').patchPNG( );
				$('#slideRight').patchPNG( );
			}

		}
	});	
}


function LaunchItuneStyle( )
{
	$('#iTuneZone').scrollLeft(0);
	indexItem = -1
	toRight( )
	$('#iTuneBack').fadeTo( 0, CoverFlowOpacity );
}



function toLarge( img, i )
{
	var ratio = $(img).height()/$(img).width();
	
	var destW = CoverFlowWidthBig;
	var destH = parseInt(CoverFlowWidthBig*ratio);
	
	destLeft = CoverFlowTotalWidth + parseInt( (destW+CoverFlowMargesWidth) / 2 )
	destLeft -= parseInt( CoverFlowTotalWidth/2 );
	destLeft += parseInt(CoverFlowWidthInfos/2)

	var pos = 0;
	$(img).parents("td:first").prevAll("td").each( function () {
		if ( pos++ == 0 )	destLeft += CoverFlowWidthMedium + CoverFlowMargesWidth;
		else				destLeft += CoverFlowWidthSmall + CoverFlowMargesWidth;
	});
	
	var _img = $(img);
	
	CoverFlowAnimation++;
	$('#iTuneZone').animate({ 
			scrollLeft: destLeft+"px"
		}, CoverFlowAnimationTimer, function( ) {
			CoverFlowAnimation--;
		}
	);
	toSize( img, i, destW, destW + CoverFlowWidthInfos )
}
	

function drawCurrentInfos( ) 
{
	if ( CoverFlowAnimation != 0 ) return;
	if ( !aItems[ indexItem ] ) return;
	
	$('#Infos').drawOver( $(aItems[ indexItem ].get('img')) ).show();

	//PATCH CALLAGE
	var delta = 180 - $(aItems[ indexItem ].get('img')).height( );
	if ( delta > 0 )
		$('#Infos').css('top', ( $('#Infos').offset().top - delta ) +'px' );
	
	//oItem.add( "id", $(this).attr('id') );
	$('#InfosModele').html( aItems[ indexItem ].get( "modele" ) )
	$('#InfosPrix').html( aItems[ indexItem ].get( "prix" ) )
	$('#InfosKm').html( aItems[ indexItem ].get( "kilometrage" ) )
	$('#InfosEquipements').html( aItems[ indexItem ].get( "equipements" ) )
	$('#DistriTel').html( aItems[ indexItem ].get( "tel" ) )
	
	
	// Test cookie pour Ajouter / Retirer
	var sKey = aItems[ indexItem ].get( "id" ) + ":" + aItems[ indexItem ].get( "modele" )
	var sFavoris = getCookie( "FavorisVO" )
	if ( sFavoris == null ) sFavoris = "";
	if ( sFavoris.indexOf( sKey ) >= 0 )
		$('#SlideSelectionLib').html( "> Retirer de ma sélection" )
	else
		$('#SlideSelectionLib').html( "> Ajouter à ma sélection" )
}
	
function toMin( img, index )
{
	toSize( img, index, CoverFlowWidthSmall, CoverFlowWidthSmall)
}
function toMedium( img, index )
{
	toSize( img, index, CoverFlowWidthMedium, CoverFlowWidthMedium)
}

function toSize( img, index, width, widthTd )
{
	var ratio = $(img).height()/$(img).width();
	
	var destW = width;
	var destH = parseInt(width*ratio);
	
	CoverFlowAnimation++;
	$(img).animate({ 
			width: destW+"px",
			height: destH+"px"
		}, CoverFlowAnimationTimer, function( ) {
			CoverFlowAnimation--;
			// Si large --> Infos
			if ( width == CoverFlowWidthBig )
				setTimeout( "drawCurrentInfos( )", 100);
		} );
	CoverFlowAnimation++;
	$(img).parents("td:first").animate({ 
			width: (widthTd)+"px"
		}, CoverFlowAnimationTimer, function( ) {
			CoverFlowAnimation--;
		} );
		
}		

