//Global var
Params = new Array(new Array(), new Array());
MenuIds = new Array();

//default PullDownMenu configuration
pdm_defMainMenuLeftOffset = 197+'px';
pdm_defBottomMenuLeftOffset = 570+'px';
pdm_defMainMenuTopOffset = 137+'px';
pdm_defMainMenuWidth = 120+'px';
pdm_defBottomMenuWidth = 90+'px';
pdm_defOptionHeight = 21;// (without 'px') to make height for FF
pdm_defMainMenuOptionsHeight = pdm_defOptionHeight+'px'
pdm_defBottomMenuOptionsHeight = 14+'px';
pdm_defMainMenuHolderName = 'MainMenuHolder';
pdm_defBottomMenuHolderName = 'BottomMenuHolder';
pdm_defMainMenuFontColor = '#EEEEEE';
pdm_defBottomMenuFontColor = '#EEEEEE';
pdm_defMainMenuFontSize = 11+'px';
pdm_defBottomMenuFontSize = 10+'px';
pdm_defMainMenuFontWeight = 'bold';
pdm_defBottomMenuFontWeight = 'normal';
pdm_defMainMenuBackgroundColor = '#999999';
pdm_defBottomMenuBackgroundColor = '#999999';
pdm_defOptions = new Array(new Array(),new Array()); // used when user data for MenuObject are wrong
pdm_defOptions[0][0] = 'Domyślna opcja1';
pdm_defOptions[1][0] = '';


//set pulldownmenu data
Params = PrepareParams('Skrzynki pocztowe,post_box,Nadzór budowlany,build_control,Projektowanie,designing');
offer = new MenuObject(Params,197,500,137,120,100);

/*
Params = PrepareParams('option1,par1,option2,par2,option3,par3,option4,par4,option5,par5,option6,par6');
personnel = new MenuObject(Params,316,545,137,120,75);
*/

MenuIds['offer'] = offer;
//MenuIds['personnel'] = personnel;

//functions
function PrepareParams(Value)
{
  aPar = new Array(new Array(), new Array());
  aTmp = new Array();
  aTmp = Value.split(',');

  counter1 = 0;
  counter2 = 0;
  for (i=0;i<aTmp.length;i++)
  {
    aPar[counter2][counter1] = aTmp[i];

    counter2++;
    if (counter2 == 2)
    {
      counter1++;
      counter2 = 0;
    }
  }
  return aPar;
}

function MenuObject(Options,MainMenuLeftOffset,BottomMenuLeftOffset,TopOffset,MainWidth,BottomWidth)
{
  useDef = false;
  if (typeof(BottomWidth) == 'undefined')
  {
    alert('Nieprawidłowe parametry pull down menu, zastosowano ustawienia domyślne.');
    useDef = true;
  }

  this.options = useDef ? pdm_defOptions : Options;
  this.options_count = useDef ? pdm_defOptions[0].length : Options[0].length;
  this.main_left = useDef ? pdm_defMainMenuLeftOffset : MainMenuLeftOffset+'px';
  this.bottom_left = useDef ? pdm_defBottomMenuLeftOffset : BottomMenuLeftOffset+'px';
  this.top = useDef ? pdm_defMainMenuTopOffset : TopOffset+'px';
  this.main_width = useDef ? pdm_defMainMenuWidth : MainWidth+'px';
  this.bottom_width = useDef ? pdm_defBottomMenuWidth : BottomWidth+'px';
  this.height = (this.options_count * pdm_defOptionHeight)+this.options_count+'px' ;
}

function HideMenu(e)
{
  e = typeof(e) == 'undefined' ? event : e;
  e_id = document.all ? e.toElement.id.substring(0,6) : e.relatedTarget.id.substring(0,6);
  //alert(e_id);
  if (e_id != 'option')
    document.getElementById('PullDownMenuHolder').style.visibility = 'hidden';
}

function ShowMenu(event,menu_id)
{
  Hover(event);
  HideMenu(event);

  if (document.all)
  {
    if (typeof(MenuIds[event.srcElement.id]) != 'undefined')
    {
      if (event.srcElement.parentNode.parentNode.id == pdm_defMainMenuHolderName)
        MainPullDownMenuData(MenuIds[event.srcElement.id]);
      else if (event.srcElement.parentNode.parentNode.id == pdm_defBottomMenuHolderName)
        FooterPullDownMenuData(MenuIds[event.srcElement.id]);
        
      BuildPullDownMenu(MenuIds[event.srcElement.id],event);
      document.getElementById('PullDownMenuHolder').style.visibility = 'visible';
    }
  }
  else
  {
    if (typeof(MenuIds[event.target.id]) != 'undefined')
    {
      if (event.target.parentNode.parentNode.id == pdm_defMainMenuHolderName)
        MainPullDownMenuData(MenuIds[event.target.id]);
      else if (event.target.parentNode.parentNode.id == pdm_defBottomMenuHolderName)
        FooterPullDownMenuData(MenuIds[event.target.id]);
        
      BuildPullDownMenu(MenuIds[event.target.id],event);
      document.getElementById('PullDownMenuHolder').style.visibility = 'visible';
    }
  }
}

function BuildPullDownMenu(menu_id,e)
{
  // remove old options
  OptCount = document.getElementById('PullDownMenuHolder').getElementsByTagName('a').length;
  for (i=0;i<OptCount;i++)
  {
    ToRemove = document.getElementById('option'+i);
    ToRemove.parentNode.removeChild(ToRemove);
  }
  
  //add new options
  e = typeof(e) == 'undefined' ? event : e;
  e_id = document.all ? e.srcElement.parentNode.parentNode.id : e.target.parentNode.parentNode.id;
  Parent = document.getElementById('PullDownMenuHolder');
  for (i=0;i<menu_id.options_count;i++)
  {
    NewElem = document.createElement('a');
    NewElem.innerHTML = menu_id.options[0][i];
    NewElem.href = 'index.php?Option='+menu_id.options[1][i];
    NewElem.id = 'option'+i;
    NewElem.className = 'option';
    NewElem.name = 'option';
    NewElem.onmouseover =  function(event) { Hover(event) };
    NewElem.onmouseout = function(event) { Active(event) };
    if (e_id == pdm_defMainMenuHolderName)
    {
      NewElem.style.lineHeight = pdm_defMainMenuOptionsHeight;
      NewElem.style.color = pdm_defMainMenuFontColor
    }
    else if (e_id == pdm_defBottomMenuHolderName)
    {
      NewElem.style.lineHeight = pdm_defBottomMenuOptionsHeight;
      NewElem.style.color = pdm_defBottomMenuFontColor
    }

    Parent.appendChild(NewElem);
  }
}

function MainPullDownMenuData(menu_id)
{
  document.getElementById('PullDownMenuHolder').style.left = menu_id.main_left;
  document.getElementById('PullDownMenuHolder').style.top = menu_id.top;
  document.getElementById('PullDownMenuHolder').style.width = menu_id.main_width;
  document.getElementById('PullDownMenuHolder').style.height = menu_id.height;

  document.getElementById('PullDownMenuHolder').style.fontSize = pdm_defMainMenuFontSize;
  document.getElementById('PullDownMenuHolder').style.fontWeight = pdm_defMainMenuFontWeight;
  document.getElementById('PullDownMenuHolder').style.backgroundColor = pdm_defMainMenuBackgroundColor;
}

function FooterPullDownMenuData(menu_id)
{
  document.getElementById('PullDownMenuHolder').style.left = menu_id.bottom_left;
  document.getElementById('PullDownMenuHolder').style.top = '';
  document.getElementById('PullDownMenuHolder').style.bottom = '0px';
  document.getElementById('PullDownMenuHolder').style.width = menu_id.bottom_width;
  document.getElementById('PullDownMenuHolder').style.height = 'auto';

  document.getElementById('PullDownMenuHolder').style.fontSize = pdm_defBottomMenuFontSize;
  document.getElementById('PullDownMenuHolder').style.fontWeight = pdm_defBottomMenuFontWeight;
  document.getElementById('PullDownMenuHolder').style.backgroundColor = pdm_defBottomMenuBackgroundColor;
}

function Hover(e)
{
  e = typeof(e) == 'undefined' ? event : e;
  if (e)
  {
    if (document.all)
      e.srcElement.className += ' hover';
    else
      e.target.className += ' hover';
  }
}

function Active(e)
{
  e = typeof(e) == 'undefined' ? event : e;
  if (e)
  {
    if (document.all)
      e.srcElement.className = e.srcElement.className.replace('hover', '');
    else
      e.target.className = e.target.className.replace('hover', '');
  }
}
