//2009-12-10: update

document.observe('dom:loaded', function() {
	replace_amazon_images();
});

//-------------------------
// amazon image replace
//-------------------------
//require prototype.js 1.6.0
//Author: SAKAI Yugo
//2009-12-10: first release
function replace_amazon_images(){
	var _imgs = document.getElementsByTagName('img');
	
	for(var i = 0; i < _imgs.length; i ++){
		var _elem = _imgs[i];
		
		if(_elem.src.match(/^http:\/\/images\-jp\./)){
			if(_elem.complete){
				replace_amazon_image(_elem);
			}
			else{
				Event.observe(_elem, 'load', function(_event){
					var _elem = Event.elemenet(_event);
					replace_amazon_image(_elem);
				});
			}
		}
	}
}

function replace_amazon_image(_elem){
	if(_elem.width == '1'){
		_elem.src = 'http://www.officek.jp/__KSite/parts/noimage140x140.gif';
	}
}

//-------------------------
// amazon image replace (old version)
//-------------------------
function replaceImage(img,asin){
	if(img.width == '1' && img.src.match(/.01./) && asin){
		img.src = 'http://www.officek.jp/_asin/' + asin + '.09.MZZZZZZZ.jpg';
	}else if(img.width == '1'){
		img.src = img.src.replace('.09.','.01.');
	}
}
function notFoundImage(img){
	img.src = '/__KSite/parts/noimage140x140.gif';
}

//-------------------------
// slideshow
//-------------------------
//require prototype.js 1.6.0
//require scriptaculous 1.8.1
//Author: SAKAI Yugo
//update
//2009-06-25: first release
//2010-01-27: キャプション対応

var Slideshow = Class.create();
Slideshow.prototype = {
	initialize: function(){
		
		if(!document.getElementsByTagName){ return; }
		
		this.divs = new Array();
		
		//slideshowをすべて取得
		var slideshows = document.getElementsByClassName('slideshow');
		
		for (var i = 0; i < slideshows.length; i ++) {
			
			this.divs[i] = new Array();
			this.divs[i]['div'] = slideshows[i];
			this.divs[i]['image_list'] = new Array();
			this.divs[i]['image_count'] = 0;
			this.divs[i]['index'] = 0;
			//パラメータ
			this.divs[i]['shuffle'] = false;
			this.divs[i]['sec'] = 8;
			this.divs[i]['caption'] = false;
			if(slideshows[i].readAttribute('title')){
				//shuffle
				if(slideshows[i].readAttribute('title').match(/shuffle/)){
					this.divs[i]['shuffle'] = true;
				}
				//sec
				if(slideshows[i].readAttribute('title').match(/([.0-9]+)sec/)){
					this.divs[i]['sec'] = slideshows[i].readAttribute('title').match(/([.0-9]+)sec/)[1];
				}
				//caption
				if(slideshows[i].readAttribute('title').match(/caption/)){
					this.divs[i]['caption'] = true;
				}
			}
			
			this.slideshow(i);
		}
	},
	
	slideshow: function(i){
		
		//スライドショーボックス
		var div = this.divs[i]['div'];
		
		//子要素を取得(pタグ)
		var paragraphs = div.getElementsByTagName('p');
		
		//画像リストを作成
		var img_elements = div.getElementsByTagName('img');
		this.divs[i]['image_count'] = img_elements.length;
		
		this.divs[i]['id'] = 'slideshow_box_' + i;
		
		//1枚目の画像サイズを取得
		var tmp = new Image();
		tmp.src = img_elements[0].src;
		this.divs[i]['default_w'] = tmp.width;
		this.divs[i]['default_h'] = tmp.height;
		
		//各画像情報設定
		for (var k = 0; k < img_elements.length; k ++) {
			this.divs[i]['image_list'][k] = new Array();
			this.divs[i]['image_list'][k]['src'] = img_elements[k].src;
			this.divs[i]['image_list'][k]['caption'] = img_elements[k].alt;
			
			//サイズ取得
			var tmp = new Image();
			tmp.src = this.divs[i]['image_list'][k]['src'];
			var original_w = tmp.width;
			var original_h = tmp.height;
			
			//ボックスに収めるためwidth
			if(original_w <= this.divs[i]['default_w'] && original_h <= this.divs[i]['default_h']){
				this.divs[i]['image_list'][k]['w'] = original_w;
				this.divs[i]['image_list'][k]['h'] = original_h;
				this.divs[i]['image_list'][k]['marginTop'] = Math.floor((this.divs[i]['default_h'] - original_h) / 2);
			}
			else{
				var scale_w = this.divs[i]['default_w'] / original_w;
				var scale_h = this.divs[i]['default_h'] / original_h;
				var scale = scale_w;
				if(scale_w > scale_h){ scale = scale_h; }
				this.divs[i]['image_list'][k]['w'] = Math.floor(original_w * scale);
				this.divs[i]['image_list'][k]['h'] = Math.floor(original_h * scale);
				this.divs[i]['image_list'][k]['marginTop'] = Math.floor((this.divs[i]['default_h'] - original_h * scale) / 2);
			}
		}
		
		//ボックスサイズ設定
		div.setAttribute('id','slideshow_box_' + i);
		div.style.width = this.divs[i]['default_w'] + 'px';
		div.style.textAlign = 'center';
		div.style.position = 'relative';
		div.style.border = 'none';
		//キャプションがある場合
		if(this.divs[i]['caption']){
			div.style.height = this.divs[i]['default_h'] + 12 + 'px';
			div.style.lineHeight = '10px';
		}
		else{
			div.style.height = this.divs[i]['default_h'] + 'px';
		}
		
		//1枚目
		if(this.divs[i]['caption']){
			paragraphs[0].style.lineHeight = '10px';
			paragraphs[0].style.textAlign = 'center';
			paragraphs[0].style.fontSize = '10px';
			
			var now_caption = document.createElement('div');
			now_caption.style.position = 'absolute';
			now_caption.style.left = 0;
			now_caption.style.bottom = 0;
			now_caption.style.fontSize = '10px';
			now_caption.style.width = this.divs[i]['default_w'] + 'px';
			now_caption.style.textAlign = 'center';
			var _text = document.createTextNode(this.divs[i]['image_list'][0]['caption']);
			now_caption.appendChild(_text);
			paragraphs[0].appendChild(now_caption);
			
		}
		new Effect.Fade(paragraphs[0], {duration:0.5, delay: this.divs[i]['sec'] - 0.5});
		
		//1つ前記憶
		this.divs[i]['pre_src'] = this.divs[i]['image_list'][0]['src'];
		
		//ランダムに並べ替え
		if(this.divs[i]['shuffle']){
			this.shuffle(this.divs[i]['image_list']);
		}
		
		//全てremove
		var timer = window.setTimeout(function(){
			div.innerHTML = '';
		}.bind(this), this.divs[i]['sec'] * 1000);
		
		//スライドショー
		var timer2 = window.setInterval(function(){
			this.show_image(i);
		}.bind(this), this.divs[i]['sec'] * 1000 + 300);
		
	},
	
	show_image: function(i){
		var div = this.divs[i];
		
		//前回とだぶった場合は、カウントアップ
		if(div['pre_src'] == div['image_list'][div['index']]['src']){
			div['index'] ++;
			if (div['index'] >= div['image_count']){
				div['index'] = 0;
			}
		}
		
		var ind = div['index'];
		
		//画像
		var now_image = document.createElement('img');
		now_image.src = div['image_list'][ind]['src'];
		now_image.style.width = div['image_list'][ind]['w'] + 'px';
		now_image.style.height = div['image_list'][ind]['h'] + 'px';
		now_image.style.marginTop = div['image_list'][ind]['marginTop'] + 'px';
		now_image.style.marginBottom = 0;
		
		var now_image_box = document.createElement('div');
		now_image_box.style.display = 'none';
		now_image_box.appendChild(now_image);
		
		div['div'].appendChild(now_image_box);
		
		if(div['caption']){
			var now_caption = document.createElement('div');
			now_caption.style.position = 'absolute';
			now_caption.style.left = 0;
			now_caption.style.bottom = 0;
			now_caption.style.fontSize = '10px';
			now_caption.style.width = div['default_w'] + 'px';
			now_caption.style.textAlign = 'center';
			var _text = document.createTextNode(div['image_list'][ind]['caption']);
			now_caption.appendChild(_text);
			now_image_box.appendChild(now_caption);
		}
		
		
		new Effect.Appear(now_image_box, {duration:1.5});
		new Effect.Fade(now_image_box, {duration:0.5, delay: div['sec'] - 0.5});
		
		div['pre_src'] = div['image_list'][ind]['src'];
		
		div['index'] ++;
		if (div['index'] >= div['image_count']){
			div['index'] = 0;
		}
	},
	
	shuffle: function(ary){
		var i = ary.length;
		while(i){
			var j = Math.floor(Math.random() * i);
			var t = ary[--i];
			ary[i] = ary[j];
			ary[j] = t;
		}
		return ary;
	}
		
}

function initSlideshow(){ mySlideshow = new Slideshow(); }
Event.observe(window, 'load', initSlideshow, false);