/***********************************************
* 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 restarea=20;
 //2) Set top scroll speed in pixels. Script auto creates a range from 0 to top speed.
var maxspeed=7;
 //3) Set to maximum width for gallery - must be less than the actual length of the image train.
var maxwidth=1000;
 //4) Set to 1 for left start, 0 for right, 2 for center.
var startpos=0;
 //5) Set message to show at end of gallery. Enter "" to disable message.
var endofgallerymsg='<span>end</span>';

function enlargeimage(path, optWidth, optHeight){ //function to enlarge image. Change as desired.
	var actualheight=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="+actualheight+",height="+actualHeight+",resizable=yes"
	window.open(path,"", winattributes)
}

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

var iedom=document.all||document.getElementById, scrollspeed=0, movestate='', actualheight='', cross_scroll, ns_scroll, statusdiv, loadedyes=0, toptime, bottomtime;

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

function creatediv(){
}

function positiondiv(){
	var mainobjoffset=getposOffset(crossmain, "top"),
	menuheight=parseInt(crossmain.offsetHeight),
	mainobjoffsetH=getposOffset(crossmain, "top");
}

function showhidediv(what){
	if (endofgallerymsg!="") {
		positiondiv();
	}
}

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


function movetop(){
	if (loadedyes){
		movestate="top";
	var diff = ( (menuheight-actualheight)-parseInt($(cross_scroll).css('top')) );
	if (iedom && parseInt($(cross_scroll).css('top'))>(menuheight-actualheight)){
		var sspeed = scrollspeed;
		if(Math.abs(diff)<scrollspeed) sspeed=Math.abs(diff);
		$(cross_scroll).css('top', parseInt($(cross_scroll).css('top'))-sspeed+"px");
		showhidediv("hidden");
	}
	else
		showhidediv("visible");
	}
		toptime=setTimeout("movetop()",10);
}

function movebottom(){
	if (loadedyes){
		movestate="bottom";
	if (iedom&&parseInt($(cross_scroll).css('top'))<0){
		var sspeed = scrollspeed;
		if($(cross_scroll).css('top')<scrollspeed) sspeed=$(cross_scroll).css('top');
		$(cross_scroll).css('top', parseInt($(cross_scroll).css('top'))+sspeed+"px");
		showhidediv("hidden");
	}
	else
		showhidediv("visible");
	}
	bottomtime=setTimeout("movebottom()",10);
}

function motionengine(e){
	crossmain.style.top="0px";
	var mainobjoffset=getposOffset(crossmain, "top"),
	dsocx=(window.pageXOffset)? pageXOffset: ietruebody().scrollLeft,
	dsocy=(window.pageYOffset)? pageYOffset : ietruebody().scrollTop,
	curposy=window.event? event.clientY : e.clientY? e.clientY: "";
	curposy-=mainobjoffset-dsocy;
	var topbound=(menuheight-restarea)/2;
	var bottombound=(menuheight+restarea)/2;
	if (curposy>bottombound){
		scrollspeed=(curposy-bottombound)/((menuheight-restarea)/2) * maxspeed;
		clearTimeout(bottomtime);
		if (movestate!="top") movetop();
	}
	else if (curposy<topbound){
		scrollspeed=(topbound-curposy)/((menuheight-restarea)/2) * maxspeed;
		clearTimeout(toptime);
		if (movestate!="bottom") movebottom();
	}
	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){
	if (!window.opera||(window.opera&&e.relatedTarget!==null))
		if ((window.event&&!crossmain.contains(event.toElement)) || (e && e.currentTarget && e.currentTarget!= e.relatedTarget && !contains_ns6(e.currentTarget, e.relatedTarget))){
			clearTimeout(toptime);
			clearTimeout(bottomtime);
			movestate="";
		}
}

function fillup(){
	if (iedom){
		crossmain=document.getElementById? document.getElementById("motioncontainer") : document.all.motioncontainer;
		if(typeof crossmain.style.maxHeight!=='undefined')
			crossmain.style.maxHeight=maxwidth+'px';
		menuheight=crossmain.offsetHeight;
		cross_scroll=document.getElementById? document.getElementById("motiongallery") : document.all.motiongallery;
		actualheight=document.getElementById? document.getElementById("trueContainer").offsetHeight : document.all['trueContainer'].offsetHeight;
		if (startpos)
			$(cross_scroll).css('top', (menuheight-actualheight)/startpos+'px');
		crossmain.onmousemove=function(e){
			motionengine(e);
		}
		
		crossmain.onmouseout=function(e){
			stopmotion(e);
			showhidediv("hidden");
		}
	}
	loadedyes=1
	if (endofgallerymsg!=""){
		creatediv();
		positiondiv();
	}
	if (document.body.filters)
		onresize()
}
window.onload=fillup;

onresize = function(){
	if (typeof motioncontainer!=='undefined'&&motioncontainer.filters){
		//motioncontainer.style.height="0";
		$(cross_scroll).css('height', '0');
		//motioncontainer.style.height="";
		$(cross_scroll).css('height', '');
		//motioncontainer.style.height=Math.min(motioncontainer.offsetHeight, maxwidth)+'px';
		$(cross_scroll).css('height', Math.min(motioncontainer.offsetHeight, maxwidth)+'px');
	}
	menuheight=crossmain.offsetHeight;
	$(cross_scroll).css('top', startpos ? (menuheight-actualheight)/startpos+'px' : 0 );
}
