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.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.SetAdPage = function(adPage){
		this.AdPage = this.Trim(adPage);
        this.AdPage = this.AdPage.toLowerCase();		
	}

    CtvAd.prototype.SetZone = function(zone) {
		this.Zone = this.Trim(zone);
        this.Zone = this.Zone.toLowerCase();
    }

    CtvAd.prototype.SetPath = function(path) {
        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) {
		this.Site = this.Trim(site);
        this.Site = this.Site.toLowerCase();
    }
	
	CtvAd.prototype.SetSubPath = function(subPath){
		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 (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)
            }
        }

        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 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/adj/' + site + zone + this.Path + ';' + istCode + renderTile + adPage + 'sz=' + this.Width + 'x' + this.Height + subPath + keywords +  ';ord=' + CtvAd.__OrdinalString + '\"' + ' type="tex' + 't/javascript"></sc' + 'ript>';

        document.write(adCode);
    }
}
