﻿var MaxOpacity = 1;
var MinOpacity = 0.4;
var ChangeTotalTime = 1500; //was 2000
var ChangePeriod = 10;

var DisplayTime = 3000; //was 4000
var HideTime = 0;

var Times = [];

var FileNames = new Array();
FileNames[0] = "images/bg1_02a.jpg";
FileNames[1] = "images/bg1_02b.jpg";
FileNames[2] = "images/bg1_02c.jpg";
FileNames[3] = "images/bg1_02d.jpg";
FileNames[4] = "images/bg1_02e.jpg";
FileNames[5] = "images/bg1_02f.jpg";
FileNames[6] = "images/bg1_02g.jpg";

function FadeOutImage(OpacityLevel, Tag, FileNum)
{
    try
    {
        var now = new Date();
        var StartTime = Times[Tag];
		var currentNumber
        
        var Gradient = (MaxOpacity - MinOpacity) / ChangeTotalTime;
        var TimePassed = now - StartTime;
        var Value = Gradient * TimePassed;
        Value = (MaxOpacity - MinOpacity) - Value;
        
        OpacityLevel = Value > MinOpacity ? Value : MinOpacity;
        OpacityLevel = roundNumber(OpacityLevel, 2);
        
        document.getElementById(Tag).style.opacity = OpacityLevel;
        document.getElementById(Tag).style.filter = "alpha(opacity=" + (OpacityLevel * 100) + ")";
        document.getElementById(Tag).style.MozOpacity = OpacityLevel;
        
        if(OpacityLevel > MinOpacity)
        {
            setTimeout("FadeOutImage(" + OpacityLevel + ", '" + Tag + "', " + FileNum + ")", ChangePeriod);
        }
        else
        {
			//Modification by Todd, cycle images in order, but only to the size of the image array.
			
			FileNum = FileNum + 1;
			if(FileNum>=6)
			{
				FileNum = 0;
			}
			
			//Modification by Todd create random image number, but don't let it be the same as the last image
			
            // FileNum = Random(FileNames.length);
			/* while(currentNumber==FileNum){
				FileNum = Random(FileNames.length);
			} */
			
			
			 document.getElementById('header').style.backgroundImage = "url("+ FileNames[FileNum] + ")";
            // document.getElementById(Tag).style.backgroundImage = "url("+ FileNames[FileNum] + ")";
			
			 //alert(FileNames[FileNum]);
			// FileNames[FileNum]
            
            var CommandString = "Times['" + Tag + "'] = new Date();";
            setTimeout(CommandString, HideTime);
            setTimeout("FadeInImage(" + OpacityLevel + ", '" + Tag + "', " + FileNum + ")", HideTime);
        }
    }
    catch(e)
    {
    }
}
function FadeInImage(OpacityLevel, Tag, FileNum, StartTime)
{
    try
    {
        var now = new Date();
        var StartTime = Times[Tag];
        
        var Gradient = (MaxOpacity - MinOpacity) / ChangeTotalTime;
        var TimePassed = now - StartTime;
        var Value = Gradient * TimePassed;
        
        OpacityLevel = Value < MaxOpacity ? Value : MaxOpacity;
        OpacityLevel = roundNumber(OpacityLevel, 2);
        
        document.getElementById(Tag).style.opacity = OpacityLevel;
        document.getElementById(Tag).style.filter = "alpha(opacity=" + (OpacityLevel * 100) + ")";
        document.getElementById(Tag).style.MozOpacity = OpacityLevel;
        
        if(OpacityLevel < MaxOpacity)
        {
            setTimeout("FadeInImage(" + OpacityLevel + ", '" + Tag + "', " + FileNum + ")", ChangePeriod);
        }
        else
        {
            var CommandString = "Times['" + Tag + "'] = new Date();";
            setTimeout(CommandString, DisplayTime);
            setTimeout("FadeOutImage(" + OpacityLevel + ", '" + Tag + "', " + FileNum + ")", DisplayTime);
        }
    }
    catch (e)
    {
    }
}
function FadeImage(Tag, Delay, FileNum)
{
    var CommandString = "Times['" + Tag + "'] = new Date();";
    setTimeout(CommandString, Delay);
    setTimeout("FadeOutImage(1.0, '" + Tag.toString() + "', " + FileNum + ")", Delay);
}
function FadeAllImages()
{
    FadeImage('header', 0, 0);
}

function Random(Max)
{
    var Multiplier = 10;
    Multiplier = Max > 10 ? 100 : Multiplier;
    Multiplier = Max > 100 ? 1000 : Multiplier;
    
    var Result = Math.floor((Math.random()*Multiplier));
    return Result < Max ? Result : Random(Max);
}
function roundNumber(num, dec) 
{
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}
