/***********************************************
* Based on CMotion Image Gallery-  Dynamic Drive DHTML code library (www.dynamicdrive.com)
* Visit http://www.dynamicDrive.com for original CMotion source code
* This copyright notice must stay intact for legal use
* Modified for dynamic containers, horizontal and vertical options
* Carl Russell 2008
***********************************************/

 //1) Set width of the "neutral" area in the center of the gallery.
var restarea=6;
 //2) Set top scroll speed in pixels. Script auto creates a range from 0 to top speed.
var maxspeed=12;
 //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 style="font-size: 10px;">End of Gallery</span>';

var trueContainer = "trueContainer";
var motiongallery = "photo-nav";
var motioncontainer = "motioncontainer";
var galType = 'H';
var cross_scroll;
var startDiv;
var endDiv;

////NO NEED TO EDIT BELOW THIS LINE////////////

var iedom=document.all||document.getElementById, scrollspeed=0, movestate='', actualwidth='', cross_scroll, ns_scroll, statusdiv, loadedyes=0, lefttime, righttime;

function ietruebody(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
}

function creatediv(){
startDiv = document.createElement("div");
endDiv = document.createElement("div");
startDiv.setAttribute("id","statusS");
endDiv.setAttribute("id","statusE");
crossmain.appendChild(startDiv);
crossmain.appendChild(endDiv);
}

function showhidediv(statPos,status){
if (endofgallerymsg!="") {
if (statPos == 'E' && status == 'off') {
	startDiv.style.backgroundPosition="right top";
	endDiv.style.backgroundPosition="left top";
} else if(statPos == 'S' && status == 'off') {
	startDiv.style.backgroundPosition="left top";
	endDiv.style.backgroundPosition="right top";
} else {
	startDiv.style.backgroundPosition="left top";
	endDiv.style.backgroundPosition="left top";
}
}
}

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 (loadedyes){
	movestate="left";
	if (iedom&&parseInt(cross_scroll.style.left)>(menuwidth-actualwidth)){
	cross_scroll.style.left=parseInt(cross_scroll.style.left)-scrollspeed+"px";
	showhidediv("S","on");
	}
	else
	showhidediv("S","off");
	}
	lefttime=setTimeout("moveleft()",5);
}

function moveright(){
	if (loadedyes){
	movestate="right";
	if (iedom&&parseInt(cross_scroll.style.left)<0){
	cross_scroll.style.left=parseInt(cross_scroll.style.left)+scrollspeed+"px";
	showhidediv("E","on");
	}
	else
	showhidediv("E","off");
	}
	righttime=setTimeout("moveright()",5);
}

function moveup(){
	if (loadedyes){
	movestate="up"
	if (iedom&&parseInt(cross_scroll.style.top)>(menu_height-actualheight)){
	cross_scroll.style.top=parseInt(cross_scroll.style.top)-scrollspeed+"px"
	showhidediv("S","on");
	}
	else
	showhidediv("S","off");
	}
	uptime=setTimeout("moveup()",5)
}

function movedown(){
	if (loadedyes){
	movestate="down"
	if (iedom&&parseInt(cross_scroll.style.top)<0){
	cross_scroll.style.top=parseInt(cross_scroll.style.top)+scrollspeed+"px"
	showhidediv("E","on")
	}
	else
	showhidediv("E","off")
	}
	downtime=setTimeout("movedown()",5)
}

function motionengine(e){
	var mainobjoffset=getposOffset(crossmain, "left"),
	dsocx=(window.pageXOffset)? pageXOffset: ietruebody().scrollLeft,
	dsocy=(window.pageYOffset)? pageYOffset : ietruebody().scrollTop,
	curposy=window.event? event.clientX : e.clientX? e.clientX: "";
	curposy-=mainobjoffset-dsocx;
	var leftbound=(menuwidth-restarea)/2;
	var rightbound=(menuwidth+restarea)/2;
	if (curposy>rightbound){
	scrollspeed=(curposy-rightbound)/((menuwidth-restarea)/2) * maxspeed;
	clearTimeout(righttime);
	if (movestate!="left") moveleft();
	}
	else if (curposy<leftbound){
	scrollspeed=(leftbound-curposy)/((menuwidth-restarea)/2) * maxspeed;
	clearTimeout(lefttime);
	if (movestate!="right") moveright();
	}
	else
	scrollspeed=0;
	
}
function motionengineV(e){
	var dsocx=(window.pageXOffset)? pageXOffset: ietruebody().scrollLeft;
	var dsocy=(window.pageYOffset)? pageYOffset : ietruebody().scrollTop;
	var curposy=window.event? event.clientY : e.clientY? e.clientY: ""
	curposy-=mainobjoffset-dsocy
	var leftbound=(menu_height-restarea)/2
	var rightbound=(menu_height+restarea)/2
	if (curposy>rightbound){
	scrollspeed=(curposy-rightbound)/((menu_height-restarea)/2) * maxspeed
	if (window.downtime) clearTimeout(downtime)
	if (movestate!="up") moveup()
	}
	else if (curposy<leftbound){
	scrollspeed=(leftbound-curposy)/((menu_height-restarea)/2) * maxspeed
	if (window.uptime) clearTimeout(uptime)
	if (movestate!="down") movedown()
	}
	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(lefttime)
clearTimeout(righttime)
if (window.downtime) clearTimeout(downtime)
if (window.uptime) clearTimeout(uptime)
movestate="";
}
}

function galfill(){
	var cnt = document.getElementById(motioncontainer);
	if (!cnt) {	var cnt = document.getElementById(motioncontainer+'V'); galType = 'V'; motioncontainer='motioncontainerV'}
	var ele = document.getElementById(motiongallery);
		if (!ele || !cnt) {
			return;
		}
	if (galType == 'H') {
		cnt.style.overflow = 'hidden';
		ele.style.position = 'absolute';
		ele.style.whiteSpace = 'nowrap';
		if (iedom){
		crossmain=document.getElementById? document.getElementById(motioncontainer) : document.all.motioncontainer;
		if(typeof crossmain.style.maxWidth!=='undefined')
		crossmain.style.maxWidth=maxwidth+'px';
		menuwidth=crossmain.offsetWidth;
		cross_scroll=document.getElementById? document.getElementById(motiongallery) : document.all.motiongallery;
		actualwidth=document.getElementById? document.getElementById(trueContainer).offsetWidth : document.all[trueContainer].offsetWidth;
		if (startpos) {
			var icounter = $('fmrk').getWidth();
			cross_scroll.style.left= Math.max(menuwidth-actualwidth,-(icounter*(startpos - 1)))+'px';
			//cross_scroll.style.left=(menuwidth-actualwidth)/startpos+'px';
			//alert(startpos);
		}
		crossmain.onmousemove=function(e){
		motionengine(e);
		}
		
		crossmain.onmouseout=function(e){
		stopmotion(e);
		showhidediv("hidden");
		}
		}
	} else {
		cnt.style.overflow = 'hidden';
		ele.style.position = 'absolute';
		if (iedom){
		crossmain=document.getElementById? document.getElementById(motioncontainer) : document.all.motioncontainer;
		menu_height=parseInt(crossmain.style.height)
		mainobjoffset=getposOffset(crossmain, "top")
		if(typeof crossmain.style.maxWidth!=='undefined')
		crossmain.style.maxWidth=maxwidth+'px';
		menuwidth=crossmain.offsetWidth;
		cross_scroll=document.getElementById? document.getElementById(motiongallery) : document.all.motiongallery;
		actualwidth=document.getElementById? document.getElementById(trueContainer).offsetWidth : document.all[trueContainer].offsetWidth;
		actualheight=cross_scroll.offsetHeight

		crossmain.onmousemove=function(e){
		motionengineV(e);
		}
		
		crossmain.onmouseout=function(e){
		stopmotion(e);
		showhidediv("hidden");
		}
		}
	}
	loadedyes=1
	if (endofgallerymsg!=""){
		creatediv();
	}
	if (document.body.filters)
	onresize()
	if (startpos) {
		if (galType == 'H') {
			var icounter = $('fmrk').getWidth();
			cross_scroll.style.left= Math.max(menuwidth-actualwidth,-(icounter*(startpos - 1)))+'px';
		} else {
			var icounter = $('fmrk').getHeight();
			cross_scroll.style.top= Math.max(menu_height-actualheight,-(icounter*(startpos - 1)))+'px';
		}
		startpos = false;
	}
}

onresize=function(){
	if (galType == 'V') {
		return;
	}
	var cnt = document.getElementById(motioncontainer);
	var ele = document.getElementById(motiongallery);
		if (!ele || !cnt) {
			return;
		}
	if (typeof motioncontainer!=='undefined'&&motioncontainer.filters){
	motioncontainer.style.width="0";
	motioncontainer.style.width="";
	motioncontainer.style.width=Math.min(motioncontainer.offsetWidth, maxwidth)+'px';
	}
	menuwidth=crossmain.offsetWidth;
	cross_scroll.style.left=startpos? (menuwidth-actualwidth)/startpos+'px' : 0;
}