m={};m.config={outDivName:"SlidePlayer",divName:"Slides",direction:"up",tt:10,theTop:0,imgheight:140,speed:10,scrollInteval:4};
m.configSet = function(imgheight,scrollInteval,divName,outDivName,direction){
	m.config.outDivName=outDivName||m.config.outDivName; m.config.divName=divName||m.config.divName; m.config.direction=direction||m.config.direction; m.config.imgheight=imgheight||m.config.imgheight; m.config.scrollInteval=scrollInteval||m.config.scrollInteval;
}
m.getImgObj = function(){
	var obj = document.getElementById(m.config.divName);
	var imgs = obj.getElementsByTagName("div");
	return imgs;
}
m.getImgCount = function(){
	return m.getImgObj().length;
}
m.getCurPicNum = function(){
	var obj = document.getElementById(m.config.outDivName);
	var lis = obj.getElementsByTagName("ul")[0].getElementsByTagName("li");
	for(var i=0; i<lis.length; i++){
		if(lis[i].className=="Current"){
			return parseInt(lis[i].innerHTML);
		}
	}
}
m.getNumObj = function(){
	var obj = document.getElementById(m.config.outDivName);
	var lis = obj.getElementsByTagName("ul")[0].getElementsByTagName("li");
	return lis;
}
m.imgTitle=new Array();
m.setImgTitle=function(arr){
    if(m.getImgCount()>1){
	    m.imgTitle = arr.slice(0);
	    var imgobj = m.getImgObj();
	    for(var i=0;i<imgobj.length;i++){
	        if(m.imgTitle[i]!=undefined){
		        imgobj[i].title=m.imgTitle[i];
	        }
	    }
	}
}
m.setNumTitle=function(arr){
    if(m.getImgCount()>1){
	    m.imgTitle = arr.slice(0);
	    var numobj = m.getNumObj();
	    for(var i=0;i<numobj.length;i++){
	        if(m.imgTitle[i]!=undefined){
		        numobj[i].title=m.imgTitle[i];
		    }
	    }
	}	
}
m.setTitle=function(arr){
	m.setImgTitle(arr);
	m.setNumTitle(arr);
}
var autoPlayTimer;
var autoScrollTimer;
m.slide = new function(){	
	var a = { tt:m.config.tt,splitnum:m.config.speed };
	this.up = function (i){
		var obj = document.getElementById(m.config.divName);		
		var curTop=parseInt(obj.style.top.replace("px",""));		
		var space = i*m.config.imgheight-Math.abs(Math.abs(m.config.theTop)-Math.abs(curTop));
		pos=obj.offsetTop-space/a.splitnum;
		if (space/a.splitnum<1){
			obj.style.top=(m.config.theTop-i*m.config.imgheight)+"px";
			clearTimeout(autoScrollTimer);
		}else{
			obj.style.top=pos+"px";
			autoScrollTimer=setTimeout("m.slide.up("+i+")",a.tt);
		}
	};
	this.down = function (i){
		var obj = document.getElementById(m.config.divName);
		var curTop=parseInt(obj.style.top.replace("px",""));
		var space = i*m.config.imgheight-Math.abs(Math.abs(m.config.theTop)-Math.abs(curTop));
		pos=obj.offsetTop+space/a.splitnum;
		if (space/a.splitnum<1){
			obj.style.top=(m.config.theTop+i*m.config.imgheight)+"px";
			clearTimeout(autoScrollTimer);
		}else{
			obj.style.top=pos+"px";
			autoScrollTimer=setTimeout("m.slide.down("+i+")",a.tt);	
		}

	};
}
m.tag=new function(){	
	this.createTag = function(defnum){
		var str = "";
		var div = document.getElementById(m.config.outDivName);
		var ul = document.createElement("ul");
		ul.className = "SlideTriggers";		
		for(var i=1; i<=m.getImgCount(); i++){
			str += "<li title='Picture-"+i+"' class='"+((i==defnum)?"Current":"")+"' onmouseover='m.effect.goNumPic("+i+");' onmouseout='m.effect.rePlay()'>"+i+"</li>";
		}
		ul.innerHTML = str;
		div.appendChild(ul);
	}
	this.reSetPageTag = function(num){
		var ul = document.getElementById(m.config.outDivName).getElementsByTagName("ul")[0];
		var lis = ul.getElementsByTagName("li");		
		for(var i=0; i<lis.length; i++){
			lis[i].className = (i==num)?"Current":"";
		}
	}
	this.removeAllTags=function(){
		var div = document.getElementById(m.config.outDivName);
		var ul = div.getElementsByTagName("ul")[0];
		var lis = ul.getElementsByTagName("li");
		for(var i=0; i<lis.length; i++){
			ul.removeChild(lis[i]);
		}
		div.removeChild(ul);
	}
}
m.effect= new function(){
	var direction = m.config.direction;	
	this.play = function (){
		var obj = document.getElementById(m.config.divName);
		var curtop = parseInt(obj.style.top.replace("px",""));
		m.config.theTop = curtop;
		if(direction=="up" && ((curtop>0 && Math.abs(curtop/m.config.imgheight)<=m.getImgCount()-1)||(curtop<0 && Math.abs(curtop/m.config.imgheight)<m.getImgCount()-1))){
			direction = "up";
		}else{
			direction = "down";
		}
		if(direction=="down" && Math.abs(curtop/m.config.imgheight)>0){
			direction = "down";
		}else{
			direction = "up";
		}
		if(direction=='up'){
			m.slide.up(1);
			m.tag.reSetPageTag(Math.abs(curtop/m.config.imgheight)+1);
		}else if(direction=='down'){
			m.slide.down(1);
			m.tag.reSetPageTag(Math.abs(curtop/m.config.imgheight)-1);
		}	
		if(curtop>0){
			m.tag.reSetPageTag(0);
		}
		autoPlayTimer=setTimeout("m.effect.rePlay();",m.config.scrollInteval/2*1000);//		autoPlayTimer=setTimeout("m.effect.play()",m.config.scrollInteval*1000);
	}
	this.goNumPic = function (num){//if(autoScrollTimer==undefined || autoPlayTimer==undefined){clearTimeout(startTimer);startTimer=-1;}
		clearTimeout(autoScrollTimer);
		clearTimeout(autoPlayTimer);
		var curnum = m.getCurPicNum();
		var obj = document.getElementById(m.config.divName);
		obj.style.top = (curnum-1)*m.config.imgheight*-1;
		m.config.theTop=parseInt(obj.style.top.replace("px",""));
		if(curnum<num){			
			autoPlayTimer=setTimeout("m.slide.up(Math.abs("+(curnum-num)+"))",100);
		}
		if(curnum>num){
			autoPlayTimer=setTimeout("m.slide.down(Math.abs("+(curnum-num)+"))",100);
		}
		m.tag.reSetPageTag(num-1);
	}
	this.rePlay=function(){
		clearTimeout(autoScrollTimer);
		clearTimeout(autoPlayTimer);
		var curnum = m.getCurPicNum();
		var obj = document.getElementById(m.config.divName);
		obj.style.top = ((curnum-1)*m.config.imgheight*-1)+"px";
		autoPlayTimer=setTimeout("m.effect.play()",m.config.scrollInteval/2*1000);
	}
	this.setEventForPic = function(){
		var obj = document.getElementById(m.config.divName);
		var imgs = obj.getElementsByTagName("div");
		for(var i=0; i<imgs.length; i++){
			imgs[i].onmouseover = function(){
				m.effect.goNumPic(m.getCurPicNum());
			};
			imgs[i].onmouseout = function(){
				m.effect.rePlay();
			};
		}
	}	
	this.toScroll = function(){
		if(m.getImgCount()>1)
		{
			var obj = document.getElementById(m.config.divName);
			if(m.config.direction=="up")
			{
				m.config.theTop=m.config.imgheight;
			}
			else if(m.config.direction=="down")
			{
				m.config.theTop=m.config.imgheight*m.getImgCount()*-1;
			}
		    obj.style.top = m.config.theTop+"px";
		    m.tag.createTag(1);
		    m.setTitle(new Array());		
		    m.effect.setEventForPic();		
		    m.effect.play();
		}
	}
}

