var winX=0 ;
var winY=0;
var winWidth=380;
var winHeight=120;
var  winOffsetX=-10;
var  winOffsetY=10;

function markClick(e) {
  winX = e.screenX;
  winY = e.screenY;
  if (isN4 || isN6) document.routeEvent(e);
  else  e.cancelBubble = false;
  return true;
}

function clearCMframe() {
  var docFrames = document.frames ? document.frames : window.frames;
  if (docFrames.frames) {
    var fr = docFrames.frames["wfFrame"];
    if (fr) fr.location.href = "about:blank";
  }
}

function wfclose(hideOnly) {
  var el = document.getElementById("wfLayer");
  if (el) el.style.display = "none";
  var fr = document.getElementById("wfFrame");
  if (fr) fr.style.height = "120px";
  if (!hideOnly) clearCMframe();
}

function sleep(numberMillis) {
  var now = new Date();
  var exitTime = now.getTime() + numberMillis;
  while (true) {
    now = new Date();
    if (now.getTime() > exitTime) return;
  }
}

function getX(e) {
 return (isIE || isOpera6) ? e.x+document.body.scrollLeft:e.pageX //-335 ;
}

function getY(e) {
 return (isIE || isOpera6) ? e.y+document.body.scrollTop:e.pageY //-35 ;
}

function showWFList(e, url) {
  if (!url) return true;

  var baseUrl = document.getElementsByTagName("base")[0].href;
  if (!baseUrl) baseUrl = "";
  markClick(e);
  var el = document.getElementById("wfLayer");
  var target = "_blank";

  if (el) {
    var fr = window.frames ? window.frames["wfFrame"] : document.frames["wfFrame"];
    if (fr) {
      fr.location.href = baseUrl + url;
      calWin = fr;
    }
    el.style.left = getX(e) + winOffsetX;
    el.style.top = getY(e) + winOffsetY;
    el.style.width = "250px";
    el.style.display = "";
  }
  else {
    if (isIE || isOpera6) { // Internet Explorer
      calWin = window.open(baseUrl + url, target, "title='Список работ',menubar=no,location=no,channelmode=no,toolbar=no,width=" + winWidth + ",height=" + winHeight + ",left=" + (winX + winOffsetX) + ",top=" + (winY + winOffsetY));
    }
    else {
      calWin = window.open(baseUrl + url, target, "toolbar=0,width=" + winWidth + ",height=" + winHeight + ",screenx=" + (winX + winOffsetX) + ",screeny=" + (winY + winOffsetY));
    }
  }
  if (e.stopPropagation) e.stopPropagation();
  e.cancelBubble = true;
  e.returnValue = false;
  return false;
}

function checkAndShowWFList(e) {
  e = window.event || e;
  var src = window.event ? e.srcElement : e.target;
  if (!src) return true;// bioform added

  var url = null;
  //    var objTarget = null;
  try {// bioform added
    url = src.getAttribute("wfUrl");
    //	objTarget = src.getAttribute("wfTarget");
  } catch(e) {
    return true;
  }
  // if(!url || !objTarget) return true;
  if (!url && src.tagName == "BUTTON") {
    src = getFirstChildTag(src);
    try {
      url = src.getAttribute("wfUrl");
    } catch(e) {
      return true;
    }
  }

  return showWFList(e, url);
}
// Check attribute 'showWFOnLeftMB' on clicked element.
// If this attribute exists and have value is true when return true.
function showWFOnLeftMouseButton(e) {
  e = window.event || e;
  var src = window.event ? e.srcElement : e.target;
  if (!src) return false;
  var url = null;
  try {
    url = src.getAttribute("showWFOnLeftMB");
  } catch(e) {
    return false;
  }
  if (!url && src.tagName == "BUTTON") {
    src = getFirstChildTag(src);
    try {
      url = src.getAttribute("showWFOnLeftMB");
    } catch(e) {
      return false;
    }
  }
  return url == "true";
}

function initWF(e) {
  var ev = isIE ? event.button : e.which;
  // do by second or third mouse button only
  // or element have true value of showWFOnLeftMB attribute
  if (ev == 2 || ev == 3 || showWFOnLeftMouseButton(e)) {
    var defaultContextMenu = checkAndShowWFList(e);
    document.oncontextmenu = new Function(defaultContextMenu ? "return true" : "return false");
    return false;
  } else {
    wfclose();
  }
  return true;
}

if (isN4) {
  document.captureEvents(Event.MOUSEDOWN);
  document.onmousedown = initWF;
} else {
  document.onmouseup = initWF;
  document.oncontextmenu = new Function("return false");
}

//Инициализация слоя
if(!document.getElementById("wfLayer")){
  document.write('<div id="wfLayer" style="position:absolute;display:none;width:250px;height:120px;"><table cellspacing=0 cellpadding=0><tr><td class="ramka" style="padding-top:3px;">');
  document.write('<iframe style="width:100%;height:120px;" id="wfFrame" name="wfFrame" scrolling=no frameborder=0></iframe>');
  document.write('<table width="100%" bgcolor="white"><tr><td class="switchLine">&nbsp;</td></tr><tr class="out" onmouseover="this.className=\'over\'" onmouseout="this.className=\'out\'"><td style="padding-left:10px;" onclick="javascript:wfclose();">Закрыть</td></tr></table>');
  document.write('</td><td height=100%><table cellpadding=0 cellspacing=0 height=100%><tr><td style="font-size:4px;">&nbsp;</td></tr><tr><td height=100% style="width:3px; background-repeat:repeat-y;" background="admin/images/win_bg.gif" valign=top>&nbsp;</td></tr></table></td></tr>');
  document.write('<tr><td><table width="100%" cellspacing=0 cellpadding=0><tr><td style="font-size:4px;">&nbsp;</td><td style="width:98%; font-size:3px; background-repeat:repeat-x;" background="admin/images/win_vbg.gif" valign=top>&nbsp;</td></tr></table></td><td valign=top><img src="admin/images/ugol_bg.gif" width=3 height=3></td></tr></table></div>');
}

