/***********************************************
* CMotion Image Gallery- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* Visit http://www.dynamicDrive.com for source code
* This copyright notice must stay intact for legal use
* Modified for autowidth and optional starting positions in
* http://www.dynamicdrive.com/forums/showthread.php?t=11839 by jschuer1 8/5/06
* expanded to use with TYPO3: by bernd.wilke 20070731 
* moving some initialization into PHP,
* expanding some ids with content-id to include multiple galleries into one page
* show/hide arrows
***********************************************/

 //1) Set width of the "neutral" area in the center of the gallery.
//var restarea=100;
 //2) Set top scroll speed in pixels. Script auto creates a range from 0 to top speed.
//var maxspeed=20;
 //3) Set to maximum width for gallery - must be less than the actual length of the image train.
var maxwidth = Array();
 //4) Set to 1 for left start, 0 for right, 2 for center.
var startpos = Array();
 //5) Set message to show at end of gallery. Enter "" to disable message.
//var endofgallerymsg='<span style="font-size: 11px;">End of Gallery</span>';
var wraparound = Array();
var runforever = Array();

//function to enlarge image. Change as desired.
function enlargeimage(path, optWidth, optHeight)
{
	var actualWidth = Array();
	actualWidth[modid] = typeof optWidth != "undefined" ? optWidth : "600px"; //set 600px to default width
	var actualHeight = typeof optHeight != "undefined" ? optHeight : "500px"; //set 500px to  default height
	var winattributes = "width=" + actualWidth[modid] + ",height=" + actualHeight + ",resizable=yes";
	window.open(path, "", winattributes);
}

////NO NEED TO EDIT BELOW THIS LINE////////////

var iedom = document.all || document.getElementById;
var scrollspeed = 0;
var movestate = '';
var actualWidth = Array();
var cross_scroll = Array();
var ns_scroll;
var statusdiv = Array();
var arrldiv;
var arrrdiv;
var loadedyes = 0;
var lefttime;
var righttime;
var actmod;
var crossmain = Array();
var menuwidth = Array();

function ietruebody()
{
	return ((document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body);
}

function creatediv(modid)
{
	statusdiv[modid] = document.createElement("div");
	statusdiv[modid].setAttribute("id", "statusdiv" + modid);
	document.body.appendChild(statusdiv[modid]);
	statusdiv[modid] = document.getElementById("statusdiv" + modid);
	statusdiv[modid].innerHTML = endofgallerymsg;
}

function hidediv(name)
{
	if ((div = document.getElementById(name)) != null)
		div.style.visibility = 'hidden';
}
function showdiv(name)
{
	if ((div = document.getElementById(name)) != null)
		div.style.visibility = 'visible';
}

function positiondiv()
{
	if (actmod)
	{
		var mainobjoffset = getposOffset(crossmain[actmod], "left");
		var menuheight = Array();
		menuheight[actmod] = parseInt(crossmain[actmod].offsetHeight);
		var mainobjoffsetH = getposOffset(crossmain[actmod], "top");
		statusdiv[actmod].style.left = mainobjoffset + (menuwidth[actmod] / 2) - (statusdiv[actmod].offsetWidth / 2) + "px";
		statusdiv[actmod].style.top = menuheight[actmod] + mainobjoffsetH + "px";
	}
}

function showhidediv(what, modid)
{
	if (endofgallerymsg != "")
	{
		positiondiv();
		statusdiv[modid].style.visibility = what;
	}
}

function getposOffset(what, offsettype)
{
	var totaloffset = (offsettype == "left") ? what.offsetLeft: what.offsetTop;
	var parentEl = what.offsetParent;
	while (parentEl != null)
	{
		totaloffset = (offsettype == "left") ? totaloffset + parentEl.offsetLeft : totaloffset + parentEl.offsetTop;
		parentEl = parentEl.offsetParent;
	}
	return totaloffset;
}

function getX(element)
{
	var result = 0;
	while(element != null )
	{
		result += element.offsetLeft;
		element = element.offsetParent;
	}
	return result;
}

function moveleft(modid)
{
	if (loadedyes)
	{
	    movestate = "left";
		showdiv('arr_l' + modid);
		
		//??? ie - Alex
		menuwidth[modid] = 940;
		actualWidth[modid] = 1857;
		
		bordervalue = menuwidth[modid] - actualWidth[modid];
		
		//if (iedom && parseInt(cross_scroll[modid].style.left) > bordervalue)
		{
		    newPos = parseInt(cross_scroll[modid].style.left) - scrollspeed;
		    if (wraparound[modid])
			{
		        if (-actualWidth[modid] / 2 >= newPos)
					newPos += actualWidth[modid] / 2;
			}
			else if (newPos < bordervalue)
				newPos = bordervalue;
			cross_scroll[modid].style.left = newPos + "px";
			showhidediv("hidden", modid);
			showdiv('arr_r' + modid);
		}
		/*else
		{
			showhidediv("visible", modid);
			hidediv('arr_r' + modid);
		}*/
	}
	lefttime = setTimeout("moveleft('" + modid + "')", 70);
}

function moveright(modid)
{
	if (loadedyes)
	{
	    movestate = "right";
		showdiv('arr_r' + modid);
		if (iedom && (parseInt(cross_scroll[modid].style.left) < 0 || wraparound[modid]))
		{
		    newPos = parseInt(cross_scroll[modid].style.left) + scrollspeed;
		    if (newPos>=0)
			{
				if (wraparound[modid])
				        newPos -= actualWidth[modid] / 2;
				else
					newPos = 0;
			}
			cross_scroll[modid].style.left = newPos + "px";
			showhidediv("hidden", modid);
			showdiv('arr_l' + modid);
		}
		else
		{
			showhidediv("visible", modid);
			hidediv('arr_l' + modid);
		}
	}
	righttime = setTimeout("moveright('" + modid + "')", 10);
}

function motionengine(e, modid)
{
	actmod = modid;
	var mainobjoffset = getposOffset(crossmain[modid], "left");
	var dsocx = (window.pageXOffset) ? pageXOffset: ietruebody().scrollLeft;
	var dsocy = (window.pageYOffset) ? pageYOffset : ietruebody().scrollTop;
	var curposy = window.event? event.clientX : e.clientX? e.clientX: "";
	curposy -= mainobjoffset - dsocx;
	var leftbound = (menuwidth[modid] - restarea) / 2;
	var rightbound = (menuwidth[modid] + restarea) / 2;
	if (curposy > rightbound)
	{
		scrollspeed = (curposy - rightbound) / ((menuwidth[modid] - restarea) / 2) * maxspeed;
		clearTimeout(righttime);
		if (movestate != "left") 
			moveleft(modid);
	}
	else if (curposy < leftbound)
	{
		scrollspeed = (leftbound - curposy) / ((menuwidth[modid] - restarea) / 2) * maxspeed;
		clearTimeout(lefttime);
		if (movestate != "right") 
			moveright(modid);
	}
	else
		scrollspeed = 0;
}

function contains_ns6(a, b)
{
	if (b !== null)
		while (b.parentNode)
			if ((b = b.parentNode) == a)
				return true;
	return false;
}

function stopmotion(e,modid)
{
	if (!window.opera || (window.opera && e.relatedTarget !== null))
		if ((window.event && !crossmain[modid].contains(event.toElement)) || (e && e.currentTarget && e.currentTarget != e.relatedTarget && !contains_ns6(e.currentTarget, e.relatedTarget)))
		{
			clearTimeout(lefttime);
			clearTimeout(righttime);
			movestate = "";
		}
	actmod = "";
}

function fillup(modid)
{
	if (!modid)
		modid = "0";
	if (iedom)
	{
		//crossmain[modid]=document.getElementById? document.getElementById("motioncontainer"+modid) : document.all.motioncontainer;
		crossmain[modid] = document.getElementById("motioncontainer" + modid);
		if(typeof crossmain[modid].style.Width !== 'undefined')
			crossmain[modid].style.Width=maxwidth[modid] + 'px';
		menuwidth[modid] = crossmain[modid].offsetWidth;
		cross_scroll[modid] = document.getElementById ? document.getElementById("motiongallery" + modid) : document.all.motiongallery;
		actualWidth[modid] = document.getElementById ? document.getElementById("trueContainer" + modid).offsetWidth : document.all['trueContainer' + modid].offsetWidth;
		if (startpos[modid])
			cross_scroll[modid].style.left = (menuwidth[modid] - actualWidth[modid]) / startpos[modid] + 'px';
		crossmain[modid].onmousemove = function(e)
		{
			stopmotion(e, modid);
			//motionengine(e,modid);
		}
	
		crossmain[modid].onmouseout=function(e)
		{
			moveleft(modid);
			
		    //    if (!runforever[modid]) stopmotion(e,modid);
			//showhidediv("hidden",modid);
		}
	}
	loadedyes = 1;
	if (endofgallerymsg != "")
	{
		creatediv(modid);
		positiondiv();
	}
	if (document.body.filters)
		onresize(modid);
}

function delay(mSec)
{
	var eDate = null;
	var eMsec = 0;
	var sDate = new Date();
	var sMsec = sDate.getTime();
	do
	{
		eDate = new Date();
		eMsec = eDate.getTime();
	} while ((eMsec-sMsec) < mSec);
}

onresize = function(modid)
{
	if (typeof motioncontainer !== 'undefined' && motioncontainer.filters)
	{
		motioncontainer.style.width = "0";
		motioncontainer.style.width = "";
		motioncontainer.style.width = maxwidth[modid] + 'px';
		if (0 < motioncontainer.offsetWidth)
			motioncontainer.style.width = Math.min(motioncontainer.offsetWidth, maxwidth[modid]) + 'px';
	}
	if (crossmain[modid] && crossmain[modid].offsetWidth)
	{
		menuwidth[modid] = crossmain[modid].offsetWidth;
		cross_scroll[modid].style.left = startpos[modid] ? (menuwidth[modid] - actualWidth[modid]) / startpos[modid] + 'px' : 0;
	}
}

