// JavaScript Document
function $(id){
    return document.getElementById(id);
}

//定义滚动对象
var zhennScroll = {
    //容器
    googsmain: $("googsmain"),
    //左按钮
    leftbtn: $("leftbtn"),
    //右按钮
    rightbtn: $("rightbtn"),
    //是否自动滚动
    autoScroll: false,
	childElem1: null,
	childElem2: null,
    //定时器
    timer: null,
    //速度，值越大，滚动越慢
    speed: 20,
    //移动的单位距离，值越小，滚动越快
    space: 2,
	//翻页宽度
	pageWidth: 192, 
    //初始化
	num: 0,
    init: function(){
        this.googsmain.innerHTML = "<div style=\"width:9999px;\" >" + this.googsmain.innerHTML + "</div>";
        this.childElem1 = this.googsmain.getElementsByTagName("ul")[0];
        this.childElem2 = document.createElement("ul");
        var infinityDiv = this.googsmain.getElementsByTagName("div")[0];
        infinityDiv.appendChild(this.childElem2);
		
		
        if(this.childElem1.offsetWidth>=this.googsmain.offsetWidth){
			this.childElem2.innerHTML = this.childElem1.innerHTML;
			//当自动滚动时
			if(this.autoScroll==true){
				this.timer = setInterval(this.scrollleft,this.speed);
				this.googsmain.onmouseover = function(){
					clearInterval(zhennScroll.timer);
				}
				this.googsmain.onmouseout = function(){
					zhennScroll.timer = setInterval(zhennScroll.scrollleft,zhennScroll.speed);
				}
			}else{   //点击按钮才可以滚动
				//增加鼠标监听
				//点击左边按钮，则向左滚动
				this.leftbtn.onclick = function(){
					if(zhennScroll.num==0){
						zhennScroll.googsmain.scrollLeft = zhennScroll.childElem1.offsetWidth;
						zhennScroll.num = 1; 
					}
					if(zhennScroll.googsmain.scrollLeft%zhennScroll.pageWidth==0){ //防止设置多个timer
						zhennScroll.timer = setInterval(zhennScroll.scrollleft,zhennScroll.speed);
					}
				}
				//点击右边按钮，则向右滚动
				this.rightbtn.onclick = function(){
					if(zhennScroll.num==0){
						zhennScroll.googsmain.scrollLeft = zhennScroll.childElem1.offsetWidth;
						zhennScroll.num = 1; 
					}
					if(zhennScroll.googsmain.scrollLeft%zhennScroll.pageWidth==0){ //防止设置多个timer
						zhennScroll.timer = setInterval(zhennScroll.scrollright,zhennScroll.speed)
					}
				}
			}
			//初始化操作，强制回到初始位置
			this.googsmain.scrollLeft = 0;
        }
    },
	//左滚动方法
	scrollleft: function(){
		if(zhennScroll.googsmain.scrollLeft>=zhennScroll.childElem1.offsetWidth){
			zhennScroll.googsmain.scrollLeft -= zhennScroll.childElem1.offsetWidth;
		}else{
			zhennScroll.googsmain.scrollLeft += zhennScroll.space;
		}
		if(!zhennScroll.autoScroll&&zhennScroll.googsmain.scrollLeft%zhennScroll.pageWidth==0&&zhennScroll.googsmain.scrollLeft>0){
			clearInterval(zhennScroll.timer);
		}
	},
	//右滚动方法
	scrollright: function(){
		if(zhennScroll.googsmain.scrollLeft<=0){
			zhennScroll.googsmain.scrollLeft += zhennScroll.childElem1.offsetWidth;
		}else{
			zhennScroll.googsmain.scrollLeft -= zhennScroll.space;
		}
		if(!zhennScroll.autoScroll&&zhennScroll.googsmain.scrollLeft%zhennScroll.pageWidth==0&&zhennScroll.googsmain.scrollLeft<zhennScroll.childElem1.offsetWidth){
			clearInterval(zhennScroll.timer);
		}
	}
	
}

window.onload = function(){
	zhennScroll.init();
};