var BaseAlpha = 0.25;

function Echo(str)
{
	var console = document.getElementById("TestConsole");
	if (console) {
		if (console.innerHTML != "") {
			console.innerHTML += "<br />"+str;
		} else {
			console.innerHTML = str;
		}
	}
}

function SliderCallback(ObjID)
{
	var obj = document.getElementById(ObjID);
	if (obj) {
		if (obj.AnimCounter <= obj.AnimLength) {
			// get all the images
			var ImgList = obj.getElementsByTagName("LI");

			// find lengths
			var pct = obj.AnimCounter/obj.AnimLength;
			pct = 0.5 - 0.5*Math.cos(pct*Math.PI);
			// Echo("Animation: "+obj.AnimCounter+"/"+obj.AnimLength+"("+pct+")");
			var x;
			var AccumulatedWidth = 0;
			for (x=0;x<obj.nImages;x++) {
				obj.CurrentWidth[x] = Math.round((1-pct)*obj.StartingWidth[x] + (pct)*obj.TargetWidth[x]);
				AccumulatedWidth += obj.CurrentWidth[x];
			}

			// take rounding errors off an inactive cell
			var inactive = (obj.ActiveObject+1)%obj.nImages;
			while (AccumulatedWidth > obj.TotalWidth) {
				// Echo("trimming");
				obj.CurrentWidth[inactive]--;
				AccumulatedWidth--;
			}
			while (AccumulatedWidth < obj.TotalWidth) {
				// Echo("padding");
				obj.CurrentWidth[inactive]++;
				AccumulatedWidth++;
			}
			Echo(AccumulatedWidth);

			// set up styles
			var alpha = 0;
			var Internals;
			for (x=0;x<obj.nImages;x++) {
				alpha = (obj.CurrentWidth[x]-obj.MinWidth)/(obj.ImgWidth-obj.MinWidth);
				alpha = (1-alpha)*BaseAlpha + (alpha);
				ImgList[x].style.width = obj.CurrentWidth[x]+"px";
				// Echo(x+": "+obj.StartingWidth[x]+" | "+obj.TargetWidth[x]+" | "+obj.CurrentWidth[x]);
				ImgList[x].style.overflow = "hidden";
				
				Internals = ImgList[x].getElementsByTagName("IMG");
				if (Internals) {
					Internals[0].style.opacity = alpha;
					Internals[0].style.filter = "alpha(opacity="+Math.floor(alpha*100)+")";
				}
			}
			
			// increment animation counter
			obj.AnimCounter++;

		}
		setTimeout("SliderCallback('"+ObjID+"');", 10);
	}
}

function ExpandSlider(ObjID, n)
{
	var obj = document.getElementById(ObjID);
	if (obj) {
		if (obj.ActiveObj != n) {
			Echo("Expanding: "+n);
			
			// move to next object
			obj.ActiveObject = n;
			// get all the images
			var ImgList = obj.getElementsByTagName("LI");
			
			// set initial widths
			var x, AccumulatedWidth;
			for (x=0;x<obj.nImages;x++) {
				if (x==n) {
					obj.TargetWidth[x] = obj.ImgWidth;
				} else {
					obj.TargetWidth[x] = obj.MinWidth;
				}
				AccumulatedWidth += obj.TargetWidth[x];
			}
	
			// take rounding errors off an inactive cell
			var inactive = (obj.ActiveObject+1)%obj.nImages;
			while (AccumulatedWidth > obj.TotalWidth) {
				obj.TargetWidth[inactive]--;
				AccumulatedWidth--;
			}
			while (AccumulatedWidth < obj.TotalWidth) {
				obj.TargetWidth[inactive]++;
				AccumulatedWidth++;
			}
			
			// reset animation
			for (x=0;x<obj.nImages;x++) {
				obj.StartingWidth[x] = obj.CurrentWidth[x];
			}
			obj.AnimCounter = 0;
		}
	}
}

function InitHomepageSlider(ObjID, TotalWidth, ImgWidth)
{
	var obj = document.getElementById(ObjID);
	if (obj) {
		// get all the images
		var ImgList = obj.getElementsByTagName("LI");
		var nImages = ImgList.length;
		
		// adjust width for the left borders
		TotalWidth = TotalWidth - (nImages-1);
		
		// save variables
		obj.TotalWidth = TotalWidth;
		obj.ImgWidth = ImgWidth;
		obj.nImages = nImages
		obj.MinWidth = Math.floor((obj.TotalWidth-obj.ImgWidth)/(obj.nImages-1));
		
		// set initial widths
		var CurrentWidth = Array();
		var x, AccumulatedWidth;
		CurrentWidth[0] = ImgWidth;
		AccumulatedWidth = CurrentWidth[0];
		for (x=1;x<obj.nImages;x++) {
			CurrentWidth[x] = obj.MinWidth;
			AccumulatedWidth += CurrentWidth[x];
		}

		// take rounding errors off the end object
		while (AccumulatedWidth > TotalWidth) {
			CurrentWidth[1]--;
			AccumulatedWidth--;
		}
		while (AccumulatedWidth < TotalWidth) {
			CurrentWidth[1]++;
			AccumulatedWidth++;
		}
		
		// set up styles
		var alpha = 0;
		var Internals;
		for (x=0;x<obj.nImages;x++) {
			if (x==0) {
				alpha = 1;
			} else {
				alpha = (CurrentWidth[x]-obj.MinWidth)/(obj.ImgWidth-obj.MinWidth);
				alpha = (1-alpha)*BaseAlpha + (alpha);
			}
			ImgList[x].style.width = CurrentWidth[x]+"px";
			ImgList[x].style.overflow = "hidden";
			
			Internals = ImgList[x].getElementsByTagName("IMG");
			if (Internals) {
				Internals[0].style.opacity = alpha;
			}
		}
		
		// initialize animation
		obj.CurrentWidth = Array();
		obj.StartingWidth = Array();
		obj.TargetWidth = Array();
		for (x=0;x<obj.nImages;x++) {
			obj.CurrentWidth[x] = CurrentWidth[x];
			obj.StartingWidth[x] = CurrentWidth[x];
			obj.TargetWidth[x] = CurrentWidth[x];
		}
		obj.AnimLength = 25;
		obj.AnimCounter = obj.AnimLength;
		obj.ActiveObject = 0;
		
		// start automatic timer
		obj.onMouseOver = DisableTimer;
		obj.onMouseOut = EnableTimer;
		obj.onMouseOut();
		setTimeout("TimerCallback('"+ObjID+"');", 6000);

		// set mouseover commands
		for (x=0;x<obj.nImages;x++) {
			ImgList[x].n = x;
			ImgList[x].ObjID = ObjID;
			ImgList[x].onmouseover = ExpandMe;
			ImgList[x].onmouseout = UnExpandMe;
		}
		
		// start animation callback
		SliderCallback(ObjID);
		
	}
}

function ExpandMe()
{
	var obj = document.getElementById(this.ObjID);
	obj.onMouseOver();
	ExpandSlider(this.ObjID, this.n);
}

function UnExpandMe()
{
	var obj = document.getElementById(this.ObjID);
	obj.onMouseOut();
}

function DisableTimer()
{
	Echo("Disable Timer");
	this.AutoTimer = false;
}

function EnableTimer()
{
	Echo("Enable Timer");
	this.AutoTimer = true;
}

function TimerCallback(ObjID)
{
	var obj = document.getElementById(ObjID);
	if (obj) {
		if (obj.AutoTimer == true) {
			var ActiveObject = (obj.ActiveObject+1)%obj.nImages;
			ExpandSlider(ObjID, ActiveObject);
		}
		setTimeout("TimerCallback('"+ObjID+"');", 10000);
	}
}
