// v.00002 21/10/02 22.11pm
count = 0;
ad = 0;
function storecookie(s,n) {
s = escape(s);
n = escape(n);
document.cookie = "name=jirotF4ads0000001,path=/,value="+s+n+"";
}
function readcookie() {
allcookies = document.cookie
	if(!allcookies) { 
		storecookie(count,ad)
	} else { 
		ok = 0;
		myStore = 'name=jirotF4ads0000001,path=/'
		if(allcookies.indexOf(myStore)!=-1) {
			str = String(allcookies).split(myStore)[1];
			ok = (!str)?0:1;
		} 
		if(ok) {
			str = str.split("=")[1];
			count = Number(str.charAt(0));
			ad = Number(str.charAt(1));
		} else {
			storecookie(count,ad)
		}
	}	
}
function checkType(a,b) {
	ok=0;
	obj = typeof(a);
	if(obj == "string" && obj != "undefined") {
		if(b) {
			ok = (a!="")?1:0;
		} else {
			ok = (a=="")?1:0;
		}
	} else {
		if(!b) {
			ok = 1;
		} else {
	  		ok=0;
	  	
	  	}
	}
	return ok;
}
function checkVert() {

x=document.c4Category;
	if(x.indexOf('.')!=-1) {
		somV = x.split('.');
		x = somV[0]+"_"+somV[1];
	}
	if(x.indexOf('-')!=-1) {
		somV = x.split('-');
		x = somV[0]+somV[1];
	}
	if(checkType(x,1)) {
		d = x;
		v = "v_"+d;
		if(checkType(eval("window."+v),1)) {
 			v= eval("window."+v);
		} else {
			v = 0
		}
	} else {
		v=eval("v_generic");
	}
return v;
}

function getAd() {
	readcookie();
	v = checkVert()	
	if (v!=0) {
		if(checkType(v,1) && checkType(document.c4Blanket,0)) {
			if (v.indexOf(',')!=-1) {
				v = v.split(',');
				//alert('multiple ads');
				checkOne = false;
				(ad< v.length)?ad=ad:ad=0
				} else {
				
				checkOne = true;
				//alert('one ad only')
				ad = 0;
			}
		} else {
			checkOne = false;
		}
		if(!checkOne && count==0) {
			//display a popup
			displayAd(v[0])
			checkTwo = true;
			if(ad==0) {
			ad++;
			}
		}
		if(!checkOne && count==3) {
			//display a blanket
			displayAd(v[ad])
			checkTwo = false;
			(ad>v.length)?ad=0:ad++;

		} 
		if (count==0 && checkOne) {
			displayAd(v)
		}
		if(checkOne) {
			(count>1)? count=0 : count++;
		} 
		if(!checkOne) {
			(count>4)? count=0 : count++;
		}
		
	}
	
storecookie(count,ad)
}
function displayAd(which) {
	if(!document.layers) {
	if(typeof(eval("window.ad"+which))!="undefined") {
		adfile = String(eval("ad"+which));
		document.write('<scr'+'ipt language"javascr'+'ipt" src="http://www.channel4.com/SSI/ads/'+adfile+'"><\/scr'+'ipt>');
	} else {
		adfile = String(eval("ad101"));
		document.write('<scr'+'ipt language="javascr'+'ipt" src="http://www.channel4.com/SSI/ads/'+adfile+'"><\/scr'+'ipt>');
	}
	}
}
getAd();



