﻿// JScript File


// function for maintaining scope
Function.prototype.bind = function(obj) { 
    var method = this, 
    temp = function() { 
        return method.apply(obj, arguments); 
        }; 
    return temp; 
} 





/********************************************************
    PHOTOS
 ********************************************************/
function Photos()
{
    this.Collection = new Array();
    this.Container = null;  // containing DIV object
    this.CurrentPhoto = null;
    this.PreviousPhoto = null;
    this.InTransition = false;
    this.CurrentTimer = null;
    this.RotateDurationMin = 6; // seconds
    this.RotateDurationMax = 10; // seconds
    this.FadeInDuration = 50;
    this.FadeOutDuration = 50;
    this.FadeInOpacity = 0;
    this.FadeOutOpacity = 100;
    this.InFadeOut = false;
    this.InFadeIn = false;
}

Photos.prototype.AddPhoto = function(strPath, strHref, intHeight, intWidth)
{
    this.Collection.push(new Photo(strPath, strHref, intHeight, intWidth));
    if (this.Container)
    {
        this.Container.style.height = intHeight;
        this.Container.style.width = intWidth;
    }
};

Photos.prototype.GetRandomPhoto = function ()
{
    var objResult = null;
    var randomnumber = Math.floor(Math.random()*this.Collection.length)
    if (randomnumber >= 0 && randomnumber < this.Collection.length)
        objResult = this.Collection[randomnumber];
        
    if (this.CurrentPhoto != objResult || this.Collection.length == 1)
        return objResult;
    else
        return this.GetRandomPhoto();
};

Photos.prototype.GetRandomTime = function()
{
    var intResult = 0;
    var randomnumber = Math.floor(Math.random()*this.RotateDurationMax)
    while (randomnumber < this.RotateDurationMin || randomnumber > this.RotateDurationMax)
    {
        randomnumber = Math.floor(Math.random()*this.RotateDurationMax);
    }    
    intResult = randomnumber * 1000;
        
    return intResult;
};

Photos.prototype.SetOpacity = function(val, obj)
{
    if (obj)
    {
        if (val >= 0)
        {
            obj.Image.style.opacity = val/100;
            obj.Image.style.filter = "alpha(opacity=" + val + ")";
        }
        else
        {
            obj.Image.style.opacity = 0;
            obj.Image.style.filter = "alpha(opacity=0)";
        }
    }

};

Photos.prototype.Rotate = function ()
{
    if (!this.InFadeOut && !this.InFadeIn)
    {
        if (this.CurrentPhoto != this.PreviousPhoto)
        {
            this.PreviousPhoto = this.CurrentPhoto;
            this.FadeOut();
        }
        
        if (!this.InFadeOut)
        {    
            var objPhoto = this.GetRandomPhoto();
            if (objPhoto)
            {
                this.CurrentPhoto = objPhoto;
                
                // if this is the first, don't fade in
                if (this.PreviousPhoto)
                {
                    this.CurrentPhoto.Image.style.filter = "alpha(opacity=0)";
                    this.CurrentPhoto.Image.style.opacity = 0;
                }
                else
                {
                    this.CurrentPhoto.Image.style.filter = "alpha(opacity=100)";
                    this.CurrentPhoto.Image.style.opacity = 100;
                }
                this.Container.appendChild(this.CurrentPhoto.Image);
            }
                
            // if this is the first, don't fade in
            if (this.PreviousPhoto)
            {
                this.FadeIn();
            }
            setTimeout(BindMethodPhotosRotate.bind(this), this.GetRandomTime());
        }
        else
            setTimeout(BindMethodPhotosRotate.bind(this), 100);
    }
    else
        setTimeout(BindMethodPhotosRotate.bind(this), 100);
    
};



Photos.prototype.FadeIn = function()
{
    if (!this.InFadeOut)
    {
        this.InTransition = true;
        this.InFadeIn = true;
        this.FadeInOpacity = this.FadeInOpacity+3;
        this.SetOpacity(this.FadeInOpacity, this.CurrentPhoto);

        if (this.FadeInOpacity >= 100)
        {
            this.FadeInOpacity = 0;
            this.InTransition = false;
            this.InFadeIn = false;
        }
        else
            this.CurrentTimer = setTimeout(BindMethodPhotosFadeIn.bind(this), this.FadeInDuration);
    }
    //try again later
    else
        this.CurrentTimer = setTimeout(BindMethodPhotosFadeIn.bind(this), this.FadeInDuration);
};

Photos.prototype.FadeOut = function()
{
    if (!this.InFadeIn)
    {
        this.InTransition = true;
        this.InFadeOut = true;
        this.FadeOutOpacity = this.FadeOutOpacity-3;
        this.SetOpacity(this.FadeOutOpacity, this.PreviousPhoto);

        if (this.FadeOutOpacity <= 0)
        {
            if (this.PreviousPhoto)
                this.Container.removeChild(this.PreviousPhoto.Image);
            this.FadeOutOpacity = 100;
            this.InTransition = false;
            this.InFadeOut = false;
            
        }
        else
            this.CurrentTimer = setTimeout(BindMethodPhotosFadeOut.bind(this), this.FadeOutDuration);
    }
    //try again later
    else
        this.CurrentTimer = setTimeout(BindMethodPhotosFadeOut.bind(this), this.FadeOutDuration);

};

function Photo(strPath, strHref, intHeight, intWidth)
{
    this.Image = document.createElement("IMG");
    this.Image.src = strPath;
    this.Image.height = intHeight;
    this.Image.width = intWidth;
    
    this.Path = strPath;
    this.Href = strHref;
    this.Height= intHeight;
    this.Width = intWidth;
}


function BindMethodPhotosRotate()
{
    this.Rotate();
}

function BindMethodPhotosFadeIn()
{
    this.FadeIn();
}

function BindMethodPhotosFadeOut()
{
    this.FadeOut();
}




