/* [www.artfairtokyo.com]
 * Summary: script for "about" category image gallery
 * LastModified: 2009-12-17
 * This script requires jquery.js
 */

function galleryPic(containerEle) {
	this.container = containerEle;
	this.$links = $('a', containerEle);
	this.$imgs = this.$links.find('img');
	this.largeImg = document.createElement('img');
	this.wrapPic = document.createElement('p');
	this.wrapCap = document.createElement('p');
	this.setup();
}
galleryPic.prototype = {
	suffix: '_on',
	setup: function() {
		var self = this;
		this.$imgs.each(function(i){
			self.setImgProp(i);
		});
		// サムネイル一覧の初期化
		this.$imgs[0].src = this.$imgs[0].newSrc;
		this.wrapCap.innerHTML = this.$imgs[0].alt;
		// 大画像の初期化
		this.wrapPic.className = 'pic';
		this.wrapCap.className = 'cap';
		this.largeImg.src = this.$links[0].href;
		this.largeImg.alt = this.$imgs[0].alt;
		$(this.wrapPic).append(this.largeImg);
		$(this.container).append(this.wrapPic).append(this.wrapCap);
		// プリロード
		this.preload();
		// イベントセット
		this.$imgs.bind("mouseover focus", function() {
			self.chLargeImg(this);
			self.chImgOn(this);
		});
		this.$links.bind("click", function() { return false; });
	},
	setImgProp: function(index) {
		var img = this.$imgs[index];
		var extPos = img.src.lastIndexOf('.');
		var fname = img.src.slice(0, extPos);
		var ext = img.src.slice(extPos, img.src.length);
		img.newSrc = fname + this.suffix + ext;
		img.orgSrc = img.src;
		img.containerLink = this.$links[index];
		return img;
	},
	chImgOn: function(img) {
		this.chAllImgOff();
		img.src = img.newSrc;
	},
	chAllImgOff: function() {
		this.$imgs.each(function() {
			this.src = this.orgSrc;
		});
	},
	chLargeImg: function(img) {
		this.largeImg.src = img.containerLink.href;
		this.largeImg.alt = img.alt;
		this.wrapCap.innerHTML = img.alt;
	},
	preload: function(imgsrc) {
		// サムネイルのプリロード
		this.$imgs.each(function() {
			(new Image()).src = this.newSrc;
		});
		// 大画像のプリロード
		this.$links.each(function() {
			(new Image()).src = this.href;
		});
	}
};
$('div.area_pic').each(function() {
	new galleryPic(this);
});
