// create object variable to hold map object
var varAdServer_TimeoutInterval = 10000;
var varAdServer_TimeoutMinimum = 10000;
var var_as_idTimeout=0;

if (window.attachEvent) {
	window.attachEvent("onload", fnInitialiseAdServer);
} else {
	window.addEventListener("load", fnInitialiseAdServer, false);
}


function fnInitialiseAdServer()
    {

    var as_arrDiv = new Array();
    var as_i=0;
    
    var as_objs = document.getElementsByTagName("DIV");
    
    for (as_y=0; as_y < as_objs.length; as_y++)
        {
        if (as_objs[as_y].className == "clsAdvert")
            {
            // found, add id's
            as_arrDiv[as_i] = as_objs[as_y].id;
            as_i++;
            }
        //end if
        }
    // next
    
    fnRandomAdvert(as_arrDiv);
    
    }

function fnRandomAdvert(as_arrDiv)
    {
    for(as_i=0;as_i < as_arrDiv.length;as_i++)
        {
        // kick off by using the timeout;
        // get random timeout
        
        // add event handlers to object
        
        var objAdHolder = document.getElementById(as_arrDiv[as_i]);
        
        objAdHolder.onmouseover = function(){fnOnAdServer_MouseOver();};
        objAdHolder.onmouseout = function(){fnOnAdServer_MouseOut(this.id);};
        
        as_varTimeOut = 10;
        setTimeout("fnGetNewAdvert('" + as_arrDiv[as_i] + "')", as_varTimeOut);
        }
    // next
    
    }
// end function

function fnGetNewAdvert(as_idDiv)
    {
	/* we now need to create an ajax call to go and get the XHTML to insert into the DIV */
	/* we'll sync the next call on it's call back */
	
	var as_http_request;
	if (window.XMLHttpRequest)
		{
		as_http_request = new XMLHttpRequest();
		as_http_request.overrideMimeType="text/xml";
		}
	else if (window.ActiveXObject)
		{
		try {as_http_request = new ActiveXObject("MSXML2.XMLHTTP");} 
		catch (as_e) 
			{try {as_http_request = new ActiveXObject("Microsoft.XMLHTTP");	} 
			catch (as_e) 
				{}
			}
		}
	// end if

	var as_objOutput;
	as_objOutput = document.getElementById(as_idDiv);
	
	var as_varURL;
	var as_varKey;
	
	as_varKey = as_idDiv.replace("idAdvert_", "");
	as_varURL = "/adserver/adholder.aspx?" + as_varKey;
			
    as_http_request.onreadystatechange = function()
        {
        if(as_http_request.readyState==4)
            {
            if(as_http_request.status==200)
                {
                as_objOutput.style.visibility="hidden";
                as_objOutput.innerHTML = as_http_request.responseText;
                as_objOutput.style.visibility="visible";
                
                // timeout for new advert is 20-40 seconds
				var as_varTimeOut = Math.random() * varAdServer_TimeoutInterval + varAdServer_TimeoutMinimum;
				var_as_idTimeout = window.setTimeout("fnGetNewAdvert('" + as_idDiv + "')", as_varTimeOut);	
                
                }
            else
                {
                as_objOutput.innerHTML = as_http_request.responseText;
                }
            // end if
            }
        // end if
        }

	
	// set true so that it is Asynchronous
	as_http_request.open("GET", as_varURL, true);
	as_http_request.send(null)

	}
	
// end function	
        
function fnOnAdServer_MouseOver()
    {
    // clear the timeout so the adverts don't keep changing
    if (var_as_idTimeout!=0)
        {
        window.clearTimeout(var_as_idTimeout);
        var_as_idTimeout = 0;        
        }
    // end if
    return(true);
    
    }
// end function

function fnOnAdServer_MouseOut(objID)
    {
    // replace the timeout
    if (var_as_idTimeout==0)
        {
        setTimeout("fnGetNewAdvert('" + objID + "')", 10);
        }
    // end if
    return(true);
    }        

