/**SCROLL CAR LIST HOMEPAGE***/

carListX = 0;

function moveCarList(newX, t) {

    carListX += newX;
    var item = $('carListScroll');
    var xMax = -255;

    $('scrollLeft').style.visibility = 'visible';
    $('scrollRight').style.visibility = 'visible';
    if (carListX <= xMax) {

        carListX = xMax;
        $('scrollRight').style.visibility = 'hidden';

    } else if (carListX >= 0) {

        carListX = 0;

        $('scrollLeft').style.visibility = 'hidden';

    } else {


    }

    moveItemNoY2(item, carListX, t);
}

function goPage(p, b) {

    if (b == 1) {
        window.open(p);
    } else {
        window.location.href = '' + p + '';
    }

}

/****Configure****/

function setColor(id) {
    document.getElementById("colorForm").value = id;
    //alert(document.getElementById("colorForm").value);
}

function color(img,title) {
					
						var morphObject = new Fx.Morph($('blocAbove'), {
						
						onComplete: function() {
						
						//Modif. Ewapps : le nom de fichier complet est transmis en paramètre
						//$('carBG').style.backgroundImage='url(images/configure/colorCars/'+img+'.jpg)';
						$('carBG').style.backgroundImage='url('+img+')';
						$('blocAbove').style.backgroundImage='none';
						$('blocAbove').style.width='0px';
						
						$('titleAbove').set('text',title);
						
						}	
						
						});
						
						morphObject.start({
   						
   						'width': 455,
    					'height': 214,
    					//Modif. Ewapps : le nom de fichier complet est transmis en paramètre
    					//'background-image':'url(images/configure/colorCars/'+img+'.jpg)'
    					'background-image':'url('+img+')'
    					
						});
					
};

function carChange(img,title,option) {
					
  					var item = $('carBG');
  					
  					var fx = new Fx.Tween(item,{
  					      duration: 200,
  					      onComplete: function(){
  					              //Modif. Ewapps : le nom de fichier complet est transmis en paramètre
  					              //item.style.backgroundImage='url(images/configure/colorCars/'+img+'.jpg)';
  					              item.style.backgroundImage='url('+img+')';
  					              item.fade('in');
  					              $('titleAbove').set('text',title);
  					              
  					      }
  					});
  					
  					if(option == 1) {
  					
  							fx.start('opacity',1,0);  
  							pushTriangle(0);
  					
  					} else {
  					
  							fx.start('opacity',0,0);   					
  					
  					}


					
					
};

function pushTriangle(i) {
					
						var morphObject2 = new Fx.Morph($('pushTriangle'));
						
						var w = i*33;
												
						morphObject2.start({
   						
   						'width': w,
    					'height': 8
    					
						});
					
};




function finitionMorph(m) {
					
						
			mySlide1.toggle();
						
						
					
};





/*************HOME***************************/



/**
* Move a div on the X axis 
* item 	{String} name of the div to move
* x 	{Int} new position on x axis
* t 	{Int} time to go to the x position (in millisec)
* i		{Int} number of the item to check if it's the last item
* max	{Int} total of items   
* @return nothing
*/


function moveItemNoY(item,x,t,i,max){

	item.set('tween', {duration: t});
		
	if(i==max){	
	
		x	=	0;	
		resetNewsSelected('newsButton0',max,1);	
		
	}else{
	
		x	=	-(x*i);
	
	}
	
	
	item.tween('margin-left', x);
				
}
/**
* Move a div on the X axis ===> not used now
* item 	{String} name of the div to move
* x 	{Int} new position on x axis
* t 	{Int} time to go to the x position (in millisec)
* i		{Int} number of the item to check if it's the last item
* max	{Int} total of items   
* @return nothing
*/
function moveItemNoY2(item,x,t,text){
	
	item.set('tween', {duration: t});
		
	if(text){item.innerHTML  = text;}
	item.tween('margin-left', x);
				
}

/**
* Set all news to a o or a &bull character if selected
* item 	{String} name of the div to move
* total	{Int} total of items   
* @return nothing
*/

function resetNewsSelected(item,total){

	
	var iName	=	'newsButton';
	
	for(var i=1;i<=(total);i++){
			
		$(iName+i).innerHTML = '<font font-size="8px">o</font>';//i;
	
	}
		n	=	parseInt(item.replace(iName,''));
		
	
		var item	=$(iName+n);
		
		item.innerHTML = '<b>&bull;</b>';//n;
		
}
/**
* Update the newsList : move to next news and update the o to a &bull
* i		{Int} number of the current item
* max	{Int} total of items   
* @return nothing
*/

function changeSelectedNews(i,max){
	
	resetNewsSelected('newsButton'+i,max);
	moveItemNoY($('mainVisualNewsItemContainer'),286,1000,(i-1),max);

}

/**
* Update the newsList : move to next news and update the o to a &bull
* i		{Int} number of the current item
* max	{Int} total of items   
* @return nothing
*/
function changeI(i,max){
	
	if(i==max){i=0;}
	i++;
		
	changeSelectedNews(i,max);
	
	timeOut  = setTimeout('changeI('+i+','+max+')',5000);
	return timeOut;
}

/**
* Get the position of a div  
* item 	{String} name of the div to get the coordonates
* @return Array [margin-left,margin-top]
*/
function findPos(item) {
	var curleft =0;
	var curtop = 0;

	if (item.offsetParent) {
		do {
			curleft += item.offsetLeft;
			curtop += item.offsetTop;
		} while (item = item.offsetParent);


	return [curleft,curtop];
	
	}
}


/*****NETWORK***********************/



/**
* update the background image of a div  
* n 	{Int} num of the div to display or hide (based on province id)
* v 	{string} state of the province over zone => 
* execute an ajax request that reload the dealer list into the networkAllDealerContainer div
* @return nothing
*/

function changeBgMap(n,v){

    var item = $('provinceMap'+n)
  	if(v==0){s="out";i=1;e=0;}else{s="int";i=0;e=1;}		// i = start of the fade, e = end of the fade
  	
  	// Added by Ewapps - START
  	// On envoie la liste des provinces avec un séparateur "," plutôt que la dernière province cliquée (recherche sur plusieurs provinces)
  	var selected_provinces = "";
  	for (i=1;i<=12;i++) {
        var item2 = $('province'+i+'Form');
        //item2 = document.getElementById('province'+i+'Form');

        if (item2.value == "1") {
            selected_provinces += i + ',';
        }
  	}
  	selected_provinces = selected_provinces.substr(0,selected_provinces.length-1)
  	// Store in hidden field : used for link to Google Maps
  	$('hidProvinceList').value = selected_provinces
  	
  	if (selected_provinces != "") {
  	    $('linkShowMap').style.display = "block";
  	}
  	else {
  	    $('linkShowMap').style.display = "none";
  	}
  	
  	
  	var dealerType = $('dealerType').value;
  	// Added by Ewapps - END
  	
   	/*UPDATED 20101027*/
 
 
 	 new Request({
 	 			url: 'AjaxDealerList.aspx?province='+selected_provinces+'&dealertype='+dealerType,
 	 				evalScripts: true,
 	 				onRequest	: function() { 
 	 					$('networkAllDealerContainer').innerHTML = '<p align="center">Loading</p><p align="center"><img src="images/loader.gif"/></p>';
					},
						
					onComplete: function(response) {
					
			  		  	 $('networkAllDealerContainer').set('html', response);
			    		 var fx = new Fx.Tween(item);
			    		 fx.start('opacity',i,e);
			    	}
	 }).send();
 
 
 
 
  
  /* OLD 
  
    var fx = new Fx.Tween(item,{
        duration: 200,
        
                     
        onComplete: function(){ 
                               
         
         	var req = new Request.HTML({
												
			url: 'AjaxDealerList.aspx?province='+selected_provinces+'&dealertype='+dealerType, 
		
		onRequest : function(){		
			
			$('networkAllDealerContainer').innerHTML = '<p align="center">Loading</p><p align="center"><img src="images/loader.gif"/></p>';
		},
		onSuccess: function(html) {		
			
	    	$('networkAllDealerContainer').set('text', '');
			$('networkAllDealerContainer').adopt(html);
			
												
		}
																	
	}).send();       
                      
                
        }
    });
    fx.start('opacity',i,e);  
    */
}

function changeBgModel(itemID,v){

    var item = $(itemID)
  	if(v==0){s="out";start=1;stop=0;}else{s="int";start=0;stop=1;}		// i = start of the fade, e = end of the fade
  	
  	// Added by Ewapps - START
  	// On envoie la liste des provinces avec un séparateur "," plutôt que la dernière province cliquée (recherche sur plusieurs provinces)
  	var selected_typologies = "";
  	for (i=1;i<=8;i++) {
        var item2 = $('carKm'+i+'Form');
        //item2 = document.getElementById('province'+i+'Form');
    
        if (item2.value == "1") {
            selected_typologies += i + ',';
        }
  	}
  	selected_typologies = selected_typologies.substr(0,selected_typologies.length-1);
  	//alert(selected_typologies);
  	
  	var selected_fuels = "";
  	for (i=1;i<=3;i++) {
        var item2 = $('carFuel'+i+'Form');
        //item2 = document.getElementById('province'+i+'Form');
    
        if (item2.value == "1") {
            selected_fuels += i + ',';
        }
  	}
  	selected_fuels = selected_fuels.substr(0,selected_fuels.length-1);
  	
  	var selected_gears = "";
//  	for (i=1;i<=3;i++) {
//        var item2 = $('carGear'+i+'Form');
        //item2 = document.getElementById('province'+i+'Form');
    
//        if (item2.value == "1") {
//            selected_gears += i + ',';
//        }
//  	}
//  	selected_gears = selected_gears.substr(0,selected_gears.length-1);
  	
  	var selected_prices = "";
  	for (i=1;i<=4;i++) {
        var item2 = $('carPrice'+i+'Form');
        //item2 = document.getElementById('province'+i+'Form');
    
        if (item2.value == "1") {
            selected_prices += i + ',';
        }
  	}
  	selected_prices = selected_prices.substr(0,selected_prices.length-1);


  	// On rafraichit le bon div en fonction du type d'affichage
    // EWAPPS : Dans notre cas, un seul type d'affichage
  	var displayType = 1 //$('ctl00_MainContent_displayType').value;
  	var containerDivID;
  	if(displayType == 1) {
  	    containerDivID = 'rangeAllModelContainer';
  	    //alert("coucou 1");
  	}
  	else {
  	    containerDivID = 'rangeAllModelContainer';
  	    //alert("coucou 2");
  	}
  	
  	
  	// On récupère le type de tri (prix, CO2, conso)
  	var compareSortType;
  	if(displayType == 1) {
  	    compareSortType = '';
  	}
  	else {
  	    compareSortType = $('compareSortType').value;
  	}
   	
  	//var dealerType = $('dealerType').value;
  	// Added by Ewapps - END
  	
  	//alert("coucou");
  	
  	//if((displayType == 1) && (n.indexOf('carType') != -1)) {
  		    //Nouvelle idée de yours --> quand on clique sur un type de véhicule on va d'office sur l'affichage comparatif
  		    //--> changement type affichage et appel ajax normal
            //displayType = 2;	    
  		    
  		    //Virer cette branche du test si ça foire...
  		    //window.location.assign('Range.aspx?mode=2&typology='+selected_typologies+'&fuel='+selected_fuels+'&gear='+selected_gears+'&price='+selected_prices);
  	//}
  	
  	//$('ctl00_MainContent_displayType').value = displayType;
  	   	
    //alert('AjaxRange.aspx?typology='+selected_typologies+'&fuel='+selected_fuels+'&gear='+selected_gears+'&price='+selected_prices+'&mode='+displayType+'&sort='+compareSortType);

//	if ($(containerDivID).getStyle('min-height') == 'auto')	{
//		$(containerDivID).setStyle('min-height', $(containerDivID).getSize().y + 6);
//	} else {
//		$(containerDivID).setStyle('min-height', $(containerDivID).getSize().y);
//    }

    var fx = new Fx.Tween(item,{
        duration: 200,
        
                     
        onComplete: function(){ 
                               
         
     	    var req = new Request.HTML({
												
		    url: 'AjaxRange.aspx?typology='+selected_typologies+'&fuel='+selected_fuels+'&gear='+selected_gears+'&price='+selected_prices+'&mode='+displayType+'&sort='+compareSortType, 
		
	    onRequest : function(){		
			
		    $(containerDivID).innerHTML = '<p align="center">Loading</p><p align="center"><img src="images/loader.gif"/></p>';
	    },
	    onSuccess: function(html) {		
			
    	    $(containerDivID).set('text', '');
		    $(containerDivID).adopt(html);
		    //moveCar('carMove',0,1000,4);
												
	    }
																	
    }).send();       
                      
                
        }
    });
    fx.start('opacity',start,stop);  
// EWAPPS : la ligne suivante devra etre mise en commentaire et remplacee par le fx.start ci-dessus quand le code .net sera OK
//    parent.displayItemTimeOut = parent.setTimeout('parent.displayItem(\'CarRange\', 5,800,1);',0);
}


function changeRangeDisplayMode(newDisplayType){
    $('ctl00_MainContent_displayType').value = newDisplayType;
    
    
    // Added by Ewapps - START
  	// On envoie la liste des provinces avec un séparateur "," plutôt que la dernière province cliquée (recherche sur plusieurs provinces)
  	var selected_typologies = "";
  	for (i=1;i<=6;i++) {
        var item2 = $('carType'+i+'Form');
        //item2 = document.getElementById('province'+i+'Form');
    
        if (item2.value == "1") {
            selected_typologies += i + ',';
        }
  	}
  	selected_typologies = selected_typologies.substr(0,selected_typologies.length-1);
  	//alert(selected_typologies);
  	
  	var selected_fuels = "";
  	for (i=1;i<=2;i++) {
        var item2 = $('carFuel'+i+'Form');
        //item2 = document.getElementById('province'+i+'Form');
    
        if (item2.value == "1") {
            selected_fuels += i + ',';
        }
  	}
  	selected_fuels = selected_fuels.substr(0,selected_fuels.length-1);
  	
  	var selected_gears = "";
  	for (i=1;i<=3;i++) {
        var item2 = $('carGear'+i+'Form');
        //item2 = document.getElementById('province'+i+'Form');
    
        if (item2.value == "1") {
            selected_gears += i + ',';
        }
  	}
  	selected_gears = selected_gears.substr(0,selected_gears.length-1);
  	
  	var selected_prices = "";
  	for (i=1;i<=4;i++) {
        var item2 = $('carPrice'+i+'Form');
        //item2 = document.getElementById('province'+i+'Form');
    
        if (item2.value == "1") {
            selected_prices += i + ',';
        }
  	}
  	selected_prices = selected_prices.substr(0,selected_prices.length-1);

    //window.location.assign('Range.aspx?mode='+mode+'&typology='+selected_typologies+'&fuel='+selected_fuels+'&gear='+selected_gears+'&price='+selected_prices);

    // On rafraichit le bon div en fonction du type d'affichage
  	var displayType = $('ctl00_MainContent_displayType').value;
  	var containerDivID;
    containerDivID = 'rangeAllModelContainer';
    
   
    // On récupère le type de tri (prix, CO2, conso)
  	//var compareSortType;
  	//if(displayType == 1) {
  	//    compareSortType = '';
  	//}
  	//else {
  	//    compareSortType = $('compareSortType').value;
  	//}

  	
    //alert("DEBUG JS : " + 'AjaxRange.aspx?typology='+selected_typologies+'&fuel='+selected_fuels+'&gear='+selected_gears+'&price='+selected_prices+'&mode='+newDisplayType+'&sort=0');                              

         
    var req = new Request.HTML({
												
	url: 'AjaxRange.aspx?typology='+selected_typologies+'&fuel='+selected_fuels+'&gear='+selected_gears+'&price='+selected_prices+'&mode='+newDisplayType+'&sort=0', 
	
    onRequest : function(){		
		
	    $(containerDivID).innerHTML = '<p align="center">Loading</p><p align="center"><img src="images/loader.gif"/></p>';
    },
    onSuccess: function(html) {		
		
	    $(containerDivID).set('text', '');
	    $(containerDivID).adopt(html);
	    //moveCar('carMove',0,1000,4);
											
    }
																	
    }).send();                           
  	
}


function setDealerType(n){

	$('dealerType').value = n;
    
    // Set link in bold
    $('dealerType'+n+'Link').style.fontWeight='bold';
    $('dealerType'+((n%2)+1)+'Link').style.fontWeight='normal';
  	
  	// Added by Ewapps - START
  	// On envoie la liste des provinces avec un séparateur "," plutôt que la dernière province cliquée (recherche sur plusieurs provinces)
  	var selected_provinces = "";
  	for (i=1;i<=12;i++) {
        var item2 = $('province'+i+'Form');
        //item2 = document.getElementById('province'+i+'Form');

        if (item2.value == "1") {
            selected_provinces += i + ',';
        }
  	}
  	selected_provinces = selected_provinces.substr(0,selected_provinces.length-1)
  	
  	var dealerType = $('dealerType').value;
	//var search = $('search').value;
  	// Added by Ewapps - END

	var req = new Request.HTML({

    url: 'AjaxDealerList.aspx?province='+selected_provinces+'&dealertype='+dealerType+'&search='+search, 
	onRequest : function(){		
		
		$('networkAllDealerContainer').innerHTML = '<p align="center">Loading</p><p align="center"><img src="images/loader.gif"/></p>';
	},
	onSuccess: function(html) {		
		
    	$('networkAllDealerContainer').set('text', '');
		$('networkAllDealerContainer').adopt(html);
		
											
	}
																	
	}).send();       
                                   
}


function setModelCompareSortType(n){
	$('compareSortType').value = n;
    
    if (n == 0) {
        $('sortTypePriceLink').style.fontWeight='bold';
        $('sortTypeCO2Link').style.fontWeight='normal';
        $('sortTypeConsoLink').style.fontWeight='normal';
    }
    else if (n == 1) {
        $('sortTypePriceLink').style.fontWeight='normal';
        $('sortTypeCO2Link').style.fontWeight='bold';
        $('sortTypeConsoLink').style.fontWeight='normal';
    }
    else {
        $('sortTypePriceLink').style.fontWeight='normal';
        $('sortTypeCO2Link').style.fontWeight='normal';
        $('sortTypeConsoLink').style.fontWeight='bold';
    }
}



function changeBgMotor(n,v){
   
    var item = $('provinceMap'+n)
  	if(v==0){s="out";i=1;e=0;}else{s="int";i=0;e=1;}		// i = start of the fade, e = end of the fade
    var fx = new Fx.Tween(item,{
        duration: 200,
        
                     
        onComplete: function(){ 
                               
         
         	var req = new Request.HTML({
												
			url: 'AjaxMotorization.aspx?fuelid=1', 
		
		onRequest : function(){		
			
			$('AllMotorizationContainer').innerHTML = '<p align="center">Loading</p><p align="center"><img src="images/loader.gif"/></p>';
					
		},
		onSuccess: function(html) {		
																		
			$('AllMotorizationContainer').set('text', '');
			$('AllMotorizationContainer').adopt(html);
			
												
		}
																	
	}).send();       
                      
                
        }
    });
    fx.start('opacity',i,e);  
               
}

/**
* update the background image of a div  
* item 	{String} name of the div to change background
* im 	{String} image of the new background image, default : null
* @return nothing
* USAGE :  network,range
*/

function changeBg(item,im,map,model){
   
 if (im == null){
   im = 'Small';
 }
    
  	var form	=	item.id+'Form';
  	//alert(form);		
							
	if($(form).value==1){var bt	=	'Out';var v=0;}else{ var bt	=	'Over';var v=1;}
							
	$(form).value = v;
	//$('search').value = '';
  	
  	if(map){
  		n = $(item).id.replace('province','');
  		changeBgMap(n,v);
  	}
  	
  	//alert("coucou 1");
  	//alert(brol);
  	if(model){
  		//n = $(item).id.replace('province','');
  		//alert("coucou 2");
  	    n = $(item).id;
  	    //alert(n);
  		changeBgModel(n,v);
  		//alert("coucou 3");
  	}

	//if(motor){
  	//
  	//	n = $(item).id.replace('motorization','');
  	//	changeBgMotor(n,v);}
  	
    var fx = new Fx.Tween(item,{
        duration: 200,
        onComplete: function(){ 
                item.setStyle('background-image','url(images/range'+im+'Button' + bt + '.jpg)');
                              
                item.fade('in');
        }
    });
    fx.start('opacity',1,0);  
}

function changeImg (item, src) {
    var fx = new Fx.Tween(item,{
        duration: 200,
        onComplete: function(){ 
                item.setProperty('src',src);
                item.fade('in');
        }
    });
    fx.start('opacity',1,0);  
}

function changeBgGeneric(item,img_normal,img_selected){
    var form = item.id+'Form';			
  					
							
	if($(form).value==1){var im	= img_normal;var v=0;}else{ var im = img_selected;var v=1;}
							
	$(form).value = v;  	
  	
    var fx = new Fx.Tween(item,{
        duration: 200,
        onComplete: function(){ 
                item.setStyle('background-image','url('+im+')');
                              
                item.fade('in');
        }
    });
    fx.start('opacity',1,0);  
}



/******RANGE; NETWORK,*****************************/

/**
* display an item with fade in
* item	{String} name of the items to be displayed  
* max 	{Int} cars total to be displaied
* speed	{Int} speed of the fadeIn
* x		{Int} incremental to update the next car
* @return nothing
*/


function displayItem(item, max, speed, x) {
    if (max != 0) {
		var n = item+''+x;
		$(n).set('tween', {duration: speed, transition: 'back:in'}).tween('opacity', 0, 1);
			
		if(x==max){
			clearTimeout('displayItemTimeOut');
			
		} else {
			x++;
			displayItemTimeOut		=	setTimeout('displayItem(\''+item+'\','+max+','+speed+','+x+')',200);
		}
	}
}	





/**

* item 	{String} name of the div to change background
* d = duration
* USAGE :  press

*/

function pressDetails(item,item2,d,s){

   		
   		/* Text */
   		var fx = new Fx.Tween($(item),{duration: d,
    	    
    	    onComplete: function(){ 
    	         $(item).style.display='none';
    	         $(item2).style.display='inline';
    	         var fx2 = new Fx.Tween($(item2),{duration: d}).start('opacity',0,1);  
    	            
    	    }
    	    
    	}).start('opacity',1,0);
    	
    	    
    	/* Buttons */
    	if(s != 1) { // 1 = start on Domready
    	
   			 if(item == 'e_details') {
   			 		
   			 		pressButton('pressButtonLeft','pressButtonBgGrey',d);	// Button Left becomes Grey
   			 		pressButton('pressButtonRight','pressButtonBgBlue',d);	// Button Right becomes Blue
   			 		    
   			 } else {
   			 		
   			 		pressButton('pressButtonLeft','pressButtonBgBlue',d);	// Button Left becomes Blue
   			 		pressButton('pressButtonRight','pressButtonBgGrey',d);	// Button Right becomes Grey
   			 				 
   			 }
    	
    	}   			
   			
           
}		
		
		/**

		* b	{String} name of the div to change background
		* d = duration
		* USAGE :  function pressDetails()

		*/

		function pressButton(b,img,d){
			
			var fx3 = new Fx.Tween($(b),{
        	
        	duration: d,
        	
        	onComplete: function(){ 
        	        
        	        $(b).setStyle('background-image','url(images/cars/'+img+'.jpg)');
        	        $(b).fade('in');
        	        
        	}
        	
    		});
    		
    		fx3.start('opacity',1,0);

		}


