function jpgMovieFactory( pName, pClass, pWidth, pHeight )
{
	var str = "jm_" +pName +" = new " +pClass +"( '" +pName +"', " +pWidth +", " +pHeight +" );";
	eval( str );

	str = "var ret = jm_" +pName;
	eval( str );

	return ret;
}

function jpgMovie()
{
}

jpgMovie.prototype.name = "";
jpgMovie.prototype.player = null;
jpgMovie.prototype.interval = 100;
jpgMovie.prototype.timer = null;
jpgMovie.prototype.cFrames = 0;
jpgMovie.prototype.iFrame = (-1);
jpgMovie.prototype.frames = new Array();
jpgMovie.prototype.targets = new Array();		// hyperlink options for each frame
jpgMovie.prototype.loaded = new Array();
jpgMovie.prototype.cLoaded = 0;
jpgMovie.prototype.pcntLoaded = 0;
jpgMovie.prototype.incrementer = 1;
jpgMovie.prototype.defaultTarget = "http://www.jpgMovie.com";

jpgMovie.prototype.setPlayer = function( player ) {
	this.player = player;
	this.name = player.name + '.movie';
}
jpgMovie.prototype.setInterval = function( interval ) {
	this.interval = interval;
}
jpgMovie.prototype.setDefaultTarget = function( target ) {
	this.defaultTarget = target;
}


jpgMovie.prototype.addFrameWithTarget = function( urlImage, urlTarget ) {

	var i = this.cFrames;

	this.loaded[i] = 0;
	this.frames[i] = new Image();
	this.targets[i] = urlTarget;

	var cant_use_keyword_this = this;
	var strMakeSureReal_i = "this.frames[i].onload = function() { cant_use_keyword_this._frameLoaded( cant_use_keyword_this, " +i +" ); };";
	eval(strMakeSureReal_i);

	this.cFrames++;
	this.incrementer = parseInt( 100 / this.cFrames );

	this.frames[i].src = urlImage;
}

jpgMovie.prototype.addFrame = function( urlImage ) {
	this.addFrameWithTarget( urlImage, this.defaultTarget );
}

jpgMovie.prototype.load = function() {
	// download movie file list from the web
	// and make calls to addFrame()
}

jpgMovie.prototype._frameLoaded = function( objRef, i ) {
	objRef.loaded[i] = 1;
	objRef.cLoaded++;
	objRef.pcntLoaded += objRef.incrementer;

	if ( objRef.cLoaded == objRef.cFrames ) {
		objRef.pcntLoaded = 100;
	}

	objRef.player.bufferedStatusCB( objRef.pcntLoaded );

	if ( objRef.pcntLoaded >= 100 ) {
		objRef.player.movieLoadedCB();
	}
}

jpgMovie.prototype.start = function() {
	this.iFrame = (-1);
	this.showNextFrame( this );
	return false;
}

jpgMovie.prototype.play = function() {
	if ( this.iFrame >= (this.cFrames - 1 )) {
		this.iFrame = (-1);
	}
	this.showNextFrame( this );
	return false;
}

jpgMovie.prototype.pause = function() {
	if ( this.timer != null ) {
		clearTimeout( this.timer );
	}
	return false;
}

jpgMovie.prototype.showNextFrame = function( objRef ) {
	objRef.iFrame++;
	if ( objRef.iFrame < objRef.cFrames )
	{
		objRef._showFrame();

		var func_ptr = function() { objRef.showNextFrame( objRef ); };
		objRef.timer = setTimeout( func_ptr, objRef.interval );
	}
	else
	{
		objRef.player.playFinishedCB();
	}
}

jpgMovie.prototype._showFrame = function() {
	if ( this.loaded[this.iFrame] == 1 ) {
		if ( this.player ) {
			this.player.showFrame(  this.frames[this.iFrame].src,
							this.targets[this.iFrame] );
		}
	}

}

jpgMovie.prototype.backFrame = function() {
	this.iFrame--;
	if ( this.iFrame < 0 ) {
		this.iFrame = this.cFrames - 1;
	}
	this._showFrame();
}

jpgMovie.prototype.forwardFrame = function() {
	this.iFrame++;
	if ( this.iFrame >= this.cFrames ) {
		this.iFrame = 0;
	}
	this._showFrame();
}


function jpgMoviePlayer( divName, width, height )
{
	this.setDivName( divName );
	this.setImageWidth( width );
	this.setImageHeight( height );
}
jpgMoviePlayer.prototype.divName;
jpgMoviePlayer.prototype.name;
jpgMoviePlayer.prototype.img;
jpgMoviePlayer.prototype.width;
jpgMoviePlayer.prototype.height;
jpgMoviePlayer.prototype.defaultTarget = "http://www.jpgMovie.com";
jpgMoviePlayer.prototype.interval = 100;
jpgMoviePlayer.prototype.bLoaded = false;
jpgMoviePlayer.prototype.bAutoPlay = false;
jpgMoviePlayer.prototype.bLooping = false;
jpgMoviePlayer.prototype.movie = null;
jpgMoviePlayer.prototype.divMovieScreen = null;
jpgMoviePlayer.prototype.divBuffering = null;
jpgMoviePlayer.prototype.divPlayButton = null;

jpgMoviePlayer.prototype.setDivName = function( divName ) {
	this.divName = divName;
	this.name = divName + '.player';
}
jpgMoviePlayer.prototype.setInitialImage = function( img ) {
	this.img = img;
}
jpgMoviePlayer.prototype.setImageWidth = function( width ) {
	this.width = width;
}
jpgMoviePlayer.prototype.setImageHeight = function( height ) {
	this.height = height;
}
jpgMoviePlayer.prototype.setAutoPlayOn = function() {
	this.bAutoPlay = true;
}
jpgMoviePlayer.prototype.setAutoPlayOff = function() {
	this.bAutoPlay = false;
}
jpgMoviePlayer.prototype.setAutoPlay = function( boolean_val ) {
	if ( boolean_val ) {
		this.setAutoPlayOn();
	} else {
		this.setAutoPlayOff();
	}
}
jpgMoviePlayer.prototype.setLoopingOn = function() {
	this.bLooping = true;
}
jpgMoviePlayer.prototype.setLoopingOff = function() {
	this.bLooping = false;
}
jpgMoviePlayer.prototype.setLooping = function( boolean_val ) {
	if ( boolean_val ) {
		this.setLoopingOn();
	} else {
		this.setLoopingOff();
	}
}

jpgMoviePlayer.prototype.setTarget = function( target ) {
	this.defaultTarget = target;
	if ( this.movie != null ) {
		this.movie.setDefaultTarget( Target );
	}
}

jpgMoviePlayer.prototype.setInterval = function( milliseconds ) {
	this.interval = milliseconds;
	if ( this.movie != null ) {
		this.movie.setInterval( milliseconds );
	}
}

jpgMoviePlayer.prototype.draw = function() {
	var html = '';
	html += '<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=2>\n';
	html += '<TR>\n';
	html += '<TD ALIGN=center>\n';
	html += '<img src="' +this.img +'" width="' +this.width +'" height="' +this.height +'" ID="' +this.divName +'_screen">';
	html += '</TD>\n';
	html += '<TR>\n';
	html += '<DIV ID="' +this.divName +'_play_button"></DIV>\n';
	html += '</TD>\n';
	html += '</TR>\n';
	html += '</TABLE>\n';

	var div = document.getElementById( this.divName );
	if ( div ) {
		div.innerHTML = html;
	} else {
		alert('jpgMoviePlayerNoBorder.draw: no such div :' +this.divName +':');
	}
}

jpgMoviePlayer.prototype.addFrame = function( url ) {
	if ( this.movie == null ) {
		this._createMovie( url, '' );
	}
	this.movie.addFrame( url );
}

jpgMoviePlayer.prototype.addFrameWithTarget = function( url, target ) {
	if ( this.movie == null ) {
		this._createMovie( url, target );
	}
	this.movie.addFrameWithTarget( url, target );
}

jpgMoviePlayer.prototype._createMovie = function( url, target ) {
	this.movie = new jpgMovie();
	this.movie.setInterval( this.interval );
	if ( target == '' ) {
		this.movie.setDefaultTarget( this.defaultTarget );
	}
	this.movie.setPlayer( this );
}

jpgMoviePlayer.prototype.load = function() {
	this.movie = new jpgMovie();
	this.movie.setPlayer( this );
	this.movie.load();
}

jpgMoviePlayer.prototype.showFrame = function( src, target ) {
	if ( this.movieScreen == null ) {
		this.movieScreen = document.getElementById( this.divName + '_screen' );
	}
	if ( this.frameTarget == null ) {
		this.frameTarget = document.getElementById( this.divName + '_target' );
	}
	if ( this.movieScreen != null ) {
		this.movieScreen.src = src;
	}
	if ( this.frameTarget != null ) {
		this.frameTarget.href = target;
	}
}

jpgMoviePlayer.prototype.bufferedStatusCB = function( pcnt ) {
	if ( this.divBuffering == null ) {
		this.divBuffering = document.getElementById( this.divName +'_buffering');
	}
	if ( this.divBuffering != null ) {
		this.divBuffering.innerHTML = 'Buffering: ' + pcnt + ' %';
	}
}

jpgMoviePlayer.prototype.movieLoadedCB = function() {
	this.bLoaded = true;
	this.clearBufferingStatus();
	if ( this.bAutoPlay ) {
		this.play();
	} else {
		this.showPlayButton();
	}
}

jpgMoviePlayer.prototype.playFinishedCB = function() {
	if ( this.bLooping ) {
		this.play();
	} else {
		this.showPlayButton();
	}
}

jpgMoviePlayer.prototype.play = function() {
	this.movie.play();
	this.showPauseButton();
	return false;
}

jpgMoviePlayer.prototype.pause = function() {
	this.movie.pause();
	this.showPlayButton();
	return false;
}

jpgMoviePlayer.prototype.backFrame = function() {
	this.movie.backFrame();
	return false;
}

jpgMoviePlayer.prototype.forwardFrame = function() {
	this.movie.forwardFrame();
	return false;
}

jpgMoviePlayer.prototype.clearBufferingStatus = function() {
	if ( this.divBuffering == null ) {
		this.divBuffering = document.getElementById( this.divName +'_buffering');
	}
	if ( this.divBuffering != null ) {
		this.divBuffering.innerHTML = '';
	}
}

jpgMoviePlayer.prototype.showPauseButton = function() {
	if ( this.divPlayButton == null ) {
		this.divPlayButton = document.getElementById( this.divName +'_play_button');
	}
	if ( this.divPlayButton != null ) {
		if ( this.pause_button != null ) {
			this.divPlayButton.innerHTML = '<A HREF="#" onClick="return jm_' +this.divName +'.pause()"><IMG SRC="' +this.pause_button.src +'" BORDER=0></A>';
		} else {
			this.divPlayButton.innerHTML = '<A HREF="#" onClick="return jm_' +this.divName +'.pause()">PAUSE</A>';
		}
	}
}

jpgMoviePlayer.prototype.showPlayButton = function() {
	if ( this.divPlayButton == null ) {
		this.divPlayButton = document.getElementById( this.divName +'_play_button');
	}
	if ( this.divPlayButton != null ) {
		if ( this.play_button != null ) {
			this.divPlayButton.innerHTML = '<A HREF="#" onClick="return jm_' +this.divName +'.play()"><IMG SRC="' +this.play_button.src +'" BORDER=0></A>';
		} else {
			this.divPlayButton.innerHTML = '<A HREF="#" onClick="return jm_' +this.divName +'.play()">PLAY</A>';
		}
	}
}
function jpgMovieSlideShowPlayer( divName, width, height )
{
	this.setDivName( divName );
	this.setImageWidth( width );
	this.setImageHeight( height );
	this.setAutoPlayOn();
	this.setLoopingOn();
	this.setInterval( 175 );
}
jpgMovieSlideShowPlayer.prototype = new jpgMoviePlayer();

jpgMovieSlideShowPlayer.prototype.draw = function() {

	this.play_button = new Image();
	this.play_button.src = "http://www.JpgMovie.com/images/player/slideshow/play.gif";
	this.pause_button = new Image();
	this.pause_button.src = "http://www.JpgMovie.com/images/player/slideshow/pause.gif";
	this.back_button = new Image();
	this.back_button.src = "http://www.JpgMovie.com/images/player/slideshow/back.gif";
	this.forward_button = new Image();
	this.forward_button.src = "http://www.JpgMovie.com/images/player/slideshow/forward.gif";

	var style_top_left = "background-image: url('http://www.JpgMovie.com/images/player/slideshow/top_left.jpg'); width: 36px; height: 35px;";
	var style_top_middle = "background-image: url('http://www.JpgMovie.com/images/player/slideshow/top_middle.jpg'); background-repeat: repeat-x; height: 35px; text-align: center; font-family: arial;";
	var style_top_right = "background-image: url('http://www.JpgMovie.com/images/player/slideshow/top_right.jpg'); width: 36px; height: 35px;";
	var style_mid_left = "background-image: url('http://www.JpgMovie.com/images/player/slideshow/middle_left.jpg'); background-repeat: repeat-y; width: 36px;";
	var style_mid_middle = "text-align: center;";
	var style_mid_right = "background-image: url('http://www.JpgMovie.com/images/player/slideshow/middle_right.jpg'); background-repeat: repeat-y; width: 36px;";
	var style_bot_left = "background-image: url('http://www.JpgMovie.com/images/player/slideshow/bottom_left.jpg'); width: 36px; height: 48px;";
	var style_bot_middle = "background-image: url('http://www.JpgMovie.com/images/player/slideshow/bottom_middle.jpg'); background-repeat: repeat-x; height: 48px;";
	var style_bot_right = "background-image: url('http://www.JpgMovie.com/images/player/slideshow/bottom_right.jpg'); width: 36px; height: 48px;";

	var html = '';
	html += '<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0>\n';
	html += '<TR>\n';
	html += ' <TD STYLE="' +style_top_left +'"></TD>\n';
	html += ' <TD STYLE="' +style_top_middle +'"><A HREF="http://www.jpgMovie.com"><IMG SRC="http://www.JpgMovie.com/images/player/slideshow/title.jpg" WIDTH=100 HEIGHT=16 BORDER=0></A></TD>\n';
	html += ' <TD STYLE="' +style_top_right +'"></TD>\n';
	html += '</TR>\n';
	html += '<TR>\n';
	html += ' <TD STYLE="' +style_mid_left +'"></TD>\n';
	html += ' <TD STYLE="' +style_mid_middle+'"><A ID="' +this.divName +'_target" HREF="' +this.defaultTarget +'"><img src="' +this.img +'" width="' +this.width +'" height="' +this.height +'" ID="' +this.divName +'_screen" BORDER=0></A></TD>\n';
	html += ' <TD STYLE="' +style_mid_right +'"></TD>\n';
	html += '</TR>\n';
	html += '<TR>\n';
	html += ' <TD STYLE="' +style_bot_left +'"></TD>\n';
	html += ' <TD STYLE="' +style_bot_middle +'" ALIGN=center>\n';
	html += '  <TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0>\n';
	html += '   <TR><TD>';
	html += '    <DIV ID="' +this.divName +'_play_button"></DIV>';
	html += '   </TD></TR>\n';
	html += '   <TR><TD>';
	html += '    <DIV ID="' +this.divName +'_buffering">0 %</DIV>';
	html += '   </TD></TR>\n';
	html += '  </TABLE>\n';
	html += ' </TD>\n';
	html += ' <TD STYLE="' +style_bot_right +'"></TD>\n';
	html += '</TR>\n';
	html += '</TABLE>\n';

	var div = document.getElementById( this.divName );
	if ( div ) {
		div.innerHTML = html;
	} else {
		alert('jpgMoviePlayer.draw: no such div :' +this.divName +':');
	}
}

jpgMoviePlayer.prototype.showPauseButton = function() {
	if ( this.divPlayButton == null ) {
		this.divPlayButton = document.getElementById( this.divName +'_play_button');
	}
	if ( this.divPlayButton != null ) {
		if ( this.pause_button != null ) {
			this.divPlayButton.innerHTML = '<A HREF="#" onClick="return jm_' +this.divName +'.pause()"><IMG SRC="' +this.pause_button.src +'" BORDER=0></A>';
		} else {
			this.divPlayButton.innerHTML = '<A HREF="#" onClick="return jm_' +this.divName +'.pause()">PAUSE</A>';
		}
	}
}

jpgMoviePlayer.prototype.showPlayButton = function() {
	if ( this.divPlayButton == null ) {
		this.divPlayButton = document.getElementById( this.divName +'_play_button');
	}
	if ( this.divPlayButton != null ) {
		var html = '';
		if ( this.back_button != null ) {
			html += '<A HREF="#" onClick="return jm_' +this.divName +'.backFrame()"><IMG SRC="' +this.back_button.src +'" BORDER=0></A>';
		}
		if ( this.play_button != null ) {
			html += '<A HREF="#" onClick="return jm_' +this.divName +'.play()"><IMG SRC="' +this.play_button.src +'" BORDER=0 HSPACE=10></A>';
		} else {
			html += '<A HREF="#" onClick="return jm_' +this.divName +'.play()">PLAY</A>';
		}
		if ( this.forward_button != null ) {
			html += '<A HREF="#" onClick="return jm_' +this.divName +'.forwardFrame()"><IMG SRC="' +this.forward_button.src +'" BORDER=0></A>';
		}
		this.divPlayButton.innerHTML = html;
	}
}

function jpgMovieLightBlueSlideShowPlayer( divName, width, height )
{
	this.setDivName( divName );
	this.setImageWidth( width );
	this.setImageHeight( height );
	this.setAutoPlayOn();
	this.setLoopingOn();
	this.setInterval( 175 );
}
jpgMovieLightBlueSlideShowPlayer.prototype = new jpgMoviePlayer();

jpgMovieLightBlueSlideShowPlayer.prototype.draw = function() {

	this.play_button = new Image();
	this.play_button.src = "http://www.JpgMovie.com/images/player/slideshow/lightblue/play.gif";
	this.pause_button = new Image();
	this.pause_button.src = "http://www.JpgMovie.com/images/player/slideshow/lightblue/pause.gif";
	this.back_button = new Image();
	this.back_button.src = "http://www.JpgMovie.com/images/player/slideshow/lightblue/back.gif";
	this.forward_button = new Image();
	this.forward_button.src = "http://www.JpgMovie.com/images/player/slideshow/lightblue/forward.gif";

	var style_top_left = "background-image: url('http://www.JpgMovie.com/images/player/slideshow/lightblue/top_left.jpg'); width: 36px; height: 35px;";
	var style_top_middle = "background-image: url('http://www.JpgMovie.com/images/player/slideshow/lightblue/top_middle.jpg'); background-repeat: repeat-x; height: 35px; text-align: center; font-family: arial;";
	var style_top_right = "background-image: url('http://www.JpgMovie.com/images/player/slideshow/lightblue/top_right.jpg'); width: 36px; height: 35px;";
	var style_mid_left = "background-image: url('http://www.JpgMovie.com/images/player/slideshow/lightblue/middle_left.jpg'); background-repeat: repeat-y; width: 36px;";
	var style_mid_middle = "text-align: center;";
	var style_mid_right = "background-image: url('http://www.JpgMovie.com/images/player/slideshow/lightblue/middle_right.jpg'); background-repeat: repeat-y; width: 36px;";
	var style_bot_left = "background-image: url('http://www.JpgMovie.com/images/player/slideshow/lightblue/bottom_left.jpg'); width: 36px; height: 48px;";
	var style_bot_middle = "background-image: url('http://www.JpgMovie.com/images/player/slideshow/lightblue/bottom_middle.jpg'); background-repeat: repeat-x; height: 48px;";
	var style_bot_right = "background-image: url('http://www.JpgMovie.com/images/player/slideshow/lightblue/bottom_right.jpg'); width: 36px; height: 48px;";

	var html = '';
	html += '<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0>\n';
	html += '<TR>\n';
	html += ' <TD STYLE="' +style_top_left +'"></TD>\n';
	html += ' <TD STYLE="' +style_top_middle +'"><A HREF="http://www.jpgMovie.com"><IMG SRC="http://www.JpgMovie.com/images/player/slideshow/lightblue/title.jpg" WIDTH=100 HEIGHT=16 BORDER=0></A></TD>\n';
	html += ' <TD STYLE="' +style_top_right +'"></TD>\n';
	html += '</TR>\n';
	html += '<TR>\n';
	html += ' <TD STYLE="' +style_mid_left +'"></TD>\n';
	html += ' <TD STYLE="' +style_mid_middle+'"><A ID="' +this.divName +'_target" HREF="' +this.defaultTarget +'"><img src="' +this.img +'" width="' +this.width +'" height="' +this.height +'" ID="' +this.divName +'_screen" BORDER=0></A></TD>\n';
	html += ' <TD STYLE="' +style_mid_right +'"></TD>\n';
	html += '</TR>\n';
	html += '<TR>\n';
	html += ' <TD STYLE="' +style_bot_left +'"></TD>\n';
	html += ' <TD STYLE="' +style_bot_middle +'" ALIGN=center>\n';
	html += '  <TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0>\n';
	html += '   <TR><TD>';
	html += '    <DIV ID="' +this.divName +'_play_button"></DIV>';
	html += '   </TD></TR>\n';
	html += '   <TR><TD>';
	html += '    <DIV ID="' +this.divName +'_buffering">0 %</DIV>';
	html += '   </TD></TR>\n';
	html += '  </TABLE>\n';
	html += ' </TD>\n';
	html += ' <TD STYLE="' +style_bot_right +'"></TD>\n';
	html += '</TR>\n';
	html += '</TABLE>\n';

	var div = document.getElementById( this.divName );
	if ( div ) {
		div.innerHTML = html;
	} else {
		alert('jpgMoviePlayer.draw: no such div :' +this.divName +':');
	}
}


