//
// imageplayer.js
//

//Setup the event
function DomReady(fn)
{
    if(document.addEventListener)
    {	//W3C
        document.addEventListener("DOMContentLoaded", fn, false);
    }
    else
    {	//IE
        document.onreadystatechange = function(){readyState(fn)}
    }
}

//IE execute function
function readyState(fn)
{
    //dom is ready for interaction
    if(document.readyState == "interactive" || document.readyState == "complete")
    {
	fn();
    }
}

//change the opacity for different browsers
function changeOpacity(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
}

function ImagePlayer(images, times, imageid, divid, cacheid, counterid, totalid)
{
    this.images=images;
    this.times=times;
    this.imageid=imageid;
    this.divid=divid;
    this.cacheid=cacheid;
    this.counterid=counterid;
    this.totalid=totalid;
    this.current=0;
    this.iterOpacityTimeoutId=null;
    this.imageSuivanteTimeoutId=null;
}

ImagePlayer.prototype.Display=function(num)
{
    //Mettre en background, la 1ere image
    this.current=num;
    document.getElementById(this.divid).style.backgroundImage = 'url('+this.images[this.current]+')';
    document.getElementById(this.imageid).src=this.images[this.current];
    document.getElementById(this.counterid).innerHTML=this.current+1;
}

ImagePlayer.prototype.InitDiapo=function()
{
    //Mettre en background, la 1ere image
    this.Display(this.images.length-1);
    document.getElementById(this.totalid).innerHTML=this.images.length;
    this.ImageSuivante(1000, +1);
}

ImagePlayer.prototype.iterOpacity=function(step, millisec, started)
{
    this.iterOpacityTimeoutId=null;

    var currentTime=new Date();
    currentTime=currentTime.getTime();
    if (started==0) { started=currentTime; }

    var opacity=Math.min(Math.round(100*(currentTime-started)/millisec), 100);
    if (step<0) { opacity=100-opacity; }

    changeOpacity(opacity, this.imageid);
    if (0<=opacity && opacity<100) {
	thisObj=this;
	var myfunc=function() { thisObj.iterOpacity(step, millisec, started); };
	this.iterOpacityTimeoutId=setTimeout(myfunc, Math.round(millisec/100));
    }
}

ImagePlayer.prototype.Blendimage=function(imagenum, millisec, step)
{
    //set the current image as background
    document.getElementById(this.divid).style.backgroundImage = "url(" + document.getElementById(this.imageid).src + ")";
    num=(imagenum+1)%this.images.length;
    document.getElementById(this.counterid).innerHTML=imagenum+1;
    //make new image
    document.getElementById(this.imageid).src=this.images[imagenum];
    // start fading image
    this.iterOpacity(step, millisec, 0);
}

ImagePlayer.prototype.ImageSuivante=function(millisec, step)
{
    this.imageSuivanteTimeoutId=null;
    var next=(this.current+step)%this.images.length;
    var next2=(next+1)%this.images.length;

    this.Blendimage(next,millisec,step);
    // Mettre la suivante en cache
    document.getElementById(this.cacheid).innerHTML='<img src="' + this.images[next2] + '" width="200" />';

    //Compte a rebours pour la suivante
    var thisObj=this;
    var myfunc=function() { thisObj.ImageSuivante(millisec, step); };
    this.imageSuivanteTimeoutId=setTimeout(myfunc, this.times[next]);
    this.current=next;
}

ImagePlayer.prototype.Stop=function()
{
    this.Pause();
    this.Display(0);
}

ImagePlayer.prototype.Play=function(){
    if (this.imageSuivanteTimeoutId==null) {
	this.ImageSuivante(1000, +1);
    }
}

ImagePlayer.prototype.Pause=function()
{
    if (this.imageSuivanteTimeoutId!=null) {
	clearTimeout(this.imageSuivanteTimeoutId);
	this.imageSuivanteTimeoutId=null;
    }
}

ImagePlayer.prototype.Next=function(){
    if (this.imageSuivanteTimeoutId!=null) {
	clearTimeout(this.imageSuivanteTimeoutId);
	this.imageSuivanteTimeoutId=null;
    }
    this.Display((this.current+1)%this.images.length);
}

ImagePlayer.prototype.Prev=function(){
    if (this.imageSuivanteTimeoutId!=null) {
	clearTimeout(this.imageSuivanteTimeoutId);
	this.imageSuivanteTimeoutId=null;
    }
    this.Display((this.current-1+this.images.length)%this.images.length);
}

