advert_exec_path = "widgets/advert/php/GetAdvertContent.php";
advert_top_margin_percent = 5;
separator = ",";


function Advert(AdvertBoxId_ContainerId)
{
  //AdvertBoxId_ContainerId is:
  //  new_adver_box_name,container_name

  AdvertBoxId_ContainerId = typeof(AdvertBoxId_ContainerId) == "undefined" ? "advert,bckg":AdvertBoxId_ContainerId;

  if (!Boolean(PrepareAdvert(AdvertBoxId_ContainerId)))
    return;
    
  //alert('PreparedAdvert');
  //alert(GetBrowserWindowHeight());
}

function PrepareAdvert(AdvertBoxId_ContainerId)
{
  Params = GetParams(AdvertBoxId_ContainerId);

  ParamsCount = Params.length;

  var i=0;
  while(i<ParamsCount)
  {
    if (typeof(Params[i+1]) == "undefined") break;
    
    if (!Boolean(document.getElementById(Params[i+1])))
      i++;
    else
    {
      ContainerId = Params[i];
      ParentContainerId = Params[i+1];
      BrowserWindowHeight = GetBrowserWindowHeight();
      ContainerHeight = GetContainerHeight(ParentContainerId);
      
      ParentContainerHeight = ContainerHeight>BrowserWindowHeight?BrowserWindowHeight:ContainerHeight;
      ParentContainerWidth = GetContainerWidth(ParentContainerId);

      if (!Boolean(makeXHRequest(advert_exec_path+"?name="+ContainerId+"&parent_width="+ParentContainerWidth+"&parent_height="+ParentContainerHeight,setInnerHTML,ContainerId,ParentContainerId)))
        i++;
      else
        i+=2;
    }
  }
  


  //alert('123');
  return true;
}

function GetParams(AdvertBoxId_ContainerId)
{
  return AdvertBoxId_ContainerId.split(separator);
}

function GetContainerHeight(ContainerId)
{
  return document.getElementById(ContainerId).offsetHeight;
}

function GetContainerWidth(ContainerId)
{
  return document.getElementById(ContainerId).offsetWidth;
}

function GetBrowserWindowHeight()
{
  if (self.innerHeight)         // all except Explorer
    return self.innerHeight;
  else if (document.documentElement && document.documentElement.clientHeight)  // Explorer 6 Strict Mode
    return document.documentElement.clientHeight;
  else if (document.body) // other Explorers
    return document.body.clientHeight;
}


function setInnerHTML(Text,ContainerId,ParentContainerId)
{

  if (Text.length == 0) return false;
//alert(1);
  destination = CreateAdvertBox(ContainerId,ParentContainerId);
  if (!Boolean(destination)) return false;

  destination.innerHTML = Text;
  
  Parent = document.getElementById(ParentContainerId);

  //adv_pos_horizontal = (destination.offsetWidth/destination.offsetHeight) >= 1?true:false;
  //if (adv_pos_horizontal)
  {
    margin_top = Math.round((advert_top_margin_percent*destination.offsetHeight)/100);
    margin_left = Math.round((Parent.offsetWidth/2) - (destination.offsetWidth/2));
  }
  //else
  {
  }
  
  destination.style.margin = margin_top+'px auto 0 '+margin_left+'px';
  destination.style.textAlign = 'center';

  // to prevent flashing efect
  setTimeout('destination.style.visibility = "visible";',150);
}


function CreateAdvertBox(ContainerId,ParentContainerId)
{
  if (Boolean(document.getElementById(ContainerId)))
    return document.getElementById(ContainerId);

  if (!Boolean(document.getElementById(ParentContainerId)))
      return false;
  //alert('asd')
  Parent = document.getElementById(ParentContainerId);
  try
  {
    NewElem = document.createElement('div');
    NewElem.id = ContainerId;
    //NewElem.innerHTML = menu_id.options[0][i];
    //NewElem.href = 'index.php?Option='+menu_id.options[1][i];
    NewElem.className = 'advert';

//    NewElem.style.height = Parent.offsetHeight;
//    NewElem.style.width = Parent.offsetWidth;
    //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);

    return NewElem;
    //return document.getElementById(ContainerId);

  }
  catch (e) {}

}


