/**
 * @author Kelly
 */

var myAnimationEngine=new AnimationClass();
var tempObj=document.createElement("span"); //this is useful for making code completion work in Aptana with passed parameters. Use by substituting your parameter with this in code, and then changing when done

function MakeInvisible(Element)
{
	Element.oldDisplay = Element.style.display;
	Element.style.display="none";
//	Element.oldVisibility = Element.style.visibility;
//	Element.style.visibility="hidden";
	
}

function MakeVisible(Element)
{
	if (Element.oldDisplay != null)
		Element.style.display=Element.oldDisplay;
	Element.oldDisplay = null;
//	if (Element.oldVisibility != null)
//		Element.style.visibility=Element.oldVisibility;
//	Element.oldVisibility = null;
}

function AnimateObjects_SelfDestruct(Element)
{
	if (Element.parentNode)
	{
		Element.parentNode.removeChild(Element);
	}
}

function AnimateObjects_setOpacity(Element,value)
{
    Element.style.opacity = value/10;
    Element.style.filter = 'alpha(opacity=' + value*10 + ')';
//	if (parseInt(value) <= 0)
//		MakeInvisible(Element);
//	else
//		MakeVisible(Element);
//	if (value <= 0)
//	{
//		MakeInvisible(Element);
		//Element.style.filter = '';
//	}
//	if (value > 0)
//		MakeVisible(Element);
//
	if (value >= 10)
		Element.style.filter = '';
}

function GetOpacity(Element)
{
	return Element.style.opacity*10;
}

function KillEventBubbling(e)
{
	if (!e) var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
}

function BlinkTextInElement(element,color1,color2,timeBetweenBlinksInMilliseconds,initOnly)
{
	element.isBlinking = true;
	element.blinkData = {"element":element,"color1":color1,"color2":color2,"timeBetweenBlinksInMilliseconds":timeBetweenBlinksInMilliseconds};

	element.stopBlinking = function(){
		this.isBlinking = false;
		if (this.blinkHandler != null &&
			this.blinkHandler != "undefined")
			RemoveAnimationItem(this.blinkHandler);
		this.blinkHandler = null;
	};

	element.startBlinking = function(){

		this.blinkHandler = AddAnimationItem(function(data){
			var now=new Date().getTime();
			if (data.element.lastBlinkTime == null ||
				data.element.lastBlinkTime == "undefined")
			{
				//first time
				data.element.lastBlinkTime = now;
				data.element.blink(true);
			}
			else
			{
				if ((data.element.lastBlinkTime + timeBetweenBlinksInMilliseconds) < now)
				{
					data.element.blink();
					data.element.lastBlinkTime = now;
				}
			}
			
		},this.blinkData);
		this.isBlinking = true;

	}


	element.blink = function(firstTime){
		if (firstTime == true)
		{
			this.style.color = color1;
			this.currentBlinkColor = 0;	
		}
		else
		{
			if (this.currentBlinkColor == 1)
			{
				this.style.color = color1;
				this.currentBlinkColor = 0;	
			}
			else
			{
				this.style.color = color2;
				this.currentBlinkColor = 1;	
			}
		}
	};
	if (initOnly != true)
		element.startBlinking();
}

function KillEventBubbling(e)
{
	if (!e) var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
}

function StopBlinkingTitle(withText)
{
	try
	{
		document.titleData.stopBlinking();
	}
	catch(e)
	{
		//if we hit this then it probably wasn't blinking to begin with
	}
	if (withText)
		document.title = withText;
	else
		if (document.titleData.text1)
			document.title = document.titleData.text1;
	
}

function BlinkTitle(text1,text2,timeBetweenBlinksInMilliseconds,initOnly)
{
	document.titleData = {};
	document.titleData.isBlinking = true;
	document.titleData.blinkData = {"element":document.titleData,"text1":text1,"text2":text2,"timeBetweenBlinksInMilliseconds":timeBetweenBlinksInMilliseconds};

	document.titleData.stopBlinking = function(){
		this.isBlinking = false;
		if (this.blinkHandler != null &&
			this.blinkHandler != "undefined")
			RemoveAnimationItem(this.blinkHandler);
		this.blinkHandler = null;
	};

	document.titleData.startBlinking = function(){

		this.blinkHandler = AddAnimationItem(function(data){
			var now=new Date().getTime();
			if (data.element.lastBlinkTime == null ||
				data.element.lastBlinkTime == "undefined")
			{
				//first time
				data.element.lastBlinkTime = now;
				data.element.blink(true);
			}
			else
			{
				if ((data.element.lastBlinkTime + timeBetweenBlinksInMilliseconds) < now)
				{
					data.element.blink();
					data.element.lastBlinkTime = now;
				}
			}
			
		},this.blinkData);
		this.isBlinking = true;

	}


	document.titleData.blink = function(firstTime){
		if (firstTime == true)
		{
			document.title = text1;
			this.currentBlinkColor = 0;	
		}
		else
		{
			if (this.currentBlinkColor == 1)
			{
				document.title = text1;
				this.currentBlinkColor = 0;	
			}
			else
			{
				document.title = text2;
				this.currentBlinkColor = 1;	
			}
		}
	};
	if (initOnly != true)
		document.titleData.startBlinking();
}





function FadeIn(Element,TimeToFadeInSeconds,startingOpacity,endingOpacity,runOnCompletion,withArguments)
{
	if (GetOpacity(Element)==endingOpacity)
		return;
	if (Element.animationTag!=null && Element.animationTag!="undefined")
	{
		//myAnimationEngine.RemoveAnimationItem(Element.animationTag);
		//Element.animationTag=null;
		return;
	}
	//if (startingOpacity!=null && startingOpacity!="undefined")
	//	AnimateObjects_setOpacity(Element,startingOpacity);
	
	//MakeVisible(Element);
	//Element.className="";
	Element.startingOpacity=startingOpacity;
	Element.endingOpacity=endingOpacity;
	Element.runOnCompletion=runOnCompletion;
	Element.withArguments=withArguments;
	var d=new Date();
	Element.StartTime=d.getTime();
	Element.EndTime=Element.StartTime+(TimeToFadeInSeconds*1000);
	var temphandler=myAnimationEngine.AddAnimationItem(FadeInRun,Element);
	Element.animationTag=temphandler;
}

function FadeInRun(Element)
{
	var d=new Date();
	var CurrentTime=d.getTime();
	var TotalDiff=Element.EndTime-Element.StartTime;
	var CurrentDiff=Element.EndTime-CurrentTime;
	if (CurrentDiff < 0)
	{
		AnimateObjects_setOpacity(Element,Element.endingOpacity);
		myAnimationEngine.RemoveAnimationItem(Element.animationTag);
		Element.animationTag=null;
		if (Element.runOnCompletion!=null)
		{
			Element.runOnCompletion(Element.withArguments);
			Element.runOnCompletion = null;
			Element.withAguments = null;
		}
		return;
	}
	var Percentage=CurrentDiff/TotalDiff;
	var OpacityDiff=Element.startingOpacity-Element.endingOpacity;
	var Opacity=(((OpacityDiff*Percentage))+Element.endingOpacity);
	if (Opacity > Element.endingOpacity)
		Opacity = Element.endingOpacity;
	//Element.innerHTML=Opacity;
	AnimateObjects_setOpacity(Element,Opacity);
	
}

function FadeOut(Element,TimeToFadeInSeconds,startingOpacity,endingOpacity,runOnCompletion,withArguments)
{
	if (GetOpacity(Element)==endingOpacity)
		return;
	if (Element.animationTag!=null && Element.animationTag!="undefined")
	{
		//myAnimationEngine.RemoveAnimationItem(Element.animationTag);
		//Element.animationTag=null;
		return;
	}
	//if (startingOpacity!=null && startingOpacity!="undefined")
	//	AnimateObjects_setOpacity(Element,startingOpacity);
	//Element.className="";
	Element.startingOpacity=startingOpacity;
	Element.endingOpacity=endingOpacity;
	Element.runOnCompletion=runOnCompletion;
	Element.withArguments=withArguments;
	var d=new Date();
	Element.StartTime=d.getTime();
	Element.EndTime=Element.StartTime+(TimeToFadeInSeconds*1000);
	var temphandler=myAnimationEngine.AddAnimationItem(FadeOutRun,Element);
	Element.animationTag=temphandler;
}

function FadeOutRun(Element)
{
	var d=new Date();
	var CurrentTime=d.getTime();
	var TotalDiff=Element.EndTime-Element.StartTime;
	var CurrentDiff=Element.EndTime-CurrentTime;
	if (CurrentDiff < 0)
	{
		AnimateObjects_setOpacity(Element,Element.endingOpacity);
		myAnimationEngine.RemoveAnimationItem(Element.animationTag);
		Element.animationTag=null;
		if (Element.runOnCompletion!=null)
		{
			Element.runOnCompletion(Element.withArguments);
			Element.runOnCompletion = null;
			Element.withAguments = null;
		}
		return;
	}
	var Percentage=CurrentDiff/TotalDiff;
//	var OpacityDiff=Element.endingOpacity-Element.startingOpacity;
//	var Opacity=(OpacityDiff*Percentage)+Element.startingOpacity;
	var OpacityDiff=Element.startingOpacity-Element.endingOpacity;
	var Opacity=(((OpacityDiff*Percentage))+Element.endingOpacity);
	if (Opacity < Element.endingOpacity)
		Opacity = Element.endingOpacity;
	//alert(Opacity);
	//Element.innerHTML=Opacity;
	AnimateObjects_setOpacity(Element,Opacity);
	
}

function AutoFadeType(TimeOfFadeIn,TimeOfFadeOut,InFade,OutFade,StartingValue,AnimationTag)
{
	this.TimeOfFadeIn=TimeOfFadeIn;
	this.TimeOfFadeOut=TimeOfFadeOut;
	this.InFade=InFade;
	this.OutFade=OutFade;
	this.StartingValue=StartingValue;
}

function AutoFade(Element,TimeOfFadeIn,TimeOfFadeOut,InFade,OutFade,StartingValue)
{
	var temphandler=myAnimationEngine.AddAnimationItem(AutoFaderun,Element);
	Element.AutoFadeInfo=new AutoFadeType(TimeOfFadeIn,TimeOfFadeOut,InFade,OutFade,StartingValue,temphandler);
	
}

function AutoFaderun(Element)
{
	
}

