// requires BrowserDetect
function swapImage_fade(id,path) {

	var image = findObj(id);
	if ( image == null ) return;

	if ( BrowserDetect.browser == 'Explorer' ) {
		image.style.filter = "blendTrans(duration=1)";
		image.filters.blendTrans.Apply();
	}
//	set_opacity(id,0.01);
		// in Firefox, the following line prevents an easy way to make an image fade in, by flashing a 100% opaque image regardless of attempts to mask it
	if( !image.oSrc ) image.oSrc = image.src; // 'old source.' necessary?
		image.src = path;
//		fade(image,1,99);
	if ( BrowserDetect.browser == 'Explorer' ) image.filters.blendTrans.Play();
}

// opacities given as 1-99(%)
function fade(img,op_start,op_end) {
	var d = 1;
	if ( op_end < op_start ) d = -1;
	for ( i = op_start; i != op_end; i += d ) {
		setTimeout( "set_opacity('"+img+"',"+(i/100)+")", i*5 );
	}
}

// opacities given as 0-100(%)
function crossfade(outid,inid) {
	time = 750;
	dt = 33;
	frames = time/dt;
	dop = 100/frames;
	frame = 0;
	for ( op = 0; op <= 100; op+=dop ) {
		setTimeout( "set_opacity('"+outid+"',"+((100-op)/100)+")", frame*dt );
		setTimeout( "set_opacity('"+inid+"',"+(op/100)+")", frame*dt );
		setTimeout( "set_opacity('"+outid+"',"+0+")", time ); // necessary to ensure completion IMPORTANT
		setTimeout( "set_opacity('"+inid+"',"+100+")", time ); // necessary to ensure completion
		++frame;
	}
}

function set_opacity(id,setval) {
	obj = findObj(id);
	obj.style.opacity = setval;
	obj.style.KhtmlOpacity = setval;
	obj.style.filter = 'alpha(opacity=' + (setval*100) + ')'; // MSIE
}

var current_url = unescape(window.location.pathname);
function refresher(time) { setTimeout("refresh()",time); }
function refresh() { window.location.href = current_url; }

// image 'preloading' is accomplished by forcing the browser to reference all of the image urls at pageload.
function preload_random_images() {
	for ( var i = 0; i < random_images.length; ++i ) {
		var dummy = new Image;
		dummy.src = random_images[i][0];
	}
}

function random_slideshow() {
	preload_random_images();
	unhide_random_image();
	// set up a timer
	var timer = setInterval( swap_images_crossfade, 10000 );
}

function unhide_random_image() {
	index = Math.floor(Math.random()*random_images.length);
	imgid = 'photo_'+index;
	set_opacity(imgid,100);
	set_info(index);
}

function swap_images_crossfade() {
	curid = '';
	// look up old photo by opacity
	for ( i = 0; i < random_images.length; ++i ) {
		curid = 'photo_'+i;
		img = findObj(curid);
		if ( img.style.opacity != 0 ) break;
	}
	if ( curid == '' ) return;
	newid = curid;
	index = 0;
	while ( newid == curid ) {
		index = Math.floor(Math.random()*random_images.length);
		newid = 'photo_'+index;
	}
	crossfade(curid,newid);
	set_info(index);
}

function set_info(index) {
	var fields = random_images[index];
	var credit = '&nbsp;', caption = '&nbsp;';
	if ( fields[2] != '' ) {
		credit = '<a class="photocredit" href="'+fields[2]+'">photo: '+fields[1]+'</a>';
	} else if ( fields[1] != '' ) {
		credit = 'photo: '+fields[1];
	}
	if ( fields[4] != '' ) {
		caption = '<a class="photocredit" href="'+fields[4]+'">'+fields[3]+'</a>';
	} else if ( fields[3] != '' ) {
		caption = fields[3];
	}
	caption_obj = findObj("caption");
	caption_obj.innerHTML = caption+'<br>'+credit;
}

function warn_ie(target_id) {
	if ( BrowserDetect.browser == 'Explorer' ) {
		obj = findObj(target_id);
		if ( obj != null ) {
			obj.innerHTML = "note: this page is not optimized for the Microsoft browser(s)";
		}
	}
}
