﻿
/*
 * Ad: an individual ad package to be displayed.
 */


var testSite = "http://www.ytv.com";
var debug = false;

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;
    }

    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 + ";";

    document.write('<scr' + 'ipt language="javascript1.1" src="' + NewAd.AdUrl + '"></scri' + 'pt>');


    //**************DEBUG************************

    if (debug) {
        //$("#ytv-main-banner-ad").append("<div style='position:absolute;z-index:2;white-space: nowrap;'>" + NewAd.AdUrl + "</div>");
        alert(NewAd.AdUrl);
    }


    //**************DEBUG************************



}


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;
}

/********************************************************
********* Function added by Boris Iriarte on Jan 7th 2010
*********************************************************/
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'	: "1196223",	
			'300x40'	: "1196225",
			'500x500'	: "1196224",
			'545x90'	: "1196227",
			'592x90'	: "1196221",
			'728x90'	: "1196220",
			'950x100'	: "1196230",			
			'950x150'	: "1196229"	
		}


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 GetKeys() {

    var pUri = GetUri(window.location);
    var fileName = pUri.fileName.replace(".aspx", "");    
    
    return fileName;
}
//*****************************************************************************************
//******************** 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);
  
    //**************DEBUG************************

    //$("#container_debug").append("DirectoryPath:" + pUri.directoryPath[1].toLowerCase() + " length: " + pUri.directoryPath.length);
    //$("#container_debug").append("URIPath:" + pUri.domain + " length: " + pUri.domain.length);
    
    //**************DEBUG************************
    var ruta = window.location.toString() ;

    if (pUri) {
        switch (pUri.domain[2].toLowerCase()) {

                /**********  Modification made to display the Adtech banner en different 
                ***********  web sites depending in (spreadheet logic)

                /***********************************************************************/
                /************************* case moviecentralca  ************************
                /**********************************************************************/
            case "moviecentral.ca":
                siteName = "moviecentral";
                siteSection = "home";
                
                                
                //One SubDirectory
                if (pUri.directoryPath.length > 2 && typeof (pUri.directoryPath[1]) != "undefined") {

                    switch (pUri.directoryPath[1].toLowerCase()) {
                        case "groups":
                            siteSection = "shows";
                            break;
                        case "schedule":
                            siteSection = "schedule";
                            break;
                        case "blogs":
                            siteSection = "movies";
                            break;
                        case "central":
                            siteSection = "central";
                            break;
                        case "video":
                            siteSection = "video";
                            break;
                        default:
                            siteSection = "other";
                    }

                }

                //Two SubDirectorys
                if (pUri.directoryPath.length > 3 && typeof (pUri.directoryPath[2]) != "undefined") {

                    switch (pUri.directoryPath[2].toLowerCase()) {

                       case "testing":
                            siteSection = "testing";
                            break;
        
                    }
                }
                
                 //No directories in the pUri
                
                if (pUri.directoryPath.length < 3 && ruta.indexOf("default.aspx") != -1)
                  {
                  siteSection = "home";
                 }
               

                
                
                break;
                /***********************************************************************/
                /************************* case treehousetv.com ************************
                /**********************************************************************/
        
        
                case "treehousetv.com":
                siteName = "treehousetv";
                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;
                            
                        default:
                            siteSection = "other";
                    }

                }

                //Two SubDirectorys
                if (pUri.directoryPath.length > 3 && typeof (pUri.directoryPath[2]) != "undefined") {

                    switch (pUri.directoryPath[2].toLowerCase()) {

                        case "testing":
                            siteSection = "testing";
                            break;  
                      
                    }
                }
                break;
                
                /***********************************************************************/
                /************************* case myviva.ca ******************************
                /**********************************************************************/
        
        
                case "myviva.ca":
                siteName = "myviva";
                siteSection = "home";
                
                
                               
                //One SubDirectory
                if (pUri.directoryPath.length > 2 && typeof (pUri.directoryPath[1]) != "undefined") {

                    switch (pUri.directoryPath[1].toLowerCase()) {
                        case "movies":
                            siteSection = "movies";
                            break;
                        case "groups":
                            siteSection = "shows";
                            break;
                        case "content":
                            siteSection = "contest";
                            break;
                        case "video":
                            siteSection = "video";
                            break;
                        default:
                            siteSection = "other";
                    }

                }

                //Two SubDirectorys
                if (pUri.directoryPath.length > 3 && typeof (pUri.directoryPath[2]) != "undefined") {

                    switch (pUri.directoryPath[2].toLowerCase()) {

                        case "recipes":
                            siteSection = "recipes";
                            break;
                      
                    }
                }
                break;
                
                 /***********************************************************************/
                /************************* case encoreavenue.ca ************************
                /**********************************************************************/

        
                case "encoreavenue.ca":
                siteName = "encoreavenue";
                siteSection = "home";
                
                
                //No directories in the pUri
                
                if (pUri.directoryPath.length == 2)// && ruta.indexOf("index.asp") != -1)
                  {
                  if (ruta.indexOf("index.asp") != -1) {
                            siteSection = "home";
                            }
                  else if  (ruta.indexOf("movies.asp") != -1) {
                            siteSection = "other";
                            }
                  else if  (ruta.indexOf("premieres.asp") != -1) {
                            siteSection = "other";
                            }         
                  else if  (ruta.indexOf("subscribe.asp") != -1) {
                            siteSection = "other";
                            }
                  else if  (ruta.indexOf("eahd.asp") != -1) {
                            siteSection = "other";
                            }         
                  else if (typeof (pUri.directoryPath[0]) != "undefined")
                            {
                  siteSection = "home";
                            }
                  else
                           {
                           siteSection = "other";
                           }          
                  }
                if (pUri.directoryPath.length > 2)
                  {
                  siteSection = "other";
                  }
             
                break;                    
                /***********************************************************************/
                /************************* case dusk.ca  *******************************
                /**********************************************************************/

        
                case "encoreavenue.ca":
                siteName = "encoreavenue";
                siteSection = "home";
                
                
                //No directories in the pUri
                
                if (pUri.directoryPath.length == 2 && ruta.indexOf("default.aspx") != -1)
                  {
                  siteSection = "home";
                  }   
                  break;         
                /***********************************************************************/
                /************************* case nickcanada.com **************************
                /**********************************************************************/
        
        
                case "nickcanada.com":
                siteName = "nickcanada";
                siteSection = "home";
                
                
                //No directories in the pUri
                

                if (pUri.directoryPath.length  < 4 ) {
                 if (ruta.indexOf("show") != -1) {
                  siteSection = "shows";
                } else if (ruta.indexOf("schedule") != -1){
                  siteSection = "schedule";
                } else if (ruta.indexOf("contest") != -1){
                  siteSection = "contest";
                } else if (ruta.indexOf("video") != -1){
                   siteSection = "video";
                } else {
                   siteSection = "home";
                }
                                    }
                                    else
                                    {
                                   if (ruta.indexOf("show") != -1) {
                                          siteSection = "shows";
                                        } else if (ruta.indexOf("schedule") != -1){
                                          siteSection = "schedule";
                                        } else if (ruta.indexOf("contest") != -1){
                                          siteSection = "contest";
                                        } else if (ruta.indexOf("video") != -1){
                                           siteSection = "video";
                                        } else {
                                           siteSection = "other";
                                        }
                                    }




                break;
                 /***********************************************************************/
                /************************* case ytv.com ********************************
                /**********************************************************************/
        
        
                case "ytv.com":
                siteName = "ytv";
                siteSection = "home";
                
                               
                //One SubDirectory
                if (pUri.directoryPath.length > 2 && typeof (pUri.directoryPath[1]) != "undefined") {

                    switch (pUri.directoryPath[1].toLowerCase()) {
                        case "shows":
                            siteSection = "shows";
                            break;
                        case "games":
                            siteSection = "games";
                            break;
                        case "contests":
                            siteSection = "contest";
                            break;
                        case "videos":
                            siteSection = "video";
                            break;
                        default:
                            siteSection = "other";
                    }

                }

                //Two SubDirectorys
                if (pUri.directoryPath.length > 3 && typeof (pUri.directoryPath[2]) != "undefined") {

                    switch (pUri.directoryPath[2].toLowerCase()) {

                        case "shows":
                            siteSection = "shows";
                            break;  
                        case "play":
                            siteSection = "games";
                            break;  

                   
                    }
                }
                
                              
                break;
        
        }
    }
    
    
    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;
}

function GetNewUrl(pAdSize) {

    var NewAd = new Ad(pAdSize, "", "");
    var AdIndex = Ad.SetInstance(NewAd);

    return NewAd.AdUrl;
}

