﻿/*
    This script contains teh main functions to rorate ads (logos) on brad pages
    It is included in the logo.ascx and logoStrip.ascx files
    
    Two methods are available
    
    1) "UP" rotation between "places" i.e. available ads move between spots on a page (used on title classification pages)

    2) "ACROSS" rotation within a place, which keeps items distinct to a location i.e. intellagencia home page, has 5 logos, 3 might be static (single items) the others rotate
        The term across comes from the recordset grouping, to imagine the extra rows wiating off the side (for a given spot)
*/

var ad;
var iArr = 0;
var showing = new Array(-1,-1,-1,-1,-1,-1,-1,-1);

var iShowing = 0; // only used in the "Up" version
var localArr;

var bDebugStatus = false;

// var arrAds - NOW passed in
// arrAds = new Array()

/*
    array of data passed in "arrData" which contains all items (comma separated)
    and each item has the full logo properties within (pipe separated)
*/
function unpack(arrName, arrData)
{
    var i;
    for (i=0; i<arrData.length; i++)
    {
        var strData = arrData[i]; // property text
        var adData = strData.split("|");
        if (adData.length >=5 )
        {        
            var ad = new adItem(adData[0], adData[1], adData[2], adData[3], adData[4], adData[5])
	        arrName[i] = ad;
//	        alert('titleID: '+ad.titleID);
        }
    }
//    alert('unpacked: '+arrName.length);

}

function addItem(theArr, imgPath, titleID, titleName, idx, xtraUrl)
{ // argument[0] is the array to add the item to
	var ad = new adItem(imgPath, titleID, titleName, idx, xtraUrl);
	theArr[iArr] = ad;
	iArr = theArr.length;
	//maxAds = iArr;
	alert('addItem: titleID: '+ad.titleID);
}

function adItem(i,t,tn,idx,u)
{
	this.image = i; // fullpath returned from proc
	this.img = new Image();
	this.img.src=this.image;
	this.titleID = t;
	this.idx = idx;
	this.titleName = tn;
	if (u)
	{
		this.url = '/hitcount.asp?id='+t+'&t=logo&url=' + u;
	}
	else
	{
		this.url = '/hitcount.asp?id='+t+'&t=logo&url=/showtitle.asp?titleID='+t;
	}
	idx++;
	return this;
}

function rotateAdsUp(theArr, pFix)
{
	var htmlStr='';
	var iItem;

    if (theArr)
    { localArr = theArr; } 
    else
    { theArr = localArr; } 
    

	iShowing = iShowing + 1;
	if (iShowing>10000) {iShowing = 0;}
	for (iArr=0; iArr<theArr.length; iArr++)
	{
		var o = window.document.getElementById(pFix + 'idLOGO'+eval(iArr+1));
		if (o)
		{
			//alert('idLOGO'+eval(iArr+1) +'\n' +iArr + '\n' + iShowing);
			iItem = eval(iShowing+iArr) % (theArr.length);
			htmlStr+='<a class="cLINK" href="'+theArr[iItem].url+ '" target="_blank"><img src="'+theArr[iItem].image+'" border="0"><br /><img src="http://www.intellagencia.com/img/spacer.gif" width="1" height="5" border="0"><br />'+theArr[iItem].titleName+'</a><br /><br />';
			o.innerHTML = htmlStr;
			htmlStr='';
		}
	}
	if (theArr.length>1)
	{
		setTimeout("rotateAdsUp(null,'"+pFix+ "')", 4000);
	}
}

function rotateAdsAcross(theArr, adPos, pFix)
{
	var htmlStr='';
	var iItem;

//    alert('adPos: ' + adPos);
    if (adPos)
    {
        if (theArr==null)
        {
            theArr = eval('arrLogo' + adPos);
        }
    }
    else
    {
        return(false);
    }

	showing[adPos] = eval(showing[adPos] + 1);
	if (showing[adPos]>10000) {showing[adPos] = 0;}
	var o = window.document.getElementById(pFix + 'idLOGO'+eval(adPos));
	if (o)
	{
		iItem = eval(showing[adPos]) % (theArr.length);
		if (bDebugStatus) 
		    {if(adPos==1) {window.status = 'showing arrPos: ' + iItem + ' showing ' + showing[adPos];}}
		    
		if (iItem<=theArr.length)
		{
		    //alert(theArr.length + '\n' + iShowing);
		    htmlStr+='<a class="cLINK" href="'+theArr[iItem].url+ '" target="_blank"><img src="'+theArr[iItem].image+'" border="0"><br /><img src="http://www.intellagencia.com/img/spacer.gif" width="1" height="5" border="0">';
		    if (theArr[iItem].titleName!='')
		    {
    		    htmlStr+='<br />'+theArr[iItem].titleName+'</a><br /><br />';
    		}
		    o.innerHTML = htmlStr;
		    htmlStr='';
        }
	}
    else
    {
        if (bDebugStatus)
        {
            window.status = 'Element not found: ' + 'idLOGO'+eval(adPos);
        }
    }

	if (theArr.length>1)
	{
		setTimeout("rotateAdsAcross(null,"+eval(adPos)+",'"+pFix+ "')", 4000);
	}
}

function startRotate(theArr,rotateMethod,adPos,pFix)
{
	for (iArr=0; iArr<theArr.length; iArr++)
	{
		var o = window.document.getElementById('idTD'+eval(iArr+1));
		if (o)
		{
			if (o.style)
			{

				o.style.border='1px dashed #c0c0c0';

				o.style.paddingTop='2px';
			}
		}
	}
	if(rotateMethod=='Up')
	{
    	rotateAdsUp(theArr, pFix);
    }
    else
	{
    	rotateAdsAcross(theArr,adPos,pFix);
    }
    
}

/*
~ ACROSS SPECIFIC
*/

var arrLogo1  = new Array();
var arrLogo2  = new Array();
var arrLogo3  = new Array();
var arrLogo4  = new Array();
var arrLogo5  = new Array();
var arrLogo6  = new Array();
var arrLogo7  = new Array();
var arrLogo8  = new Array();

function adServer_Across(iMax, pFix)
{
    var eachArr;
    if(pFix==undefined) {pFix=''}
    for (eachArr=1;eachArr<(iMax+1);eachArr++)
    {
        if (eval('arr'+eachArr))
        {
            unpack(eval('arrLogo'+eachArr), eval('arr'+eachArr));
            startRotate(eval('arrLogo'+eachArr),'across',eachArr, pFix);
        }
    }
}

/*
~ UP/GROUP SPECIFIC
*/


var arrLogosLogos  = new Array();

function adServer_Up(iMax, pFix)
{
    if(pFix==undefined) {pFix=''}
    if (eval('arrLogos'))
    {
        unpack(eval('arrLogosLogos'), eval('arrLogos'));
        startRotate(eval('arrLogosLogos'),'Up', null, pFix);
    }
}
 
