var baseurl = parent.getBaseURL();
if (baseurl.substr(0,4) != 'http') {
  var loc = document.location.toString();
  var i = loc.indexOf(baseurl);
  if (i) {
    baseurl = loc.substr(0,i) + baseurl;
  }
}
var hmenuTitle = new Array();
var hmenuImage = new Array();
var hmenuImages = new Array();
var hmenuLink = new Array();
var hmenuText = new Array();
var currentImage = 0;
var MAX_BLOCK = 5;
var MAX_IMAGES = 10;

function footerCommand() {
  if (++currentImage >= MAX_IMAGES) {
    currentImage = 0;
  }
  var count = 0;
  for (var i=1; i <= MAX_BLOCK; ++i) {
    var e = document.getElementById('diapoimage'+i);
    if (e) {
      var n = (i-1) * MAX_IMAGES;
      var image = hmenuImages[n+currentImage];
      if (image.length > 0) {
        e.src = image;
        ++count;
      }
    }
  }
  if (count == 0) {
    currentImage = -1;
    footerCommand();
  }
}

function setMenuBlock(index,title,image,link,text) {
  hmenuTitle[index] = title;
  image = image.replace(/{baseurl}/g,baseurl);
  images = image.split('|');
  hmenuImage[index] = images[0];
  var n = index * MAX_IMAGES;
  var i;
  for (i=0; i < images.length; ++i) {
    hmenuImages[n+i] = images[i];
  }
  for (; i < MAX_IMAGES; ++i) {
    hmenuImages[n+i] = '';
  }
  link = link.replace(/{baseurl}/g,baseurl);
  hmenuLink[index] = link;
  if (text == '.' || text.length == 0) {
    text = "&nbsp;";
  }else{
    text = text.replace(/  /g,'&nbsp; ');
  }
  hmenuText[index] = text.replace(/\\n/g,'<br/>');
}

function loadMenuConfig(doc) {

  var str, title, image, link, text;
  var entry, item;
  
  var source = getServerFile(doc);
  var xml = new XMLDoc(source,alert);
  var node = xml.docNode;
  var field = 0;
  var index = 0;
  
  var n = 0;
  if (node.children) {
    for (var j=0; j < node.children.length; ++j) {
      entry = node.children[j];
      if (entry.nodeType == 'ELEMENT') {
        tag = entry.tagName.toLowerCase();
        if (tag == 'entry') {
          if (entry.children) {
            image = link = text = '';
            field = 0;
            for (var k=0; field != 7 && k < entry.children.length; ++k) {
              item = entry.children[k];
              if (item.nodeType == 'ELEMENT') {
                tag = item.tagName.toLowerCase();
                str = item.getText();
                if (tag == 'image') {
                  image = str;
                  field |= 1;
                }else if (tag == 'link') {
                  link = str;
                  field |= 2;
                }else if (tag == 'text') {
                  text = str;
                  field |= 4;
                }
              }
            }
            if (field >= 3) {
              title = entry.getAttribute('title');
              if (field == 3) {
                text = '&nbsp;';
              }else{
                text = text.replace(/\.  /g,'.&nbsp; ');
                while (text.indexOf('  ') >= 0) {
                  text = text.replace(/  /g,' ');
                }
              }
              setMenuBlock(index,title,image,link,text);
              ++index;
            }
          }
        }
      }
    }
  }
} 

function emitMenuItem(n) {
  var i = n - 1;
  document.writeln('      <img id="diapoimage'+n+'" class="diapo" src="'+hmenuImage[i]+'" alt="">');
  document.writeln('      <div class="text">');
  document.writeln('        <span class="title">'+hmenuTitle[i]+'</span>');
  document.writeln(hmenuText[i]);
  document.writeln('      </div>');
}

function gotopage(menulink, validate) {
  var n;
  if (menulink == "item1") {
    n = 1;
  }else if (menulink == "item2") {
    n = 2;
  }else if (menulink == "item3") {
    n = 3;
  }else if (menulink == "item4") {
    n = 4;
  }else if (menulink == "item5") {
    n = 5;
  }else{
    n = 0;
  }
  if (n == 0) {
    return false;
  }
  var lnk = hmenuLink[n-1];
  if (!lnk || lnk.length == 0) {
    return false;
  }
  if (!validate) {
    //stopInterval();
    showLoading(true);
    fade('slider');
    //window.open(lnk, "_blank");
    window.parent.location = lnk;
  }
  return true;
}

var showLoading_x = 270;
var showLoading_y = 400;
function setLoading(x, y) {
  showLoading_x = x;
  showLoading_y = y;
}
function showLoading(enable) {
  var e = document.getElementById('h-menu-loading');
  if (e) {
    if (enable) {
  		e.style.left = showLoading_x + "px";
  		e.style.top = showLoading_y + "px";
  		e.style.zIndex = 100;
  		setStyle(e, 'z-index', 100);
  		e.style.display = 'inline';
    }else{
      e.style.left = "-3000px";
    }
  }
}


