var rotates;
var rotate_idx = -1;
var rotate_max = -1;
var can_rotate = false;
function startRotate() {
    rotates = $("#bigbox-img-container img");
    rotate_max = rotates.length;
    if(rotate_max > 1) {
        can_rotate = true;
    } else if(rotate_max == 1) {
		$(rotates[0]).fadeIn(50);
	}
    if(can_rotate) {
        doRotate(-1);
    }
}

function doAutoRotate() {
    doRotate(-1);
}

function doRotate(to_idx) {
    if(!can_rotate) return;
    if(rot_time)clearTimeout(rot_time);
    rot_time = null;
    if(rotate_idx != -1 && rotates[rotate_idx]) {
        $(rotates[rotate_idx]).fadeOut(200);
    }
    if(to_idx != -1) {
        rotate_idx = to_idx % rotate_max;
    } else {
        rotate_idx = (++rotate_idx) % rotate_max;
    }
    $(rotates[rotate_idx]).fadeIn(1500);
    rot_time = setTimeout(doAutoRotate, 8000);
}
var rot_time;
$(document).ready(function() {
    window.setTimeout(startRotate, 10);
});

