function $(id) { return document.getElementById(id); }

function addLoadEvent(func){
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function(){
			oldonload();
			func();
		}
	}
}

function addBtn() {
	if(!$('focus_turn')) return;
	var focusList = $('focus_turn').getElementsByTagName('div');
	var btnBox = document.createElement('focus_btn');
	btnBox.setAttribute('id','focus_btn');
	var SpanBox ='';
	for(var i=1; i<=focusList.length; i++ ) {
		var spanList = '<span class="normal">'+i+'</span>';
		SpanBox += spanList;
	}
	btnBox.innerHTML = SpanBox;
	$('focus_turn').appendChild(btnBox);
	$('focus_turn').getElementsByTagName('div')[0].className = 'current';
	$('focus_btn').getElementsByTagName('span')[0].className = 'current';
}

function classNormal(){
	var focusList = $('focus_turn').getElementsByTagName('div');
	var btnList = $('focus_btn').getElementsByTagName('span');
	for(var i=0; i<focusList.length; i++) {
		focusList[i].className='';
		btnList[i].className='normal';
	}
}

function classCurrent(n){
	var focusList = $('focus_turn').getElementsByTagName('div');
	var btnList = $('focus_btn').getElementsByTagName('span');
	focusList[n].className='current';
	btnList[n].className='current';
}

var autoKey = false;
function btnTurn() {
	if(!$('focus_turn')) return;
	$('focus_turn').onmouseover = function(){autoKey = true};
	$('focus_turn').onmouseout = function(){autoKey = false};	
	var focusList = $('focus_turn').getElementsByTagName('div');
	var btnList = $('focus_btn').getElementsByTagName('span');
	for (var m=0; m<btnList.length; m++){
		btnList[m].onmouseover = function() {
			classNormal();
			this.className='current';
			var n=this.childNodes[0].nodeValue-1;
			focusList[n].className='current';
		}
	}
}

setInterval('autoTurn()', 3500);
function autoTurn() {
	if(!$('focus_turn')) return;
	if (autoKey) return;
	var focusList = $('focus_turn').getElementsByTagName('div');
	var btnList = $('focus_btn').getElementsByTagName('span');
	for(var i=0; i<focusList.length; i++) {
		if (focusList[i].className == 'current') {
			var currentNum = i;
		}
	}
	if (currentNum==focusList.length-1 ){
		classNormal();
		classCurrent(0);
	} else {
		classNormal();
		classCurrent(currentNum+1);
	}

}
addLoadEvent(addBtn);
addLoadEvent(btnTurn);
