//javascript opacity functions
/***************************************************************************************************************/

function rotate_image(divid, imageid,  image_array_name, image_array_index, rotate_flag,  millisec_switch_time, millisec_fade_time){
	var index;
	var image_array = eval(image_array_name);
	if (image_array.length>1)															// do this only if there is more than one picture
		if (rotate_flag) 																// rotating: get the next one
			index = (image_array_index +1) % image_array.length;						
		else																			// get a random pic, different from the one before
			do
				index = randN(0, image_array.length-1)
			while (index == image_array_index)
	image_array_index = index;
	
	// randomize this times 
	var fadetime = One_var(millisec_fade_time , 20);									// vary it 20% up or down
	var switch_time = One_var(millisec_switch_time , 20);		

	blendimage(divid, imageid,  image_array[image_array_index], fadetime);
	
	setTimeout("rotate_image('" + 	
									divid 				+ "','" + 
									imageid 			+ "','" + 
									
									image_array_name 	+ "'," + 
									 
									image_array_index 	+ "," + 
									rotate_flag 		+ "," +  
									millisec_switch_time 	+ "," + 
									millisec_fade_time 	+
							")", 	switch_time		);
}
/***************************************************************************************************************************/
function One_var(quantity, variance){					// returns a number between quantity - variance% and quantity + varance %
	if(variance<0) variance = -variance; 				// abs value it
	var factor = (100+ randN(-variance, variance))/100
	return (  Math.round(quantity * factor));
}
/***************************************************************************************************************************/
function randN(n, m){  // return an integer between n and minclusive
  return ( n + Math.floor ( Math.random ( ) * (m -n +1)  ) );
}
/**************************************************original********************************************************
// this original function has a couple of problesm var i was not delcared, therefore caused problesm
// also it is based on dividing the time into 100 parts no matter how short the millisec period is.
// to improve I have implemented a constant time step, say 100msec and do the work based on that.
function blendimage(divid, imageid, imagefile, millisec) {
    var speed = Math.round(millisec / 100);
    var i, 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 blendimage(divid, imageid, imagefile, millisec) {
	var timeStep = 50;										// milli sedons Time steps, in milli seconds // assume millsec = 2000 timestep is 50
	var NoOfSteps = Math.ceil(millisec / timeStep);			// number of stemps needed	40 
	var opacInc = Math.ceil(100/NoOfSteps)					// each time the opacity increases  2.5 opacity increment

    document.getElementById(divid).style.backgroundImage = "url(" + document.getElementById(imageid).src + ")";		 //set the current image as background
    changeOpac(0, imageid);					//make the front image transparent, the background image which is the same image, still shows
    document.getElementById(imageid).src = imagefile;    //put the name image in (front) image, since it is transparent, it does not show yet
 	var opac =0;
	var futureTime =0;
	while (opac<100){
		opac += opacInc;					// new opacity
		futureTime += timeStep			// new time;
		setTimeout("changeOpac(" + opac + ",'" + imageid + "')", futureTime);
	}
} 
/************************************************************************shift opacity*********************************/
function shiftOpacity(id, millisec) {
    //if an element is invisible, make it visible, else make it ivisible
    if(document.getElementById(id).style.opacity == 0) {
        opacity(id, 0, 100, millisec);
    } else {
        opacity(id, 100, 0, millisec);
    }
} 
/************************************************************************changeOpac(opacity, id)*********************************/
function changeOpac(opacity, id) {					//change the opacity for different browsers
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
}
/************************************************************************opacity*********************************/
function opacity(id, opacStart, opacEnd, millisec) {
    var timeStep = 50;										// Time steps, in milli seconds 
	var NoOfSteps = Math.round(millisec / timeStep);		// number of stemps needed
	var opacInc = Math.round((opacEnd -opacStart)/NoOfSteps)
    var i, opac= opacStart;
	changeOpac(opacStart, id)
	for (i=0; i<=NoOfSteps && opac!=opacEnd; i++){
		opac += opacInc;
		if ( (opacStart > opacEnd && opac < opacEnd) || (opacStart < opacEnd && opac > opacEnd) )
			opac=opacEnd;
		timeOuts["changeOpac"] = setTimeout("changeOpac(" + opac + ",'" + id + "')",(timeStep * i));
	}
}
/******************************************************************************************************************************************/