﻿function $(id) {
	return document.getElementById(id);
}

//图片类scrollImg
Array.prototype.addBefore = function(arr) {
	if (this == []) return arr;
	else return arr.concat(this);
};
Array.prototype.addAfter = function(arr) {
	if (this == []) return arr;
	else return this.concat(arr);
};
Array.prototype.indexOf = function(substr, start) {
	var ta, rt, d = '\0';
	if (start != null) { ta = this.slice(start); rt = start; } else { ta = this; rt = 0; }
	var str = d + ta.join(d) + d, t = str.indexOf(d + substr + d);
	if (t == -1) return -1; rt += str.slice(0, t).replace(/[^\0]/g, '').length;
	return rt;
};
String.prototype.trim = function() {
	return this.replace(/^\s*|\s*$/g, "");
};

function autoSize(obj, MAX_WIDTH, MAX_HEIGHT) {
	var IMG_MARGIN = 0;
	var tImg = new Image();
	tImg.onload = function() {
		var w = this.width;
		var h = this.height;
		var tw = 0, th = 0;
		if (w <= MAX_WIDTH && h <= MAX_HEIGHT) {
			tw = w;
			th = h;
		}
		else {
			if (w/h>MAX_WIDTH/MAX_HEIGHT) {
				tw = MAX_WIDTH;
				th = MAX_WIDTH * h / w;
			}
			else {
				th = MAX_HEIGHT;
				tw = MAX_HEIGHT * w / h;
			}
		}
		obj.width = tw;
		obj.height = th;
		obj.style.marginLeft = IMG_MARGIN + (MAX_WIDTH - tw) / 2 + 'px';
		obj.style.marginTop = IMG_MARGIN + (MAX_HEIGHT - th) / 2 + 'px';
	}
	tImg.src = obj.src;
}
function autoBigSize(obj, MAX_WIDTH, MAX_HEIGHT) {
	var IMG_MARGIN = 0;
	var tImg = new Image();
	tImg.onload = function() {
		var w = this.width;
		var h = this.height;
		var tw = 0, th = 0;
		if (w/h>MAX_WIDTH/MAX_HEIGHT) {
			tw = MAX_WIDTH;
			th = MAX_WIDTH * h / w;
		}
		else {
			th = MAX_HEIGHT;
			tw = MAX_HEIGHT * w / h;
		}
		obj.width = tw;
		obj.height = th;
		//obj.style.marginLeft = IMG_MARGIN + (MAX_WIDTH - tw) / 2 + 'px';
		//obj.style.marginTop = IMG_MARGIN + (MAX_HEIGHT - th) / 2 + 'px';
	}
	tImg.src = obj.src;
}

function getSizeBig(imgobj, MAX_WIDTH, MAX_HEIGHT) {
	var W = imgobj.width;
	var H = imgobj.height;
	if (W == 0 || H == 0) {
		W = parseFloat(imgobj.style.width);
		H = parseFloat(imgobj.style.height);
	}
	if (isNaN(W) || isNaN(H)) {
		W = 1;
		H = 1;
	}
	if (W / H > MAX_WIDTH / MAX_HEIGHT) {
		return { w: MAX_WIDTH, h: MAX_WIDTH * H / W }
	} else {
		return { w: MAX_HEIGHT * W / H, h: MAX_HEIGHT }
	}
}

var ScrollImg = function(coverObj, imgNodeBox, bigImgObj) {
	_me = this;
	this.imgNodeBox = $(imgNodeBox);
	this.bigImgObj = $(bigImgObj);
	this.isContinue = false;
	this.imgIDList = [];
	this.obj = $(coverObj);
	this.oneStep = 127;
	this.allSkip = 0;
	this.goNum = 1;
	this.skip = 1;
	this.skipTime = 1;
	this.skipSum = 0;
	this.direction = 1;
	this.selNode = null;
	this.getNode = null;
	this.addLen = 0;
	this.bnt1 = null;
	this.bnt2 = null;
	this.disBnt = 0;
	this.isGoing = false;

	this.clear = function() {
		this.skipSum = 0;
		this.allSkip = 0;
		this.goNum = 1;
		this.isGoing = false;
	};
	//单步滚动,滚动一个图片的距离后停止
	this.scroll = function() {
		if (_me.checkStat()) {
			if (this.direction == -1) {
				this.clear();
				this.disableBnt(-1);
				this.enableBnt(1);
				return false;
			}
			if (this.direction == 1) {
				this.clear();
				this.disableBnt(1);
				this.enableBnt(-1);
				return false;
			}
			return false;
		}
		if (this.disBnt) this.enableBnt(this.disBnt);
		_me.allSkip = _me.oneStep * _me.goNum;
		if (_me.skipSum < _me.allSkip) {
			var nowLeft = parseInt(_me.obj.style.left);
			_me.obj.style.left = (nowLeft - _me.skip * _me.direction) + 'px';
			_me.skipSum += _me.skip;
			setTimeout('_me.scroll()', _me.skipTime);
		} else {
			_me.clear();
			_me.scrollEnd();
		}
	};
	this.scrollEnd = function() { };
	this.setCnt = function(arg) {
		this.isContinue = arg;
	};
	this.go = function(dirc) {
		this.isGoing = true;
		this.goNum = 1;
		this.direction = dirc;
		this.scroll();
		this.canGoon(this.goNum * this.oneStep / this.skip * this.skipTime);
		this.scrollEnd = function() {
			if (this.isContinue) this.scroll();
		}
	};
	this.sel = function(obj) {
		var isrc = _me.selNode.src;
		var i = "<a	href='" + _me.imgIDList[_me.selNode.id - 1] + "' target='_blank' title='点击可查看原图'><img id='showBigImg' onload='autoBigSize(this,354,266)' width='354' height='266' border=0 src='" + isrc + "'></a>";
		_me.bigImgObj.innerHTML = i;
	};
	this.goAsel = function(direc) {
		var imgs = this.obj.getElementsByTagName('img');
		var iLen = imgs.length;
		var node = null;
		for (var i = 0; i < iLen; i++) {
			if (this.selNode == imgs[i]) {
				if (direc == -1)
					if (i - 1 >= 0) {
					node = imgs[i - 1];
				} else {
					node = imgs[iLen - 1];
					if (iLen > 4) _me.obj.style.left = minLeft + 'px';
				}
				else if (direc == 1)
					if (i + 1 <= iLen - 1) {
					node = imgs[i + 1];
				} else {
					node = imgs[0];
					if (iLen > 4) _me.obj.style.left = '0px';
				}
				break;
			}
		}
		this.selScroll(node);
	};
	this.selScroll = function(selNode) {
		var node = selNode;
		var imgs = this.obj.getElementsByTagName('img');
		var iLen = imgs.length;
		var nodeNum = -1;
		for (var i = 0; i < iLen; i++) {
			if (imgs[i] == node) { nodeNum = i; imgs[i].parentNode.className = 'picA'; }
			else if (imgs[i].parentNode.className != 'albumImg') imgs[i].parentNode.className = 'pic';
		}
		if (this.selNode == node) return; //同一个图片被选中
		this.selNode = node;
		var offsetStep = nodeNum - parseInt(Math.abs(parseInt(this.obj.style.left)) / this.oneStep) - 2;
		this.goNum = Math.abs(offsetStep);
		if (offsetStep > 0) {
			this.direction = offsetStep / this.goNum;
		} else {
			this.direction = -1;
		}
		this.scroll();
		this.canGoon(this.goNum * this.oneStep / this.skip * this.skipTime);
		var isrc = _me.selNode.src;
		var ibigSrc = _me.selNode.src;
		var imgObj = getSizeBig(_me.selNode, 354, 266);
		var i = "<a	href='" + this.imgIDList[_me.selNode.id - 1] + "' target='_blank' title='点击可查看原图'><img id='showBigImg' width='" + imgObj.w + "'	height='" + imgObj.h + "'	border=0 src='" + isrc + "'></a>";
		_me.bigImgObj.innerHTML = i;
		if (imgObj.w < 354) $('showBigImg').style.marginLeft = ((354 - imgObj.w) / 2) + 'px';
		if (imgObj.h < 266) $('showBigImg').style.marginTop = ((266 - imgObj.h) / 2) + 'px';
	};


	this.checkStat = function() {
		var objLeft = parseInt(this.obj.style.left);
		var minLeft = this.minLeft();
		var imgs = this.obj.getElementsByTagName('img');
		if (this.direction == -1 && objLeft + 1 > 0) { this.getNode = imgs[0]; return true; }
		if (this.direction == 1 && objLeft - 1 < minLeft) { this.getNode = imgs[imgs.length - 1]; return true; }
		//这里的1为故意增加的余量,防止判断过度	
		return false;
	};
	this.minLeft = function() {
		return this.oneStep * (this.obj.getElementsByTagName('img').length - 4) * (-1);
	};

	this.initNode = function(selImgID) {
		while (this.imgNodeBox.firstChild)
			this.imgNodeBox.removeChild(this.imgNodeBox.firstChild);
		var iLen = this.imgIDList.length;
		for (var i = 0; i < iLen; i++) {
			var nNode = this.imgIDList[i];
			var tempNode = null;
			tempNode = this.createNode(i + 1, nNode);
			this.imgNodeBox.appendChild(tempNode);
		}
		this.init(selImgID || this.selNode.id);
		this.addLen = 0;
	};
	this.init = function(imgID) {
		var imgid = imgID || this.selNode.id;
		var imgs = this.obj.getElementsByTagName('img');
		var iLen = imgs.length;
		for (var i = 0; i < iLen; i++) {
			if (i == (imgid - 1)) {
				imgs[i].parentNode.className = 'picA'; this.selNode = imgs[i];
				if (iLen > 4) {
					if (i > 2 && iLen - i > 2) this.obj.style.left = (-1) * (i - 2) * this.oneStep + 'px';
					if (i <= 2) this.obj.style.left = "0px";
					else if (iLen - i <= 2) this.obj.style.left = (-1) * this.oneStep * (iLen - 4) + 'px';
				}
				else {
					this.obj.style.left = (4 - iLen) / 2 * this.oneStep + "px";
				}
				var objLeft = parseInt(this.obj.style.left);
				var minLeft = this.minLeft();
				if (objLeft + 1 > 0) { this.disableBnt(-1); }
				else if (objLeft - 1 < minLeft) { this.disableBnt(1); }
				this.sel();
			}
			imgs[i].onclick = function(evt) { if (_me.isGoing) return; else _me.selScroll(this); }
			imgs[i].parentNode.onclick = function() { if (_me.isGoing) return; else _me.selScroll(this.getElementsByTagName('img')[0]); }
		}

		if (this.addLen) if (this.direction == -1) this.obj.style.left = (-1) * this.addLen * this.oneStep + 'px';
		else if (this.direction == 1) this.obj.style.left = (-1) * (this.imgIDList.length - this.addLen - 4) * this.oneStep + 'px';

	};
	this.createNode = function(imgID, imgPath) {
		var rNode = document.createElement('td');
		var nImgsrc = imgPath;
		var nImg = '';
		rNode.width = '107px';
		rNode.height = '81px';
		nImg = "<div class='pic'><IMG id='" + imgID + "' width='107' height='81' src=" + nImgsrc + " onload='autoSize(this,107,81)'></div>";
		rNode.innerHTML = nImg;
		return rNode;
	};

	this.bntPicName = function(Isrc) {
		var srcArr = Isrc.split('/');
		return srcArr[srcArr.length - 1];
	};
	this.setBnt = function(bnt1, bnt2) {
		this.bnt1 = $(bnt1);
		this.bnt2 = $(bnt2);
		if (this.bnt1 && this.bnt2) this.bandBnt();
	};
	this.bandBnt = function() {
		this.bnt1.onclick = function() { return false; }
		this.bnt2.onclick = function() { return false; }
		this.bnt1.onmousedown = function() { if (_me.isGoing) return; _me.go(-1); _me.setCnt(1); return false; }
		this.bnt1.ondragstart = function() { return false; }
		this.bnt1.onmouseup = function() { _me.setCnt(0); }
		this.bnt2.onmousedown = function() { if (_me.isGoing) return; _me.go(1); _me.setCnt(1); return false; }
		this.bnt2.ondragstart = function() { return false; }
		this.bnt2.onmouseup = function() { _me.setCnt(0); }
		document.onmouseup = function() { _me.setCnt(0); }
		this.bnt1.style.cursor = 'pointer';
		this.bnt2.style.cursor = 'pointer';
	};
	this.enableBnt = function(dBnt) {
		if (!dBnt) return;
		if (!this.bnt1 || !this.bnt2) return;
		switch (dBnt) {
			case -1:
				this.bnt1.src = this.bnt1.src.replace('index_but_03_1.gif', 'index_but_03.gif');
				this.bnt1.title = '';
				this.bnt1.style.cursor = 'pointer';
				break;
			case 1:
				this.bnt2.src = this.bnt2.src.replace('index_but_04_1.gif', 'index_but_04.gif');
				this.bnt2.title = '';
				this.bnt2.style.cursor = 'pointer';
				break;
		}
	};
	this.disableBnt = function(direc) {
		//direc, 哪个按钮, -1	向前,	1向后
		if (!direc) return;
		if (!this.bnt1 || !this.bnt2) return;
		this.disBnt = direc;
		switch (direc) {
			case -1:
				this.bnt1.src = this.bnt1.src.replace('index_but_03.gif', 'index_but_03_1.gif');
				this.bnt1.title = '已到达第一张照片'; this.bnt1.style.cursor = 'default'; break;
			case 1:
				this.bnt2.src = this.bnt2.src.replace('index_but_04.gif', 'index_but_04_1.gif');
				this.bnt2.title = '已到达最后一张照片'; this.bnt2.style.cursor = 'default'; break;
		}
	};
	this.canGoon = function(t) {
		this.isGoing = true;
		window.keypress = true;
		setTimeout("_me.isGoing=false;window.keypress=false;", t);
	};

	var keypress = false;
};