var SwedenBIO = {

	init : function (){
		if (document.getElementById){
			runRandomImage();
		}
	},
	
	end : function (oEvent){
		SwedenBIO = null;
		delete SwedenBIO;
	}
}

function ajaxRequest()
{
    var xmlHttp;
    try
    {
        xmlHttp = new XMLHttpRequest();
    }
    catch(e)
    {
        var XmlHttpVersions = new Array('MSXML2.XMLHTTP.6.0',
                                        'MSXML2.XMLHTTP.5.0',
                                        'MSXML2.XMLHTTP.4.0',
                                        'MSXML2.XMLHTTP.3.0',
                                        'MSXML2.XMLHTTP',
                                        'Microsoft.XMLHTTP');
        for (i = 0; i < XmlHttpVersions.length && !xmlHttp; i++)
        {
            try
            {
                xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
            }
            catch(e) {}
        }
    }
    if (!xmlHttp)
    {
        //alert("Your browser may not support XML Requests");
    }
    else
    {
        return xmlHttp;
    }
}

// Get the random image
function getRandomImage()
{
    // The id of the div you want your images to be displayed in
    var imageDisplayDiv = "result";
    if (document.getElementById(imageDisplayDiv)!=null) {
        // The name of the file the images will come from
        var imageFile = "/SwedenBioTemplates/Pages/RandomLogo.aspx";
        // Starts a new request
        var ajax = ajaxRequest();
        // Gets the out put of the image file by sending nothing and just getting its finished product
        ajax.open("GET", imageFile, true);
        ajax.send(null);    
        // Cycles through the ajax state change
        ajax.onreadystatechange = function()
        {
            // if the ajax request is ready
            if (ajax.readyState == 4)
            {
                // Checks if it really is ready by checking status number , 200 = done
                if (ajax.status == 200)
                {
                // writes the image files output to the div
                    var body = ajax.responseText;
                    var searchStr = "=\"../../";
                    r = new RegExp(searchStr, 'g');
                    replaceStr = "=\"/";
                    body = body.replace(r, replaceStr);
                                      
                    document.getElementById(imageDisplayDiv).innerHTML = body;
                }
            }
        }
    }
}

// Runs the script
function runRandomImage()
{
    getRandomImage(); // Runs image function once
    setTimeout("runRandomImage()",5000);
}

function addEvent(oObj, evType, fn, useCapture){
	if (oObj.addEventListener){
		oObj.addEventListener(evType, fn, useCapture);
		return true;
	} else if (oObj.attachEvent){
		var r = oObj.attachEvent("on"+evType, fn);
		return r;
	} else {
		alert("Handler could not be attached");
	}
}

addEvent(window, "load",function(){SwedenBIO.init();})
addEvent(window, "unload", function(){SwedenBIO.end();}, false);


