OAS_url ='http://realmedia.channel4.com/RealMedia/ads/';
OAS_listpos = 'Top';
var OAS_query = '?';
var OAS_sitepage = '' // basic config;
//end of configuration
OAS_version = 10;
OAS_rn = '001234567890'; OAS_rns = '1234567890';
OAS_rn = new String (Math.random()); OAS_rns = OAS_rn.substring (2, 11);
function OAS_NORMAL(pos) {
document.write('<A HREF="' + OAS_url + 'click_nx.ads/' + OAS_sitepage + '/1' + OAS_rns + '@' + OAS_listpos + '!' + pos + OAS_query + '" TARGET=_top>');
document.write('<IMG SRC="' + OAS_url + 'adstream_nx.ads/' + OAS_sitepage + '/1' + OAS_rns + '@' + OAS_listpos + '!' + pos + OAS_query + '" BORDER=0 ALT="Click!"></A>');
}

// OAS AD version
function OAS_AD(pos) {
if (OAS_version >= 11 && typeof(OAS_RICH)!='undefined')
 return OAS_RICH(pos);
else
 return OAS_NORMAL(pos);
}

// function to amend list position
function appendListPos(pos) {
	if(document.getElementById("c4ad-"+pos)) {
		(OAS_listpos!="")?OAS_listpos+=","+pos:OAS_listpos+=pos;
	}
}
// returns AD content to visible area
function showAdvertOld(pos) {
	if(document.getElementById("c4ad-"+pos) && document.getElementById("c4ad-"+pos+"-parent")) {
		document.getElementById("c4ad-"+pos).innerHTML = document.getElementById("c4ad-"+pos+"-parent").innerHTML;
	}
}


function showAdvert(pos) {
	if(document.getElementById("c4ad-"+pos) && document.getElementById("c4ad-"+pos+"-parent")) {
	var ad_div = document.getElementById("c4ad-"+pos+"-parent");
	var target_div = document.getElementById("c4ad-"+pos);
	target_div.style.display='block';
	var ele_array = new Array();
	for (i=0;i<ad_div.childNodes.length;i++){
	    if(ad_div.childNodes[i].tagName != '!' && ad_div.childNodes[i].tagName != 'SCRIPT'){
		ele_array[ele_array.length] = ad_div.childNodes[i];
	    }
	}
	for (i=0;i<ele_array.length;i++){
	    target_div.appendChild(ele_array[i]);
	}
    }
}

var OAS_query="";
//returns PIPED query and removes further Query Parameters

var OAS_AccountTarget = function(url) {
var OAS_sitepage=url;
var OAS_init = function (url) { 
//remove protocol
OAS_sitepage = url.replace(/^\w+:\/\//,"");
OAS_sitepage = (OAS_sitepage.indexOf("?")!=-1)?OAS_sitepage.substring(0,OAS_sitepage.indexOf("?")):OAS_sitepage;
OAS_sitepage = OAS_rewriteRules(OAS_sitepage);
};
var C4sites= function () {
var C4siteSections = ["4car","4homes","4laughs","4od","4producers","bigbrother","community","culture","documentaries","entertainment","film","food","fourdocs","germ","health","history","interact","learning","life","lifestyle","money","more4","music","news","science","simulcast","sport","tv","video","watch_online","thetvshow"];
return 	C4siteSections.toString();
};
pipeFirstParam = function (t) {
if(t.indexOf("?")!=-1 && t.indexOf("=")!=-1) {
	s = t.substring(t.indexOf("?")+1,t.indexOf("=")+1)
		if(t.indexOf("&")!=-1) {
			//requires middle
			m = t.substring(t.indexOf("=")+1,t.split("&")[0].length)
			//e = t.substring(t.indexOf("&"),t.length);
			return "?"+(s+"|"+m+"|");
		} else {
			e = t.substring(t.indexOf("=")+1,t.length)
			return "?"+(s+"|"+e+"|");
		}
	}
return "?";
};
var params = function (t) {
	if(t.indexOf("?")!=-1) {
		OAS_query = pipeFirstParam(t);
	} else {
		OAS_query="?";
	}
	
};
params(url);
var lastDir = function(t) {
//Find Last Directory in target
var endDir = false;
var test = t.match(/\/([\w-_]+$)/);
	if(test!=null || t.indexOf("/")==-1) {
		endDir=true;
	}
return endDir;
};
var removeQuery = function (t) {
//Strip Query : t == target;
	if(t.indexOf("?")!=-1) {
		t=t.split("?")[0]
	}
	return t;
};
var addIndex = function (t) {
if(t.charAt(t.length-1)=="/") {
	//end of URL contains slash add index.html
	t+="index.html"
} else {
	//none or many slashes, either an end directory, file or query
	if(lastDir(t)) {
	t+="/index.html";
	} 
}
return t;
};
var OAS_rewriteRules = function (t) {
	
	t=addIndex(t);
	t=removeQuery(t);	
	var fDir = t.match(/\/([\w-_]+)/);
	if(fDir!=null) {
		//fName = fDir[0].replace(/\//g,"");
		if((","+C4sites()).match(","+fDir[1])) {
			t = t.replace(fDir[0],""); 
			t = t.replace(".channel4.","."+fDir[1]+".channel4.");
		} 
	}
//allows ads to be delivered from test accounts for all non prod accounts
t = t.replace(/int\.|dev\.|test\.|uat\.|stage\./,"test.")
return t;
};
OAS_init(url);
return OAS_sitepage;
};
OAS_sitepage=OAS_AccountTarget(document.location.href);

