// JavaScript Document - random photo selector
// var num_slots = 3;					// # photos, set by Template
// var pics_path = "images/s_pics/";	// set by Template, typ. "images/"
var current_slot = 0;
var max_pic_number = 31;
var time_interval = 3;
var slot_names = new Array("NOP", "sm_A", "sm_B", "sm_C", "sm_D", "sm_E");
var current_picnums = new Array("vw","wx","xy","yz","zx","zz");		// 2-char strings

function start_rotation() {	// ============= kick off the rotation every time_interval seconds
	numerate_pictures();
	setInterval("rotate()", time_interval*2200);
}

function numerate_pictures(){  // ======== initiallizes current_picnums[] so as not to repeat
	var pic_name, pic_num;
	for (i = 1; i<=num_slots; i++){
		pic_name = document.images[slot_names[i]].src;
		pic_num = pic_name.substr(pic_name.length-6,2);
		current_picnums[i] = pic_num;
	}
}

function rotate() {		// ========================= replace current picture with new one
	var newpic;
	var newpic_name;
	var still_looking = 1;

	if (current_slot++ == num_slots ) {		// increment current picnum
		current_slot = 1;					// that is, march from left to right
	}
looking:
	do {
		newpic = choose_pic();
		for (i=1; i<= num_slots; i++ ){		// reject picnum if already in use on this page
			if( newpic == current_picnums[i] || '0'+newpic == current_picnums[i]) continue looking;
		}
		// loop only terminates IFF newpic is unique
		still_looking = 0; 		// stop looking
		current_picnums[current_slot] = newpic;			// store new pic's number
		
		if (newpic < 10) {  	// construct image name of <path>/DD.jpg file
			newpic_name = pics_path + "0" + newpic + ".jpg"		// make new name, 01~09
		} else {
			newpic_name = pics_path + newpic + ".jpg";			// make new name, 10+
		}
		//	alert("Setting img["+current_slot+"] to "+newpic);
		document.images[slot_names[current_slot]].src = newpic_name;	// changed image
	
	} while (still_looking);	
}

function choose_pic(){  // ============================= Returns random picture number
	var randnum = Math.floor( Math.random()*max_pic_number+1 );
	return randnum;
}
	
	
	
var popWindow;
function popUp(url) {
  if( !popWindow || popWindow.closed){
    // open new window
	popWindow=window.open(url,"myPopupWindow",'toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,resizable=1,width=780,height=350,left=100,top=150')
 } else {
    // load new page into window
    popWindow.location = url ;
    // bring existing window to front
    popWindow.focus();
  } 
}

