

//globals
var aryPositions;
var aryAdDivs = new Array();
var adCounter = 0;
//alert(adCounter);

//classes
function clsPosition(PositionName,PositionWidth,PositionHeight,Tile){
    this.Name = PositionName;
    this.Width = PositionWidth;
    this.Height = PositionHeight;
    this.Tile = Tile;
}

function clsAd(objPosition,strDivName)
{
    this.Position = objPosition;
    this.DivName = strDivName;
}

//functions
function InitPositions()
{
    if (aryPositions == null)
    {
        aryPositions = new Array()
        aryPositions['Top'] = new clsPosition('Top',728,90,1);
        aryPositions['Bottom'] = new clsPosition('Bottom',728,90,'');
        aryPositions['Right'] = new clsPosition('Right',120,600,'');
        aryPositions['Right1'] = new clsPosition('Right1',160,600,2);
        aryPositions['Left1'] = new clsPosition('Left1',171,340,'');
        aryPositions['Left2'] = new clsPosition('Left2',180,340,3);
        aryPositions['Middle1'] = new clsPosition('Middle1',336,280,4);
        aryPositions['Middle2'] = new clsPosition('Middle2',336,280,5);
        aryPositions['Middle3'] = new clsPosition('Middle3',336,280,6);
        aryPositions['Middle4'] = new clsPosition('Middle4',336,280,7);
        aryPositions['Prestitial'] = new clsPosition('Prestitial',600,400,'');
        aryPositions['300x250'] = new clsPosition('300x250',300,250,8);
        aryPositions['Sponsor'] = new clsPosition('Sponsor',176,112,9);
        aryPositions['Sponsor2'] = new clsPosition('Sponsor',160,80,10);
        aryPositions['Sponsor3'] = new clsPosition('Sponsor',300,75,11);
    }
}

function GetPositions()
{
    InitPositions();
    return aryPositions;
}

var randomNumberForAds = Math.floor(Math.random() * 10000000);

function InjectAd(objAd)
{
    var width = objAd.Position.Width;
    var height = objAd.Position.Height;
	//alert(objAd.DivName);
    $(objAd.DivName).innerHTML = 
    
            '<IFRAME ID="iframe' + objAd.Position.Name + '" onLoad="LoadNextAd();" WIDTH=' + width + ' HEIGHT=' + height + ' MARGINWIDTH=0 MARGINHEIGHT=0 HSPACE=0 VSPACE=0 FRAMEBORDER=0 SCROLLING=NO BORDERCOLOR="#FFFFFF" BGCOLOR="BLACK" ALLOWTRANSPARENCY="true" ' +
                ' src="http://ad.doubleclick.net/adi/' + DC_ZoneName + ((objAd.Position.Name == 'Top') ? (';dcopt=ist') : ('')) + ';tile=' + objAd.Position.Tile + ';pos=;sz=' + width + 'x' + height + ';!category=default;' + DC_Keywords + 'name=;title=;ord=' + randomNumberForAds + '?">' +
            '</iframe>';
}

function Show_Ad(PositionName)
{
    //this just puts the placeholder in place - the LoadAds function then populates the placeholder with an ad after the page loads
    var objPosition = GetPositions()[PositionName];
    
    var width = objPosition.Width;
    var height = objPosition.Height;

    var divName = "divAd_" + PositionName;

    document.write("<div id='" + divName + "' class='AdPlaceHolder' style='width:" + width + "px; height:" + height + "px;'></div>");
    
    var objAd = new clsAd(objPosition,divName);
    
    aryAdDivs.push(objAd);
    adCounter++;
    //alert(objAd.DivName + '_' + adCounter);
    //alert(adCounter);
}

var globalTileIndex = 1;
function LoadNextAd() 
{
	if (globalTileIndex <= aryAdDivs.length)
	{
		InjectAd(aryAdDivs[globalTileIndex-1]);
		globalTileIndex++;
	}
 }

function LoadAds()
{
    globalTileIndex = 1;
    LoadNextAd();

    //exclude safari
    if (navigator.userAgent.toLowerCase().indexOf("safari")==-1)
    {
        setTimeout(LoadAds,300000); //refresh ads every 5 minutes    
    }
}

addLoadEvent(LoadAds);

function GetIframeDivID(iframeURL)
{
    var intStart = iframeURL.indexOf("tile=");
    var intEnd;
    var intTile;
    var objPositions;
    var strDivID;
    
    if (intStart != -1)
    {
        intStart += 5;
        intEnd = iframeURL.indexOf(";", intStart);
        
        if (intEnd != -1)
        {
            intTile = iframeURL.substring(intStart, intEnd);
            
            objPositions = GetPositions();
            
            for (var strPosition in objPositions)
            {
                if (objPositions[strPosition].Tile == intTile)
                {
                    strDivID = "divAd_" + objPositions[strPosition].Name;
                    break;
                }
            }
        }
    }
    
    return strDivID;
}

function setIframeWidth(iframeURL, newWidth)
{
    document.getElementById(getIframeDivID(iframeURL)).width = newWidth;
}

function setIframeHeight(iframeURL, newHeight)
{
    document.getElementById(getIframeDivID(iframeURL)).height = newHeight;
}
