//<![CDATA[
// AINet JavaScript
// Copyright 2008 Ronny Adsetts (ronny.adsetts@amazinginternet.com)

var fadeAllow = true;
var slideTimerFirst, slideTimerPeriod;


function slideStart(prefix, first, step, time, period) {
	/*
		Starts the cross fade functionality off. Prefix is used in all divs.
		nextid is the index of the next image to load. Rest are the times
		as the fadeIt function.
	*/

	if (slideTimerFirst || slideTimerPeriod)
	{ // If slideshow is already scheduled, bail out
		return false;
	}
	if (first != 0)
	{ // If we have a first time, run it once
		slideTimerFirst = setTimeout('slideNext(\'' + prefix + '\', ' + first + ', ' + step + ', ' + time + ')', first)
	}
	if (period)
	{ // If we have a period, schedule it
		slideTimerPeriod = setInterval('slideNext(\'' + prefix + '\', ' + first + ', ' + step + ', ' + time + ')', period);
	}
}

function slidePrev(prefix, first, step, time) {
	if (!fadeAllow)
	{
		return false;
	}
	eval('nextid = ' + prefix + 'NextPrev[' + prefix + 'CurrLive][\'prev\']');
	crossFade(prefix, nextid, first, step, time);
}

function slideNext(prefix, first, step, time) {
	if (!fadeAllow)
	{
		return false;
	}
	eval('nextid = ' + prefix + 'NextPrev[' + prefix + 'CurrLive][\'next\']');
	crossFade(prefix, nextid, first, step, time);
}

function slideStop() {
	if (slideTimerFirst)
	{
		clearTimeout(slideTimerFirst);
		slideTimerFirst = false;
	}
	if (slideTimerPeriod)
	{
		clearTimeout(slideTimerPeriod);
		slideTimerPeriod = false;
	}
}

function crossFade(prefix, nextid, first, step, time) {
	if (!fadeAllow || nextid == eval(prefix + 'CurrLive'))
	{ // If we're already running a fade or trying to fade the same image, bail out here
		return false;
	}

	eval('tmp = ' + prefix + 'NextPrev[\'' + nextid + '\'][\'load\']')
	if (!tmp)
	{ // Don't do the fade if the image has not yet loaded
		return false;
	}

	// Don't allow any fades while we're running one
	fadeAllow = false;

	// Calculate how long the fade will take to run
	// This is for fade-in/fade-out which is not implemented here yet
	//timetorun = Math.round(time * (100 / step));
	
	// Set top image to current
	setOpacity(prefix + 'DivTop', 0)
	switchImg(prefix + 'ImgTop', prefix + eval(prefix + 'CurrLive'));
	// Adjust image padding
	setStyle(prefix + 'ImgTop', 'marginTop', eval(prefix + 'NextPrev[' + prefix + 'CurrLive][\'toppad\']'))
	setStyle(prefix + 'ImgTop', 'marginLeft', eval(prefix + 'NextPrev[' + prefix + 'CurrLive][\'leftpad\']'))
	// Make sure top image is visible
	setOpacity(prefix + 'DivTop', 100)

	// Set bottom image to new image
	setOpacity(prefix + 'DivBottom', 0)
	switchImg(prefix + 'ImgBottom', prefix + nextid);
	// Adjust image padding
	setStyle(prefix + 'ImgBottom', 'marginTop', eval(prefix + 'NextPrev[\'' + nextid + '\'][\'toppad\']'))
	setStyle(prefix + 'ImgBottom', 'marginLeft', eval(prefix + 'NextPrev[\'' + nextid + '\'][\'leftpad\']'))
	// Now make bottom image visible
	setOpacity(prefix + 'DivBottom', 100)

	// Un-highlight the current thumb
	try
	{
		setOpacity('str-GallThumbImg' + eval(prefix + 'CurrLive'), 40)
	}
	catch(err) {}
	
	// Set the current image
	eval(prefix + 'CurrLive = \'' + nextid + '\'');

	// Now fade the old image out
	fadeIt(prefix + 'DivTop', 0, 0 - step, time, 'fadeAllow = true;');
	
	// Highlight the new thumb
	try
	{
		setOpacity('str-GallThumbImg' + eval(prefix + 'CurrLive'), 100)
	}
	catch(err) {}
}

function fadeIt(obj, first, step, time, func) {
	/*
		Fades in or out an image based on whether step is positive or
		negative. The fade goes from 0 to 100 so step should be an
		integer. first is how long to delay the start of the fade. time
		is the delay between fade steps in milli-seconds. func is an
		optional string to be eval'd when the opacity change is done
	*/
	
	// Set the opacity start point based on the step value
	if (step > 0)
	{
		now = 0;
	} else
	{
		now = 100;
	}

	// Escape the terminating function vars correctly
	if (func && func != '')
	{
		func = func.replace(/\'/g, '\\\'');
	}

	// Schedule the first opacity change
	setTimeout('doFade(\'' + obj + '\', ' + time + ', ' + step + ', ' + now + ', \'' + func + '\')', first);

	// Scroll the thumb panel to where it should be
	try
	{
		thumbScroll('str-GallThumb' + eval('GallPreviewCurrLive'));
	}
	catch(err) {}
}

function doFade(obj, time, step, now, func) {
	// Increment the current fade point by the step value
	now += step;

	// Cap the max/min values of the current fade point
	if (now	> 100)
	{
		now = 100;
	} else if (now < 0)
	{
		now = 0;
	}

	// Change the opacity of the image we are fading
	setOpacity(obj, now)

	// Don't repeat if we're at the max/min and eval the terminating function
	if (now == 100 || now == 0)
	{
		if (func != '')
		{
			eval(func);
		}
		return;
	}

	// Escape the terminating function vars correctly
	if (func != '')
	{
		func = func.replace(/\'/g, '\\\'');
	}

	// Schedule the next opacity change
	setTimeout('doFade(\'' + obj + '\', ' + time + ', ' + step + ', ' + now + ', \'' + func + '\')', time);
}

function setOpacity(obj, value) {
	// Object to be changed and percentagte opacity
	//alert('setOpacity');
	if (!setStyle(obj, 'opacity', value / 100))
	{
		if (!setStyle(obj, 'filter', 'alpha(opacity=' + value + ')'))
		{
			setStyle(obj, 'MozOpacity', value / 100);
		}
	}
}

function setClass(obj, value) {
	el =  bw.dom?document.getElementById(obj):bw.ie4?document.all[obj]:0;
	if (el.setAttribute)
	{
		el.setAttribute("class", value);
		el.setAttribute("className", value);
	}
	return false;
}

function hoverThumbInOut(obj, value) {
	obj.match(/^[a-z-]+(\d{8})$/i);
	objID = RegExp.$1;
	// If the hover is not the current image and the image is loaded set the opacity
	if (objID != GallPreviewCurrLive && GallPreviewNextPrev[objID]['load'])
	{
		setOpacity(obj, value);
	}
}

function setCurrThumb(obj, value) {
	if (!fadeAllow)
	{
		return false;
	}
	obj.match(/^[a-z-]+(\d{8})$/i);
	objID = RegExp.$1;
	// If the thumb image is loaded and is not the current one set the opacity
	if (GallPreviewNextPrev[objID]['load'] && GallPreviewCurrLive != objID)
	{
		setOpacity(obj, value);
	}
}

function thumbScroll(obj, amount, timer) {
	// This function is for scrolling to the currently active thumb if it is off the page
	if (scrollTopLeft + scrollOffset < 0 || scrollBottomRight + scrollOffset > scrollLength)
	{
		return;
	}
	var div = bw.dom ? document.getElementById(obj) : bw.ie4 ? document.all[obj] : 0;
	if (div)
	{
		thisedge = (scrollEdge == 'horizontal') ? 'Width' : 'Height';
		thisoffset = (scrollEdge == 'horizontal') ? 'Left' : 'Top';
		imgTopLeft = eval('div.offset' + thisoffset);
		imgBottomRight = eval('div.offset' + thisoffset) + eval('div.scroll' + thisedge);
		portTop = scrollOffset;
		portBottomRight = scrollOffset + scrollBottomRight;
		//if (!timer)
		//{
		//	alert('div.offsetTop: ' + div.offsetTop)
		//	alert('imgBottomRight: ' + imgBottomRight + '; portBottomRight: ' + portBottomRight + '; imgTopLeft: ' + imgTopLeft + '; portTop: ' + portTop);
		//}
		if (!amount)
		{
			amount = div.scrollHeight;
		}
		if (imgBottomRight > portBottomRight)
		{
			while (amount > imgBottomRight - portBottomRight)
			{
				amount = Math.ceil(amount / 2);
			}
			scrollOffset += amount;
		} else if (imgTopLeft < portTop)
		{
			while (amount > portTop - imgTopLeft)
			{
				amount = Math.ceil(amount / 2);
			}
			scrollOffset -= amount;
		} else
		{
			return;
		}
		port = div.parentNode.parentNode;
		if (scrollEdge == 'horizontal')
		{
			port.style.clip = 'rect(0px, ' + eval(scrollBottomRight + scrollOffset) + 'px, ' + scrollFixed + 'px, ' + eval(scrollTopLeft + scrollOffset) + 'px)';
			port.style.left = '-' + scrollOffset + 'px';
		} else
		{
			port.style.clip = 'rect(' + eval(scrollTopLeft + scrollOffset) + 'px, ' + scrollFixed + 'px, ' + eval(scrollBottomRight + scrollOffset) + 'px, 0px)';
			port.style.top = '-' + scrollOffset + 'px';
		}

		setTimeout('thumbScroll(\'' + obj + '\', ' + amount + ', true)', 10);
	}
}

//]]