//****************************************************
// File: lad_cycler.js  
//
// Purpose: Cycle image ads with a fade in/fade out effect.
//
// Written By: Chris Gentile
//****************************************************

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

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

ads[0] = new Object();
ads[0].href="http://www.energysmartli.com/index.php";
ads[0].alt="Energy Smart LI";

ads[1] = new Object();
ads[1].href="cooling.php";
ads[1].alt="Congratulations To Our A/C Inspection and Start-Up Winners!";

ads[2] = new Object();
ads[2].href="alternativeEnergy.php";
ads[2].alt="EmPower Solar!";

ads[3] = new Object();
ads[3].href="news.php";
ads[3].alt="We Just Made Upgrading Affordable!";

ads[4] = new Object();
ads[4].href="news.php";
ads[4].alt="Haven't heard about our Comfort Specialists?";

ads[5] = new Object();
ads[5].href="specials.php";
ads[5].alt="Get paid to boast!";

ads[6] = new Object();
ads[6].href="paymentOptions.php";
ads[6].alt="Tired of unexpected maintenance fees?";

ads[7] = new Object();
ads[7].href="payonline.php";
ads[7].alt="Pay your bills oline.";

ads[8] = new Object();
ads[8].href="services.php";
ads[8].alt="Ahhhhhh! That feels good!";

//ads[0] = new Object();
//ads[0].href="javascript:void(0)";
//ads[0].alt="We've Been Nominated! Best of Long Island 2011";

//****************************************************
// 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;
}
