var PImgPlayer = {
	_container : null,
	_index : 0,
	_items : [],
	_imgs : [],
	_timer : null,
	intervalTime : 3000,
	init : function(objId) {
		this._container = document.getElementById(objId);
		var imgNav = '<ul id="imgNav">';
		var length = this._items.length-1;
		for (var i=length; i>=0; i--) {
			imgNav += '<li><span title="'+this._items[i].title+'" onmouseover="PImgPlayer.mouseOver(this);" onmouseout="PImgPlayer.mouseOut(this);" onclick="PImgPlayer.play(\''+i+'\');return false;"';
			if (i==this._index) {
			    imgNav += ' class="selected"';
			} else {
			    imgNav += ' class="default"';
			}
			imgNav += '>'+(i+1)+'</span></li>';
		}
		var player = '<a href="'+this._items[this._index].link+'" title="'+this._items[this._index].title+'" target="_self" style="background:url('+this._items[0].img+') no-repeat top left"></a>'+imgNav;
		this._container.innerHTML = player;
		this._timer = setInterval('PImgPlayer.play()', this.intervalTime);
	},
	addItem : function(_title, _link, _imgurl) {
		this._items.push ({title:_title, link:_link, img:_imgurl});
		var img = new Image();
		img.src = _imgurl;
		this._imgs.push(img);
	},
	play : function (index) {
		if (index != null) {
   			this._index = index;
   			clearInterval(this._timer);
   			this._timer = setInterval('PImgPlayer.play()', this.intervalTime);
		} else {
  			this._index = this._index<this._items.length-1 ? this._index+1:0;
		}
		var link = this._container.getElementsByTagName("A")[0];
		if (link.filters) {
  	 		var ren = Math.floor(Math.random()*(link.filters.length));
   			link.filters[ren].Apply();
   			link.filters[ren].play();
		}
		link.href = this._items[this._index].link;
		link.title = this._items[this._index].title;
		link.style.background = 'url('+this._items[this._index].img+') no-repeat top left';
		var imgNav = '';
		for (var i=this._items.length-1; i>=0; i--) {
			imgNav += '<li><span title="'+this._items[i].title+'" onmouseover="PImgPlayer.mouseOver(this);" onmouseout="PImgPlayer.mouseOut(this);" onclick="PImgPlayer.play('+i+');return false;"';
   			if (i == this._index) {
    			imgNav += ' class="selected"';
  			} else {
   				imgNav += ' class="default"';
   			}
   			imgNav += '>'+(i+1)+'</span></li>';
		}
		document.getElementById('imgNav').innerHTML = imgNav;
	},
	mouseOver : function(obj) {
		var i = parseInt(obj.innerHTML);
		if (this._index != i-1) {
	   		obj.style.color = '#000';
		}
	},
	mouseOut : function(obj){
		obj.style.color = '#fff';
	}
}