BackgroundMovie.prototype.RootName = "BackgroundMovie";
BackgroundMovie.prototype.s = null;

function BackgroundMovie(s)
{
    this.s = s;
    this.plugin = s.getHost();
	this.MainCanvas = this.s.findname("MainCanvas");
    this.DebugBox = this.s.findname("DebugBox");
    //this.xamlObject = this.s.findname(this.RootName);
    //alert("1");
    
    this.CreateAssets();
}

BackgroundMovie.prototype.FrameCount = 32;
BackgroundMovie.prototype.Frames = new Object();

BackgroundMovie.prototype.ZoomFrameCount = 15;
BackgroundMovie.prototype.GameRoomZoomFrames = new Object();
BackgroundMovie.prototype.LivingRoomZoomFrames = new Object();
BackgroundMovie.prototype.HomeOfficeZoomFrames = new Object();

BackgroundMovie.prototype.GetIndexFromFraction = function(frac)
{
    // Get the indices of the surrounding frames.
    var i = Math.floor(frac * this.FrameCount);
    
    /*
    if (i > this.FrameCount - 1)
    {
        while (i > this.FrameCount - 1)
        {
            i = i - this.FrameCount;
        }
    }
    else if (i < 0)
    {
        while (i < 0)
        {
            i += this.FrameCount;
        }
    }
    */
    return i;
}

BackgroundMovie.prototype.GetInterpolatedImage = function(frac)
{
    // Get the indices of the surrounding frames.
    var prevImageIndex = this.GetIndexFromFraction(frac);
    
    var prevImage = this.GetImage(prevImageIndex);
        
    return prevImage;
}

BackgroundMovie.prototype.CreateAssets = function()
{
    var suffix =  generalSettings["deploy360Extension"];//".jpg";
    
    for (var i = 0; i < this.FrameCount; i++)
    {
        var imgNum = '' + (i + 1);
        if (imgNum.length < 2)
            imgNum = '0' + imgNum;
            
        //var path = "360frames/00" + imgNum + ".png";
        var path = "360frames2/360_00" + imgNum + suffix;        
        this.Frames[i] = path;
    }
    
    for (var i = 0; i < this.ZoomFrameCount; i++)
    {
        var imgNum = '' + (i + 1);
        if (imgNum.length < 2)
            imgNum = '0' + imgNum;
            
        //var path = "360frames/00" + imgNum + ".png";
        var path = "360frames2/BR_00" + imgNum + suffix;        
        this.GameRoomZoomFrames[i] = path;
    }
    for (var i = 0; i < this.ZoomFrameCount; i++)
    {
        var imgNum = '' + (i + 1);
        if (imgNum.length < 2)
            imgNum = '0' + imgNum;
            
        //var path = "360frames/00" + imgNum + ".png";
        var path = "360frames2/LR_00" + imgNum + suffix;        
        this.LivingRoomZoomFrames[i] = path;
    }    
    for (var i = 0; i < this.ZoomFrameCount; i++)
    {
        var imgNum = '' + (i + 1);
        if (imgNum.length < 2)
            imgNum = '0' + imgNum;
            
        //var path = "360frames/00" + imgNum + ".png";
        var path = "360frames2/Office_00" + imgNum + suffix;        
        this.HomeOfficeZoomFrames[i] = path;
    }    
}


BackgroundMovie.prototype.SetInterpolatedImage = function(frac)
{
    var index = this.GetIndexFromFraction(frac);
    this.CurrentFrame = index;
    var path = backgroundMovie.GetImage(index);
    
    //dd("i = " + index);
    if (path != null)
    {
        mainWindow.SetBackground(path);
    }
}

BackgroundMovie.prototype.GetImage = function(index)
{
    if (index < 0)
        return this.Frames[0];
    
    if (index >= this.FrameCount)
        return this.Frames[0];
        
    return this.Frames[index];
}

BackgroundMovie.prototype.GetRoomZoomImage = function(roomName, index)
{
    var roomArray = null;
    if (roomName == "HomeOffice") roomArray = this.HomeOfficeZoomFrames;
    else if (roomName == "LivingRoom") roomArray = this.LivingRoomZoomFrames;
    else if (roomName == "GameRoom") roomArray = this.GameRoomZoomFrames;
    
    if (index < 0)
        return roomArray[0];
    
    if (index >= this.ZoomFrameCount)
        return roomArray[this.ZoomFrameCount - 1];
        
    return roomArray[index];
}

BackgroundMovie.prototype.CurrentFrame = 0;

BackgroundMovie.prototype.BackgroundFraction = 0;

BackgroundMovie.prototype.Update = function()
{
    if (isNaN(this.currentValue)) return;
    
    backgroundMovie.BackgroundFraction = this.currentValue;
    var index = Math.floor(backgroundMovie.BackgroundFraction);//backgroundMovie.GetIndexFromFraction(backgroundMovie.BackgroundFraction);
    backgroundMovie.CurrentFrame = index;
    
    var path = backgroundMovie.GetImage(index);
    
    if (path != null)
    {
        mainWindow.SetBackground(path);
    }
    
    var ff = index / backgroundMovie.FrameCount;
    //dd("this.currentValue = " + this.currentValue + "   index = " + index + "    fc = " + backgroundMovie.FrameCount);
    slider.SetThumbPosition(ff);
    
    mainWindow.RepositionHUD(ff);
    //Debug("this.currentValue = " + this.currentValue + "           index = " + index + "            path = " + path);
}

BackgroundMovie.prototype.SetBackgroundIndexAsFloat = function(indexAsFloat)
{
    //dd("indexAsFloat = " + indexAsFloat + "     cf = " + backgroundMovie.CurrentFrame);
    
    backgroundMovie.BackgroundFraction = indexAsFloat;
    var index = Math.floor(backgroundMovie.BackgroundFraction);//backgroundMovie.GetIndexFromFraction(backgroundMovie.BackgroundFraction);
    backgroundMovie.CurrentFrame = index;
    var path = backgroundMovie.GetImage(index);
    
    if (path != null)
    {
        mainWindow.SetBackground(path);
    }
    
    var ff = index / backgroundMovie.FrameCount;
    //dd("this.currentValue = " + this.currentValue + "   index = " + index + "    fc = " + backgroundMovie.FrameCount);
    slider.SetThumbPosition(ff);
    
    mainWindow.RepositionHUD(ff);
    //Debug("this.currentValue = " + this.currentValue + "           index = " + index + "            path = " + path);
}

BackgroundMovie.prototype.AnimateToFrame = function(index, dur)
{ 
    var rotateAnim = new Animation("rotateAnim", 
	    backgroundMovie, "BackgroundFraction", backgroundMovie.CurrentFrame, index, dur);
	rotateAnim.OnFractionScript = this.Update;
	//rotateAnim.Play();
	
	return rotateAnim;	
}

BackgroundMovie.prototype.BackgroundRoomZoomTarget = null;
BackgroundMovie.prototype.BackgroundRoomZoomFrame = -1;

BackgroundMovie.prototype.GetRoomZoomAnim = function(roomName, startFrame, endFrame, dur)
{ 
    this.BackgroundRoomZoomTarget = roomName;
    
    var rotateAnim = new Animation("roomZoomAnim", 
	    backgroundMovie, "BackgroundFraction", startFrame, endFrame, dur);
	rotateAnim.OnFractionScript = this.UpdateRoomZoom;//function(anim) {dd("2 " + anim.currentValue); };//this.UpdateRoomZoom;
	//rotateAnim.Play();
	
	return rotateAnim;	
}

BackgroundMovie.prototype.UpdateRoomZoom = function(anim)
{
    //dd("BackgroundRoomZoomTarget = " + this["roomName"]);
    //backgroundMovie.BackgroundFraction = this.currentValue;
    var index = Math.floor(anim.currentValue);//backgroundMovie.GetIndexFromFraction(backgroundMovie.BackgroundFraction);
    
    backgroundMovie.BackgroundRoomZoomFrame = index;
    
    var target = backgroundMovie.BackgroundRoomZoomTarget;
    if (anim["roomName"] != null)
        target = anim["roomName"];
        
    var path = backgroundMovie.GetRoomZoomImage(target, index);
    
    //alert("zoom: " + backgroundMovie.BackgroundRoomZoomTarget + " path = " + path);
    mainWindow.SetBackground(path);
}


BackgroundMovie.prototype.UpdateZoom = function()
{
    //Debug("zoom " + this.currentValue);
    mainWindow.SetBackgroundZoom(this.currentValue);
}

BackgroundMovie.prototype.GetZoomAnim = function(startVal, endVal, dur)
{
    var zoomAnim = new Animation("zoomAnim",
        backgroundMovie, "ZoomFactor", startVal, endVal, dur);
    zoomAnim.OnFractionScript = this.UpdateZoom;
    
    return zoomAnim;
}

BackgroundMovie.prototype.GetFadeAnim = function(theName, startVal, endVal, theDur)
{
    var fadeAnim = new Animation("fadeAnim",
        MainCanvas.findname(theName), "Opacity", startVal, endVal, theDur);
        
    return fadeAnim;
}