﻿
function $gE(id) {
	return document.getElementById(id);
}

function $cE(tag) {
	return document.createElement(tag);
}

function moveElement(element, final_x, final_y, interval) {
	if (element == undefined)
		return;
	var elem = element;
	if (elem.movement) {
		clearTimeout(elem.movement);
	}
	if (!elem.style.left) {
		elem.style.left = "0px";
	}
	if (!elem.style.top) {
		elem.style.top = "0px";
	}
	var xpos = parseInt(elem.style.left);
	var ypos = parseInt(elem.style.top);
	if (xpos == final_x && ypos == final_y) {
		return true;
	}
	if (xpos < final_x) {
		var dist = Math.ceil((final_x - xpos) / 10);
		xpos = xpos + dist;
	}
	if (xpos > final_x) {
		var dist = Math.ceil((xpos - final_x) / 10);
		xpos = xpos - dist;
	}
	if (ypos < final_y) {
		var dist = Math.ceil((final_y - ypos) / 10);
		ypos = ypos + dist;
	}
	if (ypos > final_y) {
		var dist = Math.ceil((ypos - final_y) / 10);
		ypos = ypos - dist;
	}
	elem.style.left = xpos + "px";
	elem.style.top = ypos + "px";
	elem.movement = setTimeout(moveElement.Exec(element, final_x, final_y, interval), interval);
}


function ImageNews(container, hasChildren, useCutsom, selectCss, normalCss, imgHeight) {
	this._container = $gE(container);
	this.pic = '';
	this.bigPic = '';
	this.btnPic = '';
	this.txtDiv = '';
	this.txtBg = '';
	this.picCount = 0;
	this.e = this;
	this.p = true;

	this._h = imgHeight ? imgHeight : -288;
	this._sc = selectCss ? selectCss : 'current';
	this._nc = normalCss ? normalCss : 'normal';
	this._ucs = useCutsom;
	this._hc = hasChildren;
	this._c = -1;
	this._to = 3000;
}

ImageNews.prototype = {

	Init: function() {
		if (!this._container)
			window.setTimeout(this.Init, 1);
		//	如果是已经创建好的节点
		if (this._hc) {
			if (this._container.childNodes.length < 2)
				return;
			this.pic = this._container.childNodes[0];
			this.btnPic = this._container.childNodes[1];

			if (this.pic.childNodes.length != 3) return;

			this.bigPic = this.pic.childNodes[0];
			this.txtBg = this.pic.childNodes[1];
			this.txtDiv = this.pic.childNodes[2];

			this.ClearContainer(this.bigPic);
			this.ClearContainer(this.txtDiv);
			this.ClearContainer(this.btnPic);

			this.CreateUl(this.bigPic);
			this.CreateUl(this.txtDiv);
			this.CreateUl(this.btnPic);
		}
		//	没有节点,自动创建
		else {
		}
	},
	ClearContainer: function(c) {
		c.innerHTML = '';
	},
	CreateUl: function(c) {
		c.appendChild($cE('UL'));
	},
	AddItem: function(link, pic, txt) {
		if (this._ucs) {
			this.bigPic.childNodes[0].innerHTML += link;
			var u1 = $cE("Li");
			this.txtDiv.childNodes[0].appendChild(u1);
			u1.innerHTML = txt;
			u1.className = this._nc;
			//this.txtDiv.childNodes[0].innerHTML += txt;
			var u2 = $cE("Li");
			this.btnPic.childNodes[0].appendChild(u2);
			u2.innerHTML = pic;
			u2.className = this._nc;
			//this.btnPic.childNodes[0].innerHTML += pic;
		}
		this.AttachMoveEvent();
		this.picCount++;
	},
	AttachMoveEvent: function() {
		var i = this.picCount;
		var u = this.getChildElement(this.btnPic)[i];
		u.attachEvent('onmouseover', this.ProcessFocus.Exec(this, i - 1));
		u.attachEvent('onmouseout', function(e) { e.Play(e._to); }.Exec(this));
	},
	ProcessFocus: function(e, i) {
		e.RestoreNoramlCss();
		e._c = i;
		e.MovePicButton(e);
		clearInterval(e.p);
	},
	RestoreNoramlCss: function() {
		if (this._c != -1) {
			this.getChildElement(this.txtDiv)[this._c].className = this._nc;
			this.getChildElement(this.btnPic)[this._c].className = this._nc;
		}
	},
	MovePicButton: function(c) {
		c.RestoreNoramlCss();
		c._c = c._c == c.picCount - 1 ? -1 : c._c;
		c._c++;
		moveElement(c.bigPic, 0, c._c * c._h, 5);
		c.getChildElement(c.txtDiv)[c._c].className = c._sc;
		c.getChildElement(c.btnPic)[c._c].className = c._sc;
	},
	ClearItem: function() {
		this.bigPic.childNodes[0].innerHTML = '';
		this.txtDiv.childNodes[0].innerHTML = '';
		this.btnPic.childNodes[0].innerHTML = '';
	},
	getChildElement: function(ele) {
		return ele.childNodes[0].childNodes;
	},
	Play: function(t) {
		if (t)
			this._to = t;
		this.p = setInterval(this.MovePicButton.Exec(this), t);
	}
}




Function.prototype.Exec = function() {
	var __method = this;
	var arg = arguments;
	return function() {
		__method.apply(window, arg);
	}
}

