// Copyright (c) 1996-1997 Athenia Associates.
// http://www.webreference.com/js/
// License is granted if and only if this entire
// copyright notice is included. By Tomer Shiran.

function setCookie (name, value, expires, path, domain, secure) {
    var curCookie = name + "=" + escape(value) + (expires ? "; expires=" + expires : "") +
        (path ? "; path=" + path : "") + (domain ? "; domain=" + domain : "") + (secure ? "secure" : "");
    document.cookie = curCookie;
}

function getCookie (name) {
    var prefix = name + '=';
    var c = document.cookie;
    var nullstring = '';
    var cookieStartIndex = c.indexOf(prefix);
    if (cookieStartIndex == -1)
        return nullstring;
    var cookieEndIndex = c.indexOf(";", cookieStartIndex + prefix.length);
    if (cookieEndIndex == -1)
        cookieEndIndex = c.length;
    return unescape(c.substring(cookieStartIndex + prefix.length, cookieEndIndex));
}

function deleteCookie (name, path, domain) {
    if (getCookie(name))
        document.cookie = name + "=" + ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
}

function fixDate (date) {
    var base = new Date(0);
    var skew = base.getTime();
    if (skew > 0)
        date.setTime(date.getTime() - skew);
}

function rememberMe (f) {
    var now = new Date();
    fixDate(now);
    now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
    now = now.toGMTString();
    if (f.author != undefined)
       setCookie('mtcmtauth', f.author.value, now, '/', '', '');
    if (f.email != undefined)
       setCookie('mtcmtmail', f.email.value, now, '/', '', '');
    if (f.url != undefined)
       setCookie('mtcmthome', f.url.value, now, '/', '', '');
}

function forgetMe (f) {
    deleteCookie('mtcmtmail', '/', '');
    deleteCookie('mtcmthome', '/', '');
    deleteCookie('mtcmtauth', '/', '');
    f.email.value = '';
    f.author.value = '';
    f.url.value = '';
}

function hideDocumentElement(id) {
    var el = document.getElementById(id);
    if (el) el.style.display = 'none';
}

function showDocumentElement(id) {
    var el = document.getElementById(id);
    if (el) el.style.display = 'block';
}

var commenter_name;

function individualArchivesOnLoad(commenter_name) {

    hideDocumentElement('trackbacks-info');



    if (document.comments_form) {
        if (!commenter_name && (document.comments_form.email != undefined) &&
            (mtcmtmail = getCookie("mtcmtmail")))
            document.comments_form.email.value = mtcmtmail;
        if (!commenter_name && (document.comments_form.author != undefined) &&
            (mtcmtauth = getCookie("mtcmtauth")))
            document.comments_form.author.value = mtcmtauth;
        if (document.comments_form.url != undefined && 
            (mtcmthome = getCookie("mtcmthome")))
            document.comments_form.url.value = mtcmthome;
        if (document.comments_form["bakecookie"]) {
            if (mtcmtauth || mtcmthome) {
                document.comments_form.bakecookie.checked = true;
            } else {
                document.comments_form.bakecookie.checked = false;
            }
        }
    }
}

function writeTypeKeyGreeting(commenter_name, entry_id) {

}



// FontChanger
// Copyright (c) 2007 Hirotaka Ogawa
// REQUIRES: prototype.js, cookiemanager.js
FontChanger = Class.create();
FontChanger.prototype = {
  id: null,
  cookieManager: null,
  cookieName: 'style.fontSize',
  initialize: function(id) {
    this.id = id || 'fontChanger';
    this.cookieManager = new CookieManager();
    var fontSize = this.cookieManager.getCookie(this.cookieName);
    //if (fontSize) document.body.style.fontSize = fontSize;
    if (fontSize) {
		document.getElementById('container').style.fontSize = fontSize;
		var imgId;
		if (fontSize == '100%') {
			imgId = 'font-img-medium';
		} else {
			imgId = 'font-img-large';
		}
		var ficon = document.getElementById(imgId);
		var oversrc = ficon.src.replace('.gif', '_over.gif').replace('.jpg', '_over.jpg');
		ficon.src = oversrc;
		ficon.className = 'btn-current';
	}
  },
  setCookieShelfLife: function(days) {
    this.cookieManager.cookieShelfLife = days;
  },
  change: function(fontSize) {
    document.getElementById('container').style.fontSize = fontSize;
    //document.body.style.fontSize = fontSize;
    this.cookieManager.setCookie(this.cookieName, fontSize);
  },
  reset: function() {
    document.getElementById('container').style.fontSize = '';
    //document.body.style.fontSize = '';
    this.cookieManager.clearCookie(this.cookieName);
  },
  show: function() {
    var id = this.id;
    Event.observe(document.getElementById(id + '-small'), 'click', this.onClickSmall.bind(this));
    Event.observe(document.getElementById(id + '-medium'), 'click', this.onClickMedium.bind(this));
    Event.observe(document.getElementById(id + '-large'), 'click', this.onClickLarge.bind(this));
  },
  onClickSmall:  function(e) { this.change('90%' ); },
  onClickMedium: function(e) {
  	this.change('100%');
  	var flicon = document.getElementById('font-img-large');
  	var loversrc = flicon.src.replace('_over.gif', '.gif').replace('_over.jpg', '.jpg');
  	flicon.src = loversrc;
  	flicon.className = 'btn';
  	var fmicon = document.getElementById('font-img-medium');
  	mouseOverImgs();
    fmicon.onmouseover = function() {};
    fmicon.onmouseout = function() {};
  },
  onClickLarge:  function(e) {
  	this.change('120%');
  	var fmicon = document.getElementById('font-img-medium');
  	var moversrc = fmicon.src.replace('_over.gif', '.gif').replace('_over.jpg', '.jpg');
  	fmicon.src = moversrc;
  	fmicon.className = 'btn';
  	var flicon = document.getElementById('font-img-large');
  	mouseOverImgs();
    flicon.onmouseover = function() {};
    flicon.onmouseout = function() {};
  }
};
// Bootstrap
FontChanger.start = function(id) {
  var fontChanger = new FontChanger(id);
  fontChanger.show();
};

Event.observe(window, 'load', hiliteMenu, false);
function hiliteMenu() {
var menulist = {
'1-1': 'about1',
'1-2': 'about2',
'1-3': 'about3',
'25': 'about4',
'68': 'facility1',
'69': 'facility2',
'70': 'facility3',
'71': 'facility4',
'72': 'facility5',
'73': 'facility6',
'74': 'facility7',
'75': 'facility8',
'76': 'facility9',
'113': 'facility10',
'65': 'news1',
'66': 'news2',
'64': 'news3',
'16': 'program1',
'17': 'program2',
'18': 'program3',
'19': 'residence1',
'3': 'residence2',
'22': 'residence2',
'12-1': 'help1',
'12-2': 'help2',
'12-3': 'help3',
'118-1': 'about1',
'118-2': 'about2',
'118-3': 'about3',
'129': 'shop1',
'130': 'shop2',
'131': 'shop3',
'132': 'shop4',
'133': 'shop5',
'134': 'shop6'
};
if (catid) {
	var elmid = 'bt-' + menulist[catid];
	var lang = '';
	if (catid == '118-1' || catid == '118-2' || catid == '118-3') lang = '/en';
	if ($(elmid)) {
		$(elmid).src = '/imgs' + lang + '/side/bt_' + menulist[catid] + '_over.gif';
		$(elmid).className = 'btn-current';
	}
}
mouseOverImgs();
}

//IMG Mouse Over
//Event.observe(window, 'load', mouseOverImgs, false);
function mouseOverImgs() {
  var btns = $A(document.getElementsByClassName('btn'));
  btns.each(function (node){
    node.outsrc = node.src;
    node.oversrc = node.src.replace('.gif', '_over.gif').replace('.jpg', '_over.jpg');
    var overimg = new Image();
    overimg.src = node.oversrc;
    node.onmouseover = function() { this.src = this.oversrc; };
    node.onmouseout = function() { this.src = this.outsrc; };
  });
}

Array.prototype.is_in = function (obj) { 
	var i = 0; 
	var rep = false; 
	while (this[i] != null && !rep) rep = (this[i++]==obj); 
	return rep; 
}

function showCommentForm() {
	var manager = new CookieManager();
	var user = manager.getCookie('mt_user');
	if (user) {
		if ($('comments_form')) {
			$('comments_form').style.display = 'block';
		}
	}
}

function initInput(inpt) {
//alert(inpt.value);
	if (inpt.value == 'Search') inpt.value = '';
}

function revertInput(inpt) {
//alert(inpt.value);
	if (inpt.value == '') inpt.value = 'Search';
}

function scrollEffect(targetID) {
	if ($(targetID)) new Effect.ScrollTo(targetID);
}