/***********************************************
* 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
***********************************************/
var crossmain='';
function mouseOverImage(par) {
	return true;
}

 //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=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 style="font-size: 11px;">End of Gallery</span>';
var endofgallerymsg='';

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;
var scrollspeed=0;
var movestate='';
//var actualwidth='';
var ns_scroll;
var statusdiv;
var loadedyes=0;
var lefttime;
var 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, "left"),
	menuheight=parseInt(crossmain.offsetHeight),
	mainobjoffsetH=getposOffset(crossmain, "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_wrapper(menuwidth, cross_scroll, actualwidth) {
  return function() {
    moveleft(menuwidth, cross_scroll, actualwidth);
  }
}


function moveleft(menuwidth, cross_scroll, actualwidth) {
// 	alert("moveleft(menuwidth="+menuwidth+", cross_scroll="+cross_scroll+", actualwidth="+actualwidth+")");
//  	console.log('moveleft:', cross_scroll.style.left);
	if (loadedyes) {
// 		console.log('moveleft: loadedyes');
		movestate="left";
//  		console.log('moveleft: before cross_scroll.style.left = ', cross_scroll.style.left);
//   		console.log('moveleft: menuwidth=', menuwidth, ', actualwidth=', actualwidth);
		if (iedom && parseInt(cross_scroll.style.left) > (menuwidth-actualwidth)) {
//  			console.log('moveleft: moving');
			cross_scroll.style.left=parseInt(cross_scroll.style.left)-scrollspeed+"px";
//  			console.log('moveleft: after cross_scroll.style.left = ', cross_scroll.style.left);
			showhidediv("hidden");
		}
		else
		 showhidediv("visible");
	}
//  	console.log('moveleft: setting again timer');
// 	alert("moveleft(menuwidth="+menuwidth+", cross_scroll="+cross_scroll+", actualwidth="+actualwidth+")");
	//lefttime=setTimeout(moveleft, 10, menuwidth, cross_scroll, actualwidth);
	// fucking explorer doesn't let me pass arguments. well, theorically neither gecko, but...
//	lefttime=setTimeout(moveleft, 10, menuwidth, cross_scroll, actualwidth);
	lefttime=setTimeout(moveleft_wrapper(menuwidth, cross_scroll, actualwidth), 10);
}

function moveright_wrapper(cross_scroll) {
  return function() {
    moveright(cross_scroll);
  }
}


function moveright(cross_scroll){
	if (loadedyes) {
		movestate="right";
		if (iedom&&parseInt(cross_scroll.style.left)<0) {
			cross_scroll.style.left=parseInt(cross_scroll.style.left)+scrollspeed+"px";
			showhidediv("hidden");
		}
		else
			showhidediv("visible");
	}
	// look at imprecations at the end of moveleft
// 	righttime=setTimeout(moveright, 10, cross_scroll);
	righttime=setTimeout(moveright_wrapper(cross_scroll), 10);
}

function motionengine(e, crossmain, menuwidth, cross_scroll, actualwidth) {
	var mainobjoffset = getposOffset(crossmain, "left");
	var dsocx         = (window.pageXOffset)? pageXOffset: ietruebody().scrollLeft;
	var dsocy         = (window.pageYOffset)? pageYOffset : ietruebody().scrollTop;
	var 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(menuwidth, cross_scroll, actualwidth);
	}
	else if (curposy<leftbound) {
		scrollspeed=(leftbound-curposy)/((menuwidth-restarea)/2) * maxspeed;
		clearTimeout(lefttime);
		if (movestate!="right")
			moveright(cross_scroll);
	}
	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, crossmain) {
	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);
			movestate="";
		}
}

/**
 * looks in the DOM for tags with given class attribute.
 *
 * @param  node:          node from which to start the search; use document if you want to search all document tree
 * @param  searchClass:   class to search for
 * @param  tag:           types of tag to restrict the search for (e.g., only "div")
 */
function getElementsByClass(node,searchClass,tag) {
	var classElements = new Array();
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	var i, j;
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}


/**
 * Force argument binding for function call. Usually javascript is lazy in argument binding, and look for the value as far as he can. e.g.:

  f = Array()
  for(var i = 0; i < 10; i++)
    f[i] = function() { alert('' + i); }

  ALL f[i] will alert with the same value, 9, because binding of "i" is done as late as possible. To force the binding immediatly:

  a = function(msg) { alert(msg); }
  f = Array()
  for(var i = 0; i < 10; i++)
    f[i] = BindArguments(a, '' + i);

  The "a" function is made because builtin functions could not have all attributes required by BindArguments.
 */
function BindArguments(fn)
{
	var args = [];
	for (var n = 1; n < arguments.length; n++)
		args.push(arguments[n]);
	return function () { return fn.apply(this, args); };
}

function motionengine_wrapper(crossmain, menuwidth, cross_scroll, actualwidth) {
//  	console.log('running wrapper: cross_scroll is a', typeof cross_scroll);
	return function(e) {
		motionengine(e, crossmain, menuwidth, cross_scroll, actualwidth);
	};
}

function motionstop_wrapper(crossmain) {
	return function(e) {
		stopmotion(e, crossmain);
		showhidediv("hidden");
	};
}

function fillup() {
	if (iedom) {
		var crossmains = getElementsByClass(document, "motioncontainer", "div");

		for(var crossmain_index = 0; crossmain_index < crossmains.length; crossmain_index++) {
// 			console.debug('index=', crossmain_index);
			var crossmain = crossmains[crossmain_index];

			if(typeof crossmain.style.maxWidth !== 'undefined')
				crossmain.style.maxWidth=maxwidth+'px';
			var menuwidth=crossmain.offsetWidth;

			var cross_scroll= getElementsByClass(crossmain, "motiongallery", "div");
			if(cross_scroll.length != 1)
				throw new Error('more than one cross_scroll child on a motioncontainer div');
			cross_scroll = cross_scroll[0];

			var true_container = getElementsByClass(crossmain, "trueContainer", "nobr");
			if(true_container.length != 1)
				throw new Error('not exactly one trueContainer child on a motioncontainer div: there are ' + true_container.length);
			true_container = true_container[0];

			var actualwidth= true_container.offsetWidth;
//  			console.log('setting actualwidth!', actualwidth);
			if (startpos)
				cross_scroll.style.left=(menuwidth-actualwidth)/startpos+'px';
	
			crossmain.onmousemove = motionengine_wrapper(crossmain, menuwidth, cross_scroll, actualwidth);
			crossmain.onmouseout = motionstop_wrapper(crossmain);
// 			crossmain.onmouseout  = function(e) {
// 				stopmotion(e, crossmain);
// 				showhidediv("hidden");
// 			}
		}
	}
	loadedyes=1
	if (endofgallerymsg!="") {
		creatediv();
		positiondiv();
	}
	if (document.body.filters)
		onresize();
}

old_onload = window.onload
window.onload = function() {
	if (old_onload)
		old_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.offsetWidth;
	//cross_scroll.style.left=startpos? (menuwidth-actualwidth)/startpos+'px' : 0;
}
