function Adverts() {}

Adverts.map = new Map().set("top","Top");
Adverts.map.set("mpu", "Middle1");
Adverts.map.set("sponsor","Top1");
Adverts.map.set("sponsor2","Right1");

Adverts.getScriptPath = function() {
    var realMediaPrefix = "http://realmedia.channel4.com/RealMedia/ads/adstream_mjx.ads/";
	return realMediaPrefix + Adverts.getRealMediaPath();
}

Adverts.getRealMediaPath = function() {
    var realmediaPath = new StringBuilder();
    realmediaPath.append(Adverts.getHostName());
    realmediaPath.append(Adverts.getPath());
    realmediaPath.append(Adverts.getRandom());
    realmediaPath.append("@").append(Adverts.getTypes());
    //var optional = Adverts.getOptionalParameters();
   //if(optional.isNotEmpty()){
    //realmediaPath.append("?").append();
   // }
    return realmediaPath.toString();
}

Adverts.getRevenueScience = function () {
	var c4OASlocQ = location.search;
	if(typeof rsinetsegs != "undefined") {
		var segQS = rsinetsegs.length > 0 ? "s=" + rsinetsegs[0] : "";

		for (var i = 1; i < rsinetsegs.length; i++) {
			segQS += ("&s" + "=" + rsinetsegs[i]);
		}
		if(c4OASlocQ=="") {
			c4OASlocQ = "&"+segQS;
		} else {
			//console.log(c4OASlocQ)
			c4OASlocQ = "&" + c4OASlocQ.replace("?","")+"&"+segQS;
		} 
	}	
	return c4OASlocQ;
}
Adverts.getIdentityParameters = function() {
	if(C4.Identity && Identity) {
		age = Identity.getAge();
		gender = Identity.getGender();
		if(age && gender) {
			query = "age="+age+"&gender="+gender;
		} else {
			query = "";
		}
		if(OAS_query) {
			OAS_query+= query;
		} else {
			OAS_query = query;
		}
		return OAS_query;
	}
	return "";
}

/* currently not using this 
Adverts.getOptionalParameters = function() {
    var result = new Parameters();
    ["from-name", "challenge-identity"].each(function(name){
        var content = Meta.getContent(name);
        if(content.isNotEmpty()){
            result.add(name, content);
        }
    });
    return result.toString();
}
*/
/*AD Target Path Account */
Adverts.getHostName = function () {
    if (location.hostname == 'www.channel4.com') {
        return "www.4homes.channel4.com";

    }
	if (location.hostname.match("int.channel4.com")) {
        return "int.4homes.channel4.com";

    }
	if (location.hostname.match("stage.channel4.com")) {
        return "stage.4homes.channel4.com";

    }
	if (location.hostname.match("test.channel4.com")) {
        return "test.4homes.channel4.com";

    }
    return 'dev.4homes.channel4.com';
};

Adverts.getPath = function() {
    var pathname = location.pathname;
	if(pathname.match(/\/4homes/)){
		pathname=pathname.replace("/4homes","")
	}
    if (pathname.endsWith('/')) {
        pathname += 'index.html/';
    } else {
        pathname += '/';
    }
    return pathname;
}

Adverts.getRandom = function() {
    return "1" + Math.random() * 100000000000000000;
}

Adverts.getTypes = function() {
    var list = new StringBuilder();
    Adverts.required.each(function(name){
       list.append(Adverts.convertToRealMediaType(name)).append(",");
    });
    return list;
}

Adverts.required = new Array()

Adverts.write = function(type){
    if (!Adverts.required.contains(type)) {
        Adverts.required.add(type);
    }
}

Adverts.isRequiredType = function(type) {
    return Adverts.required.contains(type);
}

Adverts.convertToRealMediaType = function(type){
    return Adverts.map.get(type);
}

Adverts.listeners = new Map();


Adverts.removeIfEmpty = function(e,type,d) {
	if (e.childNodes) {
		for(a in e.childNodes) {
			if(e.childNodes[a].tagName=="IMG") {
				//is the image an empty gif
				if(e.childNodes[a].src.match(/empty\.gif/)) {
					//hide placeholder
					d.style.display="none";
				}
			}	
		}
	}
}
//Adverts Display
Adverts.display = function(type) {
    var sourceDiv = document.getElementById("advert-"+type+"-parent");
    var advertDiv = document.getElementById("advert-"+type);
    sourceDiv.style.display= "none";
    if (sourceDiv != null && advertDiv != null ) {
        var ele_array = new Array();
        for (var i = 0; i < sourceDiv.childNodes.length; i++) {
			var sd = sourceDiv.childNodes[i];
            if (sd.tagName != '!' && sd.tagName != 'SCRIPT') {
                ele_array[ele_array.length] = sd;
				//check for emptys
				
				if(sd.tagName == "A") {
				   //found anchor is it an empty reference
				   Adverts.removeIfEmpty(sd,type,advertDiv);
				   
				}
				
			}
        }
        for (i = 0; i < ele_array.length; i++) {	
			if(ele_array[i].tagName== 'IMG') {
        		if(ele_array[i].src.match(/_EMPTY_/)) {
        			ele_array[i].style.display="none";
        		}
        	}
            advertDiv.appendChild(ele_array[i]);
        }
    }
}

function return_OASRequester(as,p) {
    if (gtiVideoAds) {
		u = "http://realmedia.channel4.com/RealMedia/ads/";
		a = 'adstream_'+as+'.ads/';
		t =  new StringBuilder().append(Adverts.getHostName()).append(Adverts.getPath());
		p = (p!=null)?p:Adverts.getTypes();
		s = Adverts.getRandom();
		q = "?";
		OAS_Requester = u + a + t + '/1' + s + '@' + p + q;
	} else {
		OAS_Requester="noADs";
	}
	return OAS_Requester;
}

