/*
 * popunder code, (c) Markelov Dmitry
 */
(function( window, undefined ) {
  var document = window.document, //doc map
  feedboxPopunder = function (host,id) {
      window.fbPopunder = this;
      this.id = id || 0;
      this.host = host;
      this.i = 0;
      this.active;
      this.width = 0;
      this.height = 0;
      var id = this.id ? '&iid='+this.id : '';
      this.url=this.host+"?publisher=popunder"+id;
      this.jq = function () {
        if (typeof jQuery == 'undefined') {
          return false;
        }
	this.$=jQuery;
        return true;
      }
      this.click = function(e){
        var ret = true;
	this.a = null;
        if (this.getCookie("snPup") == "1") {return true};
        e = e || window.event;
        var t = e.target || e.srcElement;
        if (t.tagName == "IMG") t = t.parentNode;
        if (1) {
          this.a = t;
	  ret = this.open((t.tagName == "A") && (t.href.search("http") >= 0));
        }
	return ret;
      };
      this.mover = function(e){
        this.save();
      };
      this.GetData = function() {
	document.write('<scr'+'ipt type="text/javascript" src="'+this.url+'&action=data'+'"></sc'+'ript>');
	document.write('<scr'+'ipt type="text/javascript" src="'+this.host+'tpl/this/js/jquery-1.4.2.js'+'"></sc'+'ript>');
      };
      this.InitData = function(data){
	this.active = data.active;
	this.width = data.width;
	this.height = data.height;
	this.initPage();
      }
      this.initPage=function(){
	if (typeof(this.active) == "undefined") {
	  this.GetData();
	  return;
	}
	if(!this.active) return;
	this.i++;
        if(this.i<100){
          if(document && document.body && this.jq()) {
            if (typeof(window.opera) == "undefined") {
             this.$(document.body).bind('click',{_this: this}, function(e) {
                return e.data._this.click(e);
            });
            } else {
             this.$(document.body).bind('mouseup',{_this: this}, function(e) {
                return e.data._this.click(e);
              });
            }
          } else setTimeout('fbPopunder.initPage()', 100*this.i);
        }
      }

      this.initPop=function(){
	if (typeof window.mozPaintCount != "undefined"){
	  if(!this.i) {
	  var x = window.open("about:blank");
	  x.close();
         }
        }
	try {
	   if(!this.i) opener.window.focus();
	} catch (err) { }
        this.i++;
        if(this.i<100){
          if(document && document.body && this.jq()) {
           this.$(document.body).bind('mouseover',{_this: this}, function(e) {
              e.data._this.mover(e);
            });
          } else setTimeout('fbPopunder.initPop()', 100*this.i);
        }
      };
      this.get = function () {
       this.$.ajaxSetup({async: false});
        $.get(this.host, {publisher: "popunder", action: 'pop'}, function(data){fbPopunder.set(data)});
      }
      this.set = function(popup) {
       if (popup == '1') popup = 1;
       else {
          var expires = new Date();
          expires.setTime(expires.getTime() + (1000 * 14400));
          this.setCookie("snPup", "1", expires.toGMTString(), "/");
          popup = 2;
        }
        this.p = popup;
      }
      this.save = function() {
        if(this.saved) return;
       this.$.ajaxSetup({async: false});
       this.$.get(this.host, {publisher: "popunder", action: 'save'}, function(data){fbPopunder.saved=1});
      }
      this.getCookie = function (name) {
            var b=/^\s*(\S+)\s*=\s*(\S*)\s*$/,
            c=document.cookie.split(';');
            for(var i=0;i<c.length;i++)
                    if(b.exec(c[i]) && RegExp.$1==name) return unescape(RegExp.$2);
      }
      this.setCookie = function (name, value, expires, path) {
        document.cookie = name + "=" + escape(value) +
          ((expires) ? "; expires=" + expires : "") +
          ((path) ? "; path=" + path : "");
      }
      this.rewind = function(url){
	this.a.setAttribute('href',url);
      }
      this.open = function(bLink){
        this.get();
        if (typeof this.p == 'undefined') this.set('2'); //opera fix
        var popup = this.p,
        w = this.width || screen.availWidth || 1024,
        h = this.height || screen.availHeight || 650,
        ua = navigator.userAgent.toLowerCase(),
        isGecko = (ua.search('gecko') != -1),
        isIE = (ua.search('msie') != -1),
        isIE9 = (ua.search("msie 9.0") != -1),
        isFF = (ua.search('firefox') != -1),
        isOpera = (ua.search('opera') != -1),
	popSettings = 'width='+w+',height='+h+',toolbar=no,location=no,status=no,dependent=yes,titlebar=no,scrollbars=yes,directories=no',
	popName = 'fbpop'+popup,
	popURL = this.url+"&p="+popup;
	if(bLink)
	  if (isIE9) this.a.removeAttribute("target");
	  else this.a.setAttribute('target', '_blank');
        if (isFF) {
	    var wo = window.open(popURL,popName,popSettings);
	    if(wo && wo.blur) wo.blur();
	    window.focus();
	    return true;
        } else if (isIE9) {
          if(bLink) {
	    var url = this.a.href;
	    this.a.setAttribute("href", popURL);
	    window.open(url);
	    setTimeout('fbPopunder.rewind('+url+')', 10);
	  } else {
	    window.open(popURL,popName,popSettings);
	  }
	  return true;
        } else if (isIE) {
          wo = window.open(popURL,popName,popSettings);
          if(wo && wo.blur) wo.blur();
          window.focus();
          return true;
        } else if (isGecko||isOpera){
          wo = window.open(popURL,popName,popSettings);
          if(wo && wo.blur) wo.blur();
          window.focus();
          return true;
        }
      }
  }; //constructor
 
  window.feedboxPopunder = feedboxPopunder;
})(window);


