/***********************************************
* 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
***********************************************/

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

function creatediv()
{
	this.statusdiv = document.createElement("div");
	this.statusdiv.setAttribute("id","statusdiv_" + this.stripId);
	document.body.appendChild(this.statusdiv);
	this.statusdiv = document.getElementById("statusdiv_" + this.stripId);
	this.statusdiv.innerHTML = this.endofgallerymsg;
}

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

function showhidediv(what)
{
	if (this.endofgallerymsg!=="")
	{
		this.positiondiv();
		this.statusdiv.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 moveleft()
{
	if (this.loadedyes)
	{
		this.movestate="left";
		if (this.iedom&&parseInt(this.cross_scroll.style.left, 10)>(this.menuwidth-this.actualwidth))
		{
			this.cross_scroll.style.left=parseInt(this.cross_scroll.style.left, 10)-this.scrollspeed+"px";
			this.showhidediv("hidden");
		}
		else { this.showhidediv("visible"); }
	}
	this.lefttime=setTimeout(this.instanceName + ".moveleft()",10);
}

function moveright()
{
	if (this.loadedyes)
	{
		this.movestate="right";
		if (this.iedom&&parseInt(this.cross_scroll.style.left, 10)<0)
		{
			this.cross_scroll.style.left=parseInt(this.cross_scroll.style.left, 10)+this.scrollspeed+"px";
			this.showhidediv("hidden");
		}
		else { this.showhidediv("visible"); }
	}
	this.righttime=setTimeout(this.instanceName + ".moveright()",10);
}

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

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

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

function fillup()
{
	if (this.iedom)
	{
		this.crossmain=document.getElementById? document.getElementById("motioncontainer_" + this.stripId) : document.all.motioncontainer;
		if (typeof this.crossmain.style.maxwidth !== 'undefined') { this.crossmain.style.maxwidth=this.maxwidth+'px'; }
		this.menuwidth=this.crossmain.offsetWidth;
		this.cross_scroll=document.getElementById? document.getElementById("motiongallery_" + this.stripId) : document.all.motiongallery;
		this.actualwidth=document.getElementById? document.getElementById("trueContainer_" + this.stripId).offsetWidth : document.all['trueContainer_' + this.stripId].offsetWidth;

		if (this.startpos) { this.cross_scroll.style.left=(this.menuwidth-this.actualwidth)/this.startpos+'px'; }

		this.crossmain.onmousemove = new Function ('e','eval("'+this.instanceName+'.motionengine(e);");');

		this.crossmain.onmouseout = new Function ('e','eval("'+this.instanceName+'.stopmotion(e); '+this.instanceName+'.showhidediv(\\"hidden\\");");');
	}
	this.loadedyes=1;
	if (this.endofgallerymsg!=="")
	{
		this.creatediv();
		this.positiondiv();
	}
	if (document.body.filters) { this.onresize(); }
}

function cmotion(strStripId)
{
	this.instanceName = "propertyGallery_" + strStripId;
	this.restarea = 50;
	this.maxspeed = 5;
	this.maxwidth = 10;
	this.startpos = 0;
	this.stripId = strStripId;
	this.endofgallerymsg='';
	this.iedom = document.all||document.getElementById;
	this.scrollspeed = 0;
	this.movestate = '';
	this.actualwidth = '';
	this.loadedyes = 0;

	this.ietruebody = ietruebody;
	this.creatediv = creatediv;
	this.positiondiv = positiondiv;
	this.showhidediv = showhidediv;
	this.getposOffset = getposOffset;
	this.moveleft = moveleft;
	this.moveright = moveright;
	this.motionengine = motionengine;
	this.contains_ns6 = contains_ns6;
	this.stopmotion = stopmotion;
	this.fillup = fillup;
	this.onresize = function()
	{
		if (typeof motioncontainer!=='undefined'&&motioncontainer.filters)
		{
			motioncontainer.style.width="0";
			motioncontainer.style.width="";
			motioncontainer.style.width=Math.min(motioncontainer.offsetWidth, this.maxwidth)+'px';
		}
		this.menuwidth=this.crossmain.offsetWidth;
		this.cross_scroll.style.left=this.startpos? (this.menuwidth-this.actualwidth)/this.startpos+'px' : 0;
	};
}
