var id = 0;
var leftPos, topPos;

function setElementPositions(isResize) {
  if (navigator.userAgent.toLowerCase().indexOf('msie') != -1) { 
   var windowWidth = document.body.clientWidth;
   if (windowWidth <= 898) {
     leftPos = 2;
   } else {
     leftPos = (windowWidth / 2) - 449;
   }
   topPos =  275; 
  } else {   
   var mainNav = document.getElementById('nav');
   if (navigator.userAgent.toLowerCase().indexOf('firefox/3') != -1) {
     leftPos = mainNav.offsetLeft;
     topPos = mainNav.offsetTop;
   } else {
     leftPos = mainNav.offsetLeft + 2;
     topPos = mainNav.offsetTop + 1;
   }   
  }

  if (isResize) {
    oCMenu.fromLeft=leftPos;
    oCMenu.fromTop=topPos;
  }

}

function loadCoolMenus() {

setElementPositions(false);

/*** 
This is the menu creation code - place it right after you body tag
Feel free to add this to a stand-alone js file and link it to your page.
**/

//Menu object creation
oCMenu=new makeCM("oCMenu") //Making the menu object. Argument: menuname
oCMenu.onresize="setElementPositions(true)";

oCMenu.frames = 0

//Menu properties   
oCMenu.pxBetween=0
oCMenu.fromLeft=leftPos
oCMenu.fromTop=topPos
oCMenu.rows=1 
oCMenu.menuPlacement="left"

                                                             
oCMenu.offlineRoot="file:///C|/Inetpub/wwwroot/dhtmlcentral/projects/coolmenus/examples/" 
oCMenu.onlineRoot="/coolmenus/" 
oCMenu.resizeCheck=1 
oCMenu.wait=1000 
oCMenu.fillImg="cm_fill.gif"
oCMenu.zIndex=0

//Background bar properties
oCMenu.useBar=0
oCMenu.barWidth="800"
oCMenu.barHeight="menu" 
oCMenu.barClass="clBar"
oCMenu.barX=0 
oCMenu.barY=0
oCMenu.barBorderX=0
oCMenu.barBorderY=0
oCMenu.barBorderClass=""

//Level properties - ALL properties have to be spesified in level 0
oCMenu.level[0]=new cm_makeLevel() //Add this for each new level
oCMenu.level[0].width=100
oCMenu.level[0].height=28
oCMenu.level[0].regClass="clLevel0"
oCMenu.level[0].overClass="clLevel0over"
oCMenu.level[0].borderX=0
oCMenu.level[0].borderY=0
oCMenu.level[0].borderClass="clLevel0border"
oCMenu.level[0].offsetX=0
oCMenu.level[0].offsetY=0
oCMenu.level[0].rows=0
oCMenu.level[0].arrow=0
oCMenu.level[0].arrowWidth=0
oCMenu.level[0].arrowHeight=0
oCMenu.level[0].align="bottom"

//EXAMPLE SUB LEVEL[1] PROPERTIES - You have to specify the properties you want different from LEVEL[0] - If you want all items to look the same just remove this
oCMenu.level[1]=new cm_makeLevel() //Add this for each new level (adding one to the number)
oCMenu.level[1].width=175
oCMenu.level[1].height=22
oCMenu.level[1].regClass="clLevel1"
oCMenu.level[1].overClass="clLevel1over"
oCMenu.level[1].borderX=1
oCMenu.level[1].borderY=1
oCMenu.level[1].align="right" 
oCMenu.level[1].offsetX=-(oCMenu.level[0].width-2)/2+20
oCMenu.level[1].offsetY=0
oCMenu.level[1].borderClass="clLevel1border"

/******************************************
Menu item creation:
myCoolMenu.makeMenu(name, parent_name, text, link, target, width, height, regImage, overImage, regClass, overClass , align, rows, nolink, onclick, onmouseover, onmouseout) 
*************************************/

// This has been modified from the original script to build the menu from lists of links in hidden divs on each page
var winLocation = window.location + "";
//if (winLocation.indexOf('http://www.winetoursoftheworld.com') == 0) {
 addMainLink('nav56', 90);
 addMainLink('nav55', 160);
 addMainLink('nav54', 116);
 addMainLink('nav53', 168);
 addMainLink('nav108', 121);
 addMainLink('nav51', 133);
 addMainLink('nav24', 110);
/*
} else {
 addMainLink('textNav56', 90);
 addMainLink('textNav55', 160);
 addMainLink('textNav54', 116);
 addMainLink('textNav53', 168);
 addMainLink('textNav108', 121);
 addMainLink('textNav51', 133);
 addMainLink('textNav24', 110);
}
*/



oCMenu.construct();
//alert(msg);
}

var msg = '';

function addMainLink(id, width) {
  var link = document.getElementById(id);
  addLinkToMenu(link, '', width);
}


function buildMenu(container, parent) {
  var c = document.getElementById(container);
  if (!c) {
    return;
  }
  
  var links = c.getElementsByTagName('a');
  
  for (var a = 0; a < links.length; a++) {
   var link = links[a];
   addLinkToMenu(link, parent, '');
  }  
}

function addLinkToMenu(link, parent, width) {
  var parentNode = link.parentNode;
  // Filter out old non-translated text - google keeps it around in hidden span tags
  if (parentNode.className == 'google-src-text') {
    return;
  }   
    
  var text = link.innerHTML;
  if (!text) {
    return;
  }

  // Trim whitespace
  text = text.replace(/^\s*|\s*$/g,'');
  if (!text) {
    return;
  }

  var href = link.href;
  if (!href) {
    return;
  }

  var navId = link.id;
  var subId = navId.replace('textNav', 'sub');
  var subId = navId.replace('nav', 'sub');
   
  msg += navId + ":" + text + "\n";
  // Add menu item to cool menus
  if (width) {
    oCMenu.makeMenu(navId,parent,text,href,'',width);   
  } else {
    oCMenu.makeMenu(navId,parent,text,href);   
  }

  // Build out menu sublinks
  buildMenu(subId, navId);
}