addOnload(setupImageMarquis);
// addOnload(test_pickUnique);

/*
	is there a good way to load this from a file or generate it somehow using webgen?
	For the moment, we can edit the list of files. 
	
	What about showing the large ones?
*/
var marquis_image_count = 47


/*
	creates a lambda list of load handlers...
*/
function addOnload( newf ) {
	var old_onload = window.onload;
	if( typeof old_onload == "function"  ) {
		window.onload = function( ) {
			if( old_onload ) {
				old_onload( );
			}
			newf( );
		}
	} else {
		window.onload = newf;
	}
}

function test_pickUnique( ) {
	debugLog( "test_pickUnique");
	var tracker = new Object( );
	debugLog( "pick1: " + pickUnique( tracker , 3 ));
	debugLog( "pick1: " + pickUnique( tracker , 3 ));
	debugLog( "pick1: " + pickUnique( tracker , 3 ));
	debugLog( "pick1: " + pickUnique( tracker , 3 ));
	debugLog( "pick1: " + pickUnique( tracker , 3 ));
	debugLog( "pick1: " + pickUnique( tracker , 3 ));
}


/*
	makes sure that we have unique images on
	each of marquis. 

*/
function pickUnique( tracker , len  ) {
	if( ! tracker.hasOwnProperty("call_count")) tracker.call_count  = 0;
	if( tracker.call_count >= len ) return 0;	
	debugLog( "pickUnique: " + tracker.call_count);
	
	var i = 0;
	var s = "";
		
	do { 
		i = Math.floor((Math.random( ) * len));
		s = String(i);
	} while( tracker[s] == true ); 
	tracker[s] = true;
	tracker.call_count += 1;
	return i;
}


/*
	JavaScript to make interactive image marquis
	search through the document and add the requisite code to 
	
	- make sure that no two image banners end up on the same image 
*/
function setupImageMarquis( ) {
	// get all image tags that are in the right class
	debugLog( "called setupImageMarquis");
	var all_images = document.getElementsByTagName("img");  
	var tracker = new Object( );
	var i = 0;
	
	for( i = 0 ; i < all_images.length ; i++ ) {
		if( all_images[i].className == "rotating_marquis" ) {
			debugLog( "found a node of the appropriate class..." );
			debugLog( "image name is: " + all_images[i].src );
		
			var pu = pickUnique(tracker, marquis_image_count );
			// debugLog( "pu: " + pu );
			all_images[i].imgCount = pu;
			// debugLog( "makeThumbnailString: " + makeThumbnailString(pu,all_images[i].src));
			all_images[i].src = makeThumbnailString(pu,all_images[i].src);
			debugLog( "makeFullString: " + makeFullString(pu,all_images[i].src));
			all_images[i].parentNode.href = makeFullString(pu,all_images[i].src);
			// debugLog( "all_images 2" );
			setInterval( create_TimedPictureChange(all_images[i]) , 1000 * 5);
		}
	}
    debugLog( "finished setupImageMarquis");
}


/*
	given an image specified as an integer, create the thumbnail name
	- now handles the case where we are not rooted in the base directory
*/
function makeThumbnailString( i, base ) {
  return base.replace( /[0-9]+\.jpg/ , "" ) + i + ".jpg";
}


/*
	given a path (as a string) to an image, create the matching full-sized version.
	TODO: fix this function - need to make it independent of the base.

*/
function makeFullString( i, base ) {
  return base.replace( /marquis_thumbnails\/[0-9]+\.jpg/ , "")
    + "marquis_images/" + i + ".jpg";
}



/*
  JavaScript for timer event...
  called on a regular timer.  Can we have timer events on arbitrary nodes?
  Must understand how to implement timing nodes
*/
function create_TimedPictureChange( imgNode ) {

	return (function( ) {	
		// obtain the picture id from the this
		debugLog( "handle a timer event on: "  + imgNode );
		imgNode.imgCount += 1;
		if( imgNode.imgCount == marquis_image_count ) {
			imgNode.imgCount = 0;
		}
		imgNode.src = makeThumbnailString(imgNode.imgCount, imgNode.src);
		imgNode.parentNode.href = makeFullString( imgNode.imgCount, imgNode.src);
	});
	
}


/*
	A debugging printf equivalent. Log to a <pre> element with id "log" like so:
	
	<pre id="debugging_log">
	&nbsp;
	</pre>

	To not get the debugging output, simply remove the <pre> tag and then
	there will be no debugging output written. (Or so I currently belive)
*/
function debugLog( a_string ) {
	var in_page_log = document.getElementById( "debugging_log" );
	if( in_page_log != null ) {
		in_page_log.innerHTML += "\n" + a_string;
	}

}

