/*--- script libraries ---*/
/*--- m3t base ---*/
/*--- ver. 2010.07.30 ---*/


/*------------ [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 verNum;
if(IE) {
	verNum = ua.charAt(30);
	if(verNum >= 8) {
		var afterIE8 = true;
	}else if(verNum<8) {
		var beforeIE8 =true;
	}
}

var NN = ua.indexOf('Netscape')>=0;
var FF = ua.indexOf('Firefox')>=0;
var SF = ua.indexOf('Safari')>=0;
var OP = ua.indexOf('Opera')>=0;
var CR = ua.indexOf('Chrome')>=0;

/*------------ [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;
}

// ajust back btn action
function ajustBackAction(target) {
	target.blur();
}


function setSwapImages(){
	var img = d.getElementsByTagName('img');
	var bTag = d.getElementsByTagName('body')[0];
	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 (beforeIE8){
				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);'));
				//bTag.setAttribute('onunload', new Function('ajustBackAction(this);'));
			}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);');
				bTag.setAttribute('onunload','ajustBackAction(this);');
			}
		}
	}
}



// 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,'cokey_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,'cokey_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();
}


