//
// You shouldn't need to modify this file.
// See the demo web page to see how to use this.
//
var photo_scroller = {
	interval: 1000,
	baseHref: "http://www.tuplin.com/images/",
	timer: null,
	cFrames: 0,
	iFrame: 0,
	frames: new Array(),
	loaded: new Array(),
	target: null,

	setInterval: function( interval )
	{
		this.interval = interval;
	},

	setBaseHref: function( baseHref )
	{
		this.baseHref = baseHref;
		if ( this.baseHref.lastIndexOf('/') != (this.baseHref.length-1) ) {
			this.baseHref += '/';
		}
	},

	setup: function( id )
	{
		this.target = document.getElementById( id );
		if ( this.target == null ) {
			alert( 'photo_scroller cannot find the <IMG> tag with an ID="' +id +'" attribute' );
		}
	},

	add: function( url )
	{
		var i = this.cFrames;

		this.loaded[i] = 0;
		this.frames[i] = new Image();

		photo_scroller.frames[i].onload = function() { photo_scroller.loaded[i] = 1; };

		this.cFrames++;

		if ( url.indexOf("http://") == 0 ) {
			this.frames[i].src = url;
		} else if ( url.indexOf("https://") == 0 ) {
			this.frames[i].src = url;
		} else {
			this.frames[i].src = this.baseHref + url;
		}
	},

	stop: function()
	{
		if ( this.timer != null ) {
			clearTimeout( this.timer );
		}
	},

	scroll: function()
	{
		if ( this.timer != null ) {
			clearTimeout( this.timer );
		}
		this.iFrame = 0;
		this.showNextFrame();
	},

	showNextFrame: function()
	{
		if ( photo_scroller.iFrame >= photo_scroller.cFrames ) {
			photo_scroller.iFrame = 0;
		}

		if ( photo_scroller.loaded[photo_scroller.iFrame] ) {
			photo_scroller.target.src = photo_scroller.frames[photo_scroller.iFrame].src;
		}

		photo_scroller.iFrame++;

		var func_ptr = function() { photo_scroller.showNextFrame(); };
		photo_scroller.timer = setTimeout( func_ptr, photo_scroller.interval );
	}
}

