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

 //1) Set width of the "neutral" area in the center of the gallery.
var cmotion_restarea=20;
 //2) Set top scroll speed in pixels. Script auto creates a range from 0 to top speed.
var cmotion_maxspeed=10;
 //3) Set to maximum width for gallery - must be less than the actual length of the image train.
var cmotion_maxwidth=1000;
 //4) Set to 1 for left start, 0 for right, 2 for center.
var cmotion_startpos=0;

var iedom=document.all||document.getElementById, cmotion_scrollspeed=0, cmotion_movestate='', cmotion_actualwidth='', cmotion_gallery, cmotion_ns_scroll, cmotion_loadedyes=0, cmotion_lefttime, cmotion_righttime;

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


function cmotion_positiondiv(){
	var mainobjoffset=cmotion_getposOffset(cmotion_crossmain, "left"),
	menuheight=parseInt(cmotion_crossmain.offsetHeight),
	mainobjoffsetH=cmotion_getposOffset(cmotion_crossmain, "top");
}


function cmotion_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 cmotion_moveleft(){
	if (cmotion_loadedyes){
		cmotion_movestate="left";
		if (iedom&&parseInt(cmotion_gallery.style.left)>(cmotion_menuwidth-cmotion_actualwidth)){
			cmotion_gallery.style.left=parseInt(cmotion_gallery.style.left)-cmotion_scrollspeed+"px";
		}
	}
	cmotion_lefttime=setTimeout("cmotion_moveleft()",10);
}

function cmotion_moveright(){
	if (cmotion_loadedyes){
		cmotion_movestate="right";
		if (iedom&&parseInt(cmotion_gallery.style.left)<0){
			cmotion_gallery.style.left=parseInt(cmotion_gallery.style.left)+cmotion_scrollspeed+"px";
		}
	}
	cmotion_righttime=setTimeout("cmotion_moveright()",10);
}

function cmotion_moveleft_remote(){
	cmotion_scrollspeed=15;
	cmotion_moveleft()
}

function cmotion_moveright_remote(){
	cmotion_scrollspeed=15;
	cmotion_moveright()
}


function cmotion_engine(e){
	var mainobjoffset=cmotion_getposOffset(cmotion_crossmain, "left"),
	dsocx=(window.pageXOffset)? pageXOffset: cmotion_ietruebody().scrollLeft,
	dsocy=(window.pageYOffset)? pageYOffset : cmotion_ietruebody().scrollTop,
	curposy=window.event? event.clientX : e.clientX? e.clientX: "";
	curposy-=mainobjoffset-dsocx;
	var leftbound=(cmotion_menuwidth-cmotion_restarea)/2;
	var rightbound=(cmotion_menuwidth+cmotion_restarea)/2;
	if (curposy>rightbound){
		cmotion_scrollspeed=(curposy-rightbound)/((cmotion_menuwidth-cmotion_restarea)/2) * cmotion_maxspeed;
		clearTimeout(cmotion_righttime);
		if (cmotion_movestate!="left") cmotion_moveleft();
	} else if (curposy<leftbound){
		cmotion_scrollspeed=(leftbound-curposy)/((cmotion_menuwidth-cmotion_restarea)/2) * cmotion_maxspeed;
		clearTimeout(cmotion_lefttime);
		if (cmotion_movestate!="right") cmotion_moveright();
	} else {
		cmotion_scrollspeed=0;
	}
}

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

function cmotion_stopmotion(e){
	if (!window.opera||(window.opera&&e.relatedTarget!==null))
	if ((window.event&&!cmotion_crossmain.contains(event.toElement)) || (e && e.currentTarget && e.currentTarget!= e.relatedTarget && !cmotion_contains_ns6(e.currentTarget, e.relatedTarget))){
		clearTimeout(cmotion_lefttime);
		clearTimeout(cmotion_righttime);
		cmotion_movestate="";
	}
}

function cmotion_stopmotion2(e){
	if (!window.opera||(window.opera&&e.relatedTarget!==null))
	if ((window.event&&!cmotion_crossmain.contains(event.toElement)) || (e && e.currentTarget && e.currentTarget!= e.relatedTarget && !cmotion_contains_ns6(e.currentTarget, e.relatedTarget))){
		clearTimeout(cmotion_lefttime);
		clearTimeout(cmotion_righttime);
		cmotion_movestate="";
	}
}

function cmotion_fillup(){
	if (iedom){
		cmotion_crossmain=document.getElementById? document.getElementById("cmotion_container") : document.all.cmotion_container;
		if(typeof cmotion_crossmain.style.cmotion_maxwidth!=='undefined') cmotion_crossmain.style.cmotion_maxwidth=cmotion_maxwidth+'px';
		cmotion_menuwidth=cmotion_crossmain.offsetWidth;
		cmotion_gallery=document.getElementById? document.getElementById("cmotion_gallery") : document.all.cmotion_gallery;
		cmotion_actualwidth=document.getElementById? document.getElementById("cmotion_trueContainer").offsetWidth : document.all['cmotion_trueContainer'].offsetWidth;
		if (cmotion_startpos) cmotion_gallery.style.left=(cmotion_menuwidth-cmotion_actualwidth)/cmotion_startpos+'px';
		cmotion_crossmain.onmousemove=function(e){ cmotion_engine(e); }
		cmotion_crossmain.onmouseout=function(e){ cmotion_stopmotion(e); }
		$('cmotion_right').onmouseout=function(e){ cmotion_stopmotion(e); }
	}
	cmotion_loadedyes=1
	if (document.body.filters) onresize()
}

window.onload=cmotion_fillup;

onresize=function(){
	if (typeof cmotion_container!=='undefined'&&cmotion_container.filters){
		cmotion_container.style.width="0";
		cmotion_container.style.width="";
		cmotion_container.style.width=Math.min(cmotion_container.offsetWidth, cmotion_maxwidth)+'px';
	}
	cmotion_menuwidth=cmotion_crossmain.offsetWidth;
	cmotion_gallery.style.left=cmotion_startpos? (cmotion_menuwidth-cmotion_actualwidth)/cmotion_startpos+'px' : 0;
}

//***************************************************************************************************************************************************

var cmotion_offsetxpoint=15 //Customize x offset of tooltip
var cmotion_offsetypoint=0 //Customize y offset of tooltip
var ie=document.all
var ns6=document.getElementById && !document.all
var cmotion_enabletip=false
if (ie||ns6)
var cmotion_tipobj=document.all? document.all["cmotion_tooltip"] : document.getElementById? document.getElementById("cmotion_tooltip") : ""


function cmotion_tip(thetext, thecolor, thewidth){
	if (ns6||ie){
		if (typeof thewidth!="undefined") cmotion_tipobj.style.width=thewidth+"px"
		if (typeof thecolor!="undefined" && thecolor!="") cmotion_tipobj.style.backgroundColor=thecolor
		cmotion_tipobj.innerHTML=thetext
		cmotion_enabletip=true
		return false
	}
}

function cmotion_positiontip(e){
	if (cmotion_enabletip){
		var curX=(ns6)?e.pageX : event.clientX+cmotion_ietruebody().scrollLeft;
		var curY=(ns6)?e.pageY : event.clientY+cmotion_ietruebody().scrollTop;
		//Find out how close the mouse is to the corner of the window
		var rightedge=ie&&!window.opera? cmotion_ietruebody().clientWidth-event.clientX-cmotion_offsetxpoint : window.innerWidth-e.clientX-cmotion_offsetxpoint-20
		var bottomedge=ie&&!window.opera? cmotion_ietruebody().clientHeight-event.clientY-cmotion_offsetypoint : window.innerHeight-e.clientY-cmotion_offsetypoint-20
		
		var leftedge=(cmotion_offsetxpoint<0)? cmotion_offsetxpoint*(-1) : -1000
		
		//if the horizontal distance isn't enough to accomodate the width of the context menu
		if (rightedge<cmotion_tipobj.offsetWidth) //move the horizontal position of the menu to the left by it's width
			cmotion_tipobj.style.left=ie? cmotion_ietruebody().scrollLeft+event.clientX-cmotion_tipobj.offsetWidth+"px" : window.pageXOffset+e.clientX-cmotion_tipobj.offsetWidth+"px"
		else if (curX<leftedge)
			cmotion_tipobj.style.left="5px"
		else 	//position the horizontal position of the menu where the mouse is positioned
			cmotion_tipobj.style.left=curX+cmotion_offsetxpoint+"px"
		
		//same concept with the vertical position
		if (bottomedge<cmotion_tipobj.offsetHeight)
			cmotion_tipobj.style.top=ie? cmotion_ietruebody().scrollTop+event.clientY-cmotion_tipobj.offsetHeight-cmotion_offsetypoint+"px" : window.pageYOffset+e.clientY-cmotion_tipobj.offsetHeight-cmotion_offsetypoint+"px"
		else
			cmotion_tipobj.style.top=curY+cmotion_offsetypoint+"px"
		cmotion_tipobj.style.visibility="visible"
	}
}

function cmotion_hidetip(){
	if (ns6||ie){
		cmotion_enabletip=false
		cmotion_tipobj.style.visibility="hidden"
		cmotion_tipobj.style.left="-1000px"
		cmotion_tipobj.style.backgroundColor=''
		cmotion_tipobj.style.width=''
	}
}