/*****************************************************************************************
This creates Arraylist to store all the location and ids of iframe.
ReferAd refreshes location of frame provided by id.
RefreshAll refreshes all the location of all Iframes.
ShowAdd refreshes the location of the Ads using timeout event @assigned interval
*****************************************************************************************/

var iframeList=new Array();
var rand = 0;
var allAds=false;

function iframeClass(id,url)
{
    this.id=id;
    this.url=url;
}
function showAdd(Ads, url, interval, loadFirstTime)
{   
    var pos = addtoIframeList(Ads, url);
    var tilePosRegExp = /tile=$/i;
    if(loadFirstTime!=null && loadFirstTime)
    {
        /*ensure adding the pos value to tile key if the url last attribute is tile= and no value*/
        if (tilePosRegExp.test(url)==true)
        {
            getElem(Ads).src=url + pos + ';ord='+rand+'?';
        }
        else
        {
            getElem(Ads).src=url + ';ord='+rand+'?';
        }
        if(interval>0)
            setTimeout(function(){showAdd(Ads,url,interval)},interval,true);
     }
}

function addtoIframeList(id,url)
{
    if(iframeList.length==0)
    {
        rand = Math.random()*100000000000000000;
    }
    for(var i=0; i<iframeList.length; i++)
    {
        if(iframeList[i].id == id)
        {
            if(allAds == false)
            {   
                rand = Math.random()*100000000000000000;
            }
            return i+1;
        }
    }
    var obj=new iframeClass(id,url);
    iframeList[iframeList.length]=obj;
    return iframeList.length;
}
function refreshAd(id,url)
{
    showAdd(id,url,0,true);
}
function refreshAllAds()
{
    rand = Math.random()*100000000000000000;
    allAds=true;
    for(var i=0;i<iframeList.length;i++)
    {
        showAdd( iframeList[i].id, iframeList[i].url, 0, true);
    }
    allAds=false;
}
function refreshAdById(id)
{    
    if(iframeList!=null)
    for(var i=0;i<iframeList.length;i++)
    {
        if(iframeList[i].id == id)
        {
            showAdd(id,iframeList[i].url,0,true);
        }
    }
}
function getElem(id)
{
    if(document.all)
        return document.all(id);
    else 
        return document.getElementById(id);
}