var videoPlayerReady;
var currentPID;
var internal;
var PID;

////////////////////////////////////////////////////////////////////////////////////////
// calls from js to flash

/**
 * 
 * external interface call for playing a new release url
 * 
 * @param releaseURL		the new release url to load in
 * @param autoPlayVideo		whether to autoplay or not
 * @return					a boolean whether the release was able to load. 
 * 							if an ad is playing and the controlls are disabled 
 * 							then the release will not load
 * 
 */
function videoPlay (releaseURL, autoPlayVideo) {
	if(videoPlayerReady == true) {
		$('4KidsPlayer').extPlay(releaseURL, autoPlayVideo);
		currentPID = PID;
	}
}

/**
 * pauses the video player
 * @param pauseIt	set to 'true' to pause, false to resume
 */
function videoPause (pauseIt) {
	if(videoPlayerReady == true) {
		$('4KidsPlayer').extPause(pauseIt);
	}
}

/**
 * seek to a postiion in the currently playing video
 * @param seekPos	the time to seek to in milliseconds
 */
function videoSeek(seekPos) {
	if(videoPlayerReady == true) {
		$('4KidsPlayer').extSeek(seekPos);
	}
}

/**
 * @return the current volume level in range of 0 - 100
 */
function videoGetVolume() {
	if(videoPlayerReady == true) {
		inlineAlert( 'videoGetVolume() '+ $('4KidsPlayer').extGetVolume() );
		return $('4KidsPlayer').extGetVolume();
	}
}

/**
 * @return the current position in the video in milliseconds
 */
function videoGetPosition() {
	if(videoPlayerReady == true) {
		inlineAlert( 'getPosition() '+ $('4KidsPlayer').extGetPosition() );
		return $('4KidsPlayer').extGetPosition();
	}
}


////////////////////////////////////////////////////////////////////////////////////////
//received calls from flash to js

var videoPlayerReady;

function videoPlayerHello() {
	videoPlayerReady = true;
	inlineAlert('hello: flash video player ready. flash <-> javascript integration initialized');
}

/**
 * called when release is stopped (paused)
 */
function onVideoStop() {
	inlineAlert('onVideoStop');
}

/**
 * called when release starts and resumes form pause
 */
function onVideoStart() {
	inlineAlert('onVideoStart');
}

/**
 * called when release ends
 */
function onVideoEnded() {
	inlineAlert('onVideoEnded');
}

// CALLED WHEN AN AD STARTS PLAYING.
// banners is an array of Banner objects as described in the actionscript "com.theplatform.pdk.data.Banner"
// see PDK documentation for details  
function onAdStart(banners) {
	var msg = "-----------------------------\n";
	msg += "onAdStart:";
	for(var i=0; i < banners.length; i++){
		msg += "\nbanner " + i;
		msg += "\n    testAd: " + banners[i].testAd;
		msg += "\n    host: " + banners[i].host;
		msg += "\n    discarded: " + banners[i].discarded;
		msg += "\n    loaded: " + banners[i].loaded;
		msg += "\n    isMaxSize: " + banners[i].isMaxSize;
		msg += "\n    bannerSize: " + banners[i].bannerSize;
		msg += "\n    bannerHeight: " + banners[i].bannerHeight;
		msg += "\n    bannerWidth: " + banners[i].bannerWidth;
		msg += "\n    bannerType: " + banners[i].bannerType;
		msg += "\n    region: " + banners[i].region;
		msg += "\n    src: " + banners[i].src;
		msg += "\n    target: " + banners[i].target;
		msg += "\n    alt: " + banners[i].alt;
		msg += "\n    href: " + banners[i].href;
		
		syncRoadBlock(banners[i].src);
	}
	inlineAlert(msg);
}


function inlineAlert(msg){
	document.getElementById('msgText').value += msg + "\n";
}

function syncRoadBlock(adTag){
	//alert(adTag);
	a = adTag.split(';');
	if (a.length>0)
	{
            for (x=0; x<=a.length-1; x++)
			{
				if (a[x].indexOf('sz=') == 0)
				{
					size = a[x].substring(3);
                    dims = size.split('x');
                    height = dims[0];
                    width = dims[1];
					adTag = adTag.replace("pfadx", "adi");

					loadRBs('myAd',height,width,adTag);
                }
            }
	}
}
function loadRBs(s,w,h,adTag){
    document.getElementById(s).innerHTML = '<iframe src="' + adTag + '" id="ifr_companion" width="'+w+'" height="'+h+'" marginwidth=0 marginheight=0 hspace=0 vspace=0 frameborder=0 scrolling=no>' + '</iframe>';
}