/**
* Filename: rotatingbanner.js
* 
* This script provides the rotating banner functionality
* 
* Target Date   Change Date   Name           Description
* ------------  ------------  -----------    --------------
* 11/16/2007	11/12/2007	  Aby Tino		 Changed the check for landing URL for the youth banner
* 10/19/2007    10/08/2007    DAM            Fixed bug in goToAdd() from last change.
* 10/19/2007    10/03/2007    DAM            Refactored to take an input parameter object instead of reading the
*                                            the hidden fields.
* 09/14/2007    09/12/2007    DAM            Fixed ? bug created by url encoding.
* 09/14/2007    09/05/2007    DAM            Added UTF-8 decoding for banner hrefs.
* 09/14/2007	08/25/2007    DAM			 Added comments.  Changed thumbnails to point to number images.
*                                            Added the option of a like to be opened in a new window.
*                                            These changes were made by Pat Kelly and entered by me in the comments.
*/

// Global variables - set up bannerArray and incrementing behavior
var currentCount = 0;
var oldAd = 0;
var newAd = 0;
var masterOpacity = 1;
var defaultTimer = 2000;

var crossFadeInterval;
var startFadeTimeout;
var bannerArray;

// startFade (function, Arguments: none) The initialization trigger for a fade sequence.
// This sets elements up for the crossFade function, resets older elements, and clears the 
// startFadeTimeout timer which sets the duration between transitions.		
function startFade(){
	currentCount++;
	if(currentCount > bannerArray.length-1){currentCount = 0;}
	oldAd = newAd;
	newAd = currentCount;
	ad1 = document.getElementById(bannerArray[oldAd].id);
	ad2 = document.getElementById(bannerArray[newAd].id);
	ad1.style.zIndex = 999;
	setOpacity(ad2,0.01);
	hilite_tn(document.getElementById(ad1.id + "_thumbnail").firstChild,"deselect");
	hilite_tn(document.getElementById(ad2.id + "_thumbnail").firstChild,"select");
		
	ad2.style.zIndex = 1000;
	if(startFadeTimeout)window.clearTimeout(startFadeTimeout);
	crossFadeInterval = setInterval("crossFade()",30);
}

// setOpacity (function, Arguments: target Object, opacity value (as decimal .01 to .99))
// function simply sets opacity for several formats to ensure similar behavior, has checks for limits. 
function setOpacity(obj,value){
	if(document.all){
		obj.style.filter = "alpha(opacity=" + Math.ceil(value*100) + ")";
	}
	obj.style.opacity = (value >= 1)?0.99:((value <= 0)?0.01:value);
	obj.style.mozOpacity = (value >= 1)?0.99:((value <= 0)?0.01:value);
}
		

// crossFade (function, Arguments:none;)
// function called in the crossFadeInterval argument, set for every .03 seconds. 
// "adds" .02 opacity (plus or minus) to the fade. 
function crossFade(){
	ad1 = document.getElementById(bannerArray[oldAd].id);
	ad2 = document.getElementById(bannerArray[newAd].id);
	masterOpacity = masterOpacity - .02;
	setOpacity(ad2,(1-masterOpacity));
	
	if(masterOpacity <= 0){
		clearInterval(crossFadeInterval);
		ad1.style.zIndex = 1;
		setOpacity(ad2,1);
		masterOpacity = 1;
		hilite_tn(document.getElementById(ad1.id + "_thumbnail").firstChild,"deselect");
		hilite_tn(document.getElementById(ad2.id + "_thumbnail").firstChild,"select");
		if(startFadeTimeout)window.clearTimeout(startFadeTimeout);
		startFadeTimeout = (bannerArray[newAd].timer)?setTimeout("startFade()",bannerArray[newAd].timer):setTimeout("startFade()",defaultTimer);
		
		;
	}
}
		
// crossFadePause (function, Arguments: pause (true or false, depending on whether or not the startFadeTimeout
// is acting on mouseover (pause=true) which resets the crossFade parameters and snaps one ad to the front, 
// or mouseout (pause=false) which sets the timer to execute on the next interval (starts the fade behavior again) 
function crossFadePause(pause){
	if(pause){
		if(crossFadeInterval)clearInterval(crossFadeInterval);
		if(startFadeTimeout)window.clearTimeout(startFadeTimeout);
		ad1 = document.getElementById(bannerArray[oldAd].id);
		ad2 = document.getElementById(bannerArray[newAd].id);
		if(masterOpacity != 1){
			if(masterOpacity > 0){
				ad1.style.zIndex = 1;
				setOpacity(ad2,1);
				hilite_tn(document.getElementById(ad1.id + "_thumbnail").firstChild,"deselect");
				hilite_tn(document.getElementById(ad2.id + "_thumbnail").firstChild,"select");

				
			}else{
				setOpacity(ad1,1);
				ad1.style.zIndex = 1000;
				ad2.style.zIndex = 1;
			}
			masterOpacity = 1;
		}
	}else{
		masterOpacity = 1;
		startFadeTimeout = (bannerArray[newAd].timer)?setTimeout("startFade()",bannerArray[newAd].timer):setTimeout("startFade()",defaultTimer);
	}
}

// crossFadeInterrupt(function, Arguments: ad (numeric) - array index refers to the bannerArray[ad][property]
// used for clicking behavior on thumbnail images - kills transition, moves select to proper element, resets elements to rotate.
function crossFadeInterrupt(ad){
	if(crossFadeInterval)clearInterval(crossFadeInterval);
	if(startFadeTimeout)window.clearTimeout(startFadeTimeout);
	ad1 = document.getElementById(bannerArray[oldAd].id);
	ad2 = document.getElementById(bannerArray[newAd].id);
	ad3 = document.getElementById(bannerArray[ad].id);
	setOpacity(ad1,0);
	setOpacity(ad2,0);
	setOpacity(ad3,1);
	hilite_tn(document.getElementById(ad1.id + "_thumbnail").firstChild,"deselect");
	hilite_tn(document.getElementById(ad2.id + "_thumbnail").firstChild,"deselect");
	hilite_tn(document.getElementById(ad3.id + "_thumbnail").firstChild,"select");
	
	ad1.style.zIndex = 1;
	ad2.style.zIndex = 1;
	ad3.style.zIndex = 1000;
	currentCount = ad;
	oldAd = ad;
	newAd = ad;
}

// goToAd (function, Arguments: none) - handles the redirection onclick and appends statistics variables to the url
// modified to throw a popup window if the URL is prefixed with "popup_" 	
function goToAd(){
 if(bannerArray[newAd].landingPageUrl){
	var tempLocation = bannerArray[newAd].landingPageUrl;
	if(tempLocation.match(/\?/)){
	  tempLocation += "&";
	} else {
	  tempLocation += "?";
	}
	tempLocation += "order=" + (newAd + 1) + "," + ((bannerArray[newAd].timer)?bannerArray[newAd].timer:defaultTimer);
	if(!tempLocation.match("idg=")){
	  tempLocation += "&offerName=" + bannerArray[newAd].imageName;
	}
	if(bannerArray[newAd].popup){
		try{openHelpWnd(tempLocation);}catch(e){}
	}else{
		document.location.href = tempLocation;		  
	}
  }
}
	
// hilite_tn (function, arguments: obj (thumbnail), onoff (behavior is setting particular one to on, select, or off)
// modified to use classes for this element, avoid absolute image pathing. 
function hilite_tn(obj,onoff){
	if(onoff == "on"){
		obj.src = obj.selectImg; 
	}else if(onoff == "select"){
		obj.src = obj.selectImg; 
		obj.className = "select";
	}else if(onoff == "off"){
		if(!obj.className.match("select")){
			obj.src = obj.defaultImg; 
		}
	}else{
		obj.className = "";
		obj.src = obj.defaultImg;
	}
}
		
// buildBannerSet
function buildBannerSet(paramObj){
	try{
		// check if we have a valid parameter object
    	if(!paramObj) throw "noParameters";
    
    	// assign the banner array
		bannerArray = paramObj.banners;
	
    	// check if we have banners
    	if(!bannerArray || bannerArray.length == 0) throw "noBannerSpecified";
    
    	// validate bannerArray
    	for(i=0;i<bannerArray.length;i++){
        	if(!bannerArray[i]) throw "invalidBanner";
			if(!bannerArray[i].id || !bannerArray[i].id.match("banner")) throw "invalidBannerId";
			if(!bannerArray[i].imageSrc || !bannerArray[i].imageSrc.match("http")) throw "invalidBannerImage";
    		if(bannerArray[i].landingPageUrl){
				if(!bannerArray[i].landingPageUrl.match("http")){
					throw "invalidBannerUrl";
				}	
			}
    		if(!bannerArray[i].thumbnailImageSrc || !bannerArray[i].thumbnailImageSrc.match("http")) throw "invalidThumbnailImage";
    		if(!bannerArray[i].thumbnailSelectedImageSrc || !bannerArray[i].thumbnailSelectedImageSrc.match("http")) throw "invalidThumbnailSelectedImage";
    	}
    
        // set the default timer
        if(paramObj.defaultTimer){
          defaultTimer = parseInt(paramObj.defaultTimer);
        }

        // create the images
		var bannerContainer = document.getElementById("bannerContainer");
		var thumbnailContainer = document.getElementById("thumbnailSubContainer");
		if(bannerArray.length > 1){
			 bannerContainer.onmouseover= function (){crossFadePause(true)}
			 bannerContainer.onmouseout= function(){crossFadePause(false)}
		}
		bannerContainer.removeChild(document.getElementById("defaultBanner"));
		
		for(i=0;i<bannerArray.length;i++){
			
			var bannerDiv = document.createElement("DIV");
			var bannerImg = document.createElement("IMG");
			var thumbDiv = document.createElement("DIV");
			var thumbImg = document.createElement("IMG");
			
			bannerContainer.appendChild(bannerDiv);
			bannerDiv.appendChild(bannerImg);
			if(bannerArray[i].landingPageUrl){
				bannerDiv.onclick = goToAd;	
			}	
			else
			{
				bannerDiv.style.cursor = "default";
			}
			bannerDiv.id = bannerArray[i].id;
			bannerImg.src = bannerArray[i].imageSrc;
			bannerDiv.style.zIndex = (i > 0)?"1":"1002";
			bannerDiv.className = "bannerImage";
			
			if(bannerArray.length > 1){
				thumbnailContainer.appendChild(thumbDiv);
			 		thumbDiv.id = bannerArray[i].id + "_thumbnail";
			 		thumbDiv.className = "thumbnailImageContainer";
			 		thumbDiv.index = i;
			 		thumbDiv.onclick = function(){crossFadeInterrupt(this.index)};
			 		thumbDiv.appendChild(thumbImg);
			  			thumbImg.src = bannerArray[i].thumbnailImageSrc;
			  			thumbImg.defaultImg = bannerArray[i].thumbnailImageSrc;
			  			thumbImg.selectImg = bannerArray[i].thumbnailSelectedImageSrc;
			  			if(i==0){
			  				thumbImg.src = bannerArray[i].thumbnailSelectedImageSrc;
			  			}
			  			thumbImg.onmouseover= function(){hilite_tn(this,'on')};
			  			thumbImg.onmouseout= function(){hilite_tn(this,'off')};
			  			thumbImg.className = (i > 0)?"":"select";
			 }
		}
		document.getElementById("thumbnailContainer").style.zIndex = "10000";
		if(bannerArray.length > 1){
			startFadeTimeout = (bannerArray[0].timer)?setTimeout("startFade()",bannerArray[0].timer):setTimeout("startFade()",defaultTimer);
		}
	}catch(e){}
}
