
var timeToShow = 6000; // milliseconds
var timeToDelay = 10*1000; // milliseconds

document.write("<style>#popupWin{position: absolute; z-index: 9999;right: 5px; bottom: 0px;width: 200px;background: #e0e9f8; overflow:hidden;}</style>");
//document.write("<style>#popupWinClose{width:20px;height:20px;position: absolute; z-index: 1000000;right:0;top:0;cursor:pointer;}</style>");

function f_scrollTop() {
  return f_filterResults (
    window.pageYOffset ? window.pageYOffset : 0,
    document.documentElement ? document.documentElement.scrollTop : 0,
    document.body ? document.body.scrollTop : 0
  );
}

function f_filterResults(n_win, n_docel, n_body) {
  var n_result = n_win ? n_win : 0;
  if (n_docel && (!n_result || (n_result > n_docel))){
    n_result = n_docel;
  }
  return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

function f_clientHeight() {
  return f_filterResults (
    window.innerHeight ? window.innerHeight : 0,
    document.documentElement ? document.documentElement.clientHeight : 0,
    document.body ? document.body.clientHeight : 0
      );
}

var pWh // popup height
, pWAh  // popup actual height
, pWtid=-1 // timer
, pWrt; // reset timer
var pWth=-1 // hide timer
, pWtha=30000; // hide after
var pWShowBy=null
, pWxt=-1, pWB;
var mTO // main timeout
,dTO // delay timeout
, MT = 0; // main interval

var pWOnloadH=window.onload; // onload handler
window.onload=pW_load;

var pWSH=window.onscroll; // onscroll handler
window.onscroll=pW_scroll;


function pW_scroll(){
  if (pWSH!=null) pWSH();
    if (pWth!=-1){
    el=document.getElementById('popupWin');
        el.style.bottom = '';
        el.style.top =  (((f_scrollTop() + f_clientHeight()) - pWh)-0) + 'px';
        el.style.display='none';
        el.style.display='block';
  }
}

function pWShow(show){

  if (pWxt!=-1) { el.filters.blendTrans.stop(); }

    if ((pWth!=-1) && ((show!=null) && (show==pWShowBy))){
    clearInterval(pWth);
        pWth=setInterval(pWClose,pWtha);
    return;
  }
  if (pWtid!=-1) return;
  pWShowBy=show;
    el=document.getElementById('popupWin');
    el.style.left='';
    el.style.top='';
    el.style.filter='';
    el.style.bottom='0px';

    if (pWth!=-1) clearInterval(pWth); pWth=-1;
    //if (navigator.userAgent.indexOf('Opera')!=-1 || navigator.userAgent.indexOf('Firefox')!=-1){
    el.style.bottom=(-f_scrollTop())+'px';
  //}

  pWAh=0; el.style.height=pWAh+'px';
  el.style.visibility='';
  if (!pWrt){
    el.style.display='';
  }

  pWtid=setInterval(pWTT,(pWrt?1000:20));
}

  /* close btn
  cb = document.createElement('DIV');
  cb.className = 'popupWinClose';
  cb.id = 'popupWinClose';
  cb.onclick = function(){pWHide();};
  cb.onmouseover=function(){stopTimer();};
    document.getElementById('popupWin').appendChild(cb);
  */
function pW_load(){

  if (pWOnloadH!=null){ pWOnloadH();};
  el=document.getElementById('popupWin');
  el.onmouseover=function(){stopTimer();};
  el.onmouseout=function(){hidePopup();};
  pWB=el.style.bottom.substr(0,el.style.bottom.length-2);

  pWh=el.style.height;
  pWh=pWh.substr(0,pWh.length-2); pWAh=0;
  if (true){
    pWrt=true;
    pWShow(null);
  }
}

function pWTT(){
  el=document.getElementById('popupWin');

  if (pWrt){
    el.style.display='';
    clearInterval(pWtid); pWrt=false;
    pWtid=setInterval(pWTT,20);
  }

  pWAh+=4;
  if (pWAh>=pWh){
    pWAh=pWh; clearInterval(pWtid); pWtid=-1;
    if (pWtha!=-1) pWth=setInterval(pWClose,pWtha);
  }


  el.style.height=pWAh+'px';
  el.style.bottom=(-f_scrollTop())+'px';
  if(pWh == pWAh){
    startTimer();
  }
}

function pWTT2(){
  el=document.getElementById('popupWin');
  pWAh-=5;
  el.style.height=pWAh+'px';
  el.style.bottom=(+f_scrollTop())+'px';
  if(pWAh <= 5){
    clearInterval(pWtid);
    pWtid = -1;
    pWClose();
  }
}

function pWHide(hide){

    if ((pWth!=-1) && ((hide!=null) && (hide==pWShowBy))){
    clearInterval(pWth);
        pWth=setInterval(pWClose,pWtha);
    return;
  }
  if (pWtid!=-1) return;
  pWShowBy=hide;
    el=document.getElementById('popupWin');
    el.style.left='';
    el.style.top='';
    el.style.filter='';
    el.style.bottom='0px';
    if (pWth!=-1) clearInterval(pWth); pWth=-1;
    //if (navigator.userAgent.indexOf('Opera')!=-1 || navigator.userAgent.indexOf('Firefox')!=-1){
    el.style.bottom=(+f_scrollTop())+'px';
  //}
  pWAh=pWh; el.style.height=pWAh+'px';
  el.style.visibility='';
  if (!pWrt){
    el.style.display='';
  }

  pWtid=setInterval(pWTT2,(pWrt?1000:20));
}

function startTimer(){
  mTO = setTimeout("pWHide()",timeToShow);
}

function stopTimer(){
  clearTimeout(mTO);
  timeToShow -= MT;
}

function hidePopup(){
  setTimeout('pWHide()',500);
}

function popupWinespopup_dxTimer(){
  clearInterval(pWxt); pWxt=-1;
}

function startDelay(){
  dTO = setTimeout("pWShow()",timeToDelay);
}

var shownIdx = 0;
function pWClose(){
  if (pWtid==-1){
    clearTimeout(dTO);
    el=document.getElementById('popupWin');
    el.style.display='none';
    if (pWth!=-1) clearInterval(pWth); pWth=-1;
    startDelay();
    shownIdx++;
  }
}

