﻿if (typeof CtvAd == "undefined") {
    var CtvAd = function () {
        this.Height = 0;
        this.Width = 0;
        this.Path = "";
        this.AdPage = "";
        this.Zone = "";
        this.Site = "";
        this.Tile = 0;
        this.PageId = "";
        this.Section = "";
        this.SubSection = "";
        this.ContentSection = "";
        this.Environment = "";
        this.Keywords = new Array();

        this.ShouldDisplayInterstitials = false;

        CtvAd.__instance = this;

        if (!CtvAd.__OrdinalString) {
            var ordstr = CtvAd.GetOrdinalString();
            CtvAd.__OrdinalString = ordstr;
        }

        if (!CtvAd.__NumberOfAdsDisplayed) {

            CtvAd.__NumberOfAdsDisplayed = 0;
        }
    };
    
    CtvAd.GetInstance = function () {
        if (!CtvAd.__instance) {
            CtvAd.__instance = new CtvAd();
        }

        return CtvAd.__instance;
    };
    
    CtvAd.prototype.AddKeyword = function (keyword) {
        this.Keywords.push(keyword);
    };

    CtvAd.prototype.SetEnvironment = function (environment) {
        if (typeof environment !== 'undefined' && environment !== null) {
            this.Environment = this.Trim(environment);
            this.Environment = this.Environment.toLowerCase();
        }
    };
    
    CtvAd.prototype.SetPageId = function (pageId) {
        if (typeof pageId !== 'undefined' && pageId !== null) {
            this.PageId = this.Trim(pageId);
            this.PageId = this.PageId.toLowerCase();
        }
    };

    
    CtvAd.prototype.SetSection = function (section) {
        if (typeof section !== 'undefined' && section !== null) {
            this.Section = this.Trim(section);
            this.Section = this.Section.toLowerCase();
        }
    };

    CtvAd.prototype.SetSubSection = function (subSection) {
        if (typeof subSection !== 'undefined' && subSection !== null) {
            this.SubSection = this.Trim(subSection);
            this.SubSection = this.SubSection.toLowerCase();        
        }
    };

    CtvAd.prototype.SetContentSection = function (contentSection) {
        if (typeof subSection !== 'undefined' && subSection !== null) {
            this.ContentSection = this.Trim(contentSection);
            this.ContentSection = this.ContentSection.toLowerCase();
        }        
    };        
    
    CtvAd.prototype.SetAdPage = function (adPage) {
        if (typeof adPage !== 'undefined' && adPage !== null) {
            this.AdPage = this.Trim(adPage);
            this.AdPage = this.AdPage.toLowerCase();
        }
    };
    
    CtvAd.prototype.SetZone = function (zone) {
        if (typeof zone !== 'undefined' && zone !== null) {
            this.Zone = this.Trim(zone);
            this.Zone = this.Zone.toLowerCase();
        }
    };

    CtvAd.prototype.SetPath = function (path) {
        if (typeof path !== 'undefined' && path !== null) {
            this.Path = this.Trim(path);
            this.Path = this.Path.toLowerCase();
        }

        if (this.Path == '') {
            this.Path = "index";
        }
    };
    
    CtvAd.prototype.SetHeight = function (height) {
        this.Height = height;
    };
    
    CtvAd.prototype.SetWidth = function (width) {
        this.Width = width;
    };
    
    CtvAd.prototype.SetSite = function (site) {
        if (typeof site !== undefined && site !== null) {
            this.Site = this.Trim(site);
            this.Site = this.Site.toLowerCase();
        }
    };
    
    CtvAd.prototype.SetSubPath = function (subPath) {
        if (typeof subPath !== undefined && subPath !== null) {
            this.SubPath = this.Trim(subPath);
            this.SubPath = this.SubPath.toLowerCase();
        }
    };
    
    CtvAd.prototype.SetTile = function (tile) {
        //Leaderboards should always be Tile=1. If it's not - it will break sync ad units that we have running on the site. 
        this.Tile = tile;
    };
    
    CtvAd.prototype.DisplayInterstitials = function (displayInterstitials) {
        this.ShouldDisplayInterstitials = displayInterstitials;
    };
    
    CtvAd.GetOrdinalString = function () {
        return Math.ceil(Math.random() * 1000000000).toString();
    };

    CtvAd.prototype.Trim = function (value) {
        var rvalue = value;

        if (typeof value !== 'undefined' && value !== null && value.length > 0) {
            var firstChar = rvalue.charAt(0);

            if (firstChar == "/") {
                rvalue = rvalue.substring(1, rvalue.length);
            }

            var lastChar = rvalue.charAt(rvalue.length - 1);

            if (lastChar == "/") {
                rvalue = rvalue.substring(0, rvalue.length - 1);
            }

            if (rvalue.length > 0) {
                firstChar = rvalue.charAt(0);

                if (isNaN(firstChar) == false) {
                    rvalue = "x" + rvalue;
                }
            }
        }

        return rvalue;
    };

    CtvAd.prototype.Render = function () {

        var istCode = "";
        if (this.ShouldDisplayInterstitials == true && CtvAd.__NumberOfAdsDisplayed == 0) {
            istCode = "dcopt=ist;";
        }

        var renderTile = "";
        if (this.Tile > 0) {
            renderTile = "tile=" + this.Tile + ";";
        }

        CtvAd.__NumberOfAdsDisplayed += 1;

        var site = "";
        if (this.Site) {
            site = this.Site + '/';

        }

        var zone = "";
        if (this.Zone) {
            zone = this.Zone;

            if (this.Path != '') {
                zone = zone + '/';
            }
        }

        var adPage = "";

        if (this.AdPage) {
            adPage = "adpg=" + this.AdPage + ";";
        }


        var subPath = "";
        if (this.SubPath) {
            subPath = ";subpath=" + this.SubPath;
        }

        var pageId = "";

        if (this.PageId) {
            pageId = ";pageid=" + this.PageId;
        }

        var section = "";
        if (this.Section) {
            section = ";section=" + this.Section;
        }

        var subSection = "";
        if (this.SubSection) {
            subSection = ";subsection=" + this.SubSection;
        }

        var contentSection = "";
        if (this.ContentSection) {
            contentSection = ";contentsection=" + this.ContentSection;
        }

        var envr = "";

        if (this.Environment) {
            envr = ";envr=" + this.Environment;
        }

        var keywords = "";
        if (this.Keywords.length > 0) {
            for (i = 0; i < this.Keywords.length; i++) {
                keywords = keywords + ";keyword" + i + "=" + this.Keywords[i];
            }
        }

        var adCode = '<scr' + 'ipt language="JavaScript" src="http://ad.doubleclick.net/N5479/adj/' + site + zone + this.Path + pageId + envr +  section + subSection + contentSection + ';' + istCode + renderTile + adPage + 'sz=' + this.Width + 'x' + this.Height + subPath + keywords + ';coad=;ord=' + CtvAd.__OrdinalString + '\"' + ' type="tex' + 't/javascript"></sc' + 'ript>';

        document.write(adCode);
    };
}
