// design Ad object
function Ad(fullfilepath, width, height, url, target, css)
{
	this.FullFilePath = fullfilepath;
	this.Width = width;
	this.Height = height;
	this.URL = url;
	this.Target = target;
	this.CSS = css;

	var period = this.FullFilePath.indexOf(".");
	// this is done to accommodate external flash script
	this.PartialFilePath = this.FullFilePath.substring(0, period);
}

function GetRandomAdBanner()
{
	// get random index
	var index = GetRandomNumber(0, ads.length);

	// set random ad
	var ad = ads[index];

	// if ad is flash
	if (IsFlash(ad))
		DisplayFlash(ad); // display as flash
	else // ad is image
		DisplayImage(ad); // display as image
}

function DisplayImage(ad)
{
	var output = "";

	// build html output
	output += "<a class='" + ad.CSS + "' target='" + ad.Target + "' href='" + ad.URL + "'>";
	output += "<img src='" + ad.FullFilePath + "' width='" + ad.Width + "' height='" + ad.Height + "' />";
	output += "</a>";

	// write to screen
	document.write(output);
}

function DisplayFlash(ad)
{
	var output = "";

	// build html output
	output += "<scr" + "ipt type='text/javascript'>";
	output += "AC_FL_RunContent( ";
	output += "'codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0',";
	output += "'width','" + ad.Width + "','height','" + ad.Height + "','src','" + ad.PartialFilePath + "','quality','high','pluginspage',";
	output += "'http://www.macromedia.com/go/getflashplayer','movie','" + ad.PartialFilePath + "');";
	output += "</scr" + "ipt>";
	output += "<noscr" + "ipt>";
	output += "<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' ";
	output += "codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0' ";
	output += "width='" + ad.Width + "' height='" + ad.Height + "'>";
	output += "<param name='movie' value='" + ad.FullFilePath + "' />";
	output += "<param name='quality' value='high' />";
	output += "<embed src='" + ad.FullFilePath + "' quality='high' ";
	output += "pluginspage='http://www.macromedia.com/go/getflashplayer' ";
	output += "type='application/x-shockwave-flash' width='" + ad.Width + "' height='" + ad.Height + "'></embed></object>";
	output += "</noscr" + "ipt>";

	// write to screen
	document.write(output);
}

function GetRandomNumber(min, max)
{
	// get random number between 0 and max
	return Math.floor((Math.random() + min) * max)
}

function IsFlash(ad)
{
	// get position of file extension
	var fileExt = ad.FullFilePath.length - 4;

	// if filename contains .SWF
	if (ad.FullFilePath.toLowerCase().indexOf(".swf") == fileExt)
		return true;
	else // file is not flash
		return false;
}
