function changeOpac(opacity, id) 
{ 
	var object = document.getElementById(id).style;
    object.opacity = (opacity / 100); 
    object.MozOpacity = (opacity / 100); 
    object.KhtmlOpacity = (opacity / 100); 
    object.filter = "alpha(opacity=" + opacity + ")"; 
} 

function changeOpacObj(opacity, object)
{
	object.style.opacity = (opacity / 100); 
    object.style.MozOpacity = (opacity / 100); 
    object.style.KhtmlOpacity = (opacity / 100); 
    object.style.filter = "alpha(opacity=" + opacity + ")"; 
}

function pausecomp(millis) 
{
	var date = new Date();
	var curDate = null;

	do { curDate = new Date(); } 
	while(curDate-date < millis);
}
 
function opacitySync(id, opacStart, opacEnd, delay)
{
	//speed for each frame 
    //var speed = Math.round(millisec / 100); 
    //var timer = 0; 

    //determine the direction for the blending, if start and end are the same nothing happens 
    if(opacStart > opacEnd) { 
        for(i = opacStart; i >= opacEnd; i--) { 
            changeOpac(i,id);
            pausecomp(delay);
            //timer++; 
        } 
    } else if(opacStart < opacEnd) { 
        for(i = opacStart; i <= opacEnd; i++) 
            { 
            changeOpac(i,id); 
           	pausecomp(delay);
            //timer++; 
        } 
    }
}

function opacity(id, opacStart, opacEnd, millisec) 
{ 
	//speed for each frame 
    var speed = Math.round(millisec / 100); 
    var timer = 0; 

    //determine the direction for the blending, if start and end are the same nothing happens 
    if(opacStart > opacEnd) { 
        for(i = opacStart; i >= opacEnd; i--) { 
            setTimeout("changeOpac(" + i + ",'" + id +"')",(timer * speed)); 
            timer++; 
        } 
    } else if(opacStart < opacEnd) { 
        for(i = opacStart; i <= opacEnd; i++) 
            { 
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 
    } 
} 

function blendimage(divid, imageid, imagefile, millisec) 
{ 
    var speed = Math.round(millisec / 100); 
    var timer = 0; 
     
    //set the current image as background 
    document.getElementById(divid).style.backgroundImage = "url(" + document.getElementById(imageid).src + ")"; 
     
    //make image transparent 
    changeOpac(0, imageid); 
     
    //make new image 
    document.getElementById(imageid).src = imagefile; 

    //fade in image 
    for(i = 0; i <= 100; i++) { 
        setTimeout("changeOpac(" + i + ",'" + imageid + "')",(timer * speed)); 
        timer++; 
    } 
} 

function blendbackground(divid, imageid, imagefile, millisec) 
{ 
    var speed = Math.round(millisec / 100); 
    var timer = 0; 
     
    //set the current image as background 
    document.getElementById(divid).style.backgroundImage = "url(" + document.getElementById(imageid).src + ")"; 
     
    //make image transparent 
    changeOpac(0, imageid); 
     
    //make new image 
    document.getElementById(imageid).style.backgroundImage = "url(" + imagefile + ")"; 

    //fade in image 
    for(i = 0; i <= 100; i++) { 
        setTimeout("changeOpac(" + i + ",'" + imageid + "')",(timer * speed)); 
        timer++; 
    } 
}

function fadeOutImage(imgId,time)
{
	opacity(imgId,100,0,time);	
}

function swapImage(imgId,imgArray)
{
	var img = document.getElementById(imgId);
	var randNum = Math.round(Math.random()*leftImgArray.length);
	var newImg = imgArray[randNum];
	img.src = newImg;
}

function fadeInImage(imgId,time)
{
	opacity(imgId,0,100,time);
}

function imgFadeLoop(time)
{
	var interval = time / 8;
	setTimeout("fadeOutImage('leftCarImage',"+interval+")",0);
	setTimeout("swapImage('leftCarImage',leftImgArray)",interval);
	setTimeout("fadeInImage('leftCarImage',"+interval+")",interval*2);
	setTimeout("fadeOutImage('rightCarImage',"+interval+")",interval*4);
	setTimeout("swapImage('rightCarImage',rightImgArray)",interval*5);
	setTimeout("fadeInImage('rightCarImage',"+interval+")",interval*6);
	
	setTimeout("imgFadeLoop("+time+")",time);
}