﻿if (typeof(FR) == "undefined") FR = {};
FR.adverts = {};
FR.adverts.contentChecks = new Array();
FR.adverts.adVisibility = function(adunit, vis){
    var oAd = document.getElementById(adunit);
    if(oAd){
        if(vis){
            layeron(oAd, true);
        }else{
            layeroff(oAd);
        }
    }
}
FR.adverts.adHeight = function(adunit, checkHeight){
    var oAd = document.getElementById(adunit);
   
    if(oAd){
        if(oAd.clientHeight<checkHeight){
            oAd.style.height=checkHeight+"px";
        }
    }
}
FR.adverts.logAdvertError = function(adunit, errCode){
    //FR.tracking.AdvertError(adunit, errCode, document.location.href, banneradcode, Ad2OnePageId);
}
FR.adverts.getQSVar = function(doc, var_name){
	var loc = doc.location.href
	if (loc.toLowerCase().indexOf(var_name) <= 0)
		{return '';}
	else if (loc.toLowerCase().indexOf('&', loc.toLowerCase().indexOf(var_name)) <= 0)
		{return loc.substring(loc.toLowerCase().indexOf(var_name) + var_name.length + 1)}
	else
		{return loc.substring(loc.toLowerCase().indexOf(var_name) + var_name.length + 1, loc.toLowerCase().indexOf('&', loc.toLowerCase().indexOf(var_name)))}
}
FR.adverts.getContent = function(adunit, doc){
    if (typeof(this.contentChecks[adunit]) == "undefined") {
        this.contentChecks[adunit] = {};
        this.contentChecks[adunit].count = 0;
        this.contentChecks[adunit].foundContent = false;
    }
    if(this.contentChecks[adunit].foundContent) {
        //doc.write response triggers body.onload. if weve already found content then we dont care about this next response
        return false;   
    }
    this.contentChecks[adunit].count++;
    if(this.contentChecks[adunit].count > 4) {
        //this function has been called 4 times aready and no content has been found. need to log lack of response
        this.logAdvertError(adunit, 'slowAd');
        return false;   
    }
    var a = doc.getElementsByTagName("object");
    var b = doc.getElementsByTagName("img");
    var c = doc.getElementsByTagName("iframe");
    switch(adunit){
        case "mpu" :
            if(a.length>0){ // if there is an object element
                FR.adverts.adHeight(adunit, a[0].getAttribute("height")); //send object height to adHeight function
                this.contentChecks[adunit].foundContent = true;
            } else if(c.length>0){ //if there is an iframe
                FR.adverts.adHeight(adunit, c[0].getAttribute("height")); //send iframe height to adHeight function
                this.contentChecks[adunit].foundContent = true;
            } else if(b.length>0){ //if there is an img
                if(b[0].getAttribute("height")) { //if the img has height attribute
                    if (b[0].getAttribute("height")>1){ //and the height is greater than 1 - not a tracking pixel
                        FR.adverts.adHeight(adunit, b[0].getAttribute("height")); //send img height to adHeight function
                        this.contentChecks[adunit].foundContent = true;
                    }
                }else{             //if the img has NO height attribute
                    if(b[0].src.indexOf("600")>-1){ //look in the source of the image for 600 -mega sky
                        FR.adverts.adHeight(adunit,"630");
                        this.contentChecks[adunit].foundContent = true;
                    }else{                          //its an mpu
                       // top.FR.adverts.adHeight(adunit,"280"); //make sure the adhtm iframe is the right size for mpu
                        //removed default height: defined in stylesheets
                        this.contentChecks[adunit].foundContent = true;
                    }
                }
            }else{ //if nothing has loaded, wait and try again from the top
                setTimeout(this.getContent.bind(this, adunit, doc), 1000);    
            }
        break;
        case "spon" :
            if(b.length>0){ // if an image is showing
                if (b[0].src.indexOf("AE3.gif")>-1||b[0].src.indexOf("AE2.gif")>-1||b[0].src.indexOf("AE1.gif")>-1||b[0].src.indexOf("AE0.gif")>-1||b[0].src.indexOf("AE4.gif")>-1){ //check that it's an ITV blank image being served
                    FR.adverts.adVisibility("spon", false); //hide spon iframe
                }else{
                    FR.adverts.adVisibility("spon", true); //if a blank image is not being served, show spon iframe
                }
            }else{
                FR.adverts.adVisibility("spon", true); //if an image is not being served, show spon iframe
            }
        break;
        case "banner" :
        break;        
    }
}
