/***********************************************
* 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
***********************************************/
/*
* Modify 16-03-2008
* by object.live
*/
 //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=20;
 //3) Set to maximum width for gallery - must be less than the actual length of the image train.
var maxwidth=2000000;
 //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='';

var crossmain = new Array();
var cross_scroll = new Array();
var actualwidth = new Array();

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

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

var iedom=document.all||document.getElementById, scrollspeed=0, movestate='', ns_scroll, statusdiv, loadedyes=0, lefttime, righttime;

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

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

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

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

function moveright(i){
if (loadedyes){
movestate="right";
if (iedom&&parseInt(cross_scroll[i].style.left)<0){
	var lpos = parseInt(cross_scroll[i].style.left)+scrollspeed;
	if(lpos>0) lpos=0;
	cross_scroll[i].style.left = lpos+"px";
	showhidediv("hidden");	
}
else
	showhidediv("visible");
	//cross_scroll[i].style.left = "0px"
}
righttime=setTimeout("moveright("+i+")",10);
}

function motionengine(el, e){
	//alert(el.id);
	//for(var i=0; i<crossmain.length; i++){
		var n = parseInt( el.id.split("_")[1]);
		//alert(n)
		var mainobjoffset=getposOffset(el, "left"),
		dsocx=(window.pageXOffset)? pageXOffset: ietruebody().scrollLeft,
		dsocy=(window.pageYOffset)? pageYOffset : ietruebody().scrollTop,
		curposx=window.event? event.clientX : e.clientX? e.clientX: "";
		curposx-=mainobjoffset-dsocx;
		var leftbound=(menuwidth-restarea)/2;
		var rightbound=(menuwidth+restarea)/2;
		if (curposx>rightbound){
			scrollspeed=(curposx-rightbound)/((menuwidth-restarea)/2) * maxspeed;
			clearTimeout(righttime);
			if (movestate!="left") moveleft(n);
		}
		else if (curposx<leftbound){
			scrollspeed=(leftbound-curposx)/((menuwidth-restarea)/2) * maxspeed;
			clearTimeout(lefttime);
			if (movestate!="right") moveright(n);
		}
		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[0].contains(event.toElement)) || (e && e.currentTarget && e.currentTarget!= e.relatedTarget && !contains_ns6(e.currentTarget, e.relatedTarget))){
			clearTimeout(lefttime);
			clearTimeout(righttime);
			movestate="";
		}
}

function fillup(){
	//actualwidth=1330;	
	if (iedom){
		crossmain[0] = document.getElementById? document.getElementById("motioncontainer_0") : document.all.motioncontainer_0;
		cross_scroll[0]=document.getElementById? document.getElementById("motiongallery_0") : document.all.motiongallery_0;
		crossmain[1] = document.getElementById? document.getElementById("motioncontainer_1") : document.all.motioncontainer_1;
		cross_scroll[1]=document.getElementById? document.getElementById("motiongallery_1") : document.all.motiongallery_1;
		crossmain[2] = document.getElementById? document.getElementById("motioncontainer_2") : document.all.motioncontainer_2;
		cross_scroll[2]=document.getElementById? document.getElementById("motiongallery_2") : document.all.motiongallery_2;
		crossmain[3] = document.getElementById? document.getElementById("motioncontainer_3") : document.all.motioncontainer_3;
		cross_scroll[3]=document.getElementById? document.getElementById("motiongallery_3") : document.all.motiongallery_3;
		//crossmain[4] = document.getElementById? document.getElementById("motioncontainer_4") : document.all.motioncontainer_4;
		//cross_scroll[4]=document.getElementById? document.getElementById("motiongallery_4") : document.all.motiongallery_4;
		
		for(var i=0; i<crossmain.length; i++){
			//alert(cross_scroll[i].innerHTML)
			actualwidth[i] = parseInt( cross_scroll[i].getElementsByTagName("UL")[0].style.width );
			//alert(actualwidth[i]);
			
			if(typeof crossmain[i].style.maxWidth!=='undefined') crossmain[0].style.maxWidth=maxwidth+'px';
			menuwidth=crossmain[i].offsetWidth;
			//alert(menuwidth)
			if (startpos) cross_scroll[i].style.left=(menuwidth-actualwidth[i])/startpos+'px';
			crossmain[i].onmousemove=function(e){
				motionengine(this, e);
			};
			crossmain[i].onmouseout=function(e){
				stopmotion(e);
				showhidediv("hidden");
			}
		}
		
		
		//actualwidth=document.getElementById? document.getElementById("trueContainer").offsetWidth : document.all['trueContainer'].offsetWidth;
		//??????????????????????????? ????????????????????? firefox (Fix Size area for Firefox Scrolling)
	}
	loadedyes=1
	if (endofgallerymsg!=""){
		creatediv();
		positiondiv();
	}
	if (document.body.filters)
	onresize()
}
window.onload=fillup;

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