// Variables
var obj1, obj2, cycle, fadeInCycle, fadeOutCycle, ads;

// Populate the ads array
ads = new Array();

ads[0] = new Object();
ads[0].href="possiBill.php";
ads[0].alt="PossiBill: Bill Presentment, Payment and Paperless Billing Solution";

ads[1] = new Object();
ads[1].href="onPoint.php";
ads[1].alt="OnPoint: Loyalty Rewards Marketing Program";

ads[2] = new Object();
ads[2].href="http://www.getpricepoint.com/index.php";
ads[2].alt="Price Point: The Real-Time Price Protection Enrollment System";



//****************************************************
// Method: StartAdCycle  
//
// Purpose: Entry point of script/sets up objects.
//****************************************************
function StartAdCycle(id1, id2)
{
	obj1 = document.getElementById(id1);
	obj1.opacity = 1;
	obj1.fadeIn = true;
	obj1.fadeOut = false;
	obj1.ad = 1;

	obj2 = document.getElementById(id2);
	obj2.opacity = 0;
	obj2.fadeIn = false;
	obj2.fadeOut = true;
	obj2.ad = 2;
		
	cycle = setInterval(cycleImages, 12000);
}

//****************************************************
// Method: StartAdCycle  
//
// Purpose: Entry point of script/sets up objects.
//****************************************************
function cycleImages()
{
	obj1.fadeIn = !obj1.fadeIn;
	obj1.fadeOut = !obj1.fadeOut;
	
	obj2.fadeIn = !obj2.fadeIn;
	obj2.fadeOut = !obj2.fadeOut;
	
	fadeInCycle = setInterval(fadeIn, 50);
	fadeOutCycle = setInterval(fadeOut, 50);
}

//****************************************************
// Method: fadeIn  
//
// Purpose: Fades an obj to 100% opacity.
//****************************************************
function fadeIn()
{
	var obj = (obj1.fadeIn == true ? obj1 : obj2);
	
	if (obj.opacity >= .5)
		obj.parentNode.href = ads[obj.ad - 1].href;
	if (obj.opacity >= 1)
	{
		clearInterval(fadeInCycle);
		obj.opacity = 1;
	}
	else
		obj.opacity += .05;
	setOpacity(obj);
}

//****************************************************
// Method: fadeOut  
//
// Purpose: Fades an obj to 0% opacity.
//****************************************************
function fadeOut()
{
	var obj = (obj1.fadeOut == true ? obj1 : obj2);
	
	if (obj.opacity <= 0)
	{
		clearInterval(fadeOutCycle);
		obj.opacity = 0;
		swapImage(obj);
	}
	else
		obj.opacity -= .05;
	setOpacity(obj);
}

//****************************************************
// Method: setOpacity
//
// Purpose: Sets the opacity on an object.
//****************************************************
function setOpacity(obj)
{
	obj.style.opacity = obj.opacity;
	if (obj.filters)
		obj.filters.item("alpha").opacity = obj.opacity * 100;
}

//****************************************************
// Method: swapImage 
//
// Purpose: Changes the ad associated with the obj.
//****************************************************
function swapImage(obj)
{
	if (obj.ad % 2 == 0)
	{
		if (obj.ad < ads.length - 2)
			obj.ad += 2;
		else
			obj.ad = 1;
	}
	else
	{
		if (obj.ad < ads.length - 1)
			obj.ad += 2;
		else
			obj.ad = 2;
	}
	
	obj.src = "images/ads/home_page_ad_" + obj.ad + ".jpg";
	obj.alt = ads[obj.ad - 1].alt;
}
