/**
* 
* Javascript for ad retrieval and display
* called by flash when event occurs that may trigger an ad
 * params:
 *	actionId:String - the id of the action 
 *	infoType:String[optional] - the id of the info passed
 *	info:String[optional] - info associated with the event
 *
 * List of actionIds and the expected infoType
 * clickHour, hourIndex
 * clickTitle, songId
 * clickArtist, artistName
 * clickVersions, songId
 * clickBonusMusic, disabled
 * clickRedeem
 * openRecommendations
 * openSearch
 * clickSearch, artistName - will be empty if title search
 * clickPlaylist, type
 * sendCartToThirdParty, type 
 * sendSongToThirdParty, songId
 * logCartSong, songId
 * logSongListenedTo, songId
 *
*/

var productId = 'theHours';
var tagId = 'theHours';
var adElementId = 'adElement'; //Id of the element where the url will be displayed

var adWidth = 0; // set on page initialization in m2g.js
var adHeight = 0; // set on page initialization
var widgetWidth = 0; // set on page initialization
var widgetHeight = 0; // set on page initialization
var widgetBgColor = "FFFFFF";

// Events to enable ad auto refresh feature
window.onfocus=onFocusAction;
window.onblur=onBlurAction;

var adTimerId=0;

var windowFocus=false;
var readyToServeAd=false;
var adURL="";

/*
Function Name: widgetAction
Description: invoked by swf, passes call onto updateAd()
*/
function widgetAction(actionId, infoType, info){
	//clickEventHandler(actionId, infoType, info);	
}

/*
Function Name: clickEventHandler
Description: Function that takes three parameters actionId, infoType and info(optional) and prepares the ad url.
Parameter: actionId - Id of the action performed in the Flash widget. Inactivity case of 1 min is handled in auto refresh
		   infoType - information asssociated with actionId
		   info - Optional parameter containing the artistName.
Return: Void.
*/
function clickEventHandler(actionId, infoType, info) 
{
	var url; //Complete url of the ad
	var baseUrl = getDefaultAdURL();
	var infoObj = {};
	if (infoType == "songData") {
		infoObj = decodeJSON(info);
	}
	
	switch(actionId.toUpperCase())
	{
		case "WELCOME":
		case "CLICKHOUR":
		case "CLICKCHECKOUT":
		case "CLICKTITLE":
		case "LOGSONGLISTENEDTO":
			url = baseUrl;
		break;		
		case "CLICKARTIST":
			url = baseUrl + "&artistName="+escape(infoObj.artistName);				
		break;
		case "CLICKSEARCH":
			if(nullString(info))
				url = baseUrl;
			else
				url = baseUrl + "&artistName="+escape(info);				
		break;
		case "CLICKVERSIONS":
		case "CLICKBONUSMUSIC":
		case "CLICKREDEEM":
		case "OPENRECOMMENDATIONS":
		case "OPENSEARCH":
		case "CLICKPLAYLIST":
		case "SENDCARTTOTHIRDPARTY":
		case "SENDSONGTOTHIRDPARTY":
		case "LOGCARTSONG":
		case "USERACTIVITY":
		break;
	}
	
	adURL=url;
	readyToServeAd=true;
	serveAd("clickEventHandler-" + actionId); //Here we are assuming window will have focus. The windowfocus event is not fired in this case
}

/*
Function Name: getDefaultAdURL
Description: Function creates the default ad url
Parameters: None.
Return: url as string.
*/
function getDefaultAdURL()
{
	var url= getAdGateway() + "&productId="+productId+"&tagId="+tagId;
	return (url);
}

/*
Function Name: adAutoRefresh
Description: Function that handles the inactivity case i.e refreshes ad after a fixed refresh interval of 1 min based on window focus
Parameters: None.
Return:Void.
*/
function adAutoRefresh()
{
	adURL=getDefaultAdURL();
	readyToServeAd=true;
	if (windowFocus)
		serveAd("adAutoRefresh");
}

/*
Function Name: resetTimeout
Description: Function that sets the timer and then invoke the ad.
Parameters: None.
Return:Void.
*/
function resetTimeout()
{
	var adInterval=getadInterval();
	adTimerId=0;
	if (adInterval > 0) {
		adTimerId=setInterval("adAutoRefresh()", adInterval);
	}
}

/*
Function Name: serveAd
Description: Function gets invoked from User click, auto-refresh (if applicable), window-focus event.
Parameters: Calling function description for showing debugging message.
Return:Void.
*/
function serveAd(sCallingCode)
{
	var url="";
	
	if (readyToServeAd && (!nullString(adURL)))
	{	
		url=adURL;
		readyToServeAd=false;
		adURL=""
		clearInterval(adTimerId);
		
		if (window.frames[adElementId]) {
			window.frames[adElementId].location = url;			
		}
		//Following line should be uncommented for debugging
		//alert(sCallingCode + ": adURL=" + url);
		resetTimeout();
	}
}

/*
Function Name: onFocusAction
Description: Function gets invoked on window-focus event.
Parameters: None.
Return:Void.
*/
function onFocusAction(){
	windowFocus=true;
	serveAd("onFocusAction");
}

/*
Function Name: onBlurAction
Description: Function gets invoked on change in window-focus event.
Parameters: None.
Return:Void.
*/
function onBlurAction(){
	windowFocus=false;
}

/*
Function Name: getadInterval
Description: Function that returns the time to be used by setInterval method.
Parameters: None.
Return:int
*/
function getadInterval() {
	
	var defaultAutoRefreshInterval = 2 * 60 * 1000;  
	var adInt = adAutoRefreshInterval; //adAutoRefreshInterval is defined in m2g.js
	
	if (adInt > 0)
		return(adInt * 1000);
	else
		return defaultAutoRefreshInterval;
}

/*
Function Name: getAdGateway
Description: Returns the url for the Ad.
Parameters: None.
Return:String
*/
function getAdGateway() {
	return getGatewayDomain()+"m2g-api/adserve?stationId=" + getStation();
}

/*
Function Name: getAdHtml
Description: Returns HTML content of an ad.
Parameters: None.
Return:String
*/
function getAdHtml() {
	var result = "";
	result += '<div id="adcontent" align="center" style="height:'+adHeight+'px;width:'+widgetWidth+'px;background:#'+widgetBgColor+'">';
	result += '<IFRAME id="'+adElementId+'" name="'+adElementId+'" width="'+adWidth+'" height="'+adHeight+'" frameborder="0" marginheight="0" marginwidth="0" scrolling="no" src="dummyAd.html"></IFRAME>';
	result += '</div>';
	return result;
}
