// JavaScript Document

// 图片播放器
function images(){
	//私有属性
	var imageIndex = 1;
	//公有属性
	this.parentObject = null;
	this.width = 640;					//图片轮播父对象宽度
	this.height = 480;					//图片轮播父对象高度
	this.imagesArray = null;
	this.time = 3000;
	
	//私有方法
	function addObject(object,objectTag,cssClass){
		var tmpObject;
		
		tmpObject = document.createElement(objectTag);
		tmpObject.setAttribute('class',cssClass);
		object.appendChild(tmpObject);
		
		return tmpObject;
	}
	
	function addButton(objectArray,imagesPath){
		var tmpObject;
		
		for(var i = 1; i < imagesPath.length + 1; i++){
			tmpObject = addObject(objectArray[3],'li','');
			tmpObject.innerHTML = i;
			tmpObject.style.cssText = 'float:left; margin:4px 5px; display:block; width:14px; height:14px; text-align:center; line-height:14px; border:1px #555 solid; cursor:pointer; overflow:hidden';
			tmpObject.onclick = function(){
				imageIndex = this.innerHTML - 1;
				objectArray[1].setAttribute('src',imagesPath[imageIndex][0]);
				this.style.background = '#fff';
			}
		}
	}
	
	function imagePlay(objectArray,imagesPath,time){
		var timerId,imagesCount = imagesPath.length,tmp,liSet;
		liSet = objectArray[3].getElementsByTagName('li');
		liSet[0].style.background = '#fff';
		objectArray[1].setAttribute('src',imagesPath[0][0]);
		objectArray[0].setAttribute('href',imagesPath[0][2]);
		objectArray[2].innerHTML = imagesPath[0][1];
		timerId = setInterval(function(){
			for(var i = 0; i < liSet.length; i++){
				liSet[i].style.background = 'none';
			}
			if(imageIndex < imagesCount){
				objectArray[1].setAttribute('src',imagesPath[imageIndex][0]);
				objectArray[2].innerHTML = imagesPath[imageIndex][1];
			}
			else{
				imageIndex = 0;
				objectArray[1].setAttribute('src',imagesPath[imageIndex][0]);
				objectArray[2].innerHTML = imagesPath[imageIndex][1];
			}
			liSet[imageIndex].style.background = '#fff';
			objectArray[0].setAttribute('href',imagesPath[imageIndex][2]);
			imageIndex ++;
		},time);
		
		return timerId;
	}
	
	function imageStop(timerId){
		clearInterval(timerId);
	}
	
	//公有方法
	this.config = function(objectId,imagesArray,time){
		this.parentObject = document.getElementById(objectId);
		this.width = this.parentObject.offsetWidth;					//图片轮播父对象宽度
		this.height = this.parentObject.offsetHeight;				//图片轮播父对象高度
		this.imagesArray = imagesArray;
		this.time = time;
	}
	
	this.imageLoad = function(){
		var objectArray = new Array(),containerObject,barObject,buttonContainerObject,timerId;
		
		containerObject = addObject(this.parentObject,'div','bgColor');
		containerObject.style.cssText ='position:relative; width:' + this.width + 'px; height:'+ this.height +'px;'
		
		objectArray[0] = addObject(containerObject,'a','');
		objectArray[0].style.cssText ='position:absolute; width:' + this.width + 'px; height:'+ this.height +'px;';
		objectArray[0].setAttribute('target','_blank');
		
		objectArray[1] = addObject(objectArray[0],'img','');
		objectArray[1].style.cssText ='position:absolute; width:' + this.width + 'px; height:'+ this.height +'px; border:0 none;'

		barObject = addObject(containerObject,'div','barColor');
		tmp = this.height - 24;
		barObject.style.cssText ='position:absolute; left:0; top:' + tmp + 'px; z-index:100; width:' + this.width + 'px; height:24px;line-height:24px; filter:alpha(opacity=70); -moz-opacity:0.7; opacity:0.7; background:#e8e8e8;'
		
		
		objectArray[2] = addObject(barObject,'div','imagesTitle');
		objectArray[2].style.cssText = 'float:left; margin-left:10px; height:24px; line-height:24px;';
		
		objectArray[3] = addObject(barObject,'ul','imagesTitle');
		objectArray[3].style.cssText = 'float:right; margin: 0 10px; padding:0; height:24px; display:block; list-style:none;';
		addButton(objectArray,this.imagesArray);
		
		//图片自动播放
		
		timerId = imagePlay(objectArray,this.imagesArray,this.time);
		
	}
}
