var ddMenu = {
  topNavContainerId : 'topNav',
  
  //panels MUST contain an entry per each topNav button, in same order
  //if a button shouldn't have a popup, initialize as element with empty first entry, e.g.: ['', 0, 0, Array()]
  panels : Array(
    ['', 0, 0, Array()],
    
    ['ddPanel_about', 48, 20, Array(
      ['History', 'http://www.beazley.com/about_beazley/history.aspx'],
      ['Beazley worldwide', 'http://www.beazley.com/about_beazley/beazley_worldwide.aspx'],
      ['Beazley at Lloyd\'s', 'http://www.beazley.com/about_beazley/beazley_at_lloyds.aspx'],
      ['The Board', 'http://investor.relations.beazley.com/investorrelations/board/'],      
      ['Management team', 'http://www.beazley.com/about_beazley/mgt_team.aspx'],    
      ['Underwriting overview', 'http://www.beazley.com/about_beazley/underwriting_overview.aspx'],
      ['Ratings', 'http://www.beazley.com/about_beazley/ratings.aspx'],
      ['Key facts', 'http://www.beazley.com/about_beazley/key_facts.aspx'],
      ['Clients', 'http://www.beazley.com/about_beazley/clients.aspx'],
      ['Contact us', 'http://www.beazley.com/about_beazley/contact_us/global_office_locations.aspx']
    )],
    
    ['ddPanel_lines', 174, 20, Array(
      ['Life, Accident &amp; Health', 'http://www.beazley.com/business_lines/accident__life.aspx'],
      ['Marine', 'http://www.beazley.com/business_lines/marine.aspx'],
      ['Political Risks & Contingency', 'http://www.beazley.com/business_lines/political_risks__contingency.aspx'],
      ['Property', 'http://www.beazley.com/business_lines/property_group.aspx'],
      ['Reinsurance', 'http://www.beazley.com/business_lines/reinsurance.aspx'],
      ['Professional Liability', 'http://www.beazley.com/business_lines/_professional_liability.aspx'],
      ['Management Liability', 'http://www.beazley.com/business_lines/_management_liability.aspx'],
	  ['Environmental Liability', 'http://www.beazley.com/business_lines/_environmental_liability.aspx'],
      ['Programmes', 'http://www.beazley.com/business_lines/_programmes.aspx'],
      ['Specialty Treaty', 'http://www.beazley.com/business_lines/_specialty_treaty.aspx']
    )],

    ['ddPanel_claims', 305, 20, Array(
      ['Notify a claim', 'http://www.beazley.com/claims_service.aspx'], 
      ['Our approach', 'http://www.beazley.com/claims_service/our_approach.aspx'],
      ['Case studies', 'http://www.beazley.com/claims_service/case_studies.aspx']
    )],
    
    ['ddPanel_access', 380, 20, Array(
      ['Lloyd\'s broker information', 'http://www.beazley.com/broker_access/lloyds_broker_information.aspx'],
      ['UK & international brokers', 'http://www.beazley.com/broker_access/uk__international_brokers.aspx'],
      ['US brokers', 'http://www.beazley.com/broker_access/us_brokers.aspx'],
      ['Ratings', 'http://www.beazley.com/broker_access/ratings.aspx'],
      ['Frequently asked questions', 'http://www.beazley.com/broker_access/frequently_asked_questions.aspx']
    )],

    ['ddPanel_invest', 508, 20, Array(
      ['Summary Information', 'http://investor.relations.beazley.com/investorrelations/suminfo/kpi/'],
      ['The Board', 'http://investor.relations.beazley.com/investorrelations/board/'],    
      ['Share Price Information', 'http://investor.relations.beazley.com/investorrelations/shareinfo/shareprice/'],
      ['Regulatory News', 'http://investor.relations.beazley.com/investorrelations/rns/'],
      ['Results and Presentations', 'http://investor.relations.beazley.com/investorrelations/resultspresentations/results/'],
      ['Debt Investors', 'http://investor.relations.beazley.com/investorrelations/debtinvestors/'],
      ['Shareholder Services', 'http://investor.relations.beazley.com/investorrelations/shareholderservices/agm/'],
      ['Financial Calendar', 'http://investor.relations.beazley.com/investorrelations/calendar/'],
      ['Dividend Access Plan', 'http://investor.relations.beazley.com/investorrelations/div_access/'],
      ['Corporate Governance', 'http://investor.relations.beazley.com/investorrelations/corpgov/'],
      ['CSR Policy', 'http://investor.relations.beazley.com/investorrelations/csrpolicy/'],
      ['Contacts', 'http://investor.relations.beazley.com/investorrelations/contact/ircontacts/']
    )]
  ),
  aTags : null,
  activePanel : '',
  
  init : function(arg1, arg2) {
    //collect top nav buttons and itterate through them
    this.aTags = this.collectButtons(this.topNavContainerId);
    for (var i=0; i<this.aTags.length; i++) {
      //init button id
      this.aTags[i].id = 'trigger_'+i;
      if (this.panels[i][0] != '') {
        //add panel to DOM
        this.buildPanel(this.panels[i][0], this.panels[i][3]);
        //set button mouse event handlers
        this.aTags[i].onmouseover = this.mouseOverEvent;
        this.aTags[i].onmouseout = this.mouseOutEvent;
        //set panel mouse event handler
        var panelNode = document.getElementById(this.panels[i][0]);
        if (panelNode != null) { panelNode.onmouseout = this.mouseOutEvent; }
      }
    }
  },
  
  mouseOverEvent : function(e) {
    if (!e) { var e = window.event; }
  	var tg = (window.event) ? e.srcElement : e.target;
    //
    idx = tg.id.split('_',2);
    ddMenu.showPanel(ddMenu.panels[idx[1]], ddMenu.topNavContainerId);
  },

  mouseOutEvent : function(e) {
  	if (!e) { var e = window.event; }
  	var tg = (window.event) ? e.srcElement : e.target;
  	var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;
//alert('moving from '+tg.nodeName+'.'+tg.id+' to '+reltg.nodeName+'.'+reltg.id);
  	if ((reltg.id == ddMenu.activePanel) || (reltg.nodeName == 'LI')) return; //moved to panel (or to LI - dirty workaround) - ignore event
  	var s = '';
  	while ((reltg != tg) && (reltg.nodeName != 'BODY') && (reltg.id != ddMenu.activePanel)) { reltg = reltg.parentNode; s += ' > '+reltg.nodeName+'.'+reltg.id; }
//alert('destination is contained in '+s); //reltg.nodeName+'.'+reltg.id);
  	if ((tg == reltg) || (reltg.id == ddMenu.activePanel)) return; //moved to an element contained by panel - ignore event  
  	//
  	ddMenu.hidePanel();
  },
  
  collectButtons : function(rootId) {
    //capture A tags from navigation buttons (DIV.UL.LI.A) into array
    var o = document.getElementById(rootId);
    var ret = Array();
    var c = 0;
    for (var i=0; i<o.childNodes.length; i++) {
      if (o.childNodes[i].nodeName == 'UL') {
        var ulNode = o.childNodes[i];
        for (var j=0; j<ulNode.childNodes.length; j++) {
          if (ulNode.childNodes[j].nodeName == 'LI') {
            var liNode = ulNode.childNodes[j];
            for (var k=0; k<liNode.childNodes.length; k++) {
              if (liNode.childNodes[k].nodeName == 'A') {
                ret[c++] = liNode.childNodes[k];
              }
            }
          }
        }
        break;
      }
    }
    return ret;
  },
  
  showPanel : function(panelData, posId, shiftX, shiftY) {
    if ((this.activePanel != '') && (this.activePanel != panelData[0])) { this.hidePanel(); }  //hide previous panel, if any
//    var the_style = this.getStyleObject(panelData[0]);
    var anchorNode = document.getElementById(posId);
    var panelNode = document.getElementById(panelData[0]);
    if (panelNode) {
      if (anchorNode) {
        panelNode.style.top = (this.findPosY(anchorNode)+parseInt(panelData[2]))+'px';
        panelNode.style.left = (this.findPosX(anchorNode)+parseInt(panelData[1]))+'px';
      }
      panelNode.style.display = 'inline';
      this.addIFrame(panelNode);
      this.activePanel = panelData[0];
    }
  },

  hidePanel : function() {
    if (this.activePanel == '') return;
    var the_style = this.getStyleObject(this.activePanel);
    if (the_style != false) { the_style.display = 'none'; }
    this.removeIFrame(document.getElementById(this.activePanel));
    this.activePanel = '';
  },
  
  getStyleObject : function(objectId) {
    if (document.getElementById && document.getElementById(objectId)) {
      return document.getElementById(objectId).style;
    } else if (document.all && document.all(objectId)) {
      return document.all(objectId).style;
    } else {
      return false;
    }
  },
  
  findPosX : function(obj) {
    var curleft = 0;
    if(obj.offsetParent)
      while(1) {
        curleft += obj.offsetLeft;
        if(!obj.offsetParent) break;
        obj = obj.offsetParent;
      }
    else if(obj.x)
      curleft += obj.x;
    return curleft;
  },
  
  findPosY : function(obj) {
    var curtop = 0;
    if(obj.offsetParent)
      while(1) {
        curtop += obj.offsetTop;
        if(!obj.offsetParent) break;
        obj = obj.offsetParent;
      }
    else if(obj.y)
      curtop += obj.y;
    return curtop;
  },
  
  addIFrame : function(node) {
    h = node.clientHeight;
    w = node.clientWidth;
    var iframe = document.createElement('iframe');
    //iframe.setAttribute('allowtransparency', 'true');
    //iframe.setAttribute('frameborder', '0');
    with (iframe.style) {
      position = 'absolute';
      height = h + 2 + 'px';
      width = w + 2 + 'px';
      left = node.style.left;
      top = node.style.top;
      border = '0';
      borderWidth = '0';
      borderStyle = 'none';
      backgroundColor = 'transparent';
      filter = 'alpha(Opacity=0)';
      opacity = '0';
    }
    node.parentNode.appendChild(iframe);
  },
  
  removeIFrame : function(node) {
    iframe = node.parentNode.lastChild;
    if (iframe.nodeName == "IFRAME") {
      iframe.parentNode.removeChild(iframe);
    }
  },
  
  buildPanel : function(panelId, panelItems) {
    var htmlContent = '';
    for (var i=0; i<panelItems.length; i++) {
      htmlContent += '<a href="'+panelItems[i][1]+'">'+panelItems[i][0]+'</a>';
    }
    var panelNode = document.createElement('div');
    panelNode.setAttribute('id', panelId);
    panelNode.setAttribute('class', 'quickPanel');
    panelNode.setAttribute('className', 'quickPanel');
    panelNode.innerHTML = '<div id="pContent">'+htmlContent+'</div><div id="pBottom"></div>';
    document.body.appendChild(panelNode);
  }
};
