﻿// browser detection
var browser = {
    version: parseInt(navigator.appVersion),
    isNetscape: navigator.appName.indexOf("Netscape") != -1,
    isMicrosoft: navigator.appName.indexOf("Microsoft") != -1
};

function SetLargeImage(id, p)
{
    var obj = document.getElementById(id);
    obj.src = p;
}


// Move an element directly on top of another element (and optionally
// make it the same size)
function Cover(bottom, top, ignoreSize) {
    var location = Sys.UI.DomElement.getLocation(bottom);
    top.style.position = 'absolute';
    top.style.top = location.y + 'px';
    top.style.left = location.x + 'px';
    if (!ignoreSize) {
        top.style.height = bottom.offsetHeight + 'px';
        top.style.width = bottom.offsetWidth + 'px';
    }
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

document.onmousemove = trackMouse;

var menu_length = 846;
var menu_middle_point = 423;
var menu_left_area = 0;
var menu_middle_area = 280;
var menu_right_area = 560;

var to;

// JScript File
// operation of submenu
function setImage(id, img, submenu_id, visibility)
{
    var obj = document.getElementById(id);
    //obj.src = img;
    if(visibility == 'visible') {
        // show immediately
        //alert("submenu will be seen");
        if(to != null)
            clearTimeout(to);
            
        ShowIt('submenu');
        SetVisibilitySubMenu(submenu_id);
        AdjustPositionVertical(submenu_id, id);
    }
    else {
        // hide it after some seconds
        //alert("submenu can be hidden shortly");
        to = setTimeout("HideIt('" + submenu_id + "')", 1000);
    }   
}  

function SetVisibilitySubMenu(id)
{
    // first, hide all submenu
   SetVisibilityAllSubMenu('submenu', 'hidden'); 
   
        
   // set visibile for id
   document.getElementById(id).style.visibility = 'visible';
}

function SetVisibilityAllSubMenu(id, vis)
{
    var children = document.getElementById('submenu').getElementsByTagName('div');
   
   for(i=0; i<children.length;i++)
        document.getElementById(children[i].id).style.visibility = vis;
 }
 
// align position of target(submenu) to source(mainmenu)
// for horizontal submenu
function AdjustPositionHorizontal(target_id, source_id)
{
    var coord = getElementPosition(source_id);
    
   var h = GetHeight(document.getElementById(source_id));

   var x_start = coord.left;
   
   var y_end = coord.top + h;
   
   var target = document.getElementById(target_id);
   
   var target_width = GetWidth(target);
   
   target.style.top = y_end + 10;
   if(x_start < menu_middle_area)
        target.style.left = x_start;
   else if(x_start < menu_right_area)
        target.style.left = x_start - target_width/2;
   else
        target.style.left = menu_length - target_width;
}

function AdjustPositionVertical(target_id, source_id) {
    var coord = getElementPosition(source_id);
    var h = GetHeight(document.getElementById(source_id));

    var x_start = coord.left;
    var y_end = coord.top + h;


    var target = document.getElementById(target_id);

    if (browser.isMicrosoft) {
        // IE
        target.style.top = y_end + 5;
        target.style.left = x_start;
    }
    else {
        target.style.top = y_end + 5 + "px";
        target.style.left = x_start + "px";
    }
    
    
    
}

function ShowIt(id)
{
    var obj = document.getElementById(id);
    
    obj.style.visibility = 'visible';
   
}
var mouseX, mouseY;

var subMenuEntered = 0;
function SetFlag(flag)
{
/*
    if(flag == 1)
        alert("Entered to submenu");
    else
        alert("Out from submenu");  
      */
    subMenuEntered = flag;
}

// immediately hide submenu if flag is 1
/*
function HideSubmenu()
{
    HideIt('submenu');

}
*/

function HideSubmenu(id)
{
    HideIt('submenu' + id);
    
}


function HideIt(id)
{
    // check if the mouse cursor within the submenu area
    // then do not hide it 
   //alert(id + ' will be hidden');
    
   var sub = document.getElementById(id); 
   
   var coord = getElementPosition(id);
   var w = GetWidth(sub);
   var h = GetHeight(sub);
   
   var x_start = coord.left;
   var x_end = coord.left + w;
   
   var y_start = coord.top;
   var y_end = coord.top + h;
   
   
   //alert(x_start + ' ' + x_end + ' ' + y_start + ' ' + y_end + ', ' + mouseX + ' ' + mouseY);
   
   if(mouseX >= x_start && mouseX <= x_end && mouseY >= y_start && mouseY <= y_end) {
        // do nothing
   }
   else {
        sub.style.visibility = 'hidden';
        //SetVisibilityAllSubMenu('submenu', 'hidden');
   }
   
    //alert(w + ' ' + h);  
    
   
    
    //
}



function trackMouse(e) {
    if (browser.isMicrosoft) {
        mouseX = event.clientX + document.body.scrollLeft;
        mouseY = event.clientY + document.body.scrollTop;
    }
    else {
        mouseX = e.pageX;
        mouseY = e.pageY;
    }

   
}
  
function getElementPosition(elemID) {
    var offsetTrail = document.getElementById(elemID);
    var offsetLeft = 0;
    var offsetTop = 0;
    while (offsetTrail) {
        offsetLeft += offsetTrail.offsetLeft;
        offsetTop += offsetTrail.offsetTop;
        offsetTrail = offsetTrail.offsetParent;
    }
    if (navigator.userAgent.indexOf("Mac") != -1 && 
        typeof document.body.leftMargin != "undefined") {
        offsetLeft += document.body.leftMargin;
        offsetTop += document.body.topMargin;
    }
    return {left:offsetLeft, top:offsetTop};
}

/*
function mouseX(evt) {
if (evt.pageX) return evt.pageX;
else if (evt.clientX)
   return evt.clientX + (document.documentElement.scrollLeft ?
   document.documentElement.scrollLeft :
   document.body.scrollLeft);
else return null;
}
function mouseY(evt) {
if (evt.pageY) return evt.pageY;
else if (evt.clientY)
   return evt.clientY + (document.documentElement.scrollTop ?
   document.documentElement.scrollTop :
   document.body.scrollTop);
else return null;
}
*/

GetWidth = function(elem) {
	function _Convert(val) {
		if (!val) {return;}
		val = val.replace("px","");
		if (isNaN(val)) {return 0;}
		return parseInt(val);
	}
	var currentStyle;
	if (elem.currentStyle) { currentStyle = elem.currentStyle; }
	else if (window.getComputedStyle) {	currentStyle = document.defaultView.getComputedStyle(elem, null); }
	else { currentStyle = elem.style; }
	return (elem.offsetWidth -
		_Convert(currentStyle.marginLeft) -
		_Convert(currentStyle.marginRight) -
		_Convert(currentStyle.borderLeftWidth) -
		_Convert(currentStyle.borderRightWidth));
}
 
GetHeight = function(elem) {
	function _Convert(val) {
		if (!val) {return;}
		val = val.replace("px","");
		if (isNaN(val)) {return 0;}
		return parseInt(val);
	}
	var currentStyle;
	if (elem.currentStyle) { currentStyle = elem.currentStyle; }
	else if (window.getComputedStyle) {	currentStyle = document.defaultView.getComputedStyle(elem, null); }
	else { currentStyle = elem.style; }
	return (elem.offsetHeight -
		_Convert(currentStyle.marginTop) -
		_Convert(currentStyle.marginBottom) -
		_Convert(currentStyle.borderTopWidth) -
		_Convert(currentStyle.borderBottomWidth));	
}


function ToggleButton(id, img)
{
    var btn = document.getElementById(id);
    btn.src = img;
}




// Font Size Change ---------------------------------//

var minFS = 8;
var maxFS = 20;
function increaseFontSize() {
    var p = document.getElementsByTagName('p');
    for (i = 0; i < p.length; i++) {      
        if (p[i].style.fontSize) {
            var s = parseInt(p[i].style.fontSize.replace("px", ""));
        } else {
        var s = 12;
        
        }
        if (s != maxFS) {
            s += 2;
        }
        p[i].style.fontSize = s + "px"
        p[i].style.lineHeight = 1.3;
       // p[i].style.lineHeight = p[i].style.lineHeight + 0.01;
    }
}
function decreaseFontSize() {
    var p = document.getElementsByTagName('p');
    for (i = 0; i < p.length; i++) {
        if (p[i].style.fontSize) {
            var s = parseInt(p[i].style.fontSize.replace("px", ""));
        } else {
           var s = 12;
           p[i].style.lineHeight = 1.3;
        }
        if (s != minFS) {
            s -= 2;
        }
        p[i].style.fontSize = s + "px"
        p[i].style.lineHeight = 1.3;
    }
}



function resetFontSize() {
    var p = document.getElementsByTagName('p');
    for (i = 0; i < p.length; i++) {
        var s = 12;
        p[i].style.fontSize = s + "px"
    }
}

// (End) Font Size Change  -----------------------------//