
//Begin GlobalVariables
var oRegionHeaderSlideshow = new RegionHeaderSlideshow();

var oSlideTracker = new SlideTracker();

var _TYPE_HOMESLIDE = "HomeSlide";
var _TYPE_PROMOSLIDE = "PromoSlide";
var _TYPE_HOMESLIDESET = "HomeSlideSet";
//end GlobalVariables

//Begin RegionHeaderSlideshow Object
function RegionHeaderSlideshow()
{
	this.homeSlides = new Array();
	this.promoSlides = new Array();
	
	this.numSlides = 0;
	this.maxSlidesInSingleType = 0;
}
RegionHeaderSlideshow.prototype.AddHomeSlide = RegionHeaderSlideshow_AddHomeSlide;
RegionHeaderSlideshow.prototype.AddPromoSlide = RegionHeaderSlideshow_AddPromoSlide;
RegionHeaderSlideshow.prototype.AddSlideToTypeSpecificArray = RegionHeaderSlideshow_AddSlideToTypeSpecificArray;

function RegionHeaderSlideshow_AddPromoSlide(sImagePath, sImageLink)
{
	var oNewSlide;

	//Tell slidetracker that this Region has a promo slide
	oSlideTracker.AddSlideType(_TYPE_PROMOSLIDE);
	
	//Create a new promo slide
	oNewSlide = new PromoSlide(sImagePath, sImageLink);

	//Add it to the promoslides array
	this.AddSlideToTypeSpecificArray(oNewSlide, _TYPE_PROMOSLIDE);	
}

function RegionHeaderSlideshow_AddHomeSlide(iGroupNumber, sImagePath, sImageLink)
{
	var bSlideSetFound = false;
	var oSetToAddSlideToTypeSpecific;
	var oNewSlide;

	//Tell slidetracker that this Region has a Home slide
	oSlideTracker.AddSlideType(_TYPE_HOMESLIDE);

	//check to see if the slide group for the slide being added already exists
	for(var i=0; i < this.homeSlides.length; i++)
	{
		var oSlideSet = this.homeSlides[i];

		if(oSlideSet.type == _TYPE_HOMESLIDESET)
		{
			if(oSlideSet.groupNumber == iGroupNumber)
			{
				oSetToAddSlideToTypeSpecific = oSlideSet;
				bSlideSetFound = true;
				break;
			}	
		}

	}
	
	//Add the slide group for this slide if it has not yet been added.
	if(bSlideSetFound == false)
	{
		oNewSlide = new HomeSlideSet(iGroupNumber);
		
		oSetToAddSlideToTypeSpecific = this.AddSlideToTypeSpecificArray(oNewSlide, _TYPE_HOMESLIDESET);
	}
	
	//add the slide to the slide group
	oSetToAddSlideToTypeSpecific.AddHomeSlide(sImagePath, sImageLink);
}

function RegionHeaderSlideshow_AddSlideToTypeSpecificArray(oNewSlide, sSlideType)
{
	var iHomeSlidesArrayLength = this.homeSlides.length;
	var iPromoSlidesArrayLength = this.promoSlides.length;
	
	switch(sSlideType)
	{
		case _TYPE_HOMESLIDESET:
			//add the slide to the home slide array
			this.homeSlides[iHomeSlidesArrayLength] = oNewSlide;
			
			//track how many unique slides exist for this region
			this.numSlides = this.numSlides + 1;
			
			//keep track of which type of slide has the most slides for this region.
			//this is used to balance the slide counts so that they are interlaced.
			if(this.homeSlides.length > this.maxSlidesInSingleType)
			{
				this.maxSlidesInSingleType = this.homeSlides.length;
			}
			
			return this.homeSlides[iHomeSlidesArrayLength];
		
		case _TYPE_PROMOSLIDE:
			//add the slide to the promo slide array
			this.promoSlides[iPromoSlidesArrayLength] = oNewSlide;
			
			//track how many unique slides exist for this region
			this.numSlides = this.numSlides + 1;
			
			//keep track of which type of slide has the most slides for this region.
			//this is used to balance the slide counts so that they are interlaced.
			if(this.promoSlides.length > this.maxSlidesInSingleType)
			{
				this.maxSlidesInSingleType = this.promoSlides.length;
			}
			
			return this.promoSlides[iHomeSlidesArrayLength];
	}

}

//end RegionHeaderSlideshow Object


//Begin HomeSlideSet Object
function HomeSlideSet(iGroupNumber)
{
	this.type = _TYPE_HOMESLIDESET;
	
	this.homeSlides = new Array();
	this.groupNumber = iGroupNumber;
}

HomeSlideSet.prototype.AddHomeSlide = HomeSlideSet_AddHomeSlide;


function HomeSlideSet_AddHomeSlide(sImagePath, sImageLink)
{
	var iArrayLength = 0;
	
	iArrayLength = this.homeSlides.length;

	//if this slide group has 4 or less homeslides then add this homeslide to the slide group
	if(iArrayLength <= 4)
	{
		this.homeSlides[iArrayLength] = new HomeSlide(sImagePath, sImageLink);
	}
}
//End HomeSlideSet Object

//Begin HomeSlide Object
function HomeSlide(sImagePath, sImageLink)
{
	this.type = _TYPE_HOMESLIDE;
	
	this.imagePath = sImagePath;
	
	if(sImageLink == "")
	{
		sImageLink = "#";
	}
	
	this.imageLink = sImageLink;
}
//End HomeSlide Object

//Begin PromoSlide Object
function PromoSlide(sImagePath, sImageLink)
{
	this.type = _TYPE_PROMOSLIDE;
	
	this.imagePath = sImagePath;
	this.imageLink = sImageLink;
}
//End PromoSlide Object

//Begin SlideTracker Object
function SlideTracker()
{
	this.availableSlideTypes = new Array();
	this.currentSlideType = "";
	this.nextSlideType = "";
	this.currentSlideTypeIndex = -1;
	this.numSlidesToDisplay = 0;
}

SlideTracker.prototype.GetNextSlideType = SlideTracker_GetNextSlideType;
SlideTracker.prototype.AddSlideType = SlideTracker_AddSlideType;
SlideTracker.prototype.SlideTypeAlreadyAvailable = SlideTracker_SlideTypeAlreadyAvailable;
SlideTracker.prototype.GetNextSlide = SlideTracker_GetNextSlide;
SlideTracker.prototype.BalanceSlideCount = SlideTracker_BalanceSlideCount;

function SlideTracker_AddSlideType(sSlideType)
{
	var bSlideTypeAlreadyAvailable = false;
	
	//check to see if this slide type has already been initialized for this region
	bSlideTypeAlreadyAvailable = this.SlideTypeAlreadyAvailable(sSlideType);
	
	//if the slide type has not been initialized, add it now
	if(bSlideTypeAlreadyAvailable == false)
	{
		switch(sSlideType)
		{
			case _TYPE_HOMESLIDE:
				this.availableSlideTypes[this.availableSlideTypes.length] = new HomeSlideType();
				
				break;
			
			case _TYPE_PROMOSLIDE:
				this.availableSlideTypes[this.availableSlideTypes.length] = new PromoSlideType();
				break;
		}
		
		//sort the available types so that higher priority types are displayed first
		this.availableSlideTypes.sort(comparePriority);
	}
}

function SlideTracker_SlideTypeAlreadyAvailable(sSlideType)
{
	for(var i=0; i < this.availableSlideTypes.length; i++)
	{
		if(sSlideType == this.availableSlideTypes[i].slideType)
		{
			return true;
		}
	}
	
	return false;
}

function SlideTracker_GetNextSlideType()
{	
	//get the next slide type...starting over at the begining of the list if necessary
	if(this.currentSlideTypeIndex + 1 == this.availableSlideTypes.length)
	{
		this.currentSlideTypeIndex = 0;
	}
	else
	{
		this.currentSlideTypeIndex = this.currentSlideTypeIndex + 1;
	}
	
	return this.availableSlideTypes[this.currentSlideTypeIndex];
}


function SlideTracker_GetNextSlide()
{
	var oNextSlideType = this.GetNextSlideType();
	var iCurSlideIndex = oNextSlideType.nextSlideIndex;
	
	switch(oNextSlideType.slideType)
	{
		case _TYPE_HOMESLIDE:
			//Get the next slide of this type so that it can be written to the page
			//if all of the slides of this type have been used already, start over at the begining and display the first one again
			if(iCurSlideIndex < oRegionHeaderSlideshow.homeSlides.length)
			{
				oNextSlideType.nextSlideIndex = oNextSlideType.nextSlideIndex + 1;

				return oRegionHeaderSlideshow.homeSlides[iCurSlideIndex];
			}
			else
			{
				oNextSlideType.nextSlideIndex = 0;
				
				return oRegionHeaderSlideshow.homeSlides[oNextSlideType.nextSlideIndex];
			}
			
		case _TYPE_PROMOSLIDE:
			//Get the next slide of this type so that it can be written to the page
			//if all of the slides of this type have been used already, start over at the begining and display the first one again
			if(iCurSlideIndex < oRegionHeaderSlideshow.promoSlides.length)
			{
				oNextSlideType.nextSlideIndex = oNextSlideType.nextSlideIndex + 1;
				
				return oRegionHeaderSlideshow.promoSlides[iCurSlideIndex];
			}
			else
			{
				oNextSlideType.nextSlideIndex = 0;
				
				return oRegionHeaderSlideshow.promoSlides[oNextSlideType.nextSlideIndex];
			}
	}
}

function SlideTracker_BalanceSlideCount()
{
	this.numSlidesToDisplay = oRegionHeaderSlideshow.maxSlidesInSingleType * this.availableSlideTypes.length;
}


function comparePriority(a,b) { return a.priority - b.priority; }

//End SlideTracker Object

//Begin PromoSlideType Object
function PromoSlideType()
{
	this.slideType = _TYPE_PROMOSLIDE;
	this.nextSlideIndex = 0;	
	this.priority = 2;
}
//End PromoSlideType Object

//Begin HomeSlideType Object
function HomeSlideType()
{
	this.slideType = _TYPE_HOMESLIDE;
	this.nextSlideIndex = 0;
	this.priority = 1;
}
//End HomeSlideType Object

