
//------------------------------------------------
//画像のロールオーバー処理 (2010/02/23 更新)
//------------------------------------------------

//変数の初期化
var rlovPreloadHash = new Array();
var rlovImgList = new Array();
var rlovInitTemp = null;
var rlovIE6 = typeof(document.documentElement.style.maxHeight)=='undefined';

//マウスイベント
function rlovOnMouseOver() {
	if (this.src != this.rlovOnImagePath) {
		this.src = this.rlovOnImagePath;
	}
}
function rlovOnMouseOut() {
	if (this.src != this.rlovOffImagePath) {
		this.src = this.rlovOffImagePath;
	}
}

//内容をテキストに変換
function rlovTextContent(element) {
	var text = element.innerHTML, i, s, pre, suf, c;
	while (text.match(/^([\s\S]*)<(ol|ul)>([\s\S]*?)<\/\2>([\s\S]*)$/im)) {
		tag = RegExp.$2.toLowerCase();
		pre = RegExp.$1;
		suf = RegExp.$4;
		text = RegExp.$3;
		if (tag=='ol') {
			for (i=1; (s=text.replace(/<li>/i, ''+i+'. '))!=text; i++) {
				text = s;
			}
		} else {
			for (i=1; (s=text.replace(/<li>/i, i==1 ? '' : ' / '))!=text; i++) {
				text = s;
			}
		}
		text = pre + ' ' + text + ' ' + suf;
	}
	text = text.replace(/<\/h[1-6]>([\s\S]*?<)/ig, ' ： $1');
	text = text.replace(/<\/?(?:p|pre|div|table|form|fieldset|address|blockquote|h[1-6]|[duo]l|center)(\s?.*?)\/?>/ig, ' ');
	text = text.replace(/[\s\n\r]*<br.*?>[\s\n\r]*|<.*?>/ig, '');
	text = text.replace(/\s+/g, ' ');
	text = text.replace(/^ | $/g, '');
	while (text.match(/&([a-z]+);/im)) {
		c = '';
		switch (RegExp.$1) {
			case 'amp' : c = '&'; break;
			case 'lt'  : c = '<'; break;
			case 'gt'  : c = '>'; break;
			default    : c = '';
		}
		text = RegExp.leftContext + c + RegExp.rightContext;
	}
	return text;
}

//ロールオーバー処理の初期化関数
function rlovInit() {
	var taglist, tag, img, on_url, off_url, i, j, s;
	//要素の列挙
	taglist = document.getElementsByTagName("*");
	
	for (i = 0; i < taglist.length; i++ ) {
		tag = taglist[i]; 
		img = null;
		
		if (!tag.className) {
			continue;
		}
		
		//rolloverクラス
		if (tag.className.match(/(^|\s)rollover($|\s)/)) {
			//imgタグを探す
			if (tag.tagName.toLowerCase()=='img') {
				img = tag;
			} else {
				for (j = 0; j < tag.childNodes.length; j++ ) {
					if (tag.childNodes[j].tagName.toLowerCase()=='img') {
						img = tag.childNodes[j];
						break;
					}
				}
			}
			if (img) {
				//画像パスの取得
				off_url = img.src;
				on_url = off_url.replace(/(?:_off)?(\.[a-z0-9]+$)/, "_on$1");
				
				//イベントの関連付け
				img.setAttribute('rlovOnImagePath', on_url);
				img.setAttribute('rlovOffImagePath', off_url);
				img.onmouseover = rlovOnMouseOver;
				img.onmouseout = rlovOnMouseOut;
				
				//画像の先読み
				if (!rlovPreloadHash[on_url]) {
					rlovPreloadHash[on_url] = new Image();
					rlovPreloadHash[on_url].src = on_url;
				}
				continue;
			}
		}
		
		//textimageクラス
		if (tag.className.match(/(^|\s)textimage($|\s)/)) {
			//タイトル属性を設定する.
			if (!tag.title) {
				tag.title = rlovTextContent(tag);
			}
			
			//IE6の場合は背景画像付きの要素を画像要素に置き換える.
			if (rlovIE6 && !tag.rlovOnload && tag.currentStyle && tag.currentStyle.backgroundImage
			    && tag.currentStyle.backgroundImage.match(/(_on|_off)\./)
			) {
				//画像パスの取得
				s = tag.currentStyle.backgroundImage.replace(/url\((["']?)(.*?)\1\)/, '$2');
				if (!s.match(/^(.*?)_(?:off|on)(\.[a-z0-9]+)$/)) {
					continue;
				}
				off_url = RegExp.$1 + '_off' + RegExp.$2;
				on_url  = RegExp.$1 + '_on' + RegExp.$2;
				
				if (tag.tagName.toLowerCase() != 'a') {
					for (j = 0; j < tag.childNodes.length; j++ ) {
						if (tag.childNodes[j].tagName.toLowerCase() == 'a'){
							tag = tag.childNodes[j];
							tag.style.zIndex = '1';
							break;
						}
					}
				}
				tag.setAttribute('rlovOnload', true);
				
				//img要素の作成
				img = new Image();
				img.style.border = '0';
				img.style.margin = '0';
				img.style.padding = '0';
				img.style.position = 'static';
				img.style.verticalAlign = 'bottom';
				img.style.backgroundImage = 'none';
				
				//必要なデータを要素に埋め込む
				img.setAttribute('rlovParent', tag);
				img.setAttribute('rlovOnImagePath', on_url);
				img.setAttribute('rlovOffImagePath', off_url);
				
				//イベントの関連付け
				if (on_url) {
					img.onmouseover = rlovOnMouseOver;
					img.onmouseout = rlovOnMouseOut;
				}
				
				img.onload = function() {
					this.onload = null;
					var tag = this.rlovParent;
					
					//子要素の削除
					while (tag.firstChild) {
						tag.removeChild(tag.firstChild);
					}
					
					//親要素に追加
					tag.appendChild(this);
					
					//背景の削除
					tag.style.backgroundImage = 'none';
					
					//画像の先読み
					if (!rlovPreloadHash[this.rlovOnImagePath]) {
						rlovPreloadHash[this.rlovOnImagePath] = new Image();
						rlovPreloadHash[this.rlovOnImagePath].src = this.rlovOnImagePath;
					}
					if (!rlovPreloadHash[this.rlovOffImagePath]) {
						rlovPreloadHash[this.rlovOffImagePath] = new Image();
						rlovPreloadHash[this.rlovOffImagePath].src = this.rlovOffImagePath;
					}
				}
				
				img.src = off_url;
				rlovImgList.push(img);
				
				continue;
			}
		}
	}
	
	//他の初期化関数を呼び出す
	if (rlovInitTemp) {
		rlovInitTemp();
	}
}

//初期化関数を登録する
if (window.onload) {
	//既にonloadが指定されていた場合は関数を保存する
	rlovInitTemp = window.onload;
}
window.onload = rlovInit;




