var next_img;				// Image object for next image
var next_idx;				// Index of next image
var next_loaded;			// Flag: next image is loaded
var timer_expired;			// Flag: time to switch images
var fade_out;				// Image object being faded out
var fade_in;				// Image object being faded in
var fade_start;				// Time when fade started
var fade_intid;				// Fade interval ID

function rotation_start() {
	// Reset flags.
	timer_expired = false;
	next_loaded = false;

	// Get the cookie to find out which image we're showed most recently.
	var cookies = document.cookie.split(';');
	var lastone;
	for (var i = 0; i < cookies.length; i++) {
	    if (cookies[i].substr(0, 16) == 'schola_rotation=') {
	        lastone = cookies[i].substr(16);
	    }
	    if (cookies[i].substr(0, 17) == ' schola_rotation=') {
	        lastone = cookies[i].substr(17);
	    }
	}
	if (!lastone) {
	    lastone = photolist[1];
	}

	// Find that image in the photo list.
	var lastidx;
	for (lastidx = 0; lastidx < photolist.length; lastidx++) {
	    if (photolist[lastidx] == lastone) break;
	}
	if (lastidx >= photolist.length) {
	    lastone = photolist[1];
	    lastidx = 1;
	}
	
	// Find the next image.
	nextidx = lastidx+1;
	if (nextidx >= photolist.length) nextidx = 0;

	// Prefetch next image.
	next_img = new Image();
	next_img.onload = rotation_loaded;
	next_img.src = photodir + '/' + photolist[nextidx];

	// Set the interval timer.
	setTimeout("rotation_timer_done()", 5000);
}

function rotation_loaded() {
	next_loaded = true;
	rotation_check();
}

function rotation_timer_done() {
	timer_expired = true;
	rotation_check();
}

function rotation_check() {
	// Wait until both the interval timer expires and the prefetch finishes.
	if (!next_loaded || !timer_expired) {
		return;
	}

	// Which side is next?
	var side;
	if (nextidx % 2 == 0) {
		side = 'rotation-img-left';
	} else {
		side = 'rotation-img-right';
	}

	// Find the current image on that side.   It will be faded out.
	var div = document.getElementById('rotation');
	for (var i = 0; i < div.childNodes.length; i++) {
		if (div.childNodes[i].className == side) {
			fade_out = div.childNodes[i];
		}
	}
	fade_out.style.zIndex = 0;

	// Set up the image to be faded in.
	fade_in = next_img;
	fade_in.className = side;
	fade_in.style.zIndex = 100;
	fade_in.style.opacity = 0;
	fade_in.style.filter = 'alpha(opacity=0)';

	// Start the fade.
	fade_start = new Date().getTime();
	div.appendChild(fade_in);
	fade_intid = setInterval("rotation_fade()", 100);

	// Set the cookie.
	document.cookie = 'schola_rotation='+photolist[nextidx];

	// Start the prefetch of the next image.
	rotation_start();
}

function rotation_fade() {
	var div = document.getElementById('rotation');

	// How much time has elapsed since the fade started?
	var delta = new Date().getTime() - fade_start;
	var percent = Math.floor(delta / 20);

	if (percent >= 100) {
		// The fade is done.  Stop the interval timer, remove the old
		// image, and set the new image fully visible.
		clearInterval(fade_intid);
		div.removeChild(fade_out);
		fade_out = null;
		fade_in.style.opacity = 1;
		fade_in.style.filter = '';
	} else {
		// Update the opacity of the new image to fade it in.
		fade_in.style.opacity = percent / 100;
		fade_in.style.filter = 'alpha(opacity='+percent+')';
	}
}

function editRegistration(anchor)
{
    var row = anchor.parentNode.parentNode;
    var cells = [];
    for (var i = 0; i < row.childNodes.length; i++) {
        if (row.childNodes[i].nodeType == document.ELEMENT_NODE) {
            cells[cells.length] = row.childNodes[i];
        }
    }
    var firstname = trim(cells[0].innerHTML);
    var lastname = trim(cells[1].innerHTML).substr(6); // remove &nbsp;
    var qtyclass = trim(cells[2].innerHTML);
    while (firstname == '') {
        row = row.previousSibling;
        while (row.nodeType != document.ELEMENT_NODE) {
            row = row.previousSibling;
        }
        cells = [];
        for (var i = 0; i < row.childNodes.length; i++) {
            if (row.childNodes[i].nodeType == document.ELEMENT_NODE) {
                cells[cells.length] = row.childNodes[i];
            }
        }
        firstname = trim(cells[0].innerHTML);
        lastname = trim(cells[1].innerHTML).substr(6); // remove &nbsp;
    }
    firstname = firstname.replace('&amp;', '&');
    lastname = lastname.replace('&amp;', '&');
    var idx = qtyclass.indexOf(' ');
    var qty, clazz;
    if (idx < 0) {
        qty = qtyclass;
        clazz = '';
    } else {
        qty = qtyclass.substr(0, idx);
        clazz = trim(qtyclass.substr(idx));
    }
    document.addForm.firstname.value = firstname;
    document.addForm.origfirst.value = firstname;
    document.addForm.lastname.value = lastname;
    document.addForm.origlast.value = lastname;
    document.addForm.qty.value = qty;
    document.addForm.origqty.value = qty;
    for (var i = 0; i < document.addForm.type.options.length; i++) {
        if (document.addForm.type.options[i].value == clazz) {
            document.addForm.type.selectedIndex = i;
            break;
        }
    }
    document.addForm.origtype.value = clazz;
    document.addForm.submit.value = 'Save';
}

function trim(str)
{
    str = str.replace(/^\s+/, '');
    str = str.replace(/\s+$/, '');
    return str;
}

