addOnload(rolloverLinks);
addOnload(choosePic);

function addOnload(newFunction) {
	//sets up a variable oldOnload - if window.onload has already been set the value is stored here
	var oldOnload = window.onload;
	
	//if oldOnload is a function then its called    if it is a function do the following
	if (typeof oldOnload == "function") {
		
		//when the window loads run the function
		window.onload = function() {
			//if oldOnload is a function 
			if (oldOnload) {
				//run the function oldOnload
				oldOnload();
			}
			//then run the new function
			newFunction();
		}
	}
	//otherwise if oldOnload wasn't a function then when the window loads run the newFunction
	else {
		window.onload = newFunction;
	} 
}

//declares the function rolloverInit
function rolloverLinks() {
	//creates a for loop  finds the number of images
	for (var i=0; i<document.images.length; i++) {
		//if statement that says if the parent tag is an a tag
		if (document.images[i].parentNode.tagName == "A") {
			//then run the function setupRollover with the number of images being passed in
			setupRolloverLinks(document.images[i]);
		}
	}
}

//declares the function setupRollover with the parameter thisImage
function setupRolloverLinks(thisImage) {
	//
	thisImage.outImage = new Image();
	thisImage.outImage.src = thisImage.src;
	thisImage.onmouseout = function() {
		this.src = this.outImage.src;
	}

	thisImage.overImage = new Image();
	thisImage.overImage.src = "images/links/" + thisImage.id + "_rollover.png";
	thisImage.onmouseover = function() {
		this.src = this.overImage.src;
	}
}



//set up the array and the variable that contains the number of items in the array
var adImages = new Array("images/sites/realty_site.jpg","images/sites/bnb_site.jpg","images/sites/vend_site.jpg");
var thisAd = 0;

//sets up function
function choosePic() {
	//variable thisAd gets the value of a math expression    random number is generated and rounded down   multiplied by adImages.length
	thisAd = Math.floor((Math.random() * adImages.length));
	//source of the image adBanner is set based on the array adImages  and the value at this moment is dependent on the value of thisAd
	document.getElementById("adBanner").src = adImages[thisAd];
	
	rotate();
}
//declares function
function rotate() {
	//add one to thisAd value
	thisAd++;
	//checks to see if the value of thisAd is equal to the adImages.length array   if it is then set the value of thisAd back to 0
	if (thisAd == adImages.length) {
		thisAd = 0;
	}
	//the image that is being cycled has the id adBanner  says that the new sources for adBanner are in the array adImages  and the value of the variable thisAd defines which image the browser should use
	document.getElementById("adBanner").src = adImages[thisAd];

//tells the script how often to change the image in the banner   setTimeout lets you specify that an action should occur on a particular schedule   function rotate is called every 3 seconds
	setTimeout(rotate, 5 * 1000);
}

