//Copyright (c) 2008, Omnicognic LLC.
//All rights reserved.
//Written by Conrad Sollitt.
//Software License Agreement (BSD License)
//See full version of InfoFind for comments and full license.

var InfoFind_Lite = {

Version:function() { return 1.105; },
Publisher:function() { return "Omnicognic LLC"; },
WebSite:function() { return "http://www.omnicognic.com"; },
Authors:function() { return "Conrad Sollitt"; },

ByID:function(ID) {
	var Element = ID;
	if (typeof(ID) == "string") {
		if (document.getElementById) {
			Element = document.getElementById(ID);
		} else if (document.all) {
			Element = document.all[ID];
		}
	}
	return Element;
},

ByTag:function(TagName) {
	if (document.getElementsByTagName) {
		return document.getElementsByTagName(TagName);
	} else if (document.all) {
		return document.all.tags(TagName);
	}
},

AddEvent:function(Element, Event, Function) {
	Element = this.ByID(Element);
	if (Element.addEventListener) {
		Element.addEventListener(Event, Function, false);
	} else if (Element.attachEvent) {
		Element.attachEvent("on" + Event, Function);
	} else {
		var CurrentEvent = Element["on" + Event];
		if (CurrentEvent) {
			Element["on" + Event] = (function(e){CurrentEvent(e); Function(e);});
		} else {
			Element["on" + Event] = Function;
		}
	}
},

RemoveEvent:function(Element, Event, Function) {
	Element = this.ByID(Element);
	if (Element.removeEventListener) {
		Element.removeEventListener(Event, Function, false);
	} else if (Element.detachEvent) {
		Element.detachEvent("on" + Event, Function);
	} else {
		Element["on" + Event] = null;
	}
},

StopEvent:function(e) {
	e = e || window.event;
	if (e.cancelBubble && e.returnValue) {
		e.cancelBubble = true;
		e.returnValue = false;
	} else if (e.preventDefault && e.stopPropagation) {
		e.preventDefault();
		e.stopPropagation();
	}
	return true;
},

Window_GetHeight:function() {
	if (window.innerHeight) {
		return window.innerHeight;
	} else if (document.documentElement && document.documentElement.offsetHeight) {
		return document.documentElement.offsetHeight;
	} else if (document.body && document.body.clientHeight) {
		return document.body.clientHeight;
	}
	return 0;
},

Window_GetWidth:function() {
	if (window.innerWidth) {
		return window.innerWidth;
	} else if (document.documentElement && document.documentElement.offsetWidth) {
		return document.documentElement.offsetWidth;
	} else if (document.body && document.body.clientWidth) {
		return document.body.clientWidth;
	}
	return 0;
},

Document_GetHeight:function() {
	var Height = 0, Height1 = 0, Height2 = 0;
	if (document.documentElement && document.documentElement.scrollHeight) {
		Height1 = document.documentElement.scrollHeight;
	}
	if (document.body && document.body.scrollHeight) {
		Height2 = document.body.scrollHeight;
	}
	Height = Math.max(Math.max(Height1, Height2), this.Window_GetHeight());
	return Height;
},

Document_GetWidth:function(){
	var Width = 0, Width1 = 0, Width2 = 0;
	if (document.documentElement && document.documentElement.scrollWidth) {
		Width1 = document.documentElement.scrollWidth;
	}
	if (document.body && document.body.scrollWidth) {
		Width2 = document.body.scrollWidth;
	}
	Width = Math.max(Math.max(Width1, Width2), this.Window_GetWidth());
	return Width;
},

Document_GetMousePosition:function(e) {
	e = e || window.event;
	var Cursor = { Left:0, Top:0 };
	if (e.pageX || e.pageY) {
		Cursor.Left = e.pageX;
		Cursor.Top = e.pageY;
	} else if (document.documentElement && (document.documentElement.clientLeft || document.documentElement.clientTop)) {
		Cursor.Left = e.clientX + document.documentElement.scrollLeft - document.documentElement.clientLeft;
		Cursor.Top = e.clientY + document.documentElement.scrollTop - document.documentElement.clientTop;
	} else if (document.body && (document.body.clientLeft || document.body.clientTop)) {
		Cursor.Left = e.clientX + document.body.scrollLeft - document.body.clientLeft;
		Cursor.Top = e.clientY + document.body.scrollTop - document.body.clientTop;
	}
	return Cursor;
},

Document_GetScrollPosition:function() {
	var Location = { Left:0, Top:0 };
	if (window.pageXOffset || window.pageYOffset) {
		Location.Left = window.pageXOffset;
		Location.Top = window.pageYOffset;
	} else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
		Location.Left = document.documentElement.scrollLeft;
		Location.Top = document.documentElement.scrollTop;
	} else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
		Location.Left = document.body.scrollLeft;
		Location.Top = document.body.scrollTop;
	}
	return Location;
},

Element_GetPosition:function(Element) {
	Element = this.ByID(Element);
    var Location = { Left:0, Top:0, Width:0, Height:0 };
    if (Element.style && Element.style.width && !isNaN(parseInt(Element.style.width))) { 
		Location.Width = parseInt(Element.style.width);
	} else if (Element.offsetWidth) {
		Location.Width = Element.offsetWidth;
    }
    if (Element.style && Element.style.height && !isNaN(parseInt(Element.style.height))) { 
		Location.Height = parseInt(Element.style.height);
	} else if (Element.offsetHeight) {
		Location.Height = Element.offsetHeight;
    }
    if (Element.style.position == "fixed" || Element.style.position == "absolute") {
        Location.Left = Element.offsetLeft;
        Location.Top = Element.offsetTop;
    } else {
        while (Element) {
            if (Element.offsetLeft) Location.Left += Element.offsetLeft;
            if (Element.offsetTop) Location.Top += Element.offsetTop;
            Element = Element.offsetParent;
        }
    }
    return Location;
},

Element_Move:function(Element, Top, Left, Height, Width, bFixed) {
	Element = this.ByID(Element);
	if (Element && Element.style) {
		if (Top || Left) {
			if (bFixed) {
				try {
					Element.style.position = "fixed";
				} catch(e) {
					Element.style.position = "absolute";
				}
			} else {
				Element.style.position = "absolute";
			}
		}
		if (Top || Left) Element.style.position = "absolute";
		if (Top) Element.style.top = Top + "px";
		if (Left) Element.style.left = Left + "px";
		if (Height) Element.style.height = Height + "px";
		if (Width) Element.style.width = Width + "px";
	}
},

Element_SetOpacity:function(Element, Percent) {
	Element = this.ByID(Element);
	if (Element && Element.style) {
        if (typeof(Element.style.opacity) == "string") {
			Element.style.opacity = (Percent / 100);
		} else if (typeof(Element.style.filter) == "string") { 
			var Filter = "DXImageTransform.Microsoft.Alpha";
			var FilterText = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + Percent + ")";
			/*@cc_on
				@if (@_jscript_version < 5.5)
					Filter = "alpha";
					FilterText = "alpha(opacity=" + Percent + ")";
				@end
			@*/
			var bCreateFilter = true;
			if (Element.filters && Element.filters.length > 0) {
				var Alpha = Element.filters[Filter];
				if (Alpha) {
					Alpha.opacity = Percent;
					bCreateFilter = false;
				}
			}
			if (bCreateFilter) {
				Element.style.filter = FilterText;
			}
		} else if (typeof(Element.style.MozOpacity) == "string") {
			Element.style.MozOpacity = (Percent / 100);
		} else if (typeof(Element.style.KhtmlOpacity) == "string") {
			Element.style.KhtmlOpacity = (Percent / 100);
		}
	}
},

RoundToDecimal:function(number) { 
	return Math.round(number * 100) / 100;
},

Random:function(MinNumber, MaxNumber) {
	return Math.floor(Math.random() * (MaxNumber - MinNumber + 1) + MinNumber);
},

Animate:function(ElementID, Seconds, AnimationScript, NumberOfAnimations) {
	var Element = this.ByID(ElementID);
	if (!Element.id) return;
	ElementID = "'" + Element.id + "'";
	
	var Animations = NumberOfAnimations || 100, bError = false;
	var Interval = Seconds * 1000 / Animations;
	var n = 0, js = "";
	var bOpacity = false, OpacityStart = 0, OpacityEnd = 0, OpacityCurrent = 0, OpacityAdd = 0;
	var WinHeight = this.Window_GetHeight(), WinWidth = this.Window_GetWidth();
	var DocOffset = this.Document_GetScrollPosition();
	var Location = this.Element_GetPosition(Element);
	var bResize = false, SizeStart = 0, SizeEnd = 0, SizeCurrent = 0, SizeAdd = 0;
	var Top = 0, Left = 0, Height = 0, Width = 0;
	var bRollDown = false, bRollUp = false, bRollRight = false, bRollLeft = false;
	var TargetTop = 0, StartTop = 0, TargetLeft = 0, StartLeft = 0;
	var bMoveTo = false, bMoveTop = false, bMoveLeft = false;
	var bMoveCircle = false, Angle = 0, Radius = 0, Degree = 0, DegreeAdd = 0;
	var Direction = 0;
	
	AnimationScript = AnimationScript.split(";");
	for (n = 0; n < AnimationScript.length; n++) {
		AnimationScript[n] = AnimationScript[n].split(",");
		switch (AnimationScript[n][0]) {
			case "Opacity":
				OpacityStart = AnimationScript[n][1] || OpacityStart;
				OpacityEnd = AnimationScript[n][2] || OpacityEnd;
				bOpacity = (OpacityStart > 0 || OpacityEnd > 0);
				if (bOpacity) {
					OpacityCurrent = Number(OpacityStart);
					OpacityAdd = Number((OpacityEnd - OpacityStart) / Animations);
				} else {
					bError = true;
				}
				break;
			case "Resize":
				SizeStart = AnimationScript[n][1] || SizeStart;
				SizeEnd = AnimationScript[n][2] || SizeEnd;
				bResize = (SizeStart > 0 || SizeEnd > 0);
				if (bResize) {
					SizeCurrent = Number(SizeStart / 100);
					SizeAdd = Number((SizeEnd - SizeStart) / Animations / 100);
				} else {
					bError = true;
				}
				break;
			case "RollDown":
				bRollDown = true;
				break;
			case "RollUp":
				bRollUp = true;
				break;
			case "RollRight":
				bRollRight = true;
				break;
			case "RollLeft":
				bRollLeft = true;
				break;
			case "MoveTo":
				TargetTop = AnimationScript[n][1] || 0;
				StartTop = Number(Location.Top);
				TargetLeft = AnimationScript[n][2] || 0;
				StartLeft = Number(Location.Left);
				bMoveTo = true;
				break;
			case "MoveToRandomBorder":
				StartTop = Number(Location.Top);
				StartLeft = Number(Location.Left);
				TargetTop = this.Random(0, (DocOffset.Top + WinHeight - Location.Height));
				TargetLeft = this.Random(0, (DocOffset.Left + WinWidth - Location.Width));
				switch (this.Random(0, 3)) {
					case 0:
						TargetTop = Number(DocOffset.Top);
						bMoveTo = true;
						break;
					case 1:
						TargetLeft = Number(DocOffset.Left + WinWidth - Location.Width);
						bMoveTo = true;
						break;
					case 2:
						TargetTop = Number(DocOffset.Top + WinHeight - Location.Height);
						bMoveTo = true;
						break;
					case 3:
						TargetLeft = Number(DocOffset.Left);
						bMoveTo = true;
						break;
				}
				break;
			case "MoveFromTop":
				bMoveTop = true;
				TargetTop = Number(Location.Top);
				StartTop = Number(DocOffset.Top);
				break;
			case "MoveToTop":
				bMoveTop = true;
				TargetTop = Number(DocOffset.Top);
				StartTop = Number(Location.Top);
				break;
			case "MoveFromLeft":
				bMoveLeft = true;
				TargetLeft = Number(Location.Left);
				StartLeft = Number(DocOffset.Left);
				break;
			case "MoveToLeft":
				bMoveLeft = true;
				TargetLeft = Number(DocOffset.Left);
				StartLeft = Number(Location.Left);
				break;
			case "MoveFromBottom":
				bMoveTop = true;
				TargetTop = Number(Location.Top);
				StartTop = Number(DocOffset.Top + WinHeight - Location.Height);
				break;
			case "MoveToBottom":
				bMoveTop = true;
				TargetTop = Number(DocOffset.Top + WinHeight - Location.Height);
				StartTop = Number(Location.Top);
				break;
			case "MoveFromRight":
				bMoveLeft = true;
				TargetLeft = Number(Location.Left);
				StartLeft = Number(DocOffset.Left + WinWidth - Location.Width);
				break;
			case "MoveToRight":
				bMoveLeft = true;
				TargetLeft = Number(DocOffset.Left + WinWidth - Location.Width);
				StartLeft = Number(Location.Left);
				break;
			case "MoveInCircle":
				Radius = AnimationScript[n][1] || Radius;
				Direction = AnimationScript[n][2] || Direction;
				bMoveCircle = (Radius > 0);
				if (bMoveCircle) {
					DegreeAdd = (360 / Animations);
					if (Direction == 1 || Direction == 3) {
						DegreeAdd = -DegreeAdd;
					}
					if (Direction == 2 || Direction == 3) {
						Radius = -Radius;
					}
					StartLeft = Number(Location.Left);
					StartTop = Number(Location.Top);
				} else {
					bError = true;
				}
				break;
			case "":
				break;
			default:
				throw "Unknown Animation Script: " + AnimationScript[n][0];
		}
		if (bError) {
			throw "Invalid Animation Script Parameters for " + AnimationScript[n][0];
		}
	}
	
	for (n = 0; n <= Animations; n++) {
		js = "";
		if (bOpacity) {
			js += "InfoFind_Lite.Element_SetOpacity(" + ElementID + ", " + OpacityCurrent + "); ";
			OpacityCurrent += OpacityAdd;
		}
		if (bMoveCircle) {
			Degree = Degree + DegreeAdd;
			Angle = Degree * Math.PI / 180;
			Location.Top = InfoFind_Lite.RoundToDecimal(StartTop + (Radius * Math.cos(Angle)) - Radius);
			Location.Left = InfoFind_Lite.RoundToDecimal(StartLeft + (Radius * Math.sin(Angle)) + (Radius < 0 ? 0 : Radius) - ((Direction == 0 || Direction == 3) ? (Math.PI * 2) : -(Math.PI * 2)));
			js += "InfoFind_Lite.Element_Move(" + ElementID + ", " + Location.Top + ", " + Location.Left + "); ";
		} else if (bMoveTo || bMoveTop || bMoveLeft) {
			if (bMoveTo || bMoveTop) Location.Top = StartTop + ((TargetTop - StartTop) * n / Animations);
			if (bMoveTo || bMoveLeft) Location.Left = StartLeft + ((TargetLeft - StartLeft) * n / Animations);
			js += "InfoFind_Lite.Element_Move(" + ElementID + ", " + Location.Top + ", " + Location.Left + "); ";
		}
		if (bResize) {
			js += "InfoFind_Lite.Element_Move(" + ElementID + ", null, null, " + InfoFind_Lite.RoundToDecimal(Location.Height * SizeCurrent) + ", " + InfoFind_Lite.RoundToDecimal(Location.Width * SizeCurrent) + "); ";
			SizeCurrent += SizeAdd;
		} else if (bRollDown) {
			Height = Location.Height * n / Animations;
			js += "InfoFind_Lite.Element_Move(" + ElementID + ", " + Location.Top + ", " + Location.Left + ", " + Height + "); ";
		} else if (bRollUp) {
			Height = Location.Height * n / Animations;
			Top = Location.Top + Location.Height - Height;
			js += "InfoFind_Lite.Element_Move(" + ElementID + ", " + Top + ", " + Location.Left + ", " + Height + "); ";
		} else if (bRollRight) {
			Width = Location.Width * n / Animations;
			js += "InfoFind_Lite.Element_Move(" + ElementID + ", " + Location.Top + ", " + Location.Left + ", null, " + Width + "); ";
		} else if (bRollLeft) {
			Width = Location.Width * n / Animations;
			Left = Location.Left + Location.Width - Width;
			js += "InfoFind_Lite.Element_Move(" + ElementID + ", " + Location.Top + ", " + Left + ", null, " + Width + "); ";
		}
		if (js != "") window.setTimeout(js, n * Interval);
	}
},

FadeOut:function(ElementID, Seconds) { this.Opacity(ElementID, 100, 0, Seconds); },
FadeIn:function(ElementID, Seconds) { this.Opacity(ElementID, 0, 100, Seconds); },
FadeOutAndIn:function(ElementID, Seconds) {
	Seconds = Seconds / 2 - 0.5;
	this.FadeOut(ElementID, Seconds);
	window.setTimeout("InfoFind_Lite.FadeIn('" + InfoFind_Lite.ByID(ElementID).id + "', " + Seconds + ")", (Seconds * 1000) + 500);
},
Opacity:function(ElementID, StartPercent, EndPercent, Seconds) { this.Animate(ElementID, Seconds, "Opacity," + StartPercent + "," + EndPercent); },
Resize:function(ElementID, StartPercent, EndPercent, Seconds) {	this.Animate(ElementID, Seconds, "Resize," + StartPercent + "," + EndPercent); },
RollDown:function(ElementID, Seconds) { this.Animate(ElementID, Seconds, "RollDown"); },
RollUp:function(ElementID, Seconds) { this.Animate(ElementID, Seconds, "RollUp"); },
RollRight:function(ElementID, Seconds) { this.Animate(ElementID, Seconds, "RollRight"); },
RollLeft:function(ElementID, Seconds) { this.Animate(ElementID, Seconds, "RollLeft"); },
MoveTo:function(ElementID, Top, Left, Seconds) { this.Animate(ElementID, Seconds, "MoveTo," + Top + "," + Left); },
MoveToRandomBorder:function(ElementID, Seconds) { this.Animate(ElementID, Seconds, "MoveToRandomBorder"); },
MoveFromRight:function(ElementID, Seconds) { this.Animate(ElementID, Seconds, "MoveFromRight"); },
MoveFromTop:function(ElementID, Seconds) { this.Animate(ElementID, Seconds, "MoveFromTop"); },
MoveFromLeft:function(ElementID, Seconds) { this.Animate(ElementID, Seconds, "MoveFromLeft"); },
MoveFromBottom:function(ElementID, Seconds) { this.Animate(ElementID, Seconds, "MoveFromBottom"); },
MoveToRight:function(ElementID, Seconds) { this.Animate(ElementID, Seconds, "MoveToRight"); },
MoveToTop:function(ElementID, Seconds) { this.Animate(ElementID, Seconds, "MoveToTop"); },
MoveToLeft:function(ElementID, Seconds) { this.Animate(ElementID, Seconds, "MoveToLeft"); },
MoveToBottom:function(ElementID, Seconds) { this.Animate(ElementID, Seconds, "MoveToBottom"); },
MoveInCircle:function(ElementID, Seconds, Radius, Direction) { this.Animate(ElementID, Seconds, "MoveInCircle," + Radius + "," + Direction); }

};
