/*
 SlideShow v1.0
 By Kan 2009-8-25
*/
var $ = function (id) {
	return "string" == typeof id ? document.getElementById(id) : id;
};
var Bind = function(object, fun) {
	var args = Array.prototype.slice.call(arguments).slice(2);
	return function() {
		return fun.apply(object, args.concat(Array.prototype.slice.call(arguments)));
	}
}
var forEach = function(array, callback, thisObject){
	if(array.forEach){
		array.forEach(callback, thisObject);
	}else{
		for (var i = 0, len = array.length; i < len; i++) { callback.call(thisObject, array[i], i, array); }
	}
}

var SlideShow = function(slider, imgs) {
	this._slider = $(slider);
	this._timer = null;//定时器
	this._target = [];
	this._count = imgs.length;//切换数量
	
	this.Index = 0;//当前索引
	this.TimeIntval = 5000,//滑动延时
	this.Onstart = null;
	
	for(i=0;i<this._count;i++)
	{
		var t_link = document.createElement('a');
		t_link.setAttribute("target","_blank");
		t_link.href=imgs[i]['link'];
		t_link.innerHTML = '<img src="'+imgs[i]["img"]+'">';
		this._slider.appendChild(t_link);
	}
	this._target = this._slider.getElementsByTagName("img") ;
};
SlideShow.prototype = {
  //开始切换
  Run: function(index) {
	//修正index
	index == undefined && (index = this.Index);
	index < 0 && (index = this._count - 1) || index >= this._count && (index = 0);
	this.Index=index;
	//设置参数
	for(i=0;i<this._count;i++)
		this._target[i].style.display = "none";
	this._target[index].style.display = "block";
	
	this.onStart();
  },
  //自动
  AutoRun: function() {
	clearTimeout(this._timer);
	this.Run(this.Index);
	this.Index++;
	this._timer = setTimeout(Bind(this, this.AutoRun), this.TimeIntval);
  },
  //暂停
  PauseRun: function() {
	clearTimeout(this._timer);
  },
  //继续
  GoonRun: function() {
	clearTimeout(this._timer);
	this._timer = setTimeout(Bind(this, this.AutoRun), this.TimeIntval);
  },
  FadeOut: function()
  {
	//window.alert(target.filters.alpha.Opacity);
	if(this._img.filters.alpha.Opacity>0)
	{
		this._img.filters.alpha.Opacity--;
		this.FadeOut();
	}
	else
	{
		this._target = this.Index++;
		this.FadeIn();
	}
  },
  FadeIn: function()
  {
	if(this._img.filters.alpha.Opacity<100)
	{
		this._img.filters.alpha.Opacity++;
		this.FadeIn();
	}
  }
};
