//REQUIRES _JS_GENERAL/garwin.dom.js
//REQUIRES jquery-latest.js
/***********************************************
* CMotion Image Gallery II- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* Visit http://www.dynamicDrive.com for source code
* Modified by jscheuer1 for vertical orientation, at http://www.dynamicDrive.com/forums
*http://www.dynamicdrive.com/dynamicindex4/cmotiongallery2.htm
***********************************************/
var motiongallery = function(i_restarea,i_maxspeed,i_endofgallerymsg) {
//DEFAULT
//FOR BOTH X,Y
this.restarea=200; //1) width of the "neutral" area in the center of the gallery in px
this.maxspeed=7; //2) top scroll speed in pixels. Script auto creates a range from 0 to top speed.
this.endofgallerymsg="<span style='font-size: 11px'>End of Gallery</span>"; //3) message to show at end of gallery. Enter "" to disable message.
this.startpos=2; //4) Set to 1 for left start, 0 for right, 2 for center.
this.direction = "vertical";//horizontal or vertical will be defined during init depending if "nobr"(horizontal) tag is present or not)
//FOR X
this.maxwidth=1000;//4) Set to maximum width for gallery - must be less than the actual length of the image train.
/////////////
if(i_restarea!=null)
	this.restarea = i_restarea;
if(i_maxspeed!=null)
	this.maxspeed = i_maxspeed;
if(i_endofgallerymsg!=null)
	this.endofgallerymsg = i_endofgallerymsg;

////NO NEED ASSIGN EXTERNAL ARGUMENTS BELOW THIS LINE////////////
this.iedom=document.all||document.getElementById;
this.scrollspeed=0;
this.movestate="";

this.crossmotioncontainer;
this.crossmotiongallery;

// Y SLIDER
this.motioncontainer_heightoffset2body='';
this.motioncontainer_height='';
this.motiongallery_height='';

//	X SLIDER
this.motioncontainer_widthoffset2body='';
this.motioncontainer_width='';
this.motiongallery_width='';
this.actualwidthmotiongallery;

this.loadedyes=0;

this.statusdiv;
}
motiongallery.prototype = {
ietruebody:function(){
	return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
},
//////the following for this.endofgallerymsg//////////////////////////////////////////////
creatediv:function(){
	this.statusdiv=document.createElement("div");
	this.statusdiv.setAttribute("id","statusdiv");
	document.body.appendChild(this.statusdiv);
	this.statusdiv=document.getElementById("statusdiv");
	this.statusdiv.innerHTML=this.endofgallerymsg;
},
positiondiv:function(){
		if(this.direction == "horizontal"){
			this.statusdiv.style.left=this.motioncontainer_heightoffset2body+(this.motioncontainer_width/2)-(this.statusdiv.offsetWidth/2)+"px";
			this.statusdiv.style.top=this.motioncontainer_height+this.motioncontainer_widthoffset2body+"px";
		}
		else{
			this.statusdiv.style.left=this.motioncontainer_widthoffset2body+(this.motioncontainer_width/2)-(this.statusdiv.offsetWidth/2)+"px";
			this.statusdiv.style.top=(this.motioncontainer_height/2)-(this.statusdiv.offsetHeight/2)+this.motioncontainer_heightoffset2body+"px";
		}
},
showhidediv:function(what){
	if (this.endofgallerymsg!=""){
		this.positiondiv();	//NEEDED IN CASE WINDOWS SIZE CHANGES I THINK 
		this.statusdiv.style.visibility=what;
	}
},
//////END the following for this.endofgallerymsg END//////////////////////////////////////////////
moveup:function(){
	if (this.loadedyes){
		this.movestate="up";
		if (this.iedom&&parseInt(this.crossmotiongallery.style.top)>(this.motioncontainer_height-this.motiongallery_height))
		{
			this.crossmotiongallery.style.top=parseInt(this.crossmotiongallery.style.top)-this.scrollspeed+"px";
			this.showhidediv("hidden");
		}
		else
		{
			//this.positiondivY();	
			this.showhidediv("visible");
		}
	}
	var _this = this;
	uptime=setTimeout(function(){_this.moveup()},10);
},
movedown:function(){
	if (this.loadedyes){
		this.movestate="down";
		if (this.iedom&&parseInt(this.crossmotiongallery.style.top)<0)
		{
			this.crossmotiongallery.style.top=parseInt(this.crossmotiongallery.style.top)+this.scrollspeed+"px";
			this.showhidediv("hidden");
		}
		else
		{
			//this.positiondivY();	
			this.showhidediv("visible");
		}
	}
	var _this = this;
	downtime=setTimeout(function(){_this.movedown()},10);
},
moveleft:function(){
	if(this.loadedyes){
		this.movestate="left";
		if (this.iedom&&parseInt(this.crossmotiongallery.style.left)>(this.motioncontainer_width-this.actualwidthmotiongallery)){
			this.crossmotiongallery.style.left=parseInt(this.crossmotiongallery.style.left)-this.scrollspeed+"px";
			this.showhidediv("hidden");
		}
		else
			this.showhidediv("visible");
	}
	var _this = this;
	lefttime=setTimeout(function(){_this.moveleft()},10);
	//lefttime=setTimeout("moveleft()",10);
},
moveright:function(){
	if (this.loadedyes){
		this.movestate="right";
		if (this.iedom&&parseInt(this.crossmotiongallery.style.left)<0){
			this.crossmotiongallery.style.left=parseInt(this.crossmotiongallery.style.left)+this.scrollspeed+"px";
			this.showhidediv("hidden");
		}
		else
			this.showhidediv("visible");
	}
	var _this = this;
	righttime=setTimeout(function(){_this.moveright()},10);	
	//righttime=setTimeout("moveright()",10);
},

/////////////BUTTON MOVE UP/DOWN BY ME////////////////////////////////
buttonmotionengine:function(e){
	this.scrollspeed=this.maxspeed;
	if (window.uptime) clearTimeout(uptime);

	if (this.movestate=="down") 
		this.movedown();
	else if(this.movestate=="up")
		this.moveup();
},
mousemotionengine:function(e){
	//document.getElementById("debugging").innerHTML += "mousemotionengine//////////////////////////////////<br>";
	if(this.direction == "vertical")
	{
		var dsocy=(window.pageYOffset)? pageYOffset : this.ietruebody().scrollTop;
		var withinmotioncontainermouseposy=(garwin.dom.positions.getmousepos(e).Y+dsocy)-this.motioncontainer_heightoffset2body;//OLD curposy more less ;)
		///
		var leftbound=(this.motioncontainer_height-this.restarea)/2;
		var rightbound=(this.motioncontainer_height+this.restarea)/2;
		
		if (withinmotioncontainermouseposy>rightbound){
			//document.getElementById("debugging").innerHTML += "withinmotioncontainermouseposyrightbound<br>";
			this.scrollspeed=(withinmotioncontainermouseposy-rightbound)/((this.motioncontainer_height-this.restarea)/2) * this.maxspeed;
			//document.getElementById("debugging").innerHTML += "this.scrollspeed="+this.scrollspeed+"<br>";
			if (window.downtime) clearTimeout(downtime);
			if (this.movestate!="up") this.moveup();
		}
		else if (withinmotioncontainermouseposy<leftbound){
			//document.getElementById("debugging").innerHTML += "withinmotioncontainermouseposyleftbound<br>";		
			this.scrollspeed=(leftbound-withinmotioncontainermouseposy)/((this.motioncontainer_height-this.restarea)/2) * this.maxspeed;
			//document.getElementById("debugging").innerHTML += "this.scrollspeed="+this.scrollspeed+"<br>";
			if (window.uptime) clearTimeout(uptime);
			if (this.movestate!="down")this.movedown();
		}
		else
			this.scrollspeed=0;
		//document.getElementById("debugging").innerHTML += "mousemotionengine//////END////////////////////////////<br>";
	}
	else
	{
		var dsocx=(window.pageXOffset)? pageXOffset: this.ietruebody().scrollLeft;
		var withinmotioncontainermouseposx=garwin.dom.positions.getmousepos(e).X-this.motioncontainer_widthoffset2body-dsocx;
				
		///
		var leftbound=(this.motioncontainer_width-this.restarea)/2;
		var rightbound=(this.motioncontainer_width+this.restarea)/2;
				
		if (withinmotioncontainermouseposx>rightbound){
			this.scrollspeed=(withinmotioncontainermouseposx-rightbound)/((this.motioncontainer_width-this.restarea)/2) * this.maxspeed;
			clearTimeout(window.righttime);
			if (this.movestate!="left") this.moveleft();
		}
		else if (withinmotioncontainermouseposx<leftbound){
			this.scrollspeed=(leftbound-withinmotioncontainermouseposx)/((this.motioncontainer_width-this.restarea)/2) * this.maxspeed;
			clearTimeout(window.lefttime);
			if (this.movestate!="right") this.moveright();
		}
		else
			this.scrollspeed=0;
	}
},
/////////////BUTTON MOVE UP/DOWN BY ME///END/////////////////////////////	
stopmotion:function(e){
	if (!window.opera||(window.opera&&e.relatedTarget!==null))
	if ((window.event&&!this.crossmotioncontainer.contains(event.toElement)) || (e && e.currentTarget && e.currentTarget!= e.relatedTarget && !this.contains_ns6(e.currentTarget, e.relatedTarget))){
		if (window.downtime) clearTimeout(downtime);
		if (window.uptime) clearTimeout(uptime);
		if (window.lefttime) clearTimeout(lefttime);
		if (window.righttime) clearTimeout(righttime);		
		this.movestate="";
	}
},
contains_ns6:function(a, b) {
	while (b.parentNode)
	if ((b = b.parentNode) == a)
	return true;
	return false;
},
setmotiongallery2ctrlposition:function(i_targetctrlid){
	var tc = document.getElementById(i_targetctrlid);
	tc.style.borderColor = "#BBB";
	var tcoffsetTop = parseInt(tc.offsetTop);
	//alert("i_targetctrlid="+i_targetctrlid+" tcoffsetTop="+tcoffsetTop);
	if(tcoffsetTop>0)
	{
		var tcoffsetHeight = parseInt(tc.offsetHeight);	
		//alert("tcoffsetHeight="+tcoffsetHeight);		
		var centeroffset = Math.round((this.motioncontainer_height/2)-(tcoffsetHeight/2));
		//alert("centeroffset="+centeroffset);
		//alert(document.getElementById("motiongallery").style.top);
		document.getElementById("motiongallery").style.top=-(tcoffsetTop-centeroffset)+"px";
		//alert(document.getElementById("motiongallery").style.top);
	}
},
resize:function(){
	if (typeof this.crossmotioncontainer!=='undefined'&&this.crossmotioncontainer.filters){
		this.crossmotioncontainer.style.width="0";
		this.crossmotioncontainer.style.width="";
		//document.getElementById("debugging").innerHTML += "this.crossmotioncontainer.offsetWidth="+this.crossmotioncontainer.offsetWidth+"<br>";
		//document.getElementById("debugging").innerHTML += "this.maxwidth="+this.maxwidth+"<br>";
		this.crossmotioncontainer.style.width=Math.min(this.crossmotioncontainer.offsetWidth, this.maxwidth)+'px';
		//document.getElementById("debugging").innerHTML += "this.crossmotioncontainer.style.width="+this.crossmotioncontainer.style.width+"<br>";
	}
	this.motioncontainer_width=this.crossmotioncontainer.offsetWidth;
	this.crossmotiongallery.style.left=this.startpos? (this.motioncontainer_width-this.actualwidthmotiongallery)/this.startpos+'px' : 0;
	//document.getElementById("debugging").innerHTML += "this.crossmotiongallery.style.left="+this.crossmotiongallery.style.left+"<br>";
},
init: function() {
	//alert("init");
	//document.getElementById("debugging").innerHTML += "init//////////////////////////////////<br>";
	//document.getElementById("debugging").innerHTML += "this.iedom="+this.iedom+"<br>";
	if (this.iedom){
	//	alert("init2");
		this.crossmotioncontainer=document.getElementById? document.getElementById("motioncontainer") : document.all.motioncontainer;
		if(this.crossmotioncontainer==null){return;}
		this.crossmotiongallery=document.getElementById? document.getElementById("motiongallery") : document.all.motiongallery;
		if(this.crossmotiongallery==null){return;}
		//////X
		var NoBrMotionGallery = document.getElementById? document.getElementById("trueContainer") : document.all['trueContainer'];
		if(NoBrMotionGallery!=null)
		{
			this.actualwidthmotiongallery=document.getElementById? document.getElementById("trueContainer").offsetWidth : document.all['trueContainer'].offsetWidth;
			this.direction = "horizontal";
		}

		// Y SLIDER
		this.motioncontainer_height=parseInt(this.crossmotioncontainer.offsetHeight);//USED TO BE this.motioncontainer_height=parseInt(this.crossmotioncontainer.style.height);
		this.motioncontainer_heightoffset2body = garwin.dom.positions.getctrlpos2(this.crossmotioncontainer).Y;
		this.motiongallery_height=parseInt(this.crossmotiongallery.offsetHeight);
				
		//	X SLIDER
		this.motioncontainer_width=parseInt(this.crossmotioncontainer.offsetWidth);
		this.motioncontainer_widthoffset2body=garwin.dom.positions.getctrlpos2(this.crossmotioncontainer).X;
		this.motiongallery_width=parseInt(this.crossmotiongallery.offsetWidth);

		//document.getElementById("debugging").innerHTML += "this.crossmotiongallery.style.left1="+this.crossmotiongallery.style.left+"<br>";
			
		if(this.direction == "horizontal")
		{
			this.maxwidth = this.motioncontainer_width; // SET MAX WIDTH TO INITIATE WIDTH BY ME
			
			if(typeof this.crossmotioncontainer.style.maxWidth!=='undefined')
				this.crossmotioncontainer.style.maxWidth=this.maxwidth+'px';
			if(this.startpos){
				this.crossmotiongallery.style.left=(this.motioncontainer_width-this.actualwidthmotiongallery)/this.startpos+'px';
				//document.getElementById("debugging").innerHTML += "this.crossmotiongallery.style.left2="+this.crossmotiongallery.style.left+"<br>";
			}
		}
		//document.getElementById("debugging").innerHTML += "this.motioncontainer_height="+this.motioncontainer_height+"<br>";
		//document.getElementById("debugging").innerHTML += "this.motioncontainer_heightoffset2body="+this.motioncontainer_heightoffset2body+"<br>";
		//document.getElementById("debugging").innerHTML += "this.motiongallery_height="+this.motiongallery_height+"<br>";

		//document.getElementById("debugging").innerHTML += "this.motioncontainer_width"+this.motioncontainer_width+"<br>";
		//document.getElementById("debugging").innerHTML += "this.motioncontainer_widthoffset2body="+this.motioncontainer_widthoffset2body+"<br>";
		//document.getElementById("debugging").innerHTML += "this.motiongallery_width="+this.motiongallery_width+"<br>";

		var _this = this;
		this.crossmotioncontainer.onmousemove=function(e){
			_this.mousemotionengine(e);
		}
		this.crossmotioncontainer.onmouseout=function(e){
			_this.stopmotion(e);
			_this.showhidediv("hidden");
		}
		/////////////BUTTON MOVE UP/DOWN BY ME////////////////////////////////
		var MoveButtonDown = document.getElementById("motionbuttondown");
		if(MoveButtonDown)
		{
			MoveButtonDown.onmouseover= function(e){
				_this.movestate="down";
				_this.buttonmotionengine(e);
			}
			MoveButtonDown.onmouseout=function(e){
				_this.stopmotion(e);
				_this.showhidediv("hidden");
			}
		}
		var MoveButtonUp = document.getElementById("motionbuttonup");
		if(MoveButtonUp)
		{
			MoveButtonUp.onmouseover= function(e){
				_this.movestate="up";	
				_this.buttonmotionengine(e);
			}
			MoveButtonUp.onmouseout=function(e){
				_this.stopmotion(e);
				_this.showhidediv("hidden");
			}
		}
		/////////////BUTTON MOVE UP/DOWN BY ME////END////////////////////////////
	}
	if (window.opera){//TO TEST
		this.crossmotiongallery.style.top=this.motioncontainer_height-this.motiongallery_height+'px';
		setTimeout('this.crossmotiongallery.style.top=0', 10);
	}
	this.loadedyes=1
	if (this.endofgallerymsg!=""){
		this.creatediv();
		this.positiondiv();
	}
	if (document.body.filters&&this.direction == "horizontal")
			garwin.events.AddEvent(window,'resize',garwin.createDelegate(this, this.resize), true);
}
};

var myMotionGallery = new motiongallery(null,null,null);
if(navigator.userAgent.indexOf('MSIE')!=-1)
	$(document).ready(function(){myMotionGallery.init();});
else
	garwin.events.AddEvent(window,'load',function(){myMotionGallery.init();}, false);	

//window.onload=function(){myMotionGallery.init();};
//$(document).ready(function(){myMotionGallery.init();});

