﻿
if (typeof (FR) == "undefined") { FR = {}; }
FR.adverts = {};

FR.adverts.contentChecks = [];

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.UnanimisPositionAds = function (frame, div) {
	//Function expected to be called only from Top banner for Unanimis
	var iFrame = window.top.document.getElementById(frame);
	div = document.getElementById(div);
	if (iFrame && div) {
		if (iFrame.contentDocument) {
			iFrame.contentDocument.body.innerHTML = "<div style='position:absolute; left:0px; top:0px;'>" + div.innerHTML + "</div>";
		}
		else if (iFrame.contentWindow) {
			iFrame.contentWindow.document.body.innerHTML = "<div style='position:absolute; left:0px; top:0px;'>" + div.innerHTML + "</div>";
		}
		//Remove ads from frame1
		//div.parentNode.removeChild(div);
	}
};

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;
    }
};


/////////////////////////////// More ads impressions ///////////////////////////////////////
ImpressionsSetup = function (){
    //Reload adverts on closing popups and selecting items in accordion panels
    var c = Sys.Application.getComponents();
    for (var i = 0; i < c.length; i++) {
        if ((Object.getType(c[i]).getName() == 'AjaxControlToolkit.ModalPopupBehavior') ||
        (Object.getType(c[i]).getName() == 'AjaxControlToolkit.ModalPopupExtender') ||
        (Object.getType(c[i]).getName() == 'Happy.AddFriendBehavior')) {
            if ((c[i].get_id().indexOf('JoinLinkModalPopupExtender') > -1) ||
            (c[i].get_id().indexOf('modal') > -1)) {
                c[i].get_events().removeHandler('hiding', ReloadAds);
                c[i].get_events().addHandler('hiding', ReloadAds);
            }
        }
        if ((window.document.location.href.toLowerCase().indexOf('groupsearch.aspx') > -1) &&
        (Object.getType(c[i]).getName() == 'AjaxControlToolkit.AccordionBehavior')) {
            c[i].remove_selectedIndexChanging(ReloadAds);
            c[i].add_selectedIndexChanging(ReloadAds);
        }
    }
};

ReloadAds = function(event) {
    if ((event._backgroundElement) && (event._backgroundElement.style.display != 'none')) {
        FR.onLoad.loadAds();
    }
    if (Object.getType(event).getName() == 'AjaxControlToolkit.AccordionBehavior') {
        FR.onLoad.loadAds();
    }
};
