var interval = 3;
var random_display = 0; // 0 = no, 1 = yes
var isFirst = 1;
var direction = 0;
interval *= 1000;
var image_index = 0;

var isFinished = 0;

/// set up the image list
var image_list = new Array();

image_list[0] = new imageItem("images/04_smallbusinesscentre.gif", 179, 114, "http://www.sbcentre.ca/");
//image_list[1] = new imageItem("images/02_emage_gfx.gif", 179, 114, "http://www.emageplus.ca");
image_list[1] = new imageItem("images/03_LEDC.gif", 179, 114, "http://www.ledc.com/home/");
//image_list[3] = new imageItem("images/04_pricewaterhousecoopers.gif", 179, 114, "http://www.pwc.com/");
//image_list[4] = new imageItem("images/05_siskinds.gif", 179, 114, "http://www.siskinds.com/");
image_list[2] = new imageItem("images/06_stillercentre.gif", 179, 114, "http://www.stillercentre.com/");
image_list[3] = new imageItem("images/09_techalliance.gif", 179, 114, "http://www.techalliance.ca/");


var number_of_image = image_list.length;
var fadeTargetId = 'adImage'; /* change this to the ID of the fadeable object */

var	fadeTarget;
var preInitTimer;

function imageItem(image_location, img_width, img_height, img_href) 
{
	this.image_item = new Image();
	this.image_item.src = image_location;
	this.image_item.width = img_width;
	this.image_item.height = img_height;
	this.image_item.img_href = img_href;
}

function get_ImageItemLocation(imageObj) 
{
	return(imageObj.image_item.src)
}

function get_ImageItemWidth(imageObj) { return (imageObj.image_item.width); }
function get_ImageItemHeight(imageObj) { return (imageObj.image_item.height); }

function generateIT(x, y) 
{
	var range = y - x + 1;
	return Math.floor(Math.random() * range) + x;
}

function getNextImage() 
{
	if (random_display || isFirst == 1) 
	{
		image_index = generateIT(0, number_of_image-1);
		isFirst = 0;
	}
	else {
		image_index = (image_index+1) % number_of_image;
	}
	
	//var new_image = get_ImageItemLocation(image_list[image_index]);
	var new_image = image_list[image_index];

	return(new_image);
}

/* functions */

function preInit() {
	if ((document.getElementById)&&(fadeTarget=document.getElementById(fadeTargetId))) {
		fadeTarget.style.visibility = "hidden";
		if (typeof preInitTimer != 'undefined') clearTimeout(preInitTimer); /* thanks to Steve Clay http://mrclay.org/ for this small Opera fix */
	} else {
		preInitTimer = setTimeout("preInit()",2);
	}
}

function fadeInit() {
	if (document.getElementById) {
		preInit(); /* shouldn't be necessary, but IE can sometimes get ahead of itself and trigger fadeInit first */

		if (fadeTarget.style.MozOpacity!=null) {  
			/* Mozilla's pre-CSS3 proprietary rule */
			fadeTarget.style.MozOpacity = 0;
		} else if (fadeTarget.style.opacity!=null) {
			/* CSS3 compatible */
			fadeTarget.style.opacity = 0;
		} else if (fadeTarget.style.filter!=null) {
			/* IE's proprietary filter */
			fadeTarget.style.filter = "alpha(opacity=0)";
		}
		fadeTarget.style.visibility = 'visible';


		window.setTimeout("fadeIn(0)", 0);
	}
}

function fadeIn(opacity) {
	if (fadeTarget) {
		if (opacity < 100) {
			if (fadeTarget.style.MozOpacity!=null) {
				/* Mozilla's pre-CSS3 proprietary rule */
				fadeTarget.style.MozOpacity = (opacity/100)-.001;
			} else if (fadeTarget.style.opacity!=null) {
				/* CSS3 compatible */
				fadeTarget.style.opacity = (opacity/100)-.001;
			} else if (fadeTarget.style.filter!=null) {
				/* IE's proprietary filter */
				fadeTarget.style.filter = "alpha(opacity="+opacity+")";
				/* worth noting: IE's opacity needs values in a range of 0-100, not 0.0 - 1.0 */ 
			}
			opacity += 10;
			window.setTimeout("fadeIn("+opacity+")", 30);
		}
		else
		{
			opacity = 100;
			window.setTimeout("fadeOut("+opacity+")", interval-1000);
		}
	}
}

function fadeOut(opacity) {
	if (fadeTarget) {
		if (opacity > 0) {
			if (fadeTarget.style.MozOpacity!=null) {
				/* Mozilla's pre-CSS3 proprietary rule */
				fadeTarget.style.MozOpacity = (opacity/100)-.001;
			} else if (fadeTarget.style.opacity!=null) {
				/* CSS3 compatible */
				fadeTarget.style.opacity = (opacity/100)-.001;
			} else if (fadeTarget.style.filter!=null) {
				/* IE's proprietary filter */
				fadeTarget.style.filter = "alpha(opacity="+opacity+")";
				/* worth noting: IE's opacity needs values in a range of 0-100, not 0.0 - 1.0 */ 
			}
			opacity -= 10;

			window.setTimeout("fadeOut("+opacity+")", 30);
		}
		else
		{
			rotateImage('test');
		}
	}
}

function rotateImage(place) 
{
	document[place].style.visibility = 'hidden';
	var new_image = getNextImage();
	document[place].width = new_image.image_item.width;
	document[place].height = new_image.image_item.height;
	document[place].src = new_image.image_item.src;
	
	var el = document.getElementById('rotatingImage');
	el.href = new_image.image_item.img_href;
	
	/// set up the margins of the image
	var image = document.getElementById('adImage');
	var TOPMARGIN = 30;
	var BOXHEIGHT = 110;
	var IMAGEHEIGHT = new_image.image_item.height;

	var TOPMARGIN = BOXHEIGHT/2-IMAGEHEIGHT/2+TOPMARGIN;
	image.style.margin = TOPMARGIN+"px 0 0 0";
	
	fadeInit();
}


function loadRandomImage()
     {
     if(document.getElementById('splash'))
                 {
				 	var headerImg = document.getElementById('splash');
					headerImg.innerHTML = "<a href='rules.html'><img id='splash_img' src='images/Header_"+rnd(3)+".jpg' width='692' height='355' alt='Submit your business plan' /></a>";
                 }
     }
     
function rnd(n)
     {
     return Math.floor(Math.random() * n) + 1;
     }

window.onload = loadRandomImage;