/* ==== slider nameSpace ==== */
var imageMenu = function() {
	/* ==== private methods ==== */
	function getElementsByTag(object, tag) {
		var o = object.getElementsByTagName(tag);
		if (o.length == 1) o = o[0];
		return o;
	}
	/* ==== Slider Constructor ==== */
	function Slider(oCont, speed, iW) {
		this.slides = [];
		this.over   = false;
		this.S      = this.S0 = speed;
		this.iW     = iW;
		this.oc     = document.getElementById(oCont);
		this.frm    = getElementsByTag(this.oc, 'li');
		this.NF     = this.frm.length;
		this.resize();
		for (var i = 0; i < this.NF; i++) {
			this.slides[i] = new Slide(this, i);
		}
		this.oc.parent = this;
		this.view      = this.slides[0];
		this.Z         = this.mx;
		/* ==== on mouse out event ==== */
		this.oc.onmouseout = function () {
			//鼠标移出效果
			//this.parent.mouseout();
			return false;
		}
	}
	Slider.prototype = {
		/* ==== animation loop ==== */
		run : function () {
			this.Z += this.over ? (this.mn - this.Z) * .5 : (this.mx - this.Z) * .5;
			this.view.calc();
			var i = this.NF;
			while (i--) this.slides[i].move();
		},
		/* ==== resize  ==== */
		resize : function () {
			this.wh = this.oc.clientWidth;
			this.ht = this.oc.clientHeight;
			this.wr = this.wh * this.iW;
			this.r  = this.ht / this.wr;
			this.mx = this.wh / this.NF;
			this.mn = (this.wh * (1 - this.iW)) / (this.NF - 1);
		},
		/* ==== rest  ==== */
		mouseout : function () {
			this.over = false;
		}
	}
	/* ==== Slide Constructor ==== */
	Slide = function (parent, N) {
		this.parent = parent;
		this.N      = N;
		this.x0     = this.x1 = N * parent.mx;
		this.obj    = parent.frm[N];
		this.img    = getElementsByTag(this.obj, 'img');
		this.obj.style.left = Math.floor(this.x0) + 'px';
		/* ==== mouse events ==== */
		this.obj.parent = this;
		this.obj.onmouseover = function() {
			this.parent.over();
			return false;
		}
	}
	Slide.prototype = {
		/* ==== target positions ==== */
		calc : function() {
			var that = this.parent;
			// left slides
			for (var i = 0; i <= this.N; i++) {
				that.slides[i].x1 = i * that.Z;
			}
			// right slides
			for (var i = this.N + 1; i < that.NF; i++) {
				//that.slides[i].x1 = that.wh - (that.NF - i) * that.Z;
				//默认显示第一个
				that.slides[i].x1 = that.wh - (that.NF - i) * that.mn;
			}
		},
		/* ==== HTML animation : move slides ==== */
		move : function() {
			var that = this.parent;
			var s = (this.x1 - this.x0) / that.S;
			/* ==== lateral slide ==== */
			if (this.N && Math.abs(s) > .5) {
				this.obj.style.left = Math.floor(this.x0 += s) + 'px';
			}
		},
		/* ==== light ==== */
		over : function () {
			this.parent.resize();
			this.parent.over = true;
			this.parent.view = this;
			this.calc();
		}
	}
	/* ==== public method - script initialization ==== */
	return {
		init : function(Id, Speed, Width) {
			// create instances of sliders here
			// parameters : HTMLcontainer name, speed (2 fast - 20 slow)
			//this.s1 = new Slider('imageMenu', 12, 0.6);
			this.s1 = new Slider(Id, Speed, Width);
			setInterval("imageMenu.s1.run();", 12);
		}
	}
}();

