// Author: Dmitry Bystrov http://bikosite.net
// Date: 17.11.2009
// Location: Zaporizhzhya, Ukraine, Eastern Europe

function generatePopup(width, height) {
  var A;
  if (self.innerWidth!=undefined) A = [self.innerWidth,self.innerHeight];
  else{
    var D = document.documentElement;
    if (D) A = [D.clientWidth,D.clientHeight];
  }
  var left = Math.round((A[0] - width)/2);
  var top = Math.round((A[1] - height)/2);
  
  document.write('<div id="popup_window" style="position:absolute;width:' + width + 'px;height:' + height + 'px; z-index:1;left:'+left+'px;top:'+top+'px"></div>');
  
  var show_popup = false;
  
  if (!popup_disabled) {
    var popup_visit = getCookie('popup_visit');
    var popup_subscribed = 0;
    var popup_last_time = 0;
    var popup_visit_count = 1;
    
    var time = new Date();
    var now = time.getTime();
    time.setTime(now+10*365*24*60*60*1000); // 10 year after now
    now = Math.round(now/1000);
    
    if (!popup_visit) {
      show_popup = true;
    }
    else {
      popup_visit = popup_visit.split('+');
      if (popup_visit.length==3) {
        popup_subscribed = parseInt(popup_visit[0]);
        popup_last_time = parseInt(popup_visit[1]);
        popup_visit_count = parseInt(popup_visit[2]);
        
        if (popup_visit_count < 1) popup_visit_count = 1;
        else popup_visit_count++;
        
        if (!popup_subscribed) {
          var past_days = now - popup_last_time;
          if (past_days > 0) {
            past_days = past_days/(24*60*60);
            //alert(past_days); //debug
            if (past_days > popup_expire) show_popup = true;
          }
          
          if (popup_visits.in_array(popup_visit_count)) show_popup = true;
        }
        
      }
    }
    
    //alert(popup_subscribed + '+' + now + '+' + popup_visit_count); //debug
    setCookie('popup_visit', popup_subscribed + '+' + now + '+' + popup_visit_count, time, '/'); // subscribed,last_time,visits
  }
  
  if (show_popup) setTimeout('showPopup()', 1000*popup_delay);
}

function disablePopup() {
  var popup_subscribed = 1;
  var popup_last_time = 0;
  var popup_visit_count = 1;
  
  var time = new Date();
  var now = time.getTime();
  time.setTime(now+10*365*24*60*60*1000); // 10 year after now  
  now = Math.round(now/1000);
  
  var popup_visit = getCookie('popup_visit');
  if (popup_visit) {
    popup_visit = popup_visit.split('+');
    if (popup_visit.length==3) {
      popup_last_time = parseInt(popup_visit[1]);
      popup_visit_count = parseInt(popup_visit[2]);
      if (popup_visit_count < 1) popup_visit_count = 1;
      else popup_visit_count++;
    }
  }
  
  setCookie('popup_visit', popup_subscribed + '+' + now + '+' + popup_visit_count, time, '/'); // subscribed,last_time,visits
}

function showPopup() {
  var popup_window = document.getElementById("popup_window");
  if (popup_window.innerHTML=='') popup_window.innerHTML = document.getElementById("popup_wrapper").innerHTML;
  popup_window.style.visibility = 'visible';
}

function closePopup() {
  var popup_window = document.getElementById("popup_window");
  popup_window.style.visibility = 'hidden';
}

Array.prototype.in_array = function(p_val) {
	for(var i = 0, l = this.length; i < l; i++) {
		if(this[i] == p_val) {
			return true;
		}
	}
	return false;
}

/*==================================================
  Cookie functions
  ==================================================*/

function setCookie(name, value, expires, path, domain, secure) {
  document.cookie= name + "=" + escape(value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
      begin = dc.indexOf(prefix);
      if (begin != 0) return null;
  } else {
      begin += 2;
  }
  var end = document.cookie.indexOf(";", begin);
  if (end == -1) {
      end = dc.length;
  }
  return unescape(dc.substring(begin + prefix.length, end));
}

function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
      document.cookie = name + "=" +
          ((path) ? "; path=" + path : "") +
          ((domain) ? "; domain=" + domain : "") +
          "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}