// определяем браузер для того чтобы правильно работать с onClick
var ua = navigator.userAgent.toLowerCase();
// Opera
var isOpera = (ua.indexOf("opera") != -1);
// Определим Internet Explorer
var isIE = (ua.indexOf("msie") != -1 && ua.indexOf("opera") == -1 && ua.indexOf("webtv") == -1);
// Gecko = Mozilla + Firefox + Netscape
var isGecko = (ua.indexOf("gecko") != -1);
var cookname = 'gnezdo_popunder2_54';
var gnezdo_popunder = 'http://shop.gnezdo.ru/show/54/block.html';
var cooktime = 24;

function setCookie( name, value, path, domain, time ) {
	var secure = '';
	var today = new Date();
	today.setTime( today.getTime() );
	if (time == ''){
		expires = 1 * 1000 * 60 * 60 * 6;
	} 
		else expires = 1 * 1000 * 60 * 60 * time;
            
	var expires_date = new Date( today.getTime() + (expires) );
            
	document.cookie = name + "=" +escape( value ) +
	 ( ( expires ) ? ";expires=" + expires_date.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 gnezdo_open(event){
	if (document.cookie == '' || navigator.userAgent.toLowerCase().indexOf('linux') > 0) return;
	 if (isGecko){
        if (event.button==2) return;
		if (!getCookie(cookname)){
			setCookie(cookname, 1, '/', '', cooktime);// ставим куку на 1 день
			window.open(gnezdo_popunder ,'winname','toolbar=1,location=1,status=1, menubar=1,scrollbars=1,resizable=1');
			self.focus();
		}
	}	
}

function gnezdo_open_gecko(event){
	if (document.cookie == '' || navigator.userAgent.toLowerCase().indexOf('linux') > 0) return;
	if (!getCookie(cookname)){
		setCookie(cookname, 1, '/', '', cooktime);// ставим куку на 1 день
		window.open(gnezdo_popunder ,'winname','toolbar=1,location=1,status=1, menubar=1,scrollbars=1,resizable=1');
		self.focus();
	}
}

function gnezdo_openie(){
    if (document.cookie == '') return;
    if (window.event && window.event.type && window.event.type == 'click' && !getCookie(cookname)){
        setCookie(cookname, 1, '/', '', cooktime);
        window.open(gnezdo_popunder ,'winname','toolbar=1,location=1,status=1, menubar=1,scrollbars=1,resizable=1');
        self.focus();
    }
}

function gnezdo_open_opera(event){
	if (document.cookie == '') return;
		if (window.event && window.event.type && window.event.type == 'click' && !getCookie(cookname)){
			setCookie(cookname, 1, '/', '', cooktime);// ставим куку на 1 день
			window.open(gnezdo_popunder  ,'winname','toolbar=1,location=1,status=1, menubar=1,scrollbars=1,resizable=1');
		}
}

// а тут нужно навешать событий на всякие онклики
if (isOpera){
    var m = document.getElementsByTagName("a");
    for (key in m)
    {
      current=m[key];
      if (!getCookie(cookname) && !current.onclick) {
		 current.target="_blank";
         current.onclick="return gnezdo_open_opera(event)";
      }
    }
}

if (isIE){
    var m = document.getElementsByTagName("a");
    for (i=0;i<m.length;i++)
    {   
        var current=m[i];
         if (!getCookie(cookname) && !current.onclick){
            current.target = '';
            current.onclick = function(){gnezdo_openie();};
        }
    }
}

if (isGecko){
    var m = document.getElementsByTagName("a");
    for (i=0;i<m.length;i++)
    {   
        var current=m[i];
         if (!getCookie(cookname) && !current.onclick){
            current.target = '';
            current.onclick = function(){gnezdo_open_gecko();};
        }
    }
}