/*--- script libraries ---*/
/*--- m3t base ---*/
/*--- ver. 2009.02.18 ---*/

/*------------ [base] ------------*/

var w = window;
var d = document;
var ua = navigator.userAgent;
var av = navigator.appVersion;

// check OS
var WIN = ua.indexOf('Win')>=0;
var MAC = ua.indexOf('Mac')>=0;
var OSX = ua.indexOf('Mac OS X')>=0;
var LNX = ua.indexOf('Linux')>=0;

// check browser
var IE = ua.indexOf('MSIE')>=0;
var NN = ua.indexOf('Netscape')>=0;
var FF = ua.indexOf('Firefox')>=0;
var SF = ua.indexOf('Safari')>=0;
var OP = ua.indexOf('Opera')>=0;

// check version[safari]
var sfversion = 0;
var s = 0;
if (SF){
	s = ua.indexOf("Safari/",0);
	version = parseInt(ua.substring(s+7,s+99));
	if (version < 400) {
		sfversion = 1;
	}else if((version >= 400) && (version <= 499)){
		sfversion = 2;
	}else if(version >= 500){
		sfversion = 3;
	}
}

/*------------ [onload setting] ------------*/

onloadArray = new Array();

// preload images
loadImages = new Array();



/*------------ [swap images] ------------*/

// normal swap image
function swapImage(target,url) {
	target.src = url;
}

// link swap image
function swapLinkImage(target,url,act){
	if (act){
		var urlData = url.replace(/_of./g,'_on.');
	}else{
		var urlData = url.replace(/_on./g,'_of.');
	}
	target.firstChild.src = urlData;
}

function setSwapImages(){
	var img = d.getElementsByTagName('img');
	for (var i=0;i<img.length;i++){
		if (img[i].src.indexOf('_of.')>-1){
			// preload images setting
			var imgSrc = img[i].src;
			loadImages.push(imgSrc.replace(/_of./g,'_on.'));
			// set attributes
			if (ua.indexOf('MSIE')>-1){
				img[i].parentNode.setAttribute('onmouseover', new Function('swapLinkImage(this,this.firstChild.src,true);'));
				img[i].parentNode.setAttribute('onmouseout', new Function('swapLinkImage(this,this.firstChild.src,false);'));
				img[i].parentNode.setAttribute('onfocus', new Function('swapLinkImage(this,this.firstChild.src,true);'));
				img[i].parentNode.setAttribute('onblur', new Function('swapLinkImage(this,this.firstChild.src,false);'));
			}else{
				img[i].parentNode.setAttribute('onmouseover','swapLinkImage(this,this.firstChild.src,true);');
				img[i].parentNode.setAttribute('onmouseout','swapLinkImage(this,this.firstChild.src,false);');
				img[i].parentNode.setAttribute('onfocus','swapLinkImage(this,this.firstChild.src,true);');
				img[i].parentNode.setAttribute('onblur','swapLinkImage(this,this.firstChild.src,false);');
			}
		}
	}
}

// preload images
function preloadImages(){
	var loadimgImages = new Array();
	for (var i=0;i<loadImages.length;i++){
		loadimgImages[i] = new Image();
		loadimgImages[i].src = loadImages[i];
	}
}

// onload trigger
onloadArray.push(setSwapImages);



/*------------ [link window controller] ------------*/
var otherwin;
function linkController(){
	var a = d.getElementsByTagName('a');
	for (var i=0;i<a.length;i++){
		// other_window
		if (a[i].getAttribute('class')||a[i].getAttribute('className')) {
			if ((a[i].getAttribute('class')||a[i].getAttribute('className')).indexOf('other_window')>-1){
				a[i].onclick=function(){
					otherwin = w.open(this.href,'akadaira_window','toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,resizable=yes,scrollbars=yes,titlebar=yes');
					otherwin.focus();
					return false;
				}
			}
		}
		// top_window
		if (a[i].getAttribute('class')||a[i].getAttribute('className')) {
			if ((a[i].getAttribute('class')||a[i].getAttribute('className')).indexOf('top_window')>-1){
				a[i].onclick=function(){
					w.open(this.href,'_top');
					return false;
				}
			}
		}
	}
}

// onload trigger
onloadArray.push(linkController);


function linkOtherWindow(url){
	otherwin = w.open(url,'akadaira_window','toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,resizable=yes,scrollbars=yes,titlebar=yes');
	otherwin.focus();
	return false;
}



/*------------ [popup window] ------------*/

function popupWindow(url,target,width,height,event,others){
	var setOthers = new String();
	if(others){
		if (others.search(/t/i) != -1) {
			setOthers += ',toolbar=yes';
		}
		if (others.search(/l/i) != -1) {
			setOthers += ',location=yes';
		}
		if (others.search(/s/i) != -1) {
			setOthers += ',status=yes';
		}
		if (others.search(/m/i) != -1) {
			setOthers += ',menubar=yes';
		}
		if (others.search(/b/i) != -1) {
			setOthers += ',scrollbars=yes';
		}
		if (others.search(/r/i) != -1) {
			setOthers += ',resizable=yes';
		}
	}
	if (width=='full' || height=='full') {
		width = new String(screen.availWidth);
		height = new String(screen.availHeight);
	}
	w.open(url,target,'top=0,left=0,width='+width+',height='+height+setOthers);
	//
	if(event.preventDefault){
		event.preventDefault();
		event.stopPropagation();
	}
	event.returnValue=false;
	event.cancelBubble=true;
}



/*------------ [page scroll] ------------*/

var scrollSpeed = 0.1;
var scrollInterval = 10;

// browser size
var inWidth;
var inHeight;
function getBrowserSize(){
	if(IE){// IE
		inWidth = d.documentElement.clientWidth;
		inHeight = d.documentElement.clientHeight;
	}else if(FF){// Firefox
		inWidth = d.documentElement.clientWidth;
		inHeight = d.documentElement.clientHeight;
	}else if(OP){
		if(av.substring(0,3)>=9.5){// Opera 9.5
			inWidth = d.documentElement.clientWidth;
			inHeight = d.documentElement.clientHeight;
		}else{// Opera
			inWidth = d.body.clientWidth;
			inHeight = d.body.clientHeight;
		}
	}else if(SF||NN){// Safari,Netscape
		inWidth = w.innerWidth;
		inHeight = w.innerHeight;
	}
}

// get page height
var winTop;
var winBottom;
function getPageHeight(){
	//
	var bTag = d.getElementsByTagName('body')[0];
	var aTag = d.createElement('a');
	aTag.setAttribute('id','bottom');
	bTag.appendChild(aTag);
	//
	winTop = d.getElementById('top').offsetTop;
	winBottom = d.getElementById('bottom').offsetTop;
}

// get element position
function getElementPosition(elm){
	var obj = new Object();
	obj.x = elm.offsetLeft;
	obj.y = elm.offsetTop;
	// revise position
	while(elm.offsetParent) {
		elm = elm.offsetParent;
		obj.x += elm.offsetLeft;
		obj.y += elm.offsetTop;
	}
	return obj;
}

// get anchor name
function getAnchor(trg){
	var trgSharp = trg.indexOf('#');
	var trgId = trg.substring(trgSharp+1);
	return trgId;
}

// get anchor position
var thsTop;
var trgTop;
function getAnchorPosition(trg,ths){
	// base
	if(IE){
		thsTop = d.documentElement.scrollTop;
	}else{
		thsTop = w.pageYOffset;
	}
	// target
	var trgObj = d.getElementById(getAnchor(trg));
	trgTop = getElementPosition(trgObj).y;
	if(winBottom-trgTop<inHeight){
		trgTop = winBottom-inHeight;
	}
}

function scrollAction(){
	getBrowserSize();
	getPageHeight();
	thsTop = thsTop+(trgTop-thsTop)*scrollSpeed;
	var posY = Math.round(thsTop);
	w.scrollTo(0,posY);
	if(Math.abs(trgTop-thsTop)<2){
		w.scrollTo(0,trgTop);
		clearInterval(eventTimer);
	}
}

var eventTimer;
function scrollTrigger(trg,ths,event){
	if(!IE||!MAC){
		getAnchorPosition(trg,ths);
		eventTimer = setInterval(scrollAction,scrollInterval);
		//
		if(event.preventDefault){
			event.preventDefault();
			event.stopPropagation();
		}
		event.returnValue=false;
		event.cancelBubble=true;
	}
}

function checkWheel(){
	clearInterval(eventTimer);
}
function setCheckWheel(){
	if(IE||SF){
		w.onmousewheel = d.onmousewheel = checkWheel;
	}
	if(FF||NN){
		w.addEventListener('DOMMouseScroll', checkWheel, false);
	}
	if(OP){
		if(av.substring(0,3)>=9.5){// Opera 9.5
			w.attachEvent('onmousewheel', checkWheel);
		}else if(WIN){
			d.addEventListener('mousewheel',function(event){clearInterval(eventTimer);event.preventDefault();scrollBy(0, -event.wheelDelta);},false);
		}else{
			d.addEventListener('mousewheel',function(event){clearInterval(eventTimer);event.preventDefault();scrollBy(0, event.wheelDelta);},false);
		}
	}
}
if(!IE||!MAC){
	setCheckWheel();
}



/*------------ [getXML] ------------*/

// XML file
var blogXML = '/rss.xml';

// XMLHttpRequest setting
function getXHR(){
	var obj;
	try{
		obj = new XMLHttpRequest();
	}catch(e){
		try{
			obj = new ActiveXObject('Msxml2.XMLHTTP');
		}catch(e){
			obj = new ActiveXObject('Microsoft.XMLHTTP');
		}
	}
	return obj;
}

// set RSS
function setNews1(){
}
function setNews2(){
}
function setNews3(){
	if(d.getElementById('news3')){
		var news3Data = '';
		var news3Length = xmlData.getElementsByTagName('item').length;
		if(news3Length>5){
			news3Length = 5;
		}
		//
		for(i=0;i<news3Length;i++){
			if(xmlData.getElementsByTagName('item')[0].getElementsByTagName('dc:date')[0]){
				news3Data += '<dt>'+xmlData.getElementsByTagName('item')[i].getElementsByTagName('dc:date')[0].firstChild.data.substring(0,4)+'/'+xmlData.getElementsByTagName('item')[i].getElementsByTagName('dc:date')[0].firstChild.data.substring(5,7)+'/'+xmlData.getElementsByTagName('item')[i].getElementsByTagName('dc:date')[0].firstChild.data.substring(8,10)+'&nbsp;'+xmlData.getElementsByTagName('item')[i].getElementsByTagName('dc:date')[0].firstChild.data.substring(11,16)+'</dt>';
			}else{
				news3Data += '<dt>'+xmlData.getElementsByTagName('item')[i].getElementsByTagName('date')[0].firstChild.data.substring(0,4)+'/'+xmlData.getElementsByTagName('item')[i].getElementsByTagName('date')[0].firstChild.data.substring(5,7)+'/'+xmlData.getElementsByTagName('item')[i].getElementsByTagName('date')[0].firstChild.data.substring(8,10)+'&nbsp;'+xmlData.getElementsByTagName('item')[i].getElementsByTagName('date')[0].firstChild.data.substring(11,16)+'</dt>';
			}
			if (sfversion==2){//safari2
				
				news3Data += '<dd><a href="'+sf2ElementByTagName(xmlData.getElementsByTagName('item')[i],'link').firstChild.data+'" target="_blank">'+sf2ElementByTagName(xmlData.getElementsByTagName('item')[i],'title').firstChild.data+'</a></dd>';
			}else{//safar以外
				news3Data += '<dd><a href="'+xmlData.getElementsByTagName('item')[i].getElementsByTagName('link')[0].firstChild.data+'" target="_blank">'+xmlData.getElementsByTagName('item')[i].getElementsByTagName('title')[0].firstChild.data+'</a></dd>';
			}
		}	
		//
		d.getElementById('news3').innerHTML = news3Data;
	}
}

function sf2ElementByTagName(oElement, sTargetTagName){
	for(var i=0;i<oElement.childNodes.length; i++){
		var node = oElement.childNodes[i];
		if(node.nodeName == sTargetTagName){
			return node;
		}
	}return null;
}

// get XML
function getXML(){
	var xmlObj = getXHR();
	xmlObj.onreadystatechange = function(){
		if(xmlObj.readyState==4){
			if(xmlObj.status==200){
				//通信完了
				xmlData = xmlObj.responseXML;
				setNews1();
				setNews2();
				setNews3();
			}else{
				//通信エラー
			}
		}else{
			//通信中
		}
	}
	xmlObj.open('GET',blogXML+'?'+Math.floor(Math.random()*9999),true);
	xmlObj.send('');
}

// onload trigger
if(!IE||!MAC){
	onloadArray.push(getXML);
}



