/*
 * flowplayer_tracking.js
 * This JavaScript include contains functions to track SpotXchange video ads being played back
 * using FlowPlayer (http://www.flowplayer.org).
 */
var beaconsHit = [];
// This function sends tracking information back to SpotXchange
function hitBeacon(event, extra_data)
{
	// Make sure we have tracking data for this event
	if(spotx_ad.tracking[event])
	{
		if(typeof(extra_data) == 'undefined')
			extra_data = "";

		var tracking_urls = spotx_ad.tracking[event].urls;
		for(var i=0; i < tracking_urls.length; ++i)
		{
			var img = new Image();
			img.src = tracking_urls[i] + extra_data;
			beaconsHit.push(img);
		}
	}
};

var lastTimeBeaconed = null;
var midpointBeaconed = false;
// This function is called every second while a SpotXchange video ad is
// playing.  It calls hitBeacon to send the current play head location back
// to SpotXchange for reporting purposes.
function doRecurringBeacon()
{
	var player = document.getElementById("FlowPlayer");

	var time = player.getTime();
	if(time != lastTimeBeaconed)
	{
		hitBeacon("recurring", "&timerendered="+time);
	}

	if(midpointBeaconed == false)
	{
		if(time > (spotx_ad.duration / 2))
		{
			hitBeacon("midpoint");
			midpointBeaconed;
		}
	}
};

var spotx_beacon_interval = null;
function onLoadBegin(clip)
{
	// Is FlowPlayer starting to play a SpotXchange video ad?
	var clipUrl;
	if(typeof(clip.baseUrl) == 'undefined') clipUrl = clip.fileName;
	else if(clip.fileName == 'null') clipUrl = clip.baseUrl + '/';
	else clipUrl = clip.baseUrl + '/' + clip.fileName;

	if(clipUrl == spotx_ad.url && spotx_beacon_interval == null)
	{
		hitBeacon("start");
		spotx_beacon_interval = setInterval(doRecurringBeacon, spotx_ad.tracking.recurring.interval * 1000);
		lastTimeBeaconed = null;
		midpointBeaconed = false;
	}
};

function onClipDone(clip)
{
	// If the beacon interval is set then we need to finish our tracking and
	// clear out the interval.
	if(spotx_beacon_interval != null)
	{
		hitBeacon("complete");
		clearInterval(spotx_beacon_interval);
		spotx_beacon_interval = null;
	}
};

var spotx_ad;
// Handle the ad returned by spotx, this could come directly from included spotx
// javascript or from a server-side syndication.
function spotx_request_complete(ad)
{
	// if ad is null we have no ad, otherwise use the ad data to show the ad
	var fo = new SWFObject(
	    "/resources/flash/FlowPlayerClassic.swf", "FlowPlayer", "400", "355", "9", "#fff", true
	  );

	var video_url = escape('http://cdn.spotxchange.com/website/content_for_demos.flv');
    var strPlaylist = "", strLoop = "false";
	if(ad)
	{
		spotx_ad = ad;
		var clip = "{ url: '"+escape(ad.url)+"', linkUrl: '"+escape(ad.link)+"' }";

		if(ad.ad_type == 'postroll')
		{
		    strPlaylist = "playList: [ { url: '" + video_url + "' }, " + clip + " ]";
		}
		else
		{
		    // it is a pre-roll, so lets loop to avoid white screen of death
		    strLoop = "true";
			strPlaylist = "playList: [ " + clip + ", { url: '" + video_url + "' } ]";
		}

		// Show the banner
		document.getElementById("banner").innerHTML = ad.banner_html;
	}
	else
	{
		strPlaylist = "videoURL: '" + video_url + "'";
	}
	
    fo.addVariable(
        "config", 
        "{ " + strPlaylist + ", loop: " + strLoop + ", showFullScreenButton: false, showMenu: false, showScrubber: false }"
        );
	fo.addParam("allowScriptAccess", "always");
	fo.write("player");
};
