$Scroll={}

$Scroll.getRect=function(id)
{
	var ele=$M.find(id)
	var rect = ele.getBoundingClientRect();
	var top = document.documentElement.clientTop;
	var left= document.documentElement.clientLeft;
	return{
		top    :   rect.top - top,
		bottom :   rect.bottom - top,
		left   :   rect.left - left,
		right  :   rect.right - left}
}

$Scroll.ableView=function(id,fun)
{
	var rect=$Scroll.getRect(id)
	if(rect.top>=0 && rect.top<=document.documentElement.clientHeight)
	{
		if(fun)
		{
			fun(true,rect);
		}
		else
		{
			return true;
		}
	}
	else
	{
		if(fun)
		{
			fun(false,rect);
		}
		else
		{
			return false;
		}
	}
}

$Scroll.isBottom=function(id,fun)
{
	var rect=$Scroll.getRect(id)
	if(rect.bottom>0 && rect.bottom<document.documentElement.clientHeight)
	{
		if(fun)
		{
			fun(true,rect);
		}
		else
		{
			return true;
		}
	}
	else
	{
		if(fun)
		{
			fun(false,rect);
		}
		else
		{
			return false;
		}
	}
}


$Scroll.lazy=function(id)
{
	var ele=$M.find(id)
	if(!ele)
	{
		$M.info('[懒加载]未找到元素',id);
		return;
	}

	ele.rely
	(
		'body','scroll',function(d)
		{
			if(ele.id_)
			{
				cancelAnimationFrame(ele.id_)
			}
			ele.id_=requestAnimationFrame
			(
				function()
				{
					var eleList=ele.querySelectorAll('img:not([lazy-load])[lazy-src],video:not([lazy-load])[lazy-src],audio:not([lazy-load])[lazy-src]');
					if(!eleList)
					{
						$M.info('[懒加载]没有需要懒加载的元素');
						return;
					}
						
					for (var index = 0; index < eleList.length; index++) 
					{
						if($Scroll.ableView(eleList[index]))
						{
							eleList[index].src=eleList[index].getAttribute('lazy-src');
							eleList[index].setAttribute('lazy-load','true')
						}
					}
					ele.id_=false;
				}
			)
		}
	)
}


$Scroll.pull=function(ida,id)
{
	var dom=$M.find(ida);
	var domc=$M.find(id);
	if(!domc || !dom)
	{
		$M.info('[下拉]未找到元素');
		return function(){};
	}
	
	dom.listener
	(
		'touchstart,touchmove,touchend',function(ev,dom)
		{
			switch (ev.type) 
			{
				case 'touchstart':
					dom.before_=ev.touches[0].clientY
				break;
				case 'touchmove':
					if(dom.scrollTop==0 && dom.before_-ev.touches[0].clientY<0)
					{
						var target=1+Number(domc.style.height.replace('px',''));
						if(target<=50)
						{
							domc.style.height=target+'px';
						}
					}
				break;
				case 'touchend':
					if(dom.scrollTop==0)
					{
						var target=Number(domc.style.height.replace('px',''));
						if(target<35)
						{
							domc.style.height='0px';
						}
						else
						{
							domc.innerHTML='正在加载...';
							domc.style.height='50px';
						}
					}
				break;
			}
		}            
	)

	return function()
	{
		domc.style.height='0px';
	}
}

$Scroll.high=function(ida,id,fun)
{
	var dom=$M.find(ida);
	var domc=$M.find(id);
	if(!domc || !dom)
	{
		$M.info('[上拉]未找到元素');
		return function(){};
	}
	function close()
	{
		domc.style.height='0px';
	}
	dom.listener
	(
		'touchstart,touchmove,touchend',function(ev,dom)
		{
			switch (ev.type) 
			{
				case 'touchstart':
					dom.before_=ev.touches[0].clientY
				break;
				case 'touchmove':
					if((dom.scrollHeight - dom.scrollTop === dom.clientHeight) && dom.before_-ev.touches[0].clientY>0)
					{
						var target=3+Number(domc.style.height.replace('px',''));
						if(target<=50)
						{
							domc.style.height=target+'px';
						}
					}
				break;
				case 'touchend':
					var target=Number(domc.style.height.replace('px',''));
					if(target<35)
					{
						fun(false,close);
						domc.style.height='0px';
					}
					else
					{
						fun(true,close);
						domc.innerHTML='正在加载...';
						domc.style.height='50px';
					}
				break;
			}
		}            
	)

	return close;
}



$Scroll.drawer=function(id)
{
	$M.find(id,true).listener
	(
		'touchstart,touchmove,touchend',function(ev,dom)
		{
			$M.stop(ev)
			switch (ev.type) 
			{
				case 'touchstart':
					dom.start_=ev.touches[0].clientX
				break;
				case 'touchmove':
					dom.start_next_=ev.touches[0].clientX
					requestAnimationFrame
					(
						function()
						{
							dom.scrollLeft+=((dom.start_-dom.start_next_)/3);                                   
						}
					)
				break;
				case 'touchend':
					requestAnimationFrame
					(
						function()
						{
							if(dom.scrollLeft>50)
							{
								dom.scrollLeft=140
							}
							else
							{
								dom.scrollLeft=0
							}
						}
					)
				break;
			}
		}            
	)
}