
var AutoTrans = true;
var TimerFlip;
var AutoFlipTimeout = 10; // in seconds
var TransitionTimeout = 200; // in miliseconds

var aryStageItems = new Array();


var ActiveStageItemDiv;


function FirstLoad()
{   
	ActiveStageItemDiv = 'divStage' + 0;
	
	var ActiveIndex = 0;
	
	var div;
	
	
	// ## Here down to the commented section is new.
	
	div = new objController();
	
	var i = 0;
	
	while (div.Load('divStage' + i))
	{
	    if (div.Hide() == true)
	        aryStageItems[i] = '';
	    i++;
	}
	
	/*
	for (i = 0; i < aryStageItems.length; i++)
	{
		div = new objController('divStage' + i);
		div.Hide();
	}
	*/
	
	div = new objController('divStage' + ActiveIndex);
	div.Show('absolute');
	div.FadeIn(TransitionTimeout);

	ResetTimer();
	
	//DrawButtons('light');
}



function ChangeActiveStageItem(divOld, divNew)
{
    //alert(divOld + ' to ' + divNew);
    if (divOld == divNew)
	    return;
	
    var objNewDiv = new objController(divNew);
    objNewDiv.SetOpacity(1);
    objNewDiv.Show('absolute');
    objNewDiv.FadeIn(TransitionTimeout);
	
    var objOldDiv = new objController(divOld);
    objOldDiv.FadeOut(TransitionTimeout);
	//objOldDiv.Hide();
	
	var div;
	for (i = 0; i < aryStageItems.length; i++)
	{
		div = new objController('divStage' + i);
		//alert('Hide ' + i + ' ' + div.Hide());
		div.Hide();
		//alert(i);
	}
	//alert('hi');
	objNewDiv.Show('absolute');
    ActiveStageItemDiv = divNew;
    ResetTimer();
}


function ResetTimer()
{
	clearTimeout(TimerFlip);
	TimerFlip = setTimeout("AutoFlip()", (AutoFlipTimeout * 1000));
}


function Flip()
{
	var cI; // Current Index
	var nI; // New Index
	
	cI = ActiveStageItemDiv.replace('divStage', '');
	nI = parseInt(cI) + 1;
	
	// This allows us to go back to number 1 if we've ran out
	if (cI == (aryStageItems.length - 1))
		nI = 0;
	
	// Sets the selectedIndex of the ddl to our New Index 
	ActiveStageItemDiv = 'divStage' + nI;
	
	ChangeActiveStageItem('divStage' + cI, 'divStage' + nI);
}


function AutoFlip()
{
	if (AutoTrans == true)
	{
		Flip();
	}
	ResetTimer();
}

function setAuto(bln)
{
	AutoTrans = bln;
	if (bln == true)
		ResetTimer();
}

function showDiv(id)
{
	ChangeActiveStageItem(ActiveStageItemDiv, id);
}







/*################################
### Standard Div Functions     ###
################################*/

function isObject(o)
{
    var sofar = true;
    if (typeof(o) != "object")
        sofar = false;
    if (o == null)
        sofar = false;
    if (o == undefined)
        sofar = false;
    
  return sofar;
}


function isNumeric(x)
{
    // I usually use the this function like this: if (isNumeric(myVar)) { } 
    // regular expression that validates a value is numeric
    var RegExp = /^(-)?(\d*)(\.?)(\d*)$/;
    // Note: this WILL allow a number that ends in a decimal: -452.
    // compare the argument to the RegEx
    // the 'match' function returns 0 if the value didn't match
    var result = RegExp.test(x);
    return result;
}

function objController(obj)
{

    this.input = obj;
    
    this.Load = function(obj)
				{
					if (!isObject(obj))
					{
					    //alert(this.input);
						obj = document.getElementById(obj); 
					}
					
					if (!isObject(obj))
						return false;
					
					this.Object = obj;
											
					return true;
				}
	
	this.Hide = function()
				{
					if (!isObject(this.Object))
						return false;
						
				    //alert(isObject(this.Object));
				    //alert(this.input);
					
					//alert(this.Object.id);
					this.Object.style.visibility = 'hidden'; 
					this.Object.style.position = 'absolute'; 
					this.Object.style.display = 'none'; 
					
					return true;
				}
	
	this.Show = function(pos)
				{
					
					if (!isObject(this.Object))
						return false;
					//alert(this.Object.style.left);
					if (pos != 'absolute')
						pos = 'static';
					//alert(this.Object.style.left);
					this.Object.style.visibility = 'visible'; 
					this.Object.style.position = pos; 
					this.Object.style.display = 'inline'; 
					//alert(this.Object.style.left);
					
					return true;
				}
				
	this.GetLeft = function()
                    {
                        var curleft = 0;
                        
				        if (!isObject(this.Object))
					        return curleft;
					        
                        var obj = this.Object;
                        
                        if (obj.offsetParent)
                        {
                            while (obj.offsetParent)
                            {
                                curleft += obj.offsetLeft;
                                obj = obj.offsetParent;
                            }
                        }
                        else if (obj.x)
                            curleft += obj.x;
                    		
                        return curleft;
                    }
	
	this.GetTop = function()
                    {
                        var curtop = 0;
                        
					    if (!isObject(this.Object))
						    return curtop;
                        
                        var obj = this.Object;
                    	
                        if (obj.offsetParent) {
                            while (obj.offsetParent) {
                                curtop += obj.offsetTop
                                obj = obj.offsetParent;
                            }
                        }
                        else if (obj.y)
                            curtop += obj.y;
                    		
                        return curtop;
                    }
                    
                    
    this.OpacityTransition = function(intOpacBegin, intOpacEnd, intMilliseconds)
								{
								    
									// Speed for each frame
									var speed = Math.round(intMilliseconds / 100);
									var timer = 0;
								
									// Determine the direction for the blending, if start and end are the same nothing happens
									if (intOpacBegin > intOpacEnd)
									{
										for(i = intOpacBegin; i >= intOpacEnd; i--)
										{
										    if (isNumeric(i) == true && i >= 0)
    											setTimeout("SetObjectOpacity(" + i + ", '" + this.Object.id + "')",(timer * speed));
											timer++;
										}
									}
									else if (intOpacBegin < intOpacEnd)
									{
										for(i = intOpacBegin; i <= intOpacEnd; i++)
										{
										    if (isNumeric(i) == true && i >= 0)
    											setTimeout("SetObjectOpacity(" + i + ", '" + this.Object.id + "')",(timer * speed));
											timer++;
										}
									}
									
									return true;
									
								}
								
	this.GetOpacity = function()
                        {
                            if (!isObject(this.Object))
                                return 0;
                            
                            var Opac = 0;
                            
                            if (isNumeric(this.Object.style.opacity))
                                Opac = (this.Object.style.opacity * 100);
                                
                            return Opac;
                        }
								
	this.SetOpacity = function(intOpacity)
						{
						    //alert(intOpacity);
						    //intOpacity = intOpacity / 100
						    //alert(this.Object.id + ' to ' + intOpacity);
							SetObjectOpacity(intOpacity, this.Object.id);
							return;
							if (!isObject(this.Object))
							{
							    if (isObject(strID))
							        this.Object = document.getElementById(strID);
							    
							    if (!isObject(this.Object))
							        return false;
							}
							
							alert(this.Object.id);
							this.Object.opacity = (intOpacity / 100);
							this.Object.MozOpacity = (intOpacity / 100);
							this.Object.KhtmlOpacity = (intOpacity / 100);
							this.Object.filter = 'alpha(opacity=' + intOpacity + ')';
							
							return true;
						}
						
	this.FadeIn = function(timeout)
	                {
	                    this.OpacityTransition(this.GetOpacity(), 100, timeout);
	                }
	                
	this.FadeOut = function(timeout)
	                {
	                    this.OpacityTransition(this.GetOpacity(), 0, timeout);
	                }
                    
    if (!isObject(obj))
	    this.Load(obj);
	
	return this;
	
}




// Change the opacity for different browsers
function SetObjectOpacity(intOpacity, strID)
{
    //if (intOpacity > 0)
	//    show(strID);
    //else
	//    hide(strID);
		
	//alert(strID + ' to ' + intOpacity);
	/*
	if (intOpacity == 100)
	    alert(strID + ' to ' + intOpacity);
    */
    
    var obj = new objController(strID);
    
    var object = obj.Object.style;
	
    object.opacity = (intOpacity / 100);
    object.MozOpacity = (intOpacity / 100);
    object.KhtmlOpacity = (intOpacity / 100);
    object.filter = 'alpha(opacity=' + intOpacity + ')';
    
    /* if (intOpacity > 0)
        obj.Show('absolute');
    else // if (intOpacity < 10)
    {
        //alert('Hide ' + obj.Object.id);
        obj.Hide();
        alert(ActiveStageItemDiv);
    }
    
    if (obj.Object.style.visibility == 'hidden' && intOpacity > 0)
        alert('problem!');
        
    */
	

}