﻿// This class contains any logic used to modify pages in response to ads being loaded (or not loaded). 
// Any TradeMe specific ad 'hacks' should live here, not in AdServer.
var AdHandlers = function() {

    // Some mcat constants.
    var McatValue_Motors_Root = "0001-";
    var McatValue_Property_Root = "0350-";
    var McatValue_Property_Retirement_Root = "0350-9017-";
    var McatValue_Motors_Cars_Root = "0001-0268-";

    return {
        
        // This function is called whenever an ad is loaded into the page.
        onAdLoaded: function(ad) {
            
            if (!ad.IsEmpty)
            {
                // Set the width and height.
                if (ad.Width !== "" && ad.Height !== "" && ad.Width != "-1" && ad.Height != "-1") {
                    jQuery("#" + ad.ID).css("overflow", "hidden");
                }
                if (ad.Width !== "" && ad.Width !== "-1") {
                    jQuery("#" + ad.ID).css("width", ad.Width + "px");
                }
                if (ad.Height !== "" && ad.Height !== "-1") {
                    jQuery("#" + ad.ID).css("height", ad.Height + "px");
                }

                // Apply any ad-type specific hacks.
                switch (ad.Type) {
                
                    case 'Banner468x60':
                        jQuery("#sideAd").show();
                        jQuery("#SkyscraperAdLabel").show();
                        jQuery("#SkyscraperAd").css("overflow", "hidden");
                        jQuery("#SkyscraperAd").css("width", "160px");
                        jQuery("#SkyscraperAd").css("height", "600px");
                        divOfAdTwo = 'SkyscraperAd';
                        jQuery("#BannerAdLabel").show();
                        AdHandlers.showAdLabels()
                        break;
                        
                    case 'Skyscraper':
                        jQuery("#" + ad.ID).css("margin-left", "auto");
                        jQuery("#" + ad.ID).css("margin-right", "auto");
                        AdHandlers.showAdLabels()
                        
                    case 'Tower':
                        jQuery("#" + ad.ID).css("margin-left", "auto");
                        jQuery("#" + ad.ID).css("margin-right", "auto");
                        jQuery("#divTab").css("overflow", "hidden");
                        jQuery("#divTab").css("width", "200px");
                        jQuery("#divTab").css("height", "80px");
                        divOfAdTwo = 'divTab';
                        if (currentMcat.startsWith(McatValue_Motors_Root) || currentMcat.startsWith(McatValue_Property_Root)) AdHandlers.resizeMTA();
                        break;

                    case 'LandingPageIsland':
                        AdHandlers.showAdSection(ad.Type);
                        break;

                    case 'ListingOne':
                        if (currentMcat.startsWith(McatValue_Property_Retirement_Root)) AdHandlers.showRVALozenge('TradeMeAd_adIframe_listingone');
                        else if (currentMcat.startsWith(McatValue_Motors_Cars_Root)) AdHandlers.showClassicCarsLozenge('TradeMeAd_adIframe_listingone');
                        break;
                        
                }
            }
            else
            {
                // The ad came back empty.               
            }

        },

        showAdSection: function(tfn) {
            var adSec, adTitle, adSecSpacer;
            if (tfn == 'ListingOne') {
                adSec = document.getElementById('advertSection');
                if (adSec != null) {
                    adSec.style.display = 'block';
                }
                adTitle = document.getElementById('advertTitle');
                if (adTitle != null) {
                    adTitle.style.display = 'block';
                }
                adSecSpacer = document.getElementById('adSecSpacer');
                if (adSecSpacer != null) {
                    adSecSpacer.style.display = 'none';
                }
            }
            else if (tfn == 'LandingPageIsland') {
                adSec = document.getElementById('LandingPageIslandAdLabel');
                if (adSec != null) {
                    adSec.style.display = 'block';
                }
            }
        },
        
        showAdLabels : function() {
            if(document.getElementById('sideAd') != null) {
                document.getElementById('sideAd').style.display = 'block';
            }
            if(document.getElementById('BannerAdLabel') != null) {
                document.getElementById('BannerAdLabel').style.display = 'block';
            }
            if(document.getElementById('SkyscraperAdLabel') != null) {
                document.getElementById('SkyscraperAdLabel').style.display = 'block';
            }
        },
        
        resizeMTA : function() {
            var f, swfDiv;
            f = document.getElementById('TradeMeAd_adIframe_Tower');
            if(f != null) {
                swfDiv = f.contentWindow.document.getElementById('BCITOFlashAd');
                if(swfDiv != null) {
                    f.style.height = "600px";
                }
            }
        },
      
        showClassicCarsLozenge : function(ifn) {
            var f, c, scriptIndex;
            if(typeof parent.isClassicCar == 'undefined' ||!parent.isClassicCar) {
                return;
            }
            f = document.getElementById(ifn);
            if(f == null) {
                return;
            }
            c = f.contentWindow.document.body.innerHTML;
            if(c == null) {
                return;
            }
            c = c.replace(/(]*href=")[^"]*ListingTwo[^"]*www\.state\.co\.nz[^"]*(".*?]*src=")[^"]*"/i,"$1" + parent.classicCarUrl + "$2/images/ads/swann_listing_lozenge-classic.gif\"");
            c = c.replace(/class="?listingAd"?/ig,'style="display: inline; padding: 5px;"');
            scriptIndex = c.toLowerCase().indexOf("<script");
            if(scriptIndex !=- 1) {
                 c = c.substr(0, scriptIndex - 1);
            }
            f.style.display = 'none';
            document.getElementById('tmClassicCarsAdContent').innerHTML = c;
        },
        
        showRVALozenge : function(ifn) {
            var f, c, scriptIndex;
            if(typeof parent.isRVAMember == 'undefined' ||!parent.isRVAMember) {
                return;
            }
            f = document.getElementById(ifn);
            if(f == null) {
                return;
            }
            c = f.contentWindow.document.body.innerHTML;
            if(c == null) {
                return;
            }
            c = c.replace("non_RVA-ad.gif", "rva-ad.gif");
            c = c.replace("/Link.aspx?i=15118", parent.rvaUrl);
            c = c.replace(/class="?listingAd"?/ig,'style="display: inline; padding: 5px;"');
            scriptIndex = c.toLowerCase().indexOf("<script");
            if(scriptIndex !=- 1) {
                c = c.substr(0, scriptIndex - 1);
            }
            f.style.display = 'none';
            document.getElementById('tmRVAAdContent').innerHTML = c;
        }
    }
} ();

String.prototype.startsWith = function(str){
    return (this.indexOf(str) === 0);
}

jQuery(document).ready(function() { if (typeof Ads != 'undefined') Ads.subscribeToAdLoadedEvent(AdHandlers.onAdLoaded); });