
function $(id) { return document.getElementById (id)}

var _oldonload = window.onload ;
window.onload  = function  (e) {
	var menuRoot = $('menuroot') ;
	menuRoot.subitems = findChildrenByClass (menuRoot, 'menuitem') ;

	for (var i=0; i<menuRoot.subitems.length; i++) {
		initMenuItems (menuRoot.subitems[i]) ;
	}
    menuRoot.subitems[0].headItem.style.border = '0px none' ;
	for (var i=0; i<menuRoot.subitems.length; i++) {
		restoreMenuItemStates (menuRoot.subitems[i]) ;
	}

  if (_oldonload)
	  _oldonload (e) ;
}
function restoreMenuItemStates (root) {
    if (root && root.subitems) {
      for (var i=0; i<root.subitems.length; i++) {
        restoreMenuItemStates (root.subitems[i]) ;
      }

      if (!hasInCookie (root.id)) {
        toggle_menu (root, 500) ;
      }
    }

}
function initMenuItems (root) {
	var temp = findChildrenByClass (root, 'menuitemheader') ;
	if (temp.length == 0) return ;
	root.headItem = temp[0] ;
	root.bodyItem = findChildrenByClass (root, 'menuitembody')[0] ;

	root.bodyItemHeight = root.bodyItem.offsetHeight ;
	root.bodyItem.style.height = root.bodyItemHeight + "px" ;

	var swithImage = root.headItem.getElementsByTagName ('img') [0] ;
	swithImage.item = root ;
	swithImage.onclick = function () {
		toggle_menu (this.item, 7) ;
	}
	root.swithImage = swithImage ;

	root.subitems = findChildrenByClass (root.bodyItem, 'menuitem') ;

	for (var i=0; i<root.subitems.length; i++) {
		initMenuItems (root.subitems[i]) ;
	}

}

function findChildrenByClass (parent, class_name) {
	var ret = [] ;
	var c = parent.firstChild ;
	while (c) {
//		if ((''+c.className).indexOf(class_name) == 0) {
        if ((''+c.className).indexOf(class_name+"_") == 0 || c.className == class_name)
        {
			ret.push (c) ;
		}
		c = c.nextSibling ;
	}
	return ret ;
}

function toggle_menu (item, stepSize) {

 var body = item.bodyItem ;

 if (body.style.display == 'none') {
  body.stepSize = stepSize ; // Math.ceil (item.bodyItemHeight / 20) ;
  body.style.display = 'block' ;
        body.style.height = '1px';
        updateAncestors(item.parentNode, 1) ;
  addToCookie (item.id) ;
 }
 else {
        body.style.height = item.bodyItemHeight + "px";
  body.stepSize = -stepSize ; // Math.ceil (item.bodyItemHeight / 20) ;
  removeFromCookie (item.id) ;
 }
    body.style.overflow = 'hidden' ;
 if (item.task) clearTimeout (item.task) ;
 item.task = setTimeout(function () {step(item);}, 5) ;
}


function step (item) {
 var body = item.bodyItem ;

    var newHeight = $num(body.style.height) + body.stepSize ;

    if (newHeight <= Math.abs(body.stepSize)) {
        updateAncestors(item.parentNode, -$num(body.style.height)) ;
        body.style.display = 'none' ;
        item.swithImage.src = 'resources/images/header_images/06.gif' ;
//        item.swithImage.className = 'imageclosed';
    }
    else if (newHeight >=  item.bodyItemHeight) {
        updateAncestors(item.parentNode, item.bodyItemHeight-$num(body.style.height)) ;
        item.swithImage.src = 'resources/images/header_images/07.gif' ;
        body.style.height = item.bodyItemHeight + "px";
//        item.swithImage.className = 'imageopen' ;
    }
    else {
      item.bodyItem.style.height = newHeight + "px" ;
      updateAncestors(item.parentNode, body.stepSize) ;
      item.task = setTimeout(function () {step(item);}, 1) ;
 }
}
function updateAncestors (item, delta) {
  if (!item || item.id == 'menuroot') return;
  if (item.bodyItem) {
      item.bodyItemHeight += delta ;
      item.bodyItem.style.height = item.bodyItemHeight + "px" ;
  }
  updateAncestors (item.parentNode, delta) ;
}
window.$num = function (str) {
	if ((''+str).match (/(-?\d+\.\d+)/)) return parseFloat  (RegExp.$1);
	if ((''+str).match (/(-?\d+)/)) return parseInt  (RegExp.$1);
	return 0;
}

function addToCookie (id) {
	var cookie = getCookieString () ;
	var all = cookie.split (",") ;
	all.push (id) ;

	var date = new Date();
	date.setDate (date.getDate() + 30);
	var str = "portal_menu=," + all.join (",") + ", " ;
	str = str.replace (/,+/g, ",") ;


	document.cookie = str + "; expires=" + date.toGMTString() ;

}
function removeFromCookie (id) {
	var cookie = getCookieString () ;
	var all = cookie.split (",") ;
	var allMap = {}; var allToSet = [] ;

	for (var i=0; i<all.length; i++) {
		if (allMap [ all[i] ] != true) {
			allMap [ all[i] ] = true ;

			if (id != all[i]) {
				allToSet.push (all[i]) ;
			}

		}
	}

	var date = new Date();
	date.setDate (date.getDate() + 30);
	var str = "portal_menu=," + allToSet.join (",") + "," ;
	str = str.replace (/,+/g, ",") ;


    document.cookie = str + "; expires=" + date.toGMTString() ;
}
function hasInCookie (id) {
	var cookie = getCookieString () ;
	return cookie.indexOf (","+id+",") != -1 ;
}
function getCookieString () {
    var cookie = document.cookie.substring (document.cookie.indexOf ("portal_menu=")+"portal_menu=".length) ;
    var end = cookie.indexOf (";") ;
    if (end == -1) return cookie ;
    else  return cookie.substring (0, end) ;
}
