/**
 * NengaMotiv2
 * 
 * args:
 * 	selname
 * 	direction: 1: W to E, -1: E to W
 * 	animateY
 *	top: display y position
 * 	initialX: initial x position
 * 	targetY
 * 	range
 * 	holdpx
 * 	range2
 *  disabled
 * 	d
 */var NengaMotiv2 = function (args) {
	var _args = args,
		_ymax = args.targetY + args.range;
	var $_element = $(args.selname);
	var _pos = null;
	var _target = null;
	
	_args.animateY = args.animateY||false;
	_args.d = args.d||false;
	_args.disabled = args.disabled||false;

	/**
	 * init
	 */
	var init = function () {
		_target = {x: parseInt($_element.css("marginLeft")), y: _args.targetY};
		_pos = {x: _args.initialX, y: parseInt($_element.css("marginTop"))};
		$_element.css({marginLeft: (_args.initialX + "px"), marginTop: (_args.top+"px")});
dPut("NengaMotiv2: " + args.selname + ": ymax=" + _ymax + ", pos=" + _pos.x + "," + _pos.y + " " + $_element.width() + "x" + $_element.height());
	};
	
	/**
	 * calcPos
	 */
	var calcPos = function (scrollY, ticks, tickpct) {
		var px=0, py=0, v=true;
		
		var calcY = function (px) {
			px = 360*(px / 1200);
			return _args.top + Math.floor(Math.sin(px * Math.PI/180)*40);
		};
		
		var minx = -($_element.width());
		if (scrollY < (_target.y - _args.holdpx)) {
			var range2 = _args.holdpx + _args.range2;
			var target2 = _target.y - _args.holdpx;
			
			var ratio = (range2 - (scrollY - target2)) / range2;
			px  = _pos.x - ((_pos.x - _target.x) * ratio);
			if (_args.direction > 0) {
				if (px < minx) px = minx;
			}
			else {
				if (px > 1200) px = 1200;
			}
			px = Math.floor(px);
			v = (px > minx && px < 1200);
			if (_args.animateY) py = calcY(px);
			
if (_args.d) {
dPut(
"*" + scrollY + "**" + (-$_element.width())
+ ": target=[" + _target.x + "," + _target.y + "]"
+ " ymax=" + _ymax
+ ", px=" + px + "(" + ((px/1200)*10) + "), " + py
+ ", ratio=" + ratio
+ ", v=" + v
);
}
			return {x:px, y:py, v:v};
		}

		var ratio = (_args.range - (scrollY - _target.y)) / _args.range;
		px  = _pos.x - ((_pos.x - _target.x) * ratio);
		if (_args.direction > 0) {
			if (px < _target.x) px = _target.x;
		}
		else {
			if (px > _target.x) px = _target.x;
		}
		px = Math.floor(px);
		v = (px > minx && px < 1200);
		if (_args.animateY) py = calcY(px);
if (_args.d) {
dPut(
" " + scrollY
+ ", dir=" + _args.direction
+ ": target=[" + _target.x + "," + _target.y + "]"
+ " ymax=" + _ymax
+ ", px=" + px + "(" + ((px/1200)*10) + "), " + py
+ ", ratio=" + ratio
+ ", v=" + v
);
}
		return {x:px, y:py, v:v};
	};
	

	/**
	 * scroll
	 */
	this.scroll = function (scrollY, ticks, tickpct) {
//dPut(scrollY);
		if (_args.disabled) return;
		if (scrollY > _ymax) {
			$_element.css("display", "none");
			return;
		}
	
		var movePos = calcPos(scrollY, ticks, tickpct);
		if (_args.animateY) {
			$_element.css({marginLeft:(movePos.x+"px"), marginTop:(movePos.y+"px"), display:"block"});
		}
		else {
			$_element.css({marginLeft:(movePos.x+"px"), display:(movePos.v? "block" : "none")});
		}
	};

	init();
};

// #
