
var aimg = new Array();
var used = new Array(0,0,0);
var tme_checkm = null;
var tme_onld = null;
var tme_fade = null;
var fade_begin = -1;
// the object that holds the images
var id = "imgCycle";
var r_checkm = 0;
var in_ = 0;
var out_ = 1;
var loads = new Image();
loads.src = "includes/load.gif";
loads.src = "includes/imgCycle_.gif";
var fade_op, fade_ob;

// without calling function onld() nothing will begin
onld();

function pl(){
	// Total amount of images to preload
	// NOTE: mx variable should not be less than 4 due to
	// the checking in function more(); to make sure an image selected
	// isn't the same as the previous 3 images; this can of course be modified.
	mx = 6;
	
	// Change the path information, located in variable `p`
	p = "includes/img/";
	
	// file name
	fmt = "mis";
	// file extension
	et = ".jpg";
	
	if ( mx < 4 ) return 0; // make sure mx is not less than 4; if so then quit
	// begin preloading images
	for( i = 1; i <= mx; i++ ){
		aimg[i] = new Image();
		aimg[i].src = p+fmt+i+et;
	}
}

function more(){
	r = r_checkm;
	// grab the object, that holds the image display, by ID
	x = document.getElementById(id);
	while( r==used[0]||r==used[1]||r==used[2] ) r = Math.floor( Math.random()*aimg.length );
	r = (r==0)?1:r;
	//edit image size here
	img = "<div style='text-align:center;'><img src=\""+aimg[r].src+"\" width=\"567\" height=\"276\"></div>";
	src = '<table cellpadding="0" cellspacing="0" align=center><tr><td style="background: #ffffff  no-repeat center;width: 567px;height: 276px;" align=middle valign=center>';
	src += img;
	src += "</td></tr></table>";
	x.innerHTML = src;//img;
	
	// updated the used variable with last 3 images used
	// to prevent repetition
	used[2] = used[1];
	used[1] = used[0];
	used[0] = r;
	r_checkm = r;
}

function onld(){
	// grab the object, that holds the image display, by ID
	x = document.getElementById(id);
	if ( x != null && x != undefined ){
		x.style.textAlign = "center";
		setTimeout("pl();", 500); // preload
		setTimeout("checkm();", 150);
	}
	else{
		clear_Timeout( tme_onld );
		tme_onld = setTimeout( "onld();", 150 );
	}
}

function checkm(){
	// if variable aimg has no images preloaded
	if( aimg.length != 0 ){
		fade(out_, document.getElementById(id));
	}
	else{
		clear_Timeout( tme_checkm );
		// keep checking
		tme_checkm = setTimeout( "checkm();", 350 );
	}
}

function fade(op,ob){
	fade_op = op;
	fade_ob = ob;
	appname = new Array( "Netscape", "Opera" );
	isNotIE = false;
	// amount of time, in ms, to wait between each picture.
	wait = 4300;
	
	for( i = 0; i < appname.length; i++ )
		isNotIE = (navigator.appName).indexOf(appname[i])!=-1 || isNotIE;
	
	incdec = (isNotIE)? ((op)? -.15: .15): ((op)? -9.5: 9.5);
	
	if( fade_begin < 0 ){
		fade_begin = (op)? 100: 1;
		fade_begin = (isNotIE)? fade_begin/100: fade_begin;
	}
	else fade_begin += incdec;
	
	done = (op)? (fade_begin<0.01): ( (isNotIE)? (fade_begin>1): (fade_begin>100) );
	
	if( !done ){
		x = document.getElementById(id)
		if ( !isNotIE ) x.style.filter = "alpha(opacity="+ fade_begin +")";
		else x.style.opacity = fade_begin;
		
		clear_Timeout( tme_fade );
		tme_fade = setTimeout( "fade(fade_op, fade_ob);", 98 );
	}
	else{
		clear_Timeout( tme_fade );
		if ( op ){
			more();
			fade(in_, document.getElementById(id));
		}
		else{
			setTimeout( "fade(out_, document.getElementById(id));", wait );
		}
	}
}

function clear_Timeout(x){
	if ( x != null ){
		clearTimeout( x );
		x = null;
	}
}
