/*
<script language="javascript" src="mslider.js"></script>
<div id="slider" style="width:400px;height:50px;overflow:hidden"><a href="@">A</a><a href="@">A</a><a href="@">A</a><a href="@">A</a></div>
<script>
var sl = new Slider('sl','slider');
//sl.direction = 'v';
</script>
*/
function MSlider(varid , contentId , direction)
{
	this.varid = varid ;
	this.box = document.getElementById(contentId);
	this.width = -1;
	this.height = -1;
	this.cWidth = 0;
	this.cHeight = 0;
	this.amount = 10 ;//pixel per time unit
	this.delay = 30 ;//time unit
	this.itemLen = -1;
	this.direction = direction == 'v'? 'v':'h';
	this.timer1 = null ;

	this.box.style.overflow = 'hidden';
	var w = parseInt(this.box.style.width) ; 
	if(isNaN(w)) 
	{
		this.box.style.width = "400px";
		this.width = 400;
	}
	else
		this.width = w ;

	var h = parseInt(this.box.style.height) ; 
	if(isNaN(h)) 
	{
		this.box.style.height = "80px"; 
		this.width = 80;
	}
	else
		this.height = h;
	
	this.box.innerHTML = "<div><nobr>"+this.box.innerHTML+"</nobr></div>";
	this.content = this.box.childNodes.item(0);
	this.content.style.position = 'absolute';
	this.content.style.marginLeft="0px" ; this.content.style.marginTop="0px" ; 
	this.cWidth = this.content.offsetWidth;
	this.cHeight = this.content.offsetHeight;
	this.content.style.position = 'static';

	this.sliderPixel = function (tOffset)
	{
		tOffset = parseInt(tOffset);		
		if(this.direction == 'h') 
		{
			var max = this.width - this.cWidth ;
			var cm = parseInt(this.content.style.marginLeft);
		}
		else
		{
			var max = this.height - this.cHeight;
			var cm = parseInt(this.content.style.marginTop);
		}
		if(max >= 0 ) return ;//not need slider
		if(tOffset > 0 ) tOffset = 0 ;
		if(tOffset < max) tOffset = max ;
		if(tOffset > cm ) offset = this.amount ; else offset = 0 - this.amount ;
		if(tOffset ==  cm ) return ;
		var nm = cm + offset ;

		if(cm < tOffset && nm >= tOffset)
		{
			clearInterval(this.timer1);			
			this.timer1 =null;
			nm = tOffset ;
		}
		else if(cm > tOffset && nm <= tOffset)
		{
			clearInterval(this.timer1);			
			this.timer1 =null;
			nm = tOffset ;
		}
		if(this.direction == 'h')this.content.style.marginLeft = nm+"px";else if(this.direction == 'v')this.content.style.marginTop = nm+"px";
	}
	this.doSlider = function (offset)
	{
		clearInterval(this.timer1);
		this.timer1 = window.setInterval(this.varid+".sliderPixel("+offset+")" , this.delay);
	}
	this.slider = function(offsetCommand)
	{
		var ml = parseInt(this.content.style.marginLeft);
		var mt = parseInt(this.content.style.marginTop);
		if(offsetCommand == 'next'){
			if(this.direction == 'h') offset = ml - this.width;	else if(this.direction == 'v') offset = mt - this.height;
		}else if(offsetCommand == 'prev'){
			if(this.direction == 'h') offset = ml + this.width;	else if(this.direction == 'v') offset = mt + this.height;
		}else if(offsetCommand == 'begin'){
			offset = 0;
		}else if(offsetCommand == 'end'){
			if(this.direction == 'h')offset = 0 - Math.abs(this.cWidth);else if(this.direction == 'v')offset = 0 - Math.abs(this.cHeight);
		}else{
			var offsetvar = parseFloat(offsetCommand);			
			if(offsetvar>0 && offsetvar <= 1)
			{
				if(this.direction == 'h')offset = 0 - Math.abs(Math.ceil(this.cWidth*offsetvar));else if(this.direction == 'v')	offset = 0 - Math.abs(Math.ceil(this.cHeight*offsetvar));
			}
			else
				offset = 0 - Math.ceil(Math.abs(offsetvar)) ;
		}
		this.doSlider(offset);
		/*
		for(key in this)
		{
			document.write(key+" = "+this[key]+"<br/>");
		}*/
	}
	this.item = function (itemid , center)
	{
		if(this.itemLen == -1)
		{
			var itemTotal = this.content.childNodes.item(0).childNodes.length ;
			this.itemLen = Math.ceil(this.cWidth / itemTotal);
		}
		if(itemid < 1) itemid = 1;
		var pix = 0 - Math.abs(Math.ceil((itemid -1) * this.itemLen)) ;
		if(typeof(center)!= 'undefined' && center == true)
		{
			pix = pix + Math.ceil(this.width / 2);
		}
		this.doSlider(pix);	
	}
}
