/* slideBanner
*
*/
function SlidePlayer(el, width, height, itvl) {
this.el = (typeof el == "string") ? document.getElementById(el) : el;
this.width = width || 878;
this.height = height || 320;
if (this.height == -1) this.height = this._getMaxHeight();
this.cnt = $D.getElementsByClassName("Slides", "ul", this.el)[0];
this.wrapper = this.el.parentNode.className.indexOf("SliderPlayer-wrapper") != -1 ? this.el.parentNode : null;
this.len = this.cnt.getElementsByTagName("li").length;
this.idx = 0;	//current index of li
this.itvl = itvl || 3000;	//自动滚动的时间间隔
this.pause = false;
this.init();
}
SlidePlayer.prototype = {
init: function () {
this._adjustSize();
this.showPageNum();
this.show(0);
},
show: function (i) {
clearTimeout(this.loop);
if (this.pause) return;
if (this.len == 0) return;
if (i > this.len - 1) i = 0;
for (var j = 0; j < this.len; j ++)
this.showPage[j].className = "";
this.showPage[i].className = "selected";
var _this = this;
this.idx = i;
if (this.height * i == this.cnt.scrollTop) {
var _show = function () {
_this.show(i + 1);
};
_this.loop = setTimeout(_show, _this.itvl);
} else {
this.slide(i);
}
},
/**
* 方法：在右下角显示页码
*/
showPageNum: function () {
var ul0 = $D.getElementsByClassName("ShowPage", "ul", this.el)[0];
if (ul0) ul0.parentNode.removeChild(ul0);
var ul = document.createElement("ul"), li, _this = this;
ul.className = "ShowPage";
this.showPage = [];
for (var i = 0; i < this.len; i ++) {
li = mkEl("li", {"title": i + 1}, mkTxtEl(i + 1));
li.onmouseover = function () {
_this.show(parseInt(this.getAttribute("title")) - 1);
_this.pause = true;
};
li.onmouseout = function () {
_this.pause = false;
_this.show(parseInt(this.getAttribute("title")) - 1);
};
ul.appendChild(li);
this.showPage.push(li);
}
if (this.len == 1) return;
this.el.insertBefore(ul, this.el.firstChild);
},
slide: function (i) {
var sign = this.idx <= i ? 1 : -1,
step = 0,
_this = this,
scroll = function () {
if (_this.height * _this.idx == _this.cnt.scrollTop) {
_this.show(_this.idx);
return;
}
step = Math.floor((_this.height * _this.idx - _this.cnt.scrollTop) / 10);
if (step == 0) step = sign;
_this.cnt.scrollTop += step;
_this._slider = setTimeout(scroll, 10);
};
scroll();
},
/**
* 方法：移除一个滚动图片
* 参数：
* 	i: Integer 要移除的图片的下标，从0开始
*/
remove: function (i) {
if (this.len <= i) return;
var lis = this.cnt.getElementsByTagName("li"), li = lis[i];
li.parentNode.removeChild(li);
this.len = lis.length;
this.init();
},
/**
* 方法：添加一个滚动图片
* 参数：
* 	i: Integer 插入的位置，0为插入在最前面，1为插入在第二位...
* 	src: String 图片地址
* 	href: String 图片指向的URL链接
*/
add: function (i, src, href) {
var _this = this, lis = this.cnt.getElementsByTagName("li"), li, img;
if (YAHOO.env.ua.ie) src += (src.indexOf("?") == -1 ? "?" : "&") + "r=" + Math.random();
img = mkEl("img", {src: src});
img.onload = function () {
_this._adjustSize();
};
if (href) {
li = mkEl("li", null, mkEl(
"a", {href: href, target: "_blank"}, img)
);
} else {
li = mkEl("li", null, mkEl(
"div", null, img)
);
}
if (i < this.len && lis[i]) {
this.cnt.insertBefore(li, lis[i]);
} else {
this.cnt.appendChild(li);
}
this.len ++;
this.init();
setTimeout(function () {
//_this._adjustSize();
}, 100);
},
/**
* 方法：update
* 	更新滚动幻灯为另一组图片
* 参数：
* 	dl <Array>: 另一组图片
* 		格式为：[{src: "http://...jpg", url: "http://...html"}, {...}, ...]
*/
update: function (dl) {
while (this.len > 0) {
this.remove(0);
}
for (var i = dl.length - 1; i >= 0; i --) {
if (dl[i].src == "") continue;
this.add(0, dl[i].src, dl[i].url);
}
},
_getMaxHeight: function () {
var h = 0;
$D.getElementsBy(function (o) {return true;},
"img",
this.el,
function (o) {
if (h < o.height)
h = o.height;
});
return h || 200;
},
/**
* 方法：调整每一个li的显示尺寸
* 	宽度固定，高度为最大图片的高度
*/
_adjustSize: function () {
var _this = this;
this.height = this._getMaxHeight();
$D.setStyle(this.cnt, "height", this.height + "px");
$D.getElementsBy(function (o) {return true;},
"li", this.cnt, function (o) {
$D.setStyle(o, "width", _this.width + "px");
$D.setStyle(o, "height", _this.height + "px");
$D.setStyle(o, "padding-top", "0");
var img = o.getElementsByTagName("img")[0],
h = img ? (img.height || img.offsetHeight) : 0,
pt;
if (h < _this.height) {
pt = Math.floor((_this.height - h) / 2);
$D.setStyle(o, "padding-top", pt + "px");
$D.setStyle(o, "height", _this.height - pt + "px");
}
o.onmouseover = function () {
_this.pause = true;
};
o.onmouseout = function () {
_this.pause = false;
_this.show(_this.idx);
};
});
$D.setStyle(this.el, "width", this.width + "px");
$D.setStyle(this.el, "height", this.height + "px");
if (this.wrapper) $D.setStyle(this.wrapper, "height", this.height + "px");
}
};

