
var PopUp_width = 0;
var PopUp_height = 0;

function PopUp_showWaiting()
{
  PopUp_element2center('waitdiv', 128, 15);
  $("#waitdiv").show();
}

function PopUp_hideWaiting()
{
  $("#waitdiv").hide();
}

// генерация
function PopUp_getWindowCode(topText, text)
{
  code = "<table cellpadding='0' cellspacing='0' border='0' id='popup_div'>";
  
  code += "<tr height='9' valign='bottom'>";
  code += "<td width='9'><img src='"+IMAGES_URL+"popup_topleft.png' align='absmiddle'></td>";
  code += "<td class='popup_whiteCell'>&nbsp;</td>";
  code += "<td width='9'><img src='"+IMAGES_URL+"popup_topright.png' align='absmiddle'></td>";
  code += "</tr>";
  
  code += "<tr>";
  code += "<td class='popup_whiteCell'>&nbsp;</td>";
  code += "<td class='popup_whiteCell'>";
  

  code += "<table width='100%' height='100%' cellpadding='0' cellspacing='0' border='0'>";

  code += "<tr height='29'>";

  code += "<td>";
  
  code += "<table width='100%' height='100%' cellpadding='0' cellspacing='0' border='0'>";
  code += "<tr height='29'>";
  
  code += "<td align='left' id='poput_toptext'>"+topText+"</td>";
  
  code += "<td align='right'>";
  code += "<a href='#' onclick=\"PopUp_CLOSE();return false;\">";
  code += "<img src='"+IMAGES_URL+"popup_closebtn.png' align='right' border='0'>";
  code += "</a>";

  code += "</td>";
  code += "</tr>";
  
  code += "</table>";
  
  code += "</td>";
  code += "</tr>";

  code += "<tr>";
  code += "<td><div id='popup_iframediv'>";
  code += text;
  code += "</div></td>";
  code += "</tr>";
  
  code += "</table>";
  
  code += "</td>";
  code += "<td class='popup_whiteCell'>&nbsp;</td>";
  code += "</tr>";

  code += "<tr height='9' valign='top'>";
  code += "<td width='9'><img src='"+IMAGES_URL+"popup_btmleft.png' align='absmiddle'></td>";
  code += "<td class='popup_whiteCell'>&nbsp;</td>";
  code += "<td width='9'><img src='"+IMAGES_URL+"popup_btmright.png' align='absmiddle'></td>";
  code += "</tr>";
  code += "</table>";
  
  return code;
}

function PopUp_show(params)
{
  PopUp_showBB();
  PopUp_showWaiting();

  try{ delete popup_frame; }
  catch(e){ ; }  

  code = PopUp_getWindowCode(
      '', 
      "<iframe width='1000' onload='PopUp_load(this)' name='popup_frame' id='popup_frame_id' src=\""+params.url+"\" frameborder='0' width='100%' height='100%' scrolling='auto'></iframe>"
  );
  
  document.getElementById('addcodediv').innerHTML += code;
  
  $('#addcodediv').show();
  $('#popup_div').css('visibility', 'hidden');
  $('#popup_div').show();
}

function showAlert(topText, btext)
{
  PopUp_showBB();
  PopUp_showWaiting();

  try{ delete popup_frame; }
  catch(e){ ; }  

  if(topText == '')
    topText = "Информация";
  
  text  = "<div id='popup_alertBody'>";
  text += "<div>"+btext+"<div>";
  text += "</div>";
  
  code = PopUp_getWindowCode(topText, text);
  
  document.getElementById('addcodediv').innerHTML += code;
  
  $('#addcodediv').show();
  $('#popup_div').show();
  
  PopUp_hideWaiting();
  
  // Размеры текста в алерте
  height = $('#popup_alertBody').height() + 5;
  width = $('#popup_alertBody').width();
  
  $("#popup_iframediv").height(height);
  $("#popup_iframediv").width(width);
  PopUp_element2center('popup_div', width, height);
  $('#popup_div').css('visibility', 'visible');
}

// показать бг
function PopUp_showBB()
{
  objBB = document.getElementById('blackbackdiv');
  objBB.style.opacity = 7/10;
  objBB.style.filter = 'alpha(opacity=' + 7*10 + ')';
  objBB.style.width = '100%';
  objBB.style.height = $(document).height()+'px';

  w1 = $(document.body).width();
  $(document.body).css('overflow', 'hidden');
  
  razn = PopUp_getClientWidth() - w1;
  if(razn <= 0) razn = 17;
  
  $(document.body).css('margin-right', razn + 'px');
  objBB.style.display = 'inline';
}

// скрыть бг
function PopUp_hideBB()
{
  objBB = document.getElementById('blackbackdiv');
  objBB.style.display = 'none';
  objBB.style.opacity = 0/10;
  objBB.style.filter = 'alpha(opacity=' + 0*10 + ')';
  
  $(document.body).css('margin-right', '0px');
  $(document.body).css('overflow', 'auto');
}

// центрирование элемента
function PopUp_element2center(elementId, width, height)
{
  if(elementObj = document.getElementById(elementId))
  {
    if(PopUp_getShowedClientHeight() < height)
      height = PopUp_getShowedClientHeight();
    
    elementObj.style.width  = width;
    elementObj.style.height = height;
    elementObj.style.left = Math.round((PopUp_getClientWidth() - width) / 2)+'px';
    elementObj.style.top = $(document).scrollTop() + Math.round((PopUp_getShowedClientHeight() - height) / 2)+'px';
  }
}

// Ширина клиента
function PopUp_getClientWidth()
{
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientWidth:document.body.clientWidth;
}

// ширина видимой части клиента
function PopUp_getShowedClientHeight()
{
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientHeight:document.body.clientHeight;
}

// Показать окно
function PopUp_GEN(params)
{
  PopUp_show(params);
}

function getElementComputedStyle(elem, prop)
{
  if (typeof elem!="object") elem = document.getElementById(elem);  
  if (document.defaultView && document.defaultView.getComputedStyle)
  {
    if (prop.match(/[A-Z]/)) prop = prop.replace(/([A-Z])/g, "-$1").toLowerCase();
    return document.defaultView.getComputedStyle(elem, "").getPropertyValue(prop);
  }  
  if (elem.currentStyle)
  {
    var i;
    while ((i=prop.indexOf("-"))!=-1) prop = prop.substr(0, i) + prop.substr(i+1,1).toUpperCase() + prop.substr(i+2);
    return elem.currentStyle[prop];
  }
  
  return "";
}

// Получить минимальные размеры документа во фрейме
function PopUp_getFrameDocSize()
{
  winWidth = 0;
  winHeight = 0;
  
  if(pbObj = popup_frame.document.getElementById('popupbody'))
  {
    winWidth = pbObj.offsetWidth;
    winHeight = pbObj.offsetHeight;
  }
  
  return {width: parseInt(winWidth), height: parseInt(winHeight)};
}

// Обработчик события загрузки документа во фрэйм popup_iframe
function PopUp_load(ifr, width, height)
{
  // Получаем оазмеры документа во фрейме
  size = PopUp_getFrameDocSize();
  height = size.height;
  width = size.width;
  
  if(popup_frame.document.title && popup_frame.document.title != '')
    $("#poput_toptext").html(popup_frame.document.title);
  
  PopUp_hideWaiting();
  $("#popup_iframediv").height(height);
  $("#popup_iframediv").width(width);
  PopUp_element2center('popup_div', width, height);
  $('#popup_div').css('visibility', 'visible');
}

// Закрытие попапа
function PopUp_CLOSE(params)
{
  $('#popup_frame_id').remove();
  $('#popup_div').remove();
  $('#popup_div').hide();
  $('#addcodediv').hide();
  PopUp_hideBB();
}
