/**
 * 焦点图类. 
 * 创建实例时, 需同时将实例名称传进去
 */
function FocusPicture(fname, ifocus, ifocus_piclist, ifocus_btn, normal, current, imageHeight, maxCurrentNum, autoChangeInterval) {
	this.instanceName = fname; // 类实例名称

	this.ifocus = ifocus ? ifocus : "ifocus"; // 焦点图外围div的ID
	this.ifocusPicList = ifocus_piclist ? ifocus_piclist : "ifocus_piclist"; // 焦点图图片显示区域外围div的ID
	this.ifocusBtn = ifocus_btn ? ifocus_btn : "ifocus_btn"; // 焦点图感应按钮外转div的ID
	this.normalClassName_ejia = normal ? normal : "normal"; // 按钮正常时的class样式
	this.currentClassName_ejia = current ? current : "current"; // 按钮选中时的class样式

	this.imageHeight = imageHeight ? imageHeight : 348; // 图片高度, 用于图片滑动, 即滑动一张图片的高度
	this.maxCurrentNum = maxCurrentNum ? maxCurrentNum : 4; // 最大当前选中的图片号, 从0开始, 3表示第四张
	this.autoChangeInterval = autoChangeInterval ? autoChangeInterval : 4000; // 图片轮换的时间间隔, 单位为毫秒

	this.isAutoChange = true; // 设置是否自动更换图片, 为true表示自动更换, 为false表示停止
	this.slideSpeed = 10; // 图片滑行速度, 值越大, 速度越慢
	this.invokeInterval = 5; // 循环调用moveElement函数的时间间隔, 单位为毫秒

	this.setIsAutoChange = function(flag) {
		this.isAutoChange = flag;
	}

	/**
	 * 设置图片滑动方法, 每调用一次, 调整一点, 循环调用调整到指定位置
	 *
	 * @param elementID 图片div的ID
	 * @param final_x 调整后的x坐标
	 * @param final_y 调整后的y坐标
	 */
	this.moveElement_ejia = function(elementID, final_x, final_y) {
		if (!document.getElementById)
			return false;
		if (!document.getElementById(elementID))
			return false;

		var elem = document.getElementById(elementID);
		
		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) / this.slideSpeed);
			xpos = xpos + dist;
		}
		
		if (xpos > final_x) {
			var dist = Math.ceil((xpos - final_x) / this.slideSpeed);
			xpos = xpos - dist;
		}
		
		if (ypos < final_y) {
			var dist = Math.ceil((final_y - ypos) / this.slideSpeed);
			ypos = ypos + dist;
		}
		
		if (ypos > final_y) {
			var dist = Math.ceil((ypos - final_y) / this.slideSpeed);
			ypos = ypos - dist;
		}
		
		elem.style.left = xpos + "px";
		elem.style.top = ypos + "px";
		
		elem.movement = cusSetTimeout(this, this.moveElement_ejia, this.invokeInterval, elementID, final_x, final_y);
	}

	
	/**
	 * 设置按钮正常样式
	 */
	this.classNormal_ejia = function(iFocusBtnID){
		var iFocusBtns= document.getElementById(iFocusBtnID).getElementsByTagName('li'); // 按钮
		for(var i=0; i < iFocusBtns.length; i++) { // 设置按钮和描述正常样式
			iFocusBtns[i].className = this.normalClassName_ejia;
		}
	}


	/**
	 * 设置按钮选中样式
	 */
	this.classCurrent_ejia = function(iFocusBtnID, n){
		var iFocusBtns= document.getElementById(iFocusBtnID).getElementsByTagName('li');
		// 设置按钮和描述当前选中的样式
		iFocusBtns[n].className = this.currentClassName_ejia;
	}


	/**
	 * 页面加载完毕后执行方法.
	 * 
	 */
	this.iFocusChange = function() {
		//alert(this.ifocus + " " + document.getElementById(this.ifocus) + " " + (this == window));
		
		if (!document.getElementById(this.ifocus))
			return false;

		// 当前调用的实例对象
		var instance = eval(this.instanceName);

		document.getElementById(this.ifocus).instance = instance;

		// 鼠标经过时图片区域时停止轮转
		document.getElementById(this.ifocus).onmouseover = function() {
			this.instance.setIsAutoChange(false);
		};

		// 鼠标离开时图片区域时继续轮转
		document.getElementById(this.ifocus).onmouseout = function() {
			this.instance.setIsAutoChange(true);
		};

		var iFocusBtns = document.getElementById(this.ifocusBtn).getElementsByTagName('li');
		var listLengths = iFocusBtns.length;
		iFocusBtns[0].instances = instance;
		iFocusBtns[0].ifocusPicList = this.ifocusPicList;
		iFocusBtns[0].ifocusBtn = this.ifocusBtn;

		iFocusBtns[0].onmouseover = function() {
			this.instances.moveElement_ejia(this.ifocusPicList, 0, 0, 5);
			this.instances.classNormal_ejia(this.ifocusBtn);
			this.instances.classCurrent_ejia(this.ifocusBtn, 0);
		}
		for (var i = 1; i < listLengths; i++) {
			iFocusBtns[i].instances = instance;
			iFocusBtns[i].ifocusPicList = this.ifocusPicList;
			iFocusBtns[i].ifocusBtn = this.ifocusBtn;

			iFocusBtns[i].value = i;
			iFocusBtns[i].yAxis = -i * this.imageHeight;
			iFocusBtns[i].onmouseover = function() {
				this.instances.moveElement_ejia(this.ifocusPicList, 0, this.yAxis);
				this.instances.classNormal_ejia(this.ifocusBtn);
				this.instances.classCurrent_ejia(this.ifocusBtn, this.value); // 直接把i设到this.value的位置就不行了, 调用时i会随for循环的改变而改变
			}
		}
	}


	/**
	 * 定时调用该方法, 可以使图片自动更换
	 */
	this.autoiFocus = function() {
		if(!document.getElementById(this.ifocus)) return false;

		// 停止轮转
		if (!this.isAutoChange) return false;
		
		// 获取所有按钮
		var focusBtnList = document.getElementById(this.ifocusBtn).getElementsByTagName('li');
		var listLength = focusBtnList.length;
		
		// 找到当前显示的按钮索引号
		for (var i = 0; i < listLength; i++) {
			if (focusBtnList[i].className == this.currentClassName_ejia) {
				var currentNum = i;
				break;
			}
		}
		// 如果当前在最后一个图片, 则显示第一张
		if (currentNum == this.maxCurrentNum ){
			this.moveElement_ejia(this.ifocusPicList, 0, 0);
			this.classNormal_ejia(this.ifocusBtn);
			this.classCurrent_ejia(this.ifocusBtn, 0);
		} else { // 直接显示下一张
			this.moveElement_ejia(this.ifocusPicList, 0, -(currentNum + 1) * this.imageHeight);
			this.classNormal_ejia(this.ifocusBtn);
			this.classCurrent_ejia(this.ifocusBtn, currentNum + 1);
		}
	}

	/**
	 * 启动图片自动轮转的定时器
	 */
	this.startAutoChange = function() {
		cusSetInterval(this, this.autoiFocus, this.autoChangeInterval);
		// setInterval(this.autoiFocus, this.autoChangeInterval); //这样调用的话, 好像this的调用者会变成window
	}
}
