/* constants */
var intMinFlashVersion = 6
var canBeFiltered = false


/* Container for all bannercollections */
var bannerCollections = new Array()



/* ****************************************************************************
   Name: Bannercollections 
   Object-constructor Used to store collections of banners and to process rotation
**************************************************************************** */
function BannerCollection(strTargetContainer, arrBanners, intDelayTime, intWidth, intHeight, intNumberOfBannersToDisplay, intClickable)
{
	this.strTargetContainer = strTargetContainer
	this.arrBanners = arrBanners
	this.intDelayTime = intDelayTime
	this.intWidth = intWidth
	this.intHeight = intHeight
	this.intNumberOfBannersToDisplay = intNumberOfBannersToDisplay
	this.intCurrentBanner = Math.round(Math.random()*(arrBanners.length-1))
	this.intClickable = intClickable
} //BannerCollection


/* ****************************************************************************
   Name: Bannercollections.prototype.fnLoadNextBanner
   Used to set and load the next banner
**************************************************************************** */
BannerCollection.prototype.fnLoadNextBanner = function()
{
	this.intCurrentBanner++
	if (this.intCurrentBanner >= this.arrBanners.length)
	{
		this.intCurrentBanner = 0
	}
	
	var i
	var intBanner = this.intCurrentBanner - 1
	for (i = 0; i < this.intNumberOfBannersToDisplay; i++)
	{
		if (this.arrBanners[this.intCurrentBanner].fnCanPlay())
		{
			intBanner++
			if (intBanner >= this.arrBanners.length)
			{
				intBanner = 0
			}		
			this.arrBanners[intBanner].fnLoadHtml(this.strTargetContainer + "_" + i, this.intWidth, this.intHeight, this.intClickable)
		}
		else
		{
			this.fnLoadNextBanner()
		}
	}
} //Bannercollections.prototype.fnLoadNextBanner


/* ****************************************************************************
   Name: Banner
   Object-constructor to store banner-data
**************************************************************************** */
function Banner(strName, strDescription, strUrl, strImageUrl, btFlash, strTarget, btCommercial)
{
	this.strName = strName
	this.strDescription = strDescription
	this.strUrl = strUrl
	this.strImageUrl = strImageUrl
	this.strTarget = strTarget
	this.btCommercial = btCommercial
	
	
	if (btFlash==1)
	{
		if(detectFlash)
		{
			this.data = new FlashObject(strImageUrl, this.strName, 100, 100, intMinFlashVersion, "#FFFFFF")
			this.data.addParam("strUrl", this.strUrl)
			this.data.addParam("strDescription", this.strDescription);
			this.data.addParam("wmode", 'opaque')
		}
	}
	else
	{
		this.data = new Image()
		this.data.src = strImageUrl
	}
	
	this.btFlash = btFlash
} // Banner


/* ****************************************************************************
   Name: Banner.prototype.fnLoadHtml
   Writes a banner into to the html-container
**************************************************************************** */
Banner.prototype.fnLoadHtml =  function(strTargetContainer, intWidth, intHeight, intClickable)
{
	var spnRef = document.getElementById( strTargetContainer + "_spn")
	var linkRef = document.getElementById( strTargetContainer + "_href")
	var imgRef = document.getElementById( strTargetContainer + "_img")
	var flashRef = document.getElementById( strTargetContainer + "_flash")
	
	if (this.btCommercial == "1")
	{
		spnRef.className = "contBannerItem bnrIsCommerce"	
	}
	else
	{
		spnRef.className = "contBannerItem"
	}
	
	if (this.btFlash == 0)
	{
		if(flashRef != null)
			flashRef.style.display = "none"
		imgRef.src = this.data.src	
		
		if (intHeight > 0)
			imgRef.height = intHeight
		if (intWidth > 0)
			imgRef.width = intWidth
		
		imgRef.title = this.strDescription
		
		if (intClickable == 1)
		{			
			linkRef.style.display = "inline"		
			linkRef.href = this.strUrl
			linkRef.title = this.strDescription
			linkRef.target = this.strTarget
		}		
	
	}
	else
	{
		linkRef.style.display = "none"
		flashRef.style.display = "block"
		
		if (intHeight > 0)
			this.data.height = intHeight
		if (intWidth > 0)
			this.data.width = intWidth
		
		this.data.write( strTargetContainer + "_flash")
	}
	
}  //Banner.prototype.fnLoadHtml

/* ****************************************************************************
   Name: Banner.prototype.fnLoadHtml
   Checks if a banner can be displayed in the current browser
**************************************************************************** */
Banner.prototype.fnCanPlay =  function()
{
	if (this.btFlash==0)
	{
		return true
	}
	else
	{
		return detectFlash(intMinFlashVersion)
	}
} //Banner.prototype.fnCanPlay

/* ****************************************************************************
   Name: fnShowBanners
   Initializes a bannercontainer and starts rotation
**************************************************************************** */
function fnShowBanners(strTargetContainer, arrBanners, intDelayTime, intWidth, intHeight, intFadeTime, intNumberOfBannersToDisplay, intClickable, strStyle, strResourceUrl)
{
	//create new collection
	if (intNumberOfBannersToDisplay >= arrBanners.length)
	{
		intNumberOfBannersToDisplay = arrBanners.length
	}
	
	bannerCollections[strTargetContainer] = new BannerCollection(strTargetContainer, arrBanners, intDelayTime, intWidth, intHeight, intNumberOfBannersToDisplay, intClickable, strStyle)
	var strHtml = ""	
	var i
	for (i = 0; i < intNumberOfBannersToDisplay; i++)
	{ 			
		//class=\"bnrIsCommerce\"
		strHtml += "<span id=\"" + strTargetContainer + "_" + i + "_spn\" >";
		if (intClickable == 1)
		{
			strHtml += "<a href=\"#\" id=\"" + strTargetContainer + "_" + i + "_href\" class=\"" + strStyle + "\" style=\"display:none\">";
		}
		strHtml += "<img src=\"" + strResourceUrl + "/spacer.gif\" border=\"0\" title=\"\" id=\"" + strTargetContainer + "_" + i + "_img\" >";
		
		if (intClickable == 1)
		{
			strHtml += "</a>";
		}
		strHtml += "<span id=\"" + strTargetContainer + "_" + i + "_flash\"  style=\"display:none\"></span>";
		if (strStyle == "hrefBanner")
		{
			strHtml += "<span class=\"contTxtCommercial\" ><em>(advertentie)</em></span>";
		}
		strHtml += "</span>";
				
	}
	
	var container = document.getElementById(strTargetContainer)
	container.innerHTML = strHtml
	
    if (document.getElementById(strTargetContainer + "_0_img").style && document.getElementById(strTargetContainer + "_0_img").filters && intFadeTime > 0)
    {
      canBeFiltered = true     
    }

	if (canBeFiltered)
	{
		FadeTime = intFadeTime
		for (i = 0; i < intNumberOfBannersToDisplay; i++)
		{
			document.getElementById(strTargetContainer + "_" + i + "_img").style.filter="blendTrans(duration=" + intFadeTime + ")";
			document.getElementById(strTargetContainer + "_" + i + "_img").filters.blendTrans.Apply(); 
		}
	}
	  	
	//start bannerRotaion if at least one banner can play
	var boolCanPlay = false
	for (var i = 0; i<arrBanners.length;i++)
	{
		if(arrBanners[i].fnCanPlay())
			boolCanPlay = true
	}
	
	if(boolCanPlay)
		fnShowNextBanner(strTargetContainer, intFadeTime, intNumberOfBannersToDisplay)
} //fnShowBanners

/* ****************************************************************************
   Name: fnShowNextBanner
   A timed method to execute banner-rotation
**************************************************************************** */
function fnShowNextBanner(strTargetContainer, intFadeTime, intNumberOfBannersToDisplay)
{
	bannerCollections[strTargetContainer].fnLoadNextBanner()
	
	if (canBeFiltered)
	{
		var i
		for (i = 0; i < intNumberOfBannersToDisplay; i++)
		{
			document.getElementById(strTargetContainer + "_" + i + "_img").style.filter="blendTrans(duration=" + intFadeTime + ")";
			document.getElementById(strTargetContainer + "_" + i + "_img").filters.blendTrans.Apply(); 
			document.getElementById(strTargetContainer + "_" + i + "_img").filters.blendTrans.Play();
		}
	}
	//Rotation isn't neccacery if there aren't more banners than the displayed ones
	if (bannerCollections[strTargetContainer].intDelayTime > 0)
	{
		if (intNumberOfBannersToDisplay != bannerCollections[strTargetContainer].arrBanners.length)
			setTimeout("fnShowNextBanner('" + strTargetContainer +"', '" + intFadeTime +"', '" + intNumberOfBannersToDisplay +"');", bannerCollections[strTargetContainer].intDelayTime)
	}
	
}