addEvent(window, 'load', init);

function init(){
	setHover();
}

//RollOver
function setHover(){
	var elems = getElemntsByCName("rollover", "img");
	elems = elems.concat(getElemntsByCName("rollover", "input"));
	var len = elems.length;
	for(var i=0; i<len; i++){
		elems[i].onmouseover = onMouseOverHandler;
		elems[i].onmouseout = onMouseOutHandler;
	}
}

function onMouseOverHandler(){
	if(this.src.match(/\_on\.(gif|jpg)/)) return;
	this.src = this.src.replace(/\.(gif|jpg|png)/,'_ov.$1');
}
function onMouseOutHandler(){
	this.src = this.src.replace(/\_ov\.(gif|jpg|png)/,'.$1');
}


//Set
function getElemntsByCName(classname/*string*/, tag/*string*/, p/*element*/){
	var base = p || document;
	var target = tag || "*";
	var elems = base.getElementsByTagName(target);
	var len = elems.length;
	var reg = new RegExp("(?:^|\\s)" + classname + "(?:$|\\s)");
	var result = [];
	
	for(var i=0; i<len; i++){
		if(reg.test(elems[i].className)){
			result.push(elems[i]);
		}
	}
	return result;
}

function addEvent(target/*element*/, evt/*string*/, func/*function*/){
	if(window.addEventListener) target.addEventListener(evt, func, false);
	if(window.attachEvent) target.attachEvent('on'+evt, func);
}



/* --------------------------------
	Zoom Font Size
-------------------------------- */
var gws = {
	addEvent:function(target, evt, func){
		if(target.addEventListener)	
			target.addEventListener(evt, func, false);
		else if(target.attachEvent)	
			target.attachEvent("on"+evt, func);
	},
	getElementByClassName:function(tag, cName){
		var elems = document.getElementsByTagName(tag);
		var result = [];
		for(var i=0,len=elems.length; i<len; i++){
			var cNames = elems[i].className.split(" ");
			for(var j=0; j<cNames.length; j++){
				if(cNames[j]==cName) result.push(elems[i]);
			}
		}
		return result;
	}
	
}

;(function(){
	
// Cookie
function Cookie(name){ this.$name = name; var allcookies = document.cookie; if(allcookies == "") return; var cookies = allcookies.split('; '); var cookie = null; for(var i = 0; i<cookies.length; i++){ if(cookies[i].substring(0, name.length+1) == (name + "=")){ cookie = cookies[i]; break;}
}
if(cookie == null) return; var cookieval = cookie.substring(name.length+1); var a = cookieval.split("&"); for(var i = 0; i<a.length; i++)
a[i] = a[i].split(":"); for(var i=0; i<a.length; i++){ this[a[i][0]] = decodeURIComponent(a[i][1]);}
}
Cookie.prototype.store = function(daysToLive, path, domain, secure){ var cookieval = ""; for(var prop in this){ if((prop.charAt(0)=='$') || ((typeof this[prop])=='function'))
continue; if(cookieval != "") cookieval += '&'; cookieval += prop + ':' + encodeURIComponent(this[prop]);}
var cookie = this.$name + '=' + cookieval; if(daysToLive || daysToLive == 0){ cookie += "; max-age=" + (daysToLive*24*60*60);}
if(path) cookie += "; path=" + path
if(domain) cookie += "; domain=" + domain; if(secure) cookie += "; secure"; document.cookie = cookie;}
Cookie.prototype.remove = function(path, domain, secure){ for(var prop in this){ if(prop.charAt(0) != '$' && typeof this[prop] != 'function')
delete this[prop];}
this.store(0, path, domain, secure);}


// Zoom Font Size
var ModuleZoom = function(c){ this.size = [80, 93, 118]; this.defaultIdx = 1; this.selectedIdx = 1; this.container = c; this.cookie = new this.Cookie("fontsize"); if(this.cookie.sizeIdx){ this.selectedIdx = this.cookie.sizeIdx; this.change();}
}
ModuleZoom.prototype.up = function(){ if(this.selectedIdx==this.size.length-1) return; this.selectedIdx++; this.change();}
ModuleZoom.prototype.down = function(){ if(this.selectedIdx==0) return; this.selectedIdx--; this.change();}
ModuleZoom.prototype.resetZoom = function(){ this.selectedIdx = this.defaultIdx; this.change();}
ModuleZoom.prototype.change = function(){ this.cookie.sizeIdx = this.selectedIdx; this.cookie.store(null, "/"); document.getElementById(this.container).style.fontSize = this.size[this.selectedIdx]+"%";}
ModuleZoom.prototype.Cookie = Cookie;


//Setting Font Size event
function setModuleZoomEvent(){
	if(!!document.getElementById("funcZoom")){
		var zoom = new ModuleZoom('container');
		setFontSizePos();
		
		//set event
		gws.addEvent(document.getElementById("zoomSmall"), "click", function(){zoom.down(); setFontSizePos(); return false;}, false);
		gws.addEvent(document.getElementById("zoomReset"), "click", function(){zoom.resetZoom(); setFontSizePos(); return false;}, false);
		gws.addEvent(document.getElementById("zoomLarge"), "click", function(){zoom.up(); setFontSizePos(); return false;}, false);
	}
	
	function setFontSizePos(){
		var classname = "fontDefault";
		if(zoom.selectedIdx < zoom.defaultIdx) classname = "fontSmall";
		else if(zoom.selectedIdx > zoom.defaultIdx) classname = "fontLarge";
		
		document.getElementById("funcZoom").className = classname;
	}
}

gws.addEvent(window, "load", setModuleZoomEvent);

})();



/* --------------------------------
	Smooth scroll
-------------------------------- */
var targetObj;
var posY = 0;
function setSmoothScroll(){
	var elems = document.getElementsByTagName("a");
	var pathname = location.pathname;
	if(pathname.charAt(pathname.length-1) == "/") pathname += 'index.html';
	for(var i=0; i<elems.length; i++){
		var h = elems[i].href;
		if(h.match(/#\w+/)){
			var tmp = h.split("/");
			if(!tmp[tmp.length - 1].match(/.\htm/)) tmp[tmp.length - 1] = 'index.html'+tmp[tmp.length - 1];
			h = tmp.join("/");
			if(h.indexOf(pathname) > -1) elems[i].onclick = startSmoothScroll;
		}
	}
}
function startSmoothScroll(evt){
	if(window.opera) return;
	
	var evtObj = this;
	if(document.all){
		event.returnValue = false;
	}
	else{
		evt.preventDefault();
	}
	
	var n = evtObj.href.substr(evtObj.href.lastIndexOf("#")+1);
	var elems=document.all || document.getElementsByTagName("*");
	for(var i=0; i<elems.length; i++){
		if(elems[i].id==n || elems[i].name==n){
			targetObj = elems[i];
			break;
		}
	}
	if(targetObj){		
		var ph = getPageHeight();
		var wh = getWindowHeight();
		if(ph<wh) ph = wh;
		
		var offsetTrail = evtObj;
		posY = 0;
		while(offsetTrail){
			posY += offsetTrail.offsetTop;
			offsetTrail = offsetTrail.offsetParent;
		}
		offsetTrail = targetObj;
		targetObj.posY = 0;
		while(offsetTrail){
			targetObj.posY += offsetTrail.offsetTop;
			offsetTrail = offsetTrail.offsetParent;
		}
		if(targetObj.posY<0) targetObj.posY = 0;
		if(targetObj.posY>(ph-wh)) targetObj.posY = ph-wh;
		setTimeout(loop, 33);
	}
}
function loop(){	
	scrollY = document.body.scrollTop || document.documentElement.scrollTop;
	var moveY = (targetObj.posY - posY - (posY - scrollY)) / 4;		
	posY += moveY;
	if((moveY<0 && posY>scrollY) || (moveY>0 && posY<scrollY)) posY = scrollY;
	
	if(Math.abs(posY-targetObj.posY)<2) {
		scroll(0, targetObj.posY);
		return false;
	}
	scroll(0, posY);
	setTimeout(loop, 33);
}
function getPageHeight(){
	var ph;
	if(window.innerHeight && window.scrollMaxY)
		ph = window.innerHeight + window.scrollMaxY;
	else if(document.body.scrollHeight>document.body.offsetHeight)


		ph = document.body.scrollHeight;
	else
		ph = document.body.offsetHeight;
	return ph;
}
function getWindowHeight(){
	var wh;
	if(window.innerHeight)
		wh = window.innerHeight;
	else if(document.documentElement && document.documentElement.clientHeight)
		wh = document.documentElement.clientHeight;
	else if(document.body)
		wh = document.body.clientHeight;
	return wh;
}
gws.addEvent(window, "load", setSmoothScroll);



/* --------------------------------
	POP UP
-------------------------------- */
function popWinSize(strURL, lngWidth, lngHeight){
	var objWin = window.open(strURL, "pop01","width=" + lngWidth + ",height=" + lngHeight + ",toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars = no,resizable=1");
	objWin.focus();
}

function popWinScreenSize(theURL) {
var iwidth = screen.width;
var iheight = screen.height;
if(document.layers){
window.outerWidth = iwidth;
window.outerHeight = iheight;
}
window.open(theURL,"mme","width="+iwidth+",height="+iheight+",left=0,top=0,toolbar=no,status=no,menubar=no,scrollbars=no,resizable=no");
}
