var ie = document.all;	// primitive browser-check

/* setting */
var arrBannerSrcs = [
	'/i/special/hall_arrange/ha_ttl03.jpg',
	'/i/special/hall_arrange/ha_ttl06.jpg',
	'/i/special/hall_arrange/ha_ttl09.jpg'
];

var slideBy        = ie ? 2  :  1;	// slide stride: px
var fadeBy         = 5;				// change opacity by: x/100
var fadeWait       = ie ? 10 : 30;	// wait: msec
var slideWait      = ie ? 0  :  3;	// wait: msec
var AutoSelectWait = 6000;			// wait: msec

/* initilize */
var nowY = 24;	// arrow-mark margin-top(px)
var nowOpacity = 100;
var nowCont = 0;

var timerFade;
var timerMove;
var timerAutoSelect;

var objBannerImg;
var objBannerA;
var objListLi = [];

/* attach events */
Event.observe(window, 'load', function(){
	/* determine objects */
	$T('h2','prepare-top').each(function(e){
		$T('img',e).each(function(e1){
			objBannerImg = e1;
		});
	});

	/* preload images */
	arrBannerSrcs.each(function(path){
		preLoad(path);
	});

	/* main */
	var listNum = 0;
	$T('li','prepare-top').each(function(e){
		objListLi.push(e);
		var n = objListLi.length - 1;
		Event.observe(objListLi[n], 'mouseover', function(){
			clearTimeout(timerAutoSelect);
			selectCont(n);
		});
	});
	autoSelect();
});

function autoSelect(){
	timerAutoSelect = setTimeout(function(){_autoSelectChild()}, AutoSelectWait);
}
function _autoSelectChild(){
	nowCont %= objListLi.length;
	selectCont(nowCont);
	nowCont++;
	timerAutoSelect = setTimeout(function(){_autoSelectChild()}, AutoSelectWait);
}

function selectCont(n){
	if(!objBannerA){
		$T('h2','prepare-top').each(function(e){
			e.innerHTML = '<a href="#"><img src=' + objBannerImg.src + ' /></a>';

			/* determine objects */
			$T('img',e).each(function(e1){
				objBannerImg = e1;
			});
			$T('a',e).each(function(e2){
				objBannerA = e2;
			});
		});
	}

	liHighLight(n);
	
	$A(objListLi[n].getElementsByTagName('a')).each(function(e){
		objBannerA.href = e.href;
	});

	fadeTo(arrBannerSrcs[n]);
	slideTo(24 + (64+4) * n);
}

/* slide arrow-mark */
function slideTo(toY){
	clearTimeout(timerMove);
	if(!$('arrow-mark-parent').innerHTML){
		nowY = toY;
		var elem = document.createElement('img'); 
		elem.src = '/i/special/hall_arrange/tri.gif'; 
		elem.style.margin = nowY + 'px 2px 0 2px'; 
		$('arrow-mark-parent').appendChild(elem); 

		/* shake arrow-mark */
		Event.observe(elem, 'click', function(){
			elem.style.margin = nowY + 'px 4px 0 0px';
			setTimeout(function(){elem.style.margin = nowY + 'px 2px 0 2px';}, 50);
			setTimeout(function(){elem.style.margin = nowY + 'px 0px 0 4px';},100);
			setTimeout(function(){elem.style.margin = nowY + 'px 2px 0 2px';},150);
			setTimeout(function(){elem.style.margin = nowY + 'px 4px 0 0px';},200);
			setTimeout(function(){elem.style.margin = nowY + 'px 2px 0 2px';},250);
			if(slideWait>200){slideWait=0;}else{slideWait+=10;}
		});

	}else{
		$T('img','arrow-mark-parent').each(function(e){
			_slideChild(e,toY);
		});
	}
}

/* loop for slide arrow-mark */
function _slideChild(obj,toY){
	if(nowY<toY){
		nowY += slideBy;
	}else if(nowY==toY){
		return true;	// end loop if finished
	}else{
		nowY -= slideBy;
	}

	obj.style.marginTop = nowY + 'px';
	timerMove = setTimeout(function(){_slideChild(obj,toY)}, slideWait);
}

/* main banner fade-out, fade-in */
function fadeTo(toSrc){
	clearTimeout(timerFade);
	_fadeChild(toSrc,'down');
}

/* loop for main banner fade-out, fade-in */
function _fadeChild(toSrc, direction){
	if(nowOpacity==10){
		objBannerImg.src = toSrc;
		direction='up'
	}

	if(direction=='up'){
		if(nowOpacity==100){
			return true;	// end loop if finished
		}
		nowOpacity += fadeBy;
	}else{
		nowOpacity -= fadeBy;
	}

	setOpacity(objBannerImg,nowOpacity);

	timerFade = setTimeout(function(){_fadeChild(toSrc, direction)},fadeWait);
}

function liHighLight(n){
	objListLi.each(function(e){setOpacity(e, 80)});
	setOpacity(objListLi[n], 100)
}

/* change opacity of object */
function setOpacity(obj, opacity){
	if(ie){
		obj.style.filter = "alpha(opacity=" + opacity + ")";	// IE
	}else{
		obj.style.MozOpacity = opacity/100;						// Firefox
		obj.style.opacity = opacity/100;						// others
	}
}

function preLoad(src) {
	document.img = new Image();
	document.img.src = src;
}

function $T(tagName, from){
	if(!from){
		return $A(document.getElementsByTagName(tagName));
	}else if(from.prototype){
		return $A(from.getElementsByTagName(tagName));
	}else if($(from)){
		return $A($(from).getElementsByTagName(tagName));
	}else{
		return [];
	}
}
