/* START SYSTEM INIT. DON'T TOUCH THIS! */


var target_link = new Array();
var target_link_probability = new Array();
var good_location = new Array();
var open_probability = 100;
var popunder_handle = 'POPUNDER';
var popunder_props = '';
var allow_repeated_visit = false;
var cookie_life = 0;
/* END SYSTEM INIT. DON'T TOUCH THIS! */


/* START POPUNDER CONTROL */

// integer value from 0 to 100
open_probability = 60;

// popunder window handle name, it used for cookie name
window_handle = 'POPUNDER';

// popunde window properties
//popunder_props = 'width=400,height=400,location=0,resize=0';
popunder_props = 'width=underWidth,height=underHeight,screenX=underLeft,screenY=underTop,top=underTop,left=underLeft,location=yes,menubar=yes,resizable=yes,scrollbars=yes,status=yes,toolbar=yes';
//popunder_props = '';

// allow user to make repeated visit
allow_repeated_visit = false;

// number of days for cookie life, 0 for expires at end of session
cookie_life = 1;

// target links
target_link[0] = 'http://medprom.ru/medprom';
target_link[1] = 'http://medprom.ru/medprom/mpr_pmed_spec/mp_prubricator_t';
target_link[2] = 'http://medprom.ru/medprom/mpr_pmethod/mp_prubricator_t';
target_link[3] = 'http://medprom.ru/medprom/mpr_general/mp_prubricator_t';
target_link[4] = 'http://medprom.ru/medprom/mpr_med_spec/mp_rubricator_t';
target_link[5] = 'http://medprom.ru/medprom/mpr_method/mp_rubricator_t';
target_link[6] = 'http://medprom.ru/medprom/medprom/mp_psearch_t';
target_link[7] = 'http://medprom.ru/medprom/mpr_class_books';
target_link[8] = 'http://medprom.ru/medprom/mp_txt_analit';


// opening target link probabilities
target_link_probability[0] = 50;
target_link_probability[1] = 3;
target_link_probability[2] = 3;
target_link_probability[3] = 3;
target_link_probability[4] = 1;
target_link_probability[5] = 1;
target_link_probability[6] = 5;
target_link_probability[7] = 5;
target_link_probability[8] = 5;

// good location patterns
good_location[0] = 'books';
good_location[1] = 'analis';
good_location[2] = 'articles';
good_location[3] = 'encik';
good_location[4] = 'speclit'; 
good_location[5] = 'equip';
good_location[6] = 'bme';
good_location[7] = 'sprav';
good_location[7] = 'med-lib.ru/index';

/* END POPUNDER CONTROL */

function getRandom(min,max)
{
   return (Math.round(Math.random()*(max-min)))+min;
}

function good_moment() {
  var curr_probability = getRandom(0,100)
  return curr_probability <= open_probability? true: false;
}

function check_location() {
  var curr_location = window.location.href;
  for(var i = 0; i < good_location.length; i++) {
    if(curr_location.indexOf(good_location[i]) != -1) {
      return true;
    }
  }
}

function get_target_link() {
  var links_set = new Array();
  for(var i = 0; i < target_link.length; i++) {
    if(target_link[i] && typeof(target_link_probability[i]) != 'undefined' && target_link_probability[i] > 0) {
      var links_set_length = links_set.length;
      for(var j = links_set_length; j < links_set_length + target_link_probability[i]; j++) {
        links_set[links_set.length] = target_link[i];
      }
    }
  }
  return links_set[getRandom(0,links_set.length-1)];
}

function replace(string,text,by) {
// Replaces text with by in string
  var strLength = string.length, txtLength = text.length;
  if ((strLength == 0) || (txtLength == 0)) return string;
  var i = string.indexOf(text);
  if ((!i) && (text != string.substring(0,txtLength))) return string;
  if (i == -1) return string;
  var newstr = string.substring(0,i) + by;
  if (i+txtLength < strLength)
      newstr += replace(string.substring(i+txtLength,strLength),text,by);
  return newstr;
}

function open_popunder(link) {
 if(popunder_props.indexOf('underWidth') ||
    popunder_props.indexOf('underHeight') ||
    popunder_props.indexOf('underTop') ||
    popunder_props.indexOf('underLeft')) {
    var underWidth = '', underHeight = '', underTop='', underLeft=''; // default sizes
    if (document.all)
      underHeight = document.body.offsetHeight-50, underWidth = document.body.offsetWidth-20;
    else if (document.layers)
      underHeight = window.innerHeight, underWidth = window.innerWidth-16;
    if (document.all) {
      underTop  = window.screenTop-80;
      underLeft = window.screenLeft;
    }
    else if (document.layers) {
      underTop  = window.screenY;
      underLeft = window.screenX;
    }
    popunder_props=replace(popunder_props, 'underWidth', underWidth);
    popunder_props=replace(popunder_props, 'underHeight', underHeight);
    popunder_props=replace(popunder_props, 'underLeft', underLeft);
    popunder_props=replace(popunder_props, 'underTop', underTop);
    //alert('   underWidth='+underWidth+'   underHeight='+underHeight+'   underLeft='+underLeft+'   underTop='+underTop);
    //alert(popunder_props);
  }
 popunder_window=window.open(link,popunder_handle,popunder_props);
 window.focus();
}

function getCookie(name) {
  var cookies = document.cookie;
  var start = cookies.indexOf(name + '=');
  if (start == -1) return null;
  var len = start + name.length + 1;
  var end = cookies.indexOf(';',len);
  if (end == -1) end = cookies.length;
  return unescape(cookies.substring(len,end));
}

function setCookie(name, value, expires, path, domain, secure) {
        document.cookie = name + "=" + escape(value) +
        ((expires == null) ? "" : "; expires=" + expires.toGMTString()) +
        ((path == null) ? "" : "; path=" + path) +
        ((domain == null) ? "" : "; domain=" + domain) +
        ((secure == null) ? "" : "; secure");
}

function deleteCookie(name, path, domain) {
  var expires = ';expires=Thu, 01-Jan-70 00:00:01 GMT';
  (path)    ? ';path='    + path                  : '';
  (domain)  ? ';domain='  + domain                : '';

  if (getCookie(name))
    document.cookie = name + '=' + expires + path + domain;
}

function isCookieEnabled() {
  if (document.all) {
    if (!navigator.cookieEnabled) {
      return false;
    }
    else return true;
  }
  else {
    setCookie('temp','temp');
    var temp = getCookie('temp');
    if (!temp) {
      return false;
    }
    else return true;
  }
}

var repeated;

if(good_moment() && check_location()) { 
if(!allow_repeated_visit) {
  repeated = getCookie(popunder_handle);
  if(!repeated) {
    var expiration;
    if(cookie_life > 0) {
      expiration = new Date();
      var expiration_days = expiration.getTime() + (cookie_life * 24 * 60 * 60 * 1000);
      expiration.setTime(expiration_days);
    }
    setCookie(popunder_handle, 1, expiration);
  }
}  

// alert(repeated+'/'+allow_repeated_visit+'/'+expiration);

if( allow_repeated_visit || !repeated) {open_popunder(get_target_link());}

}