// ***************************************************
//
// writer by geato 2010-01-15
// mail:fairyprince@163.com , free_getao@hotmail.com
//
// ***************************************************


function NoGapScroll(own,ParentID,containerID,contentID,direction,speed,cycles,autoscroll,slideenable,manualspeed,slideMaxSpeed,eventFun)
{
	this.parentID = ParentID;
	this.containerID = containerID;
	this.contentID = contentID;
	this.direction = direction;
	this.speed = speed;
	this.callinterval = 100;
	this.step = this.speed / (1000 / this.callinterval);
	this.eventFun = eventFun;
	this.own = own;
	this.cycles = cycles;
	this.autoscroll = autoscroll;
	this.slideenable = slideenable;
	this.parentWidth = 0;
	this.parentHeight = 0;
	this.contentWidth = 0;
	this.contentHeight = 0;
	this.stop = false;
	if(slideMaxSpeed <= 0  || typeof slideMaxSpeed == 'undefined')
	{
		this.slideMaxStep = this.step * 10;
	}
	else
	{
		this.slideMaxStep = slideMaxSpeed / (1000 / this.callinterval);
	}
	this.slidestep = 0;
	this.slideDirection = '';
	if(manualspeed <= 0  || typeof manualspeed == 'undefined')
	{
		this.manualstep = this.step * 10;
	}
	else
	{
		this.manualstep = manualspeed / (1000 / this.callinterval);
	}
	this.ManualDirection = '';
	this.manualStop = false;
	this.DownEventID = '';
	this.MoveEventID = '';
}

NoGapScroll.prototype.SetManual = function (id,direction,moveenable){
	var obj = $(id);
	if(obj == null || typeof obj == 'undefined')
		return;
	
	switch(direction)
	{
		case 'left':
			if(this.direction == 'up' || this.direction == 'down')
				break;
			obj.__NGS = this;
			obj.observe('mousedown',this.OnClickLeft);
			if(moveenable)
			{
				obj.__NGS_EnableMouseMove = true;
				obj.observe('mouseover',this.OnLeft);
			}
			break;
		case 'right':
			if(this.direction == 'up' || this.direction == 'down')
				break;
			obj.__NGS = this;
			obj.observe('mousedown',this.OnClickRight);
			if(moveenable)
			{
				obj.__NGS_EnableMouseMove = true;
				obj.observe('mouseover',this.OnRight);
			}
			break;
		case 'up':
			if(this.direction == 'left' || this.direction == 'right')
				break;
			obj.__NGS = this;
			obj.observe('mousedown',this.OnClickUp);
			if(moveenable)
			{
				obj.__NGS_EnableMouseMove = true;
				obj.observe('mouseover',this.OnUp);
			}
			break;
		case 'down':
			if(this.direction == 'left' || this.direction == 'right')
				break;
			obj.__NGS = this;
			obj.observe('mousedown',this.OnClickDown);
			if(moveenable)
			{
				obj.__NGS_EnableMouseMove = true;
				obj.observe('mouseover',this.OnDown);
			}
			break;
	}
	obj = null;
}

NoGapScroll.prototype.getHtml = function (){
	var parentobj = $(this.parentID);
	if(parentobj == null || typeof parentobj == 'undefined')
		return;

	var contentobj = $(this.contentID);
	if(contentobj == null || typeof contentobj == 'undefined')
	{
		parentobj = null;
		return;
	}

	var containerobj = $(this.containerID);
	if(containerobj == null || typeof containerobj == 'undefined')
	{
		parentobj = null;
		parentobj = null;
		return;
	}

	this.parentWidth = parentobj.getWidth();
	this.parentHeight = parentobj.getHeight();
	this.contentWidth = contentobj.getWidth();
	this.contentHeight = contentobj.getHeight();

	if(this.direction == 'up' || this.direction =='down')
	{
		if(this.contentHeight < this.parentHeight)
		{
			parentobj = null;
			parentobj = null;
			return;
		}
		containerobj.setStyle('height:' + (this.parentHeight * 2 + 10)  + 'px;');
		if(this.direction =='down')
			parentobj.scrollTop = this.contentHeight;
	}
	else if(this.direction == 'left' || this.direction =='right')
	{
		if(this.contentWidth < this.parentWidth)
		{
			parentobj = null;
			parentobj = null;
			return;
		}

		containerobj.setStyle('width:' + (this.contentWidth * 2 + 10) + 'px;');
		if(this.direction =='right')
		{
			parentobj.scrollLeft = this.contentWidth;
		}
		else
		{
			parentobj.scrollLeft = 0;
		}
	}

	var contentobj_clone = contentobj.innerHTML;
	containerobj.insert({bottom : '<div class="' +  contentobj.classNames() + '">' + contentobj_clone + '</div>'});
	
	parentobj.__NGS = this;
	parentobj.observe('mouseover',this.OnMouseOver);
	parentobj.observe('mouseout',this.OnMouseOut);
	
	contentobj = null;
	containerobj = null;
	parentobj = null;
	
	setInterval(this.own + '.__timecall()',this.callinterval);
	document.observe('mousemove',NoGapScroll_OnMouseMove);
	document.observe('mouseup',NoGapScroll_OnRelease);
	if(document.__NGS == null || typeof document.__NGS == 'undefined')
	{
		document.__NGS = new Array();
	}
	document.__NGS.push(this);
}

NoGapScroll.prototype.writeHtml = function () {
	this.getHtml();
}

NoGapScroll.prototype.OnMouseOver = function (){
	if(this.__NGS == null || typeof this.__NGS == 'undefined')
		return;
	var This = this.__NGS;
	This.stop = true;;
	if(This.slideenable)
	{
		var result = GetCursorPoiint_RelativeToElement(This.parentID); 
		if(This.direction == 'up' || This.direction == 'down')
		{
			var testHeight = result.height * 0.3;
			if(result.top < testHeight)
			{
				This.slideDirection = 'down';
				This.slidestep = ((testHeight - result.top) / testHeight) * This.slideMaxStep;
			}
			else if(result.top > result.height - testHeight)
			{
				This.slideDirection = 'up';
				This.slidestep = ((result.top -  result.height + testHeight) / testHeight) * This.slideMaxStep;
			}
			else
			{
				This.slideDirection = '';
			}
		}
		else if(This.direction == 'left' || This.direction == 'right')
		{
			var testWidth = result.width * 0.3;
			if(result.left < testWidth)
			{
				This.slideDirection = 'right';
				This.slidestep = ((testWidth - result.left) / testWidth) * This.slideMaxStep;
			}
			else if(result.left > result.width - testWidth)
			{
				This.slideDirection = 'left';
				This.slidestep = ((result.left -  result.width + testWidth) / testWidth) * This.slideMaxStep;
			}
			else
			{
				This.slideDirection = '';
			}
		}
	}
}

NoGapScroll.prototype.OnMouseOut = function (){
	if(this.__NGS == null || typeof this.__NGS == 'undefined')
		return;
	var This = this.__NGS;
	This.stop = false;
	This.slideDirection = '';
}

NoGapScroll.prototype.__timecall = function (){
	if(this.ManualDirection != '')
	{
		if(this.manualStop)
			return;
		var step = this.manualstep;
		if(this.ManualDirection == 'up')
		{
			this.__scorll('up',step);
		}
		else if(this.ManualDirection =='down')
		{
			this.__scorll('down',step);
		}
		else if(this.ManualDirection == 'left')
		{
			this.__scorll('left',step);
		}
		else if(this.ManualDirection =='right')
		{
			this.__scorll('right',step);
		}
	}
	else if(this.slideDirection != '')
	{
		var step = this.slidestep;
		if(this.slideDirection == 'up')
		{
			this.__scorll('up',step);
		}
		else if(this.slideDirection =='down')
		{
			this.__scorll('down',step);
		}
		else if(this.slideDirection == 'left')
		{
			this.__scorll('left',step);
		}
		else if(this.slideDirection =='right')
		{
			this.__scorll('right',step);
		}
	}
	else
	{
		if(!this.autoscroll || this.stop)
			return;
		if(this.direction == 'up')
		{
			this.__scorll('up',this.step);
		}
		else if(this.direction =='down')
		{
			this.__scorll('down',this.step);
		}
		else if(this.direction == 'left')
		{
			this.__scorll('left',this.step);
		}
		else if(this.direction =='right')
		{
			this.__scorll('right',this.step);
		}
	}
}

NoGapScroll.prototype.__scorll = function (direct,step){
	var parentobj = $(this.parentID);
	if(parentobj == null || typeof parentobj == 'undefined')
		return;

	if(direct == 'up')
	{
		var currentPosition = parentobj.scrollTop;
		if(this.cycles)
		{
			if(currentPosition >= this.contentHeight)
			{
				parentobj.scrollTop = currentPosition - this.contentHeight + step;
			}
			else
			{
				parentobj.scrollTop += step;
			}
		}
		else
		{
			var l = this.contentHeight - this.parentHeight;
			if(currentPosition + step >= l)
			{
				parentobj.scrollTop = l;
			}
			else
			{
				parentobj.scrollTop += step;
			}
		}
	}
	else if(direct =='down')
	{
		var currentPosition = parentobj.scrollTop;
		if(this.cycles)
		{
			if(currentPosition <= 0)
			{
				parentobj.scrollTop = currentPosition + this.contentHeight - step;
			}
			else
			{
				parentobj.scrollTop -= step;
			}
		}
		else
		{
			if(currentPosition - step < 0)
			{
				parentobj.scrollTop = 0;
			}
			else
			{
				parentobj.scrollTop -= step;
			}
		}
	}
	else if(direct == 'left')
	{
		var currentPosition = parentobj.scrollLeft;
		if(this.cycles)
		{
			if(currentPosition >= this.contentWidth)
			{
				parentobj.scrollLeft = currentPosition - this.contentWidth + step;
			}
			else
			{
				parentobj.scrollLeft += step;
			}
		}
		else
		{
			var l = this.contentWidth - this.parentWidth;
			if(currentPosition + step >= l)
			{
				parentobj.scrollLeft = l;
			}
			else
			{
				parentobj.scrollLeft += step;
			}
		}
	}
	else if(direct =='right')
	{
		var currentPosition = parentobj.scrollLeft;
		if(this.cycles)
		{
			if(currentPosition <= 0)
			{
				parentobj.scrollLeft = currentPosition + this.contentWidth - step;
			}
			else
			{
				parentobj.scrollLeft -= step;
			}
		}
		else
		{
			if(currentPosition - step < 0)
			{
				parentobj.scrollLeft = 0;
			}
			else
			{
				parentobj.scrollLeft -= step;
			}
		}
	}
	parentobj = null;
}

NoGapScroll.prototype.OnClickLeft = function (){
	if(this.__NGS == null || typeof this.__NGS == 'undefined')
		return;
	var This = this.__NGS;
	This.slideDirection = '';
	This.ManualDirection = 'left';
	This.DownEventID = this.identify();
}

NoGapScroll.prototype.OnClickRight = function (){
	if(this.__NGS == null || typeof this.__NGS == 'undefined')
		return;
	var This = this.__NGS;
	This.slideDirection = '';
	This.ManualDirection = 'right';
	This.DownEventID = this.identify();
}

NoGapScroll.prototype.OnClickUp = function (){
	if(this.__NGS == null || typeof this.__NGS == 'undefined')
		return;
	var This = this.__NGS;
	This.slideDirection = '';
	This.ManualDirection = 'up';
	This.DownEventID = this.identify();
}

NoGapScroll.prototype.OnClickDown = function (){
	if(this.__NGS == null || typeof this.__NGS == 'undefined')
		return;
	var This = this.__NGS;
	This.slideDirection = '';
	This.ManualDirection = 'down';
	This.DownEventID = this.identify();
}

NoGapScroll.prototype.OnLeft = function (){
	if(this.__NGS == null || typeof this.__NGS == 'undefined')
		return;
	var This = this.__NGS;
	if(This.MoveEventID != '')
		return;
	This.slideDirection = '';
	This.ManualDirection = 'left';
	This.MoveEventID = this.identify();
}

NoGapScroll.prototype.OnRight = function (){
	if(this.__NGS == null || typeof this.__NGS == 'undefined')
		return;
	var This = this.__NGS;
	if(This.MoveEventID != '')
		return;
	This.slideDirection = '';
	This.ManualDirection = 'right';
	This.MoveEventID = this.identify();
}

NoGapScroll.prototype.OnUp = function (){
	if(this.__NGS == null || typeof this.__NGS == 'undefined')
		return;
	var This = this.__NGS;
	if(This.MoveEventID != '')
		return;
	This.slideDirection = '';
	This.ManualDirection = 'up';
	This.MoveEventID = this.identify();
}

NoGapScroll.prototype.OnDown = function (){
	if(this.__NGS == null || typeof this.__NGS == 'undefined')
		return;
	var This = this.__NGS;
	if(This.MoveEventID != '')
		return;
	This.slideDirection = '';
	This.ManualDirection = 'down';
	This.MoveEventID = this.identify();
}

NoGapScroll.prototype.OnRelease = function (){
	if(this.DownEventID != '')
	{
		var result = GetCursorPoiint_RelativeToElement(this.DownEventID); 
		if(result.IsInRect)
		{
			var obj = $(this.DownEventID);
			if(!obj.__NGS_EnableMouseMove)
			{
				this.ManualDirection = '';
			}
		}
		else
		{
			this.ManualDirection = '';
		}
		this.DownEventID = '';
	}
	this.manualStop = false;
}

NoGapScroll.prototype.OnLeave = function (){
	if(this.DownEventID != '')
	{
		var result = GetCursorPoiint_RelativeToElement(this.DownEventID); 
		if(result.IsInRect)
		{
			this.manualStop = false;
		}
		else
		{
			this.manualStop = true;
		}
	}
	else if(this.MoveEventID != '')
	{
		var result = GetCursorPoiint_RelativeToElement(this.MoveEventID); 
		if(!result.IsInRect)
		{
			this.MoveEventID = '';
			this.ManualDirection = '';
		}
	}
}

function NoGapScroll_OnMouseMove()
{
	if(this.__NGS == null || typeof this.__NGS == 'undefined')
		return;
	var This = this.__NGS;
	var c = This.size();
	for(i = 0 ; i < c ; i ++)
	{
		This[i].OnLeave();
	}
}

function NoGapScroll_OnRelease()
{
	if(this.__NGS == null || typeof this.__NGS == 'undefined')
		return;
	var This = this.__NGS;
	var c = This.size();
	for(i = 0 ; i < c ; i ++)
	{
		This[i].OnRelease();
	}
}
	