// JavaScript Document
var delay; // how long to wait between transistions
var intID;

function urhere() {
	// in the sitenav, find the current page's link, disable it and give visual indication 
	// i.e., find parent li and add id="urhere"
	for (var i=0; i<document.links.length; i++ ) {
		if (document.links[i] == window.location.href) { 
			//console.log (document.links[i]);
			document.links[i].href = "javascript:void(0);"; 
			document.links[i].parentNode.id = "urhere";
			//console.log (document.links[i].parentNode.id);
			break;
		} 
	} 
}

// open new window for copyright notice
function openWindow() {
	window.open('copyright.html','notices','height=300,width=400')
}

function initThumbnails() {
// add onClick navigation behavior to image thumbnails, speed options
	$("#thumb_imgs img").click(function() {showImg(this.id);} );
	$("#setdelay input").click(function() { changeSpeed(); } );
}

function playShow()	{
	console.log ("playShow: intID = " + intID);
	if (intID) { 
		intID = clearInterval(intID); } 
	//	to re-start after changing the play speed
	if (!delay) { delay=$("input:radio[name=playspeed]:checked").val(); }
	intID = setInterval("switchSrc()", delay);		
}

/*
auto-play: switchSrc() => nextImg()
user selects: showImg() => nextImg()
*/

function switchSrc() {
	// for auto-transition: fade the current feature, call nextImg to fade in the successor
	var newSrc;
	var f = $("#featured");
	var fi = $("#featured_img");	
	var i = Number(fi.attr("seq")); // the current img
	var j = i+1;
	if (j == portfolio.length) { j = 0; }
	newSrc = collection + "/" + portfolio[j].filename + ".jpg";			
	if (newSrc) {
		f.animate({ opacity: '-=1' }, 700, function() { 
			nextImg(newSrc,i,j);  });
	}
}
		
function nextImg(newSrc, i, j)	{
	// called by switchSrc() for auto-transition; or by showImg() after clicking a thumbnail
	// i = current img; j = next img
	// should cache the following image for better performance?
	if (i==j) { //console.log ("clicked current"); 
		return false; // clicked current thumbnail; ignore
	}
	/*		
	console.log ("current: " + i + " " + portfolio[i].filename);
	console.log ("next: " + j + " " + portfolio[j].filename);
	console.log (delay +  " msecs");
	console.log ($("input:radio[name=playspeed]:checked").val() + " radio value");
	*/
	var f = $("#featured");
	var fi = $("#featured_img");	
	var ti = $("#thisimage");
	if (Number(ti.attr("seq")) != i) { 	
	// user clicked a thumbnail while previous was fading; 
	// interrupt transition in progress; re-set to selected image, no fade-in
		j = Number(ti.attr("seq"));	
		f.css("opacity", 1);
	}
	else {
		ti.attr("id", portfolio[i].filename);
		document.getElementById(portfolio[j].filename).id = "thisimage"; 
		fi.attr( {src:newSrc, seq:j} );
		f.animate({ opacity: '+=1' }, 1000);
	}
	document.getElementById("caption").innerHTML = portfolio[j].displayname;
	document.getElementById("counter").innerHTML = (j+1) + " of " + portfolio.length;			
}

function showImg(id) {
	// user clicked thumbnail: determine which, call nextImg to display with (no fade out)
	intID = clearInterval(intID);		// if (intID) {} ?
	var fi = $("#featured_img");	
	var th = $("#"+id);
	var i = Number(fi.attr("seq"));
	var j = Number(th.attr("seq"));
	console.log ("clicked " + j);
	nextImg(collection + "/" + portfolio[j].filename + ".jpg", i, j);
	var t = $("#toggler");
	if (t.attr("src") == "images/pause.png") { 
		intID = setInterval("switchSrc()", delay); } // resume playing
} 

function toggleDisplay() {
	var t = $("#toggler");
	console.log ("clicked " + t.attr("src"));
	console.log ("intID = " + intID);	
	if (t.attr("src") == "images/pause.png") { // clicked "pause"
		t.attr({"src":"images/play.png", "alt":"play slide show", "title":"play slide show"});
		intID = clearInterval(intID);
	}
	else {	// clicked "play"
		t.attr({"src":"images/pause.png", "alt":"pause slide show", "title":"pause slide show"});
		switchSrc();
		intID = setInterval("switchSrc()", delay);
	}
	console.log ("processed: intID = " + intID);
}

function changeSpeed() { 
	delay = $("input:radio[name=playspeed]:checked").val();
	if (intID) { playShow(); }
}
