var pArr=new Array();

pArr[0]=new p('1','http://www.crazyegg.net','popunder','XX','toolbar,status,resizable,scrollbars,menubar,location,height=800,width=860');
//pArr[0]=new p('1','http://www.','popunder','XX','width=800,height=700,left=10, top=10, menubar=1, status=1, location=1, toolbar=1, scrollbars=1, resizable=1');
//pArr[0]=new p('0','http://www.crazyegg.net','popup','AT,BE,GB,FI,NL,NO,ES,SE','width=800,height=600,left=10, top=10, menubar=1, status=1, location=1, toolbar=1, scrollbars=1, resizable=1');
//pArr[1]=new p('0','http://www.crazyegg.net','popup','--,ES,DE,NL,FR','width=800,height=600,left=10, top=10, menubar=1, status=1, location=1, toolbar=1, scrollbars=1, resizable=1');
//pArr[1]=new p('0','http://www.crazyegg.net','popup','--,FR,BE,CH,DE,IT,ES','width=800,height=600,left=10, top=10, menubar=1, status=1, location=1, toolbar=1, scrollbars=1, resizable=1');
//pArr[1]=new p('0','http://www.crazyegg.net','popup','--,FR,BE,CH,DE,IT,ES','width=800,height=600,left=10, top=10, menubar=1, status=1, location=1, toolbar=1, scrollbars=1, resizable=1');
//pArr[1]=new p('0','http://www.crazyegg.net','popup','DE','width=800,height=600,left=10, top=10, menubar=1, status=1, location=1, toolbar=1, scrollbars=1, resizable=1');
//pArr[2]=new p('0','http://www.crazyegg.net','popup','RO,BG,CZ','width=850,height=650,left=10, top=10, menubar=1, status=1, location=1, toolbar=1, scrollbars=1, resizable=1');


var numbersOfPopups=pArr.length;
var pWindow='';
createCookie('testCookie',1,0);

function p(p_equ,p_url,p_type,p_geoCodes,p_prop) {
	this.t_equ=p_equ;
	this.t_url=p_url;
	this.t_type=p_type;
	this.t_geoCodes=p_geoCodes;
	this.t_properties=p_prop;
	return this;
};

function createCookie(name,value,minutes) {
	if (minutes) {
		var date = new Date();
		date.setTime(date.getTime()+(minutes*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ')
			c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0)
			return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function clickPop(geoCountryCode) {
	if(numbersOfPopups==0)
		return;
	if(  (!readCookie('clickCookie')) && (readCookie('testCookie')) ) {
			createCookie('clickCookie',1,10)
			goPop(geoCountryCode);
	}
	return;
}

function pickRandom(geoCountryCode) {
	if(!geoCountryCode)
		return Math.floor(Math.random()*(numbersOfPopups));

	var j=0;
	var counter=new Array();
	for(var i=0;i<numbersOfPopups;i++) 	{
		if(pArr[i].t_geoCodes=='')
			continue;
		if(pArr[i].t_equ==0) {
			if( (pArr[i].t_geoCodes.indexOf(geoCountryCode)!=-1))
				counter[j++]=i;
		}
		else {
			if( (pArr[i].t_geoCodes.indexOf(geoCountryCode)==-1))
				counter[j++]=i;
		}
	}
	if(counter.length>1)
		return counter[(Math.floor(Math.random()*(counter.length)))];
	else
		return counter[0];
}

function goPop(geoCountryCode) {
	var pR=pickRandom(geoCountryCode);
	if(!pArr[pR])
		return;

	if(!pWindow.closed && pWindow.location)
		pWindow.location.href = pArr[pR].t_url;
	else
		pWindow=window.open(pArr[pR].t_url, "", pArr[pR].t_properties);

	if(pArr[pR].t_type=='popup') {
		if(!pWindow.closed && pWindow.location)
			pWindow.focus();
	}
	if(pArr[pR].t_type=='popunder') {
		if(!pWindow.closed && pWindow.location)
			pWindow.blur();
	}
	return;
}


