// JavaScript Document

current_car = 0;
total_cars = 4;
start_checking =0;
speed=6000;
var carimages = new Array();
var timer;
//for (x=1; x<total_cars; x++){


//}

function startcar() {
	
	
	var timer2 = setTimeout('preload();',1500);
	timer = setInterval('nextcar(-1)',speed);
	
	
}


function preload() { 
preloading('',
		   '/images/homepage/car/car_1.jpg',
				 '/images/homepage/car/car_2.jpg',
				 '/images/homepage/car/car_3.jpg');
							
}


 
function preloading(){


for (x=1; x<preloading.arguments.length; x++){
carimages[x] = new Image();
carimages[x].src = preloading.arguments[x];
}
start_checking = 1;
}



function nextcar(jumptono) { 

if (! start_checking) { return; }
for (x=1; x<total_cars; x++){ 
if ((carimages[x].complete) && (carimages[x].width > 0)) { document.getElementById('car_'+x+'_img').src = '/images/homepage/car/car_'+x+'.jpg'; } else { return; }
}

if (jumptono > -1) {
var nextelcount = jumptono;

fadeout('car_'+current_car,1,100,0);
fadein('car_'+nextelcount,1,0,100);
current_car = nextelcount;

}
else
{
var nextelcount = current_car + 1;
if (nextelcount >= total_cars) { nextelcount = 0; }

fadeout('car_'+current_car,1500,100,0);
fadein('car_'+nextelcount,700,0,100);
current_car = nextelcount;

}




}

function jumpto(no) {
	clearTimeout(timer);
	nextcar(no);
}

function jumpdone(no) {
timer = setTimeout(restart,1000);
	
}

function restart() {
	
timer = setInterval('nextcar(-1)',speed);
	
}

function changeOpacity(el,opacity) {
  var image = document.getElementById(el);
  // For Mozilla
  image.style.MozOpacity = (opacity / 100);
  // For IE
 // console.log(opacity);
//document.getElementById('temp').innerHTML = opacity;
 
  image.style.filter = "alpha(opacity=" + opacity + ")";
  // For others
  image.style.opacity = (opacity / 100);
}


function fadein(el,milli,start,end) {
  var fadeTime = Math.round(milli/100);
		
  var i = 0;  // Fade Timer
  // Fade in
    for(j = start; j <= end; j++) {
      // define the expression to be called in setTimeout()
      var expr = "changeOpacity('" + el + "'," + j + ")";
      var timeout = i * fadeTime;
      // setTimeout will call 'expr' after 'timeout' milliseconds
      fin = setTimeout(expr,timeout);
      i++;
	}
}
	
	
function fadeout(el,milli,start,end) {
  var fadeTime = Math.round(milli/100);
		
  var i = 0;  // Fade Timer
     for(j = start; j >= end; j--) {
      var expr = "changeOpacity('" + el + "'," + j + ")";
      var timeout = i * fadeTime;
      fout = setTimeout(expr,timeout);
      i++;
    }
  
}

/* toggle() checks to see if the images has already been faded
   or not and sends the appropriate variables to opacity(); */
function toggle(el,milli) {
  // Get the opacity style parameter from the image
  var currOpacity = document.getElementById(el).style.opacity;
  if(currOpacity != 0) { // if not faded
    fade(el, milli, 100, 0);
  } else { // else the images is already faded
    fade(el, milli, 0, 100);
  }
} 

