var $ = function(id) {   
    return "string" == typeof id ? document.getElementById(id) : id;   
};   
// 实例化一个对象并调用对象的initialize方法   
var Class = {   
    create : function() {   
        return function() {   
            this.initialize.apply(this, arguments);   
        };   
    }   
};   
// 为Object添加一个extend方法   
Object.extend = function(destination, source) {   
    for ( var property in source) {   
        destination[property] = source[property];   
    }   
    return destination;   
};   
// 为对象注册事件   
var addEventHandler = function(oTarget, sEventType, fnHandler) {   
    if (oTarget.addEventListener) {   
        oTarget.addEventListener(sEventType, fnHandler, false);   
    } else if (oTarget.attachEvent) {   
        oTarget.attachEvent("on" + sEventType, fnHandler);   
    } else {   
        oTarget["on" + sEventType] = fnHandler;   
    }   
};   
var Scroll = Class.create();   
Scroll.prototype = {   
    initialize : function(outObj, inObj, options) {   
        var oScroll = this;   
        var iOut = $(outObj);   
        var iIn = $(inObj);   
        this.outHeight = iOut.offsetHeight;   
        this.inHeight = iIn.offsetHeight;     
        if (this.outHeight >= this.inHeight) return;     
        iOut.style.overflow = "hidden";   
        iIn.appendChild(iIn.cloneNode(true));     
        this.setOptions(options);   
        this.outObj = iOut;   
        this.timer = null;     
        this.side = 1; // 1：向上；2：向下   
        switch(this.options.side) {   
            case "down" :   
                this.side = -1;   
                break;   
            case "up":   
            default:   
                this.side = 1;   
        }   
        addEventHandler(iIn, "mouseover", function() {oScroll.stop();});   
        addEventHandler(iIn, "mouseout", function() {oScroll.start();});   
        this.start();   
    },   
    setOptions : function(options) {   
        this.options = {   
            step : 1, // 每次滚动的px量   
            side : "up", // 滚动的方向   
            time : 10 // 滚动的间隔时间（滚动速度）   
        };   
        Object.extend(this.options, options || {});   
    },   
    scroll : function() {   
        var inHeight = this.inHeight, outHeight = this.outHeight, iStep = this.options.step * this.side, time = this.options.time;   
        var iScrollTop = this.outObj.scrollTop;   
        if (iScrollTop >= (inHeight * 2 - outHeight)) {   
            iScrollTop -= inHeight;   
        } else if (iScrollTop <= 0) {   
            iScrollTop += inHeight;   
        }          
        this.outObj.scrollTop = iScrollTop + iStep;   
           
        var oScroll = this;   
        this.timer = setTimeout(function() {oScroll.scroll();}, time);   
    },    
    start : function() {   
        this.scroll();   
    },   
    stop : function() {   
        clearTimeout(this.timer);   
    }   
};   
function scrollDoor(){
}
scrollDoor.prototype = {
	sd : function(menus,divs,openClass,closeClass){
		var _this = this;
		if(menus.length != divs.length)
		{
			alert("菜单层数量和内容层数量不一样!");
			return false;
		}				
		for(var i = 0 ; i < menus.length ; i++)
		{	
			_this.$(menus[i]).value = i;				
			_this.$(menus[i]).onmouseover = function(){
					
				for(var j = 0 ; j < menus.length ; j++)
				{						
					_this.$(menus[j]).className = closeClass;
					_this.$(divs[j]).style.display = "none";
				}
				_this.$(menus[this.value]).className = openClass;	
				_this.$(divs[this.value]).style.display = "block";				
			}
		}
		},
	$ : function(oid){
		if(typeof(oid) == "string")
		return document.getElementById(oid);
		return oid;
	}
}

window.onload = function(){
	var SDmodel = new scrollDoor();	
	SDmodel.sd(["o01","o02","o03","o04","o05"],["d01","d02","d03","d04","d05"],"caa","cab");
	SDmodel.sd(["p01","p02","p03","p04","p05"],["e01","e02","e03","e04","e05"],"caa","cab");
	SDmodel.sd(["q01","q02","q03","q04","q05"],["f01","f02","f03","f04","f05"],"caa","cab");
	SDmodel.sd(["r01","r02","r03"],["g01","g02","g03"],"jd1","jd2");
	SDmodel.sd(["yq01","yq02"],["hz01","hz02"],"ld1","ld2");
    new Scroll("ddemo6", "ddemo7", {step : 1, side : "up", time : 20});   
	new Scroll("divry6", "divry7", {step : 1, side : "up", time : 20});
	new Scroll("divxs6", "divxs7", {step : 1, side : "up", time : 20});
	new Scroll("divhzhb6", "divhzhb7", {step : 1, side : "up", time : 20});
}

