//////////////////////////////////////////////////////////
/// Script Purprose: Generate Helios IQ ad tags - DNN 4.3
/// Version: 0.1
/// Created By: Dimitry Chertkov(2009)
//////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////
/// SET DEFAULT VARIABLES FOR GENERIC STATION SETTINGS
//////////////////////////////////////////////////////////
var DEFAULT_SITE_NAME     = "default";
var DEFAULT_AD_SIZE		  = "1x1";
var DEFAULT_ZONE          = "other";
var DEFAULT_HOMEPAGEZONE  = "home";   


//////////////////////////////////////////////////////////
// Cross-browser JavaScript support fuctions
//////////////////////////////////////////////////////////

//indexOf() function not supported by IE
if(!Array.indexOf)
{
    Array.prototype.indexOf = function(obj, start)
    {
        for(var i=(start||0); i <this.length; i++)
        {
            if(this[i]==obj)
            {
                return i;
            }
        }
        return -1;
    }
}

//////////////////////////////////////////////////////////
/// Ad Object
//////////////////////////////////////////////////////////

function Ad(site, aliasZone, keys) 
{
    this.site = (site)?site:DEFAULT_SITE_NAME;                              //site Attribute
    
    this.aliasZone = (aliasZone)?aliasZone:Ad.convertPathToZoneName();      //aliasZone Attribute

    if(keys)Ad.Keys = new Keys(keys,DEFAULT_AD_SIZE);                       //keys
    
	//page root path to be sent as key word parameter - capped at 60 characters 
    this.rootPath = (document.location.hostname != "undefined"  && document.location.hostname != "")?(document.location.pathname.toLowerCase()).substr(0,60):""; 
	
	//page tabID to be sent as key word parameter
    this.tabID = Ad.getTabID();
	
    this.adGroupId = Ad.getAdGroupID();                                     //groupID - used to goup all tags on the same page together 
    
    this.offset = Ad.getTimeZoneOffset();                                   //Timezone Offset
    
    this.write = write;                                                     //write ad tag method         
    
}
//////////////////////////////////////////////////////////
/// Ad Vars
//////////////////////////////////////////////////////////

//Site Subsection Identifiers
Ad.subsectID = {
    shows           :       ['/djsandshows','/showlist'],
    fun             :       ['/funstuff'],
    events          :       ['/concertsandevents'],
    music           :       ['/music'],
    news            :       ['/newsInfo','/news'],
    contest         :       ['/contests'],
    player          :       ['/shared/player','/shared/corus_content/corus_player'],
    video_player    :       ['/media'],
    corus_shopping  :       ['/shoppingexperience']
}

//Site Homepage Identifiers
Ad.homeID = ['/home/','/','','/index.aspx','/home/default.aspx'];

//Backup Ad IDs
Ad.BackUpId = {
    "1x1"       : "1203131",
    "1x2"       : "1203130",
    "100x100"   : "1203129",
    "120x90"    : "1203123",
    "130x50"    : "1203132",
    "150x90"    : "1203128",
    "160x600"   : "1203127",
    "170x76"    : "1203126",
    "180x150"   : "1203141",
    "180x180"   : "1203137",
    "200x100"   : "1203139",
    "240x100"   : "1203142",
    "240x90"    : "1203136",
    "25x25"     : "1203124",
    "300x250"   : "1203135",
    "300x40"    : "1203133",
	"320x50" 	: "1203138",
    "500x500"   : "1203143",
    "545x90"    : "1203147",
    "592x90"    : "1203146",
    "728x90"    : "1203148",
    "950x100"   : "1203145",
    "950x150"   : "1203144"
}

//////////////////////////////////////////////////////////
/// Ad Functions
//////////////////////////////////////////////////////////

Ad.convertPathToZoneName = function() 
{
    var ads_zone_name = DEFAULT_ZONE;

    if(document.location.hostname != "undefined"  && document.location.hostname != "")
    {
        var root_path = document.location.pathname.toLowerCase(); 
          
        if(Ad.homeID.indexOf(root_path)>-1)
        {
            ads_zone_name = DEFAULT_HOMEPAGEZONE ; 
        }
        else
        {
            var found = 0; //break out of loop if subsection ID found 
            for(n in Ad.subsectID)
            {   
                for(m in Ad.subsectID[n])
                {
                    try
                    {
                       if(root_path.search(Ad.subsectID[n][m])>-1)
                       {
                           ads_zone_name = n ;
                           found=1;
                           break;
                       } 
                    }  
                    catch(err)
	                {
	                    continue;
	                }
                             
                }
            if(found)break;           
            }
            
        }
    }
    return ads_zone_name;    
}

Ad.getTimeZoneOffset = function()
{
    var curDateTime = new Date(); 
    var offset = -(curDateTime.getTimezoneOffset()); 
    offset = (offset>0)?"+" + offset:offset;
    return offset
}

Ad.getTabID = function()
{ 
      var name = "tabid";
	  var regexS1 = "[\\?&]"+name+"=([^&#]*)";
	  var regexS2 = "[\/]"+name+"[\/]([0-9]*)[\/]";
	  var regex1 = new RegExp( regexS1 );
	  var regex2 = new RegExp( regexS2 );
	  var results = regex1.exec( window.location.href.toLowerCase());
	  if( results == null )
		results = regex2.exec( window.location.href.toLowerCase());
	  if( results == null )
	     return "";
	  else
		return results[1];
}

Ad.getAdGroupID = function()
{
    return Math.round(Math.random()*1000000);
}


//write adtag
function write(adSize,aliasZone,keys,keywords,kwlp)
{     
        //build ad alias
        var alias = this.site+"_";
        
	    alias += (aliasZone)?aliasZone + "_":this.aliasZone + "_";  //add alias zone 
	    
	    alias += (adSize)?adSize:DEFAULT_AD_SIZE;                   //add alias adSize
	    
	    
	    var newAd = new Ad(this.site,alias);                        //add ad instance to Ad.adTags
	    var adTagIndex = Ad.SetInstance(newAd);
	    //set instance size
	    newAd.adSize = (adSize)?adSize:DEFAULT_AD_SIZE;
	    //set instance alias
	    newAd.alias = alias;
	    //set instance keys
	    if(keys)Ad.setKeys(adTagIndex,keys);
      
        
        			
	    document.write('<scr'+'ipt language="javascript1.1" src="http://adserver.adtechus.com/addyn/3.0/5233.1/' + Ad.BackUpId[adSize] + '/0/-1/ADTECH;alias=' + alias + ';size=' + adSize + ';' + "key=" + this.rootPath + "+" + this.tabID + eval('(this.Keys)?this.Keys.print()+";":";"') + 'cookie=info;loc=100;target=_blank;grp=' + this.adGroupId + ';misc='+new Date().getTime()+';aduho='+this.offset+';"></scri'+'pt>');
}

//Add and instance of the Ad Object to the adTags array
Ad.SetInstance = function( adTag )
{
	if( ! Ad.adTags )
	{
		Ad.adTags = new Array();
	}
	
	Ad.adTags.push( adTag );
	
	return Ad.adTags.length - 1;
}

Ad.setKeys = function(i,keys)
{
    if(Ad.adTags[i].Keys)
	{
	    Ad.adTags[i].Keys.addKeys(keys);
	}
	else
	{
	    Ad.adTags[i].Keys = new Keys(keys); 
	}    
}
///////////////////////////////////////////////////////////////
/// Keys Object - keys param must be formatted "key+key+key+key"
///////////////////////////////////////////////////////////////
function Keys(keys)
{ 
    
    //local variables
    var maxLength = 8;      //max number of keys
    var newKeys = keys.split("+"); //temp keys array
    
    //create array of key objects
    this.key = new Array(); 
    for(i=0;i<newKeys.length;i++)
    {
        this.key[i]= new Key((this.key.length+1),newKeys[i]); 
    }
    
    //function - returns a Helios formatted list of keys. Up to 'maxLength' keys long 
    this.print = function()
    {
        var tagKeys = "+";
		var keysFound = 0;
		//find tag specific keys
        for(i=0;i<this.key.length && i<maxLength;i++)
        {
            tagKeys += this.key[i].value;
            tagKeys += (i<this.key.length-1)?"+":"";
			keysFound += 1;
		}
		//if there are generic keys available then add them to the tagKeys string
		if(keysFound<maxLength && Ad.Keys.key.length > 0)
		{
				var keySpotsRemaining = maxLength-keysFound;
				var numGenericKeys = Ad.Keys.key.length;
				var numGenericKeysToAdd = (keySpotsRemaining>numGenericKeys)?numGenericKeys:(numGenericKeys-keySpotsRemaining);
				
				for(i=0;i<numGenericKeysToAdd;i++)
				{
					tagKeys += Ad.Keys.key[i].value;
					tagKeys += (i<numGenericKeysToAdd-1)?"+":"";
				}
		}
		
        return tagKeys;
    }
    
    //function - add new keys
    this.addKeys = function(keys)
    {
        var newKeys = keys.split("+");
        for(i=0;i<newKeys.length;i++)
        {
            this.key.unshift(new Key((this.key.length+1),newKeys[i]));
        }     
    }

}

//////////////////////////////////////////////////////////
/// Key Object
//////////////////////////////////////////////////////////
function Key(num,value)
{
   this.num = num;
   this.value = value;
}



