var FadeInit = false;
var FadeDelay = 500.0;

//Credit: http://www.switchonthecode.com/
function TextFade(EID, FadeEn)
{
  var Element;  
  //Initialize the fade state on every element              
  if(!FadeInit)
  {
    //Initialize fade state of all elements
    for(var Scan = 0; Scan <= 6; Scan++)
    {
      Element = document.getElementById('Text'+Scan);
      
      if(Element.style.opacity == '1')
      { Element.FadeState = 2; } //Fully visible
      else  
      { Element.FadeState = -2; } //Fully faded
    }
    FadeInit = true;
  }
       
  if(FadeEn)
  {                    
    //Fade in the current element
    FadeElement('Text'+EID,1);
    
    //Fade out all other elements
    for(var Scan = 0; Scan <= 6; Scan++)
    {  
      if(Scan == EID) //Skip yourself
      { continue; }  
      FadeElement('Text'+Scan,-1);
    }
  }
  else
  {
    //Fade in the default element 
    FadeElement('Text0',1);    
    
    //Fade out the current element 
    FadeElement('Text'+EID,-1);
  }
}

function FadeElement(EID,FadeEn)
{  
  var Animate = false;            
  var Element = document.getElementById(EID);
  if(Element.FadeState == -1*FadeEn)
  { Element.FadeTime = FadeDelay - Element.FadeTime; } //Fade for what's left
  else if (Element.FadeState == -2*FadeEn) 
  { 
    Element.FadeTime = FadeDelay; //Fade for the entire duration
    Animate = true;
  } 
  if(Element.FadeState != 2*FadeEn)
  { Element.FadeState = 1*FadeEn; } //Fade only if it isn't done yet
  if(Animate)
  { setTimeout("AnimateFade("+new Date().getTime()+",'"+EID+"')", 50); }
} 

function AnimateFade(PreTime, EID)
{  
  var NowTime = new Date().getTime();
  var ElapseTime = NowTime - PreTime;
 
  var Element = document.getElementById(EID);
  if(Element.FadeTime <= ElapseTime)
  {
    if(Element.FadeState == 1)
    {
      Element.style.opacity = 1;
      Element.style.filter = 'alpha(opacity = 100)'; 
      Element.FadeState = 2;
    }
    else
    {   
      Element.style.opacity = 0;
      Element.style.filter = 'alpha(opacity = 0)';     
      Element.FadeState = -2;
    }
    return;
  }
 
  Element.FadeTime -= ElapseTime;
  var FadeValue = Element.FadeTime/FadeDelay;
  if(Element.FadeState == 1)
  { FadeValue = 1 - FadeValue; }

  Element.style.opacity = FadeValue;
  Element.style.filter = 'alpha(opacity = ' + (FadeValue*100) + ')';
 
  setTimeout("AnimateFade("+NowTime+",'"+EID+"')", 50);
}

function SetChange(SID)
{ 
  JavaHover();
  
  var Position;
  
  if((0 <= SID) && (SID <= 6))
  { Position = "0px " + ((-20*SID)-20) + "px"; }
  else
  { Position = "0px 20px"; }
  
  document.getElementById('TitleBanner').style.backgroundPosition = Position;
  document.getElementById('Button' + SID).style.backgroundImage = "url('/Layout/Images/Button" + SID + "B.png')"; 
}

function JavaHover()
{ 
  var ElementArr = document.getElementById("Nav").getElementsByTagName("Li");
  
  for(var iTemp = 0; iTemp < ElementArr.length; iTemp++)
  {
    ElementArr[iTemp].onmouseout = function()
    { this.className = this.className.replace(new RegExp("JavaHover"), ""); }
    ElementArr[iTemp].onmouseover = function()
    { this.className += "JavaHover"; }
  }
}
      
function HomeOver()
{ document.getElementById('HomeDS').style.top = "0px"; }
  
function HomeOut()
{ document.getElementById('HomeDS').style.top = "-250px"; }
     
function InsertString(Text)
{
  document.CommentForm.Comment.value += " " + Text;
  document.CommentForm.Comment.focus();
  document.CommentForm.Comment.scrollTop = document.CommentForm.Comment.scrollHeight;
}

function ButtonHover()
{ document.CommentForm.Button.style.backgroundPosition = "0px -24px"; }

function ButtonOut()
{ document.CommentForm.Button.style.backgroundPosition = "0px 0px"; }

function ButtonClick()
{ document.CommentForm.Button.style.backgroundPosition = "0px -48px"; }
      
