/***********************************************************************************************
Copyright (c) 2005 - Alf Magne Kalleland post@dhtmlgoodies.com Get this
and other scripts at www.dhtmlgoodies.co mYou can use this script
freely as long as this copyright message is kept intact.
***********************************************************************************************/
var menuAlignment = 'left';
// Align menu to the left or right?
var topMenuSpacer = 0;
// Horizontal space(pixels) between the main menuitems
var activateSubOnClick = false;
// if true-> Show sub menu items on click, if false, show submenu items onmouseover
var leftAlignSubItems = false;
// left align sub items t
var activeMenuItem = false;
// Don't change this option. It should initially be false
var activeTabIndex = 0;
// Index of initial active tab (0 = first tab) - If the value below is set to true, it will override this one.
var rememberActiveTabByCookie = false;
// Set it to true if you want to be able to save active tab as cookie
var MSIE = navigator.userAgent.indexOf('MSIE') >= 0 ? true: false;
var Opera = navigator.userAgent.indexOf('Opera') >= 0 ? true: false;
var navigatorVersion = navigator.appVersion.replace(/.*?MSIE ([0-9]\.[0-9]).*/g, '$1') / 1;

var seperator = '|';

function showHide() {
  if (activeMenuItem) {
      activeMenuItem.className = 'inactiveMenuItem';
      var theId = activeMenuItem.id.replace(/[^0-9]/g, '');
      document.getElementById('submenu_' + theId).style.display = 'none';
      var img = activeMenuItem.getElementsByTagName('img');
      if (img.length > 0) img[0].style.display = 'none';
  }

  var img = this.getElementsByTagName('img');
  if (img.length > 0) img[0].style.display = 'inline';

  activeMenuItem = this;
  this.className = 'activeMenuItem';

  var theId = this.id.replace(/[^0-9]/g, '');
  var obj = document.getElementById('submenu_' + theId);
  obj.style.display = 'block';

}

function HideMenu() {
  var img = this.getElementsByTagName('img');
  if (img.length > 0) img[0].style.display = 'inline';

  // activeMenuItem = this;
  // this.className = 'activeMenuItem';

  var theId = this.id.replace(/[^0-9]/g, '');
  var obj = document.getElementById('submenu_' + theId);
  obj.style.display = 'none';

  //document.getElementById('submenu').style.paddingTop = '5px';
}

/*function HideMenu() {
  var img = this.getElementsByTagName('img');
  if (img.length > 0) img[0].style.display = 'inline';

  var theId = this.id.replace(/[^0-9]/g, '');
	var obj = document.getElementById('submenu_' + theId);
	obj.style.display = 'none';
}*/

function initMenu() {
	setMenu();

  var mainMenuObj = document.getElementById('mainTextMenu');
	var subMenuObj  = document.getElementById('submenu');
  var menuItems   = mainMenuObj.getElementsByTagName('a');
  if (document.all) {
    mainMenuObj.style.visibility = 'hidden';
    document.getElementById('submenu').style.visibility = 'hidden';
  }
    /*
  if (rememberActiveTabByCookie) {
        var cookieValue = Get_Cookie('dhtmlgoodies_tab_menu_tabIndex') + '';
        cookieValue = cookieValue.replace(/[^0-9]/g, '');
        if (cookieValue.length > 0 && cookieValue < menuItems.length) {
            activeTabIndex = cookieValue / 1;
        }
    }
    */

  var subMenus = subMenuObj.getElementsByTagName('div');
  for (var i=0; i<subMenus.length; i++) {
    subMenus[i].onmouseover = showHide;
    //subMenus[i].onmouseout = HideMenu;
  }

  var currentLeftPos = 15;
  for (var no = 0; no < menuItems.length; no++) {
    if (activateSubOnClick) {
      menuItems[no].onclick = showHide;
    } else {
      menuItems[no].onmouseover = showHide;
    }

    menuItems[no].id = 'mainMenuItem' + (no + 1);
    if (menuAlignment == 'left') {
      menuItems[no].style.left = currentLeftPos + 'px';
    } else {
      menuItems[no].style.right = currentLeftPos + 'px';
    }
    currentLeftPos = currentLeftPos + menuItems[no].offsetWidth + topMenuSpacer;

    var img = menuItems[no].getElementsByTagName('IMG');
    if (img.length > 0) {
      img[0].style.display = 'none';
      if (MSIE && !Opera && navigatorVersion < 7) {
        img[0].style.bottom = '-1px';
        img[0].style.right = '-1px';
      }
    }

    /*
     * ±âº»Àº ¼±ÅÃ ¾ÈÇÑ°É·Î.
     */
    /*
        if (no == activeTabIndex) {
            menuItems[no].className = 'activeMenuItem';
            activeMenIutem = menuItems[no];
            var img = activeMenuItem.getElementsByTagName('IMG');
            if (img.length > 0) img[0].style.display = 'inline';
        } else menuItems[no].className = 'inactiveMenuItem';
    */
    menuItems[no].className = 'inactiveMenuItem';

    if (!document.all) menuItems[no].style.bottom = '-1px';
    if (MSIE && navigatorVersion < 6) menuItems[no].style.bottom = '-2px';
  }//for

  Nifty("div#submenu div","normal");

  var mainMenuLinks = mainMenuObj.getElementsByTagName('a');

  var subCounter = 1;
  var parentWidth = mainMenuObj.offsetWidth;
  while (document.getElementById('submenu_' + subCounter)) {
    var subItem = document.getElementById('submenu_' + subCounter);

    if (leftAlignSubItems) {
      // No action
    } else {
      var leftPos = mainMenuLinks[subCounter - 1].offsetLeft;
      var offWidth = mainMenuLinks[subCounter - 1].offsetWidth;
      subItem.style.position = 'absolute';

      if ((subItem.offsetWidth+leftPos) > parentWidth) {
        if (subItem.offsetWidth > parentWidth) {
          subItem.style.left = "0px";
        } else {
          //subItem.style.paddingLeft = (parentWidth-subItem.offsetWidth-offWidth) + "px";          
          /*
          if (parentWidth - subItem.offsetWidth > 10) {
            subItem.style.paddingLeft = (parentWidth - subItem.offsetWidth - 10)+'px';            
          } else {
            subItem.style.paddingLeft = (parentWidth - subItem.offsetWidth)+'px';
          }          
          */
        }
      } else {
        //subItem.style.paddingLeft = leftPos + 'px';
      }
        subItem.style.position = 'static';
    }
    /*
    if (subCounter == (activeTabIndex + 1)) {
        subItem.style.display = 'block';
    } else {
        subItem.style.display = 'none';
    }
    */
    subItem.style.display = 'none';
    subCounter++;
  }//while

  if (document.all) {
      mainMenuObj.style.visibility = 'visible';
      document.getElementById('submenu').style.visibility = 'visible';
  }

  document.getElementById('submenu').style.display = 'block';
  //document.getElementById('submenu').style.display = 'block';

}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

function createAnchor(parent, id, txt, href, title, className, target, isSeperator) {
  var pObj  = document.getElementById(parent);
  var obj   = document.createElement("a");

  obj.setAttribute("id", id);
  obj.setAttribute("href", href);
  obj.setAttribute("title", title);
  obj.setAttribute("className", className);
  obj.setAttribute("target", target);

  obj.appendChild(document.createTextNode(txt));//append text node

  pObj.appendChild(obj);

  if (isSeperator == "false") {
    //var sp = document.createElement("span");
    //sp.appendChild(document.createTextNode(seperator));
    //pObj.appendChild(sp);

    //pObj.appendChild(document.createTextNode(seperator));
    addSeperator(parent)
  }
}

function addSeperator(parent) {
  var pObj = document.getElementById(parent);
  var span = document.createElement("span");
  span.appendChild(document.createTextNode(seperator));
  pObj.appendChild(span);//append seperator
}

function createDiv(parent, id, title, rel){
  //ÀÌ¹Ì id°¡ ¸¸µé¾îÁ® ÀÖ´Ù¸é, ¸¸µéÁö ¾Ê´Â´Ù.
  if (document.getElementById(id)) return;

  var obj = document.createElement("div");
  obj.setAttribute("id", id);
  obj.setAttribute("title", title);
  obj.setAttribute("rel", rel);

  var pObj = document.getElementById(parent);
  pObj.appendChild(obj);
}

//for ul, div
function creteTags(elem, parent, id, alt, rel) {
  var obj = document.createElement(elem);
  var pObj = document.createElement(parent);
  if (obj == null || pObj == null) { return; }
  
  obj.setAttribute("id", id);
  obj.setAttribute("alt", alt);
  obj.setAttribute("title", alt);
  if (rel != "") {
     obj.setAttribute("rel", rel);
  }

  pObj.appendChild(obj);
}

function createLI(parentId, id, alt, url, className, target) {
  //ÀÌ¹Ì id°¡ ¸¸µé¾îÁ® ÀÖ´Ù¸é, ¸¸µéÁö ¾Ê´Â´Ù.
  if (document.getElementById(id))  return;

  var pObj = document.getElementById(parentId);
  if (pObj == null) { return; }

  var obj = document.createElement("li");//create li element
  if (obj == null) { return; }

  obj.setAttribute("id", id);
  obj.setAttribute("alt", alt);
  obj.setAttribute("title", alt);
  obj.setAttribute("className", className);

  with (obj.style) {
      margin = "0";
      padding = "0";
      float = "left";
  }

  var anchor = document.createElement("a");
  anchor.setAttribute("id", "");
  anchor.setAttribute("href", url);
  anchor.setAttribute("title", title);
  anchor.setAttribute("className", className);
  anchor.setAttribute("target", target);

  anchor.appendChild(document.createTextNode(title));//append text node

  obj.appendChild(anchor);//append anchor element
  pObj.appendChild(obj);//append li element
}
