// ---- environment ----
var env_obj = new TEnv();
var isMAC = ( env_obj.os == 'mac');
var isWIN = ( env_obj.os == 'win');
var isIE = ( env_obj.browser.name == 'msie');
var isNS = ( env_obj.browser.name == 'netscape');
var isOPERA = ( env_obj.browser.name == 'opera');
var isSAFARI = ( env_obj.browser.name == 'safari');
var browserVerStr = env_obj.browser.ver;
var browserVer = parseFloat( browserVerStr);

// ---- font size button script ----
var fsize_vars = new Object();
fsize_vars.avail = ( ( isNS && ( browserVer < 5)) || ( isIE && ( browserVer < 6))) ? false : true;
addLoadEvent( init_fsize_btn);

function init_fsize_btn() {
	if( fsize_vars.initialized || !fsize_vars.avail) return;
	fsize_vars.contentsarea = document.getElementById( 'contentsarea');
	fsize_vars.leftmenuarea = document.getElementById( 'snav');
	fsize_vars.cookie_obj = new TCookie( 'fsizestate');
	fsize_vars.cookie_obj.set_path( '/');
	fsize_vars.cookie_obj.set_expires( 'Tue, 1-Jan-2030 00:00:00 GMT');
	var saved_state = fsize_vars.cookie_obj.read();
	switch ( saved_state) {
		case '0':
		case '1':
		case '2':
			fsize_vars.state = parseInt( saved_state);
			break;
		default:
			fsize_vars.state = 0;
			break;
	}
	update_fsize();
	fsize_vars.initialized = true;
}

function check_fsize_avail() {
	if( !fsize_vars.avail) {
		alert( "Sorry. This function is not available on your browser. Please click the buttons of your browser to change font size. ");
		return false;
	}
	return true;
}

function fsizel_btn_rover() {
	MM_swapImage('ua_fsize_btn_l','','/image/baseelm/fsize_l_btn_on.gif',1);
}

function fsizel_btn_rout() {
	MM_swapImgRestore();
}

function fsizes_btn_rover() {
	MM_swapImage('ua_fsize_btn_s','','/image/baseelm/fsize_s_btn_on.gif',1);
}

function fsizes_btn_rout() {
	MM_swapImgRestore();
}

function fsizel_btn_clicked() {
	if( !check_fsize_avail()) return;
	if( fsize_vars.state >= 2) return;
	fsize_vars.state++;
	update_fsize();
}

function fsizes_btn_clicked() {
	if( !check_fsize_avail()) return;
	if( fsize_vars.state <= 0) return;
	fsize_vars.state--;
	update_fsize();
}

function update_fsize() {
	if( !fsize_vars.avail) return;
	var ratio;
	switch ( fsize_vars.state) {
		case 0:
			ratio = 1;
			break;
		case 1:
			ratio = 1.3;
			break;
		case 2:
			ratio = 1.6;
			break;
		default:
			ratio = 1;
			break;
	}
	ratio = parseInt( ratio * 100);
	if( fsize_vars.leftmenuarea) fsize_vars.leftmenuarea.style.fontSize = ratio + '%';
	if( fsize_vars.contentsarea) fsize_vars.contentsarea.style.fontSize = ratio + '%';
	fsize_vars.cookie_obj.set_value( fsize_vars.state);
	fsize_vars.cookie_obj.write();
}

// ---- print button script ----
function print_btn_rover() {
	MM_swapImage('ua_print_btn','','/image/baseelm/print_btn_on.gif',1);
}

function print_btn_rout() {
	MM_swapImgRestore();
}

function print_btn_clicked() {
	var loc = window.location.protocol;
	var	hos = window.location.host;
	var	pat = window.location.pathname;
	window.open('/php/print.php?url='+ loc + '//' + hos + pat);
}

// ---- popup window script ----
function open_popup( url, name, params) {
	var w = window.open( url, name, params);
	w.focus();
	return w;
}

// ---- macromedia js ----
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

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

// ---- cookie script ----
function TCookie( name) {
	this.name = name;
	this.value = null;
	this.path = null;
	this.expires = null;
}

function _TCookie_read() {
	var cookie_str = document.cookie;
	if( cookie_str.length == 0) {
		return null;
	}
	cookie_str += ';';
	var start = 0;
	var end;
	for( ; start < cookie_str.length; ) {
		end = cookie_str.indexOf( '=', start);
		if( end == -1) {
			return null;
		}
		if( cookie_str.substring( start, end) == this.name) {
			break;
		}
		start = cookie_str.indexOf( ';', end) + 2;
	}
	start += this.name.length + 1;
	end = cookie_str.indexOf(';', start);
	this.value = unescape( cookie_str.substring( start, end));
	return this.value;
}

function _TCookie_write() {
	var cookie_str = this.name + '=' + escape( this.value);
	if( this.path != null) {
		cookie_str += ';path=' + this.path;
	}
	if( this.expires != null) {
		cookie_str += ';expires=' + this.expires;
	}
	document.cookie = cookie_str;
}

function _TCookie_get_value() {
	return this.value;
}

function _TCookie_set_value( value) {
	this.value = value;
}

function _TCookie_set_expires( value) {
	this.expires = value;
}

function _TCookie_set_path( path) {
	this.path = path;
}

function _TCookie_remove() {
	var expdate = new Date();
	expdate.setHours( expdate.getHours() - 1);
	expdate = expdate.toGMTString();
	document.cookie = this.name + '=' + escape( this.value) + ';expires=' + expdate;
	this.expires = expdate;
}

TCookie.prototype.read = _TCookie_read;
TCookie.prototype.write = _TCookie_write;
TCookie.prototype.get_value = _TCookie_get_value;
TCookie.prototype.set_value = _TCookie_set_value;
TCookie.prototype.set_path = _TCookie_set_path;
TCookie.prototype.set_expires = _TCookie_set_expires;
TCookie.prototype.remove = _TCookie_remove;

// ---- environment utility ----
function TBrowserEnv() {
	this.name = '';
	this.ver = '';
	this.is_osx_ns = false;
	
	var user_agent = navigator.userAgent.toLowerCase();
	var ua_params = new Array();	
	var from = 0;
	var to = 0;

	while( from < user_agent.length) {
		if( user_agent.charAt( from) == '(') {
			to = user_agent.indexOf( ')', from);
		} else if( user_agent.charAt( from) == '[') {
			to = user_agent.indexOf( ']', from);
		} else {
			to = user_agent.indexOf( ' ', from);
		}
		to = ( to < 0) ? user_agent.length : to;
		to = ( user_agent.charAt( to) == ' ') ? to : to + 1;
		ua_params[ ua_params.length] = user_agent.substring( from, to);
		from = to + 1;
	}

	if( ua_params[0].indexOf( 'opera') >= 0) {
		this.name = 'opera';
		from = ua_params[0].indexOf( '/');
		this.ver = ua_params[0].substring( from + 1, ua_params[0].length);
	} else if( ua_params[1].indexOf( 'msie') >= 0) {
		this.name = 'msie';
		from = ua_params[1].indexOf( 'msie');
		this.ver = ua_params[1].substring( from + 5, ua_params[1].indexOf( ';', from));
	} else if( ua_params.length >= 4 && ua_params[ 3].indexOf( 'netscape') >= 0) {
		this.name = 'netscape';
		from = ua_params[ 3].indexOf( '/');
		this.ver = ua_params[ 3].substring( from + 1, ua_params[ 3].length);
	} else if( ua_params[ ua_params.length - 1].indexOf( 'safari') >= 0) {
		this.name = 'safari';
		from = ua_params[ ua_params.length - 1].indexOf( '/');
		var build_version = ua_params[ ua_params.length -1].substring( from + 1, ua_params[ ua_params.length -1].length);
		if( build_version <= 85) {
			this.ver = 1.0;
		} else {
			this.ver = 1.0;
		}
	} else if( ua_params[0].indexOf( 'mozilla') >= 0) {
		from = ua_params[0].indexOf( '/');
		this.ver = ua_params[0].substring( from + 1, ua_params[0].length);
		if( parseFloat( this.ver) < 5) {
			this.name = 'netscape';
		} else {
			this.name = 'unknown';
		}
	} else {
		this.name = 'unknown';
	}

	if( this.name != 'netscape' || Number( this.ver) < 5) {
		return;
	}
	if( ua_params[ 1].split( ';')[ 2].indexOf( 'mac os x') > 0) {
		this.is_osx_ns = true;
	}
}

function TEnv() {
	this.os = '';
	this.browser = new TBrowserEnv();

	var appVer = navigator.appVersion.toLowerCase();
	if( appVer.indexOf( 'mac') >= 0) {
		this.os = 'mac';
	} else if( appVer.indexOf( 'win') >= 0) {
		this.os = 'win';
	} else {
		this.os = 'win';
	}
}

/* ---- compatibility ----*/
var jsdir = '/image/';
