﻿
/*
 * Ad: an individual ad package to be displayed.
 */


var testSite = "http://treehousetv.com";
var debug = false;
var aliasAppendix = '';

var BackUpIds = {
    '1x1': "1196217",
    '1x2': "1196215",
    '100x100': "1196209",
    '120x90': "1196212",
    '130x50': "1196214",
    '150x90': "1196211",
    '160x600': "1196218",
    '170x76': "1196216",
    '180x150': "1196213",
    '180x180': "1196219",
    '200x100': "1196228",
    '240x100': "1196226",
    '240x90': "1196222",
    '25x25': "1196210",
    '300x250': "1181404",
    '300x40': "1196225",
    '500x500': "1196224",
    '545x90': "1196227",
    '592x90': "1196221",
    '728x90': "1196220",
    '950x100': "1196230",
    '950x150': "1196229"
}

function Ad( pAdSize, pAlias, pKeys )
{
    
		this.AdBaseUri = "http://adserver.adtechus.com";
		this.DeliveryType = "addyn";
		this.TagVersion = "3.0";
		
	
        this.NetWorkID = "5233.1";
		this.AdSize = pAdSize;
		this.AdGroupId;	
		
		this.AdUrl = "";
		this.Alias = pAlias;
		this.Key = pKeys;
		this.TheDate = new Date();

		this.AdGroupId = Ad.GetAdGroupId();
		this.AdBackupID = Ad.GetAdBackupId(pAdSize);		

}

Ad.GetAd = function(pAdSize, pAlias, pKeys) {

    if (pKeys == "" || pKeys == "undefined" || pAlias == null)
        pKeys = GetKeys();

    if (pAlias == "" || pAlias == "undefined" || pAlias == null) {
        pAlias = GetAlias() + "_" + pAdSize + aliasAppendix;
    }

    var NewAd = new Ad(pAdSize, pAlias, pKeys);
    var AdIndex = Ad.SetInstance(NewAd);

    NewAd.AdUrl = NewAd.AdBaseUri + "/" + NewAd.DeliveryType + "|" + NewAd.TagVersion + "|" + NewAd.NetWorkID + "|" + NewAd.AdBackupID + "|0|-1|size=" + NewAd.AdSize;
    NewAd.AdUrl += ";loc=100;alias=" + NewAd.Alias + ";key=" + NewAd.Key + ";grp=" + NewAd.AdGroupId + ";target=_blank;&misc=" + (Math.random() * 1000);

    document.write('<scr' + 'ipt language="javascript1.1" src="' + NewAd.AdUrl + '"></scri' + 'pt>');

    if (debug) {
        alert(NewAd.AdUrl);
    }
}


Ad.GetInstance = function( i )
{
	if( ! Ad.__instance )
	{
		throw "Trying to get an instance of the Ad before it has been initialized";
		return;
	}
	else if( i != null && i >= 0 )
	{	
		return Ad.__instance[i];
	}
	else
	{
		return Ad.__instance[ Ad.__instance.length - 1 ];
	}
}

Ad.GetAdGroupId = function()
{
	if(Ad.AdGroupId == null)
	{
		Ad.AdGroupId = Math.round(Math.random() * 1000);
	}
	
	return Ad.AdGroupId;
}

Ad.GetAdBackupId = function(size) {
    //iterate through BackUpIds hash table
    for (name in BackUpIds) {
        if (name == size) {
            return BackUpIds[name];
        }
    }
}

function GetKeys() {
    return document.location.pathname.toLowerCase();
}

/********************************************************
********* Function added by Boris Iriarte on Jan 7th 2010
*********************************************************/

Ad.GetAdBackupId = function( size )
{
	//iterate through BackUpIds hash table
	for ( name in BackUpIds)
	{
	if (name == size) {
	 return BackUpIds[name];	
	 }
	}
}

Ad.GetAdUrl = function()
{
    return Ad.GetInstance(this.AdIndex).AdUrl;      
}

Ad.SetKey = function(keys)
{
    var allKeys = keys.replace("|", "+");    
    Ad.GetInstance(this.AdIndex).Keys += allKeys;
}

Ad.SetInstance = function( pAd )
{
	if( ! Ad.__instance )
	{
		Ad.__instance = new Array();
	}
	
	Ad.__instance.push( pAd );
	
	return Ad.__instance.length - 1;
}

//*****************************************************************************************
//******************** Function modified by Boris Iriarte on Jan 13th 2009 ****************
//******************** to implement the AdTech display considering each web site section **
//*****************************************************************************************

function GetAlias() {

    var siteName;
    var siteSection;
    var pUri = GetUri(window.location);
  
    var ruta = window.location.toString() ;

    if (pUri) {

        /***********************************************************************/
        /************************* case treehousetv.com ************************
        /**********************************************************************/
        siteName = "thtv";
        siteSection = "home";

        //No directories in the pUri

        if (pUri.directoryPath.length == 2 && ruta.indexOf("default.aspx") != -1) {
            siteSection = "home";
        }

        //One SubDirectory
        if (pUri.directoryPath.length > 2 && typeof (pUri.directoryPath[1]) != "undefined") {

            switch (pUri.directoryPath[1].toLowerCase()) {
                case "watch":
                    siteSection = "shows";
                    break;
                case "forums":
                    siteSection = "parent";
                    break;
                case "bragbook":
                    siteSection = "parent";
                    break;
                case "promotions":
                    siteSection = "contest";
                    break;
                case "kids":
                    siteSection = "kids";
                    break;
                case "win":
                    siteSection = "contest";
                    if (ruta.indexOf("default.aspx") != -1) {
                        aliasAppendix = "_landing";
                    }
                    break;
                default:
                    siteSection = "other";
                    break;
            }
        }

        //Two SubDirectorys
        if (pUri.directoryPath.length > 3 && typeof (pUri.directoryPath[2]) != "undefined") {

            switch (pUri.directoryPath[2].toLowerCase()) {

                case "testing":
                    siteSection = "testing";
                    break;

            }
        }

        if (ruta.indexOf("/blogs/theme/") != -1) {
            siteSection = "parent";
        } else if (ruta.indexOf("/adtest.aspx") != -1) {
            siteSection = "test";
        }
    }
 
    return siteName + "_" + siteSection;
}


function GetUri(pUri) {

    /* parseUri JS v0.1, by Steven Levithan modified by Rob Clarkson <http://stevenlevithan.com> Splits any well-formed URI into the following parts (all are optional): */

    var UriPartNames = ["source", "protocol", "authority", "domain", "port", "path", "directoryPath", "fileName", "query", "anchor"],
			UriParts = new RegExp("^(?:([^:/?#.]+):)?(?://)?(([^:/?#]*)(?::(\\d*))?)((/(?:[^?#](?![^?#/]*\\.[^?#/.]+(?:[\\?#]|$)))*/?)?([^?#/]*))?(?:\\?([^#]*))?(?:#(.*))?").exec(pUri),
			Uri = {};

    for (var i = 0; i < 10; i++) {
        Uri[UriPartNames[i]] = (UriParts[i] ? UriParts[i] : "");
    }


    if (Uri.directoryPath.length > 0) {
        Uri.directoryPath = Uri.directoryPath.replace(/\/?$/, "/");
        Uri.directoryPath = Uri.directoryPath.split("/");	
    }

    if (Uri.domain) {

        if (Uri.domain.indexOf("localhost") != -1)
            Uri.domain = (testSite + "").match(/^https?:\/\/(\w*\.)?([^\/]+\.[\w]{2,4})\/?(\w*)\/?/i);
         else
            Uri.domain = (window.location + "").match(/^https?:\/\/(\w*\.)?([^\/]+\.[\w]{2,4})\/?(\w*)\/?/i);
    }

    return Uri;
}


