﻿

var MOA_CURRENT_FRAME = 0;
var MOA_POPUP_CONTAINER_ID = 'moaPopupContainer';
var MOA_POPUP_CURTAIN_ID = 'moaPopupCurtain';
var MOA_POPUP_INNER_ID = 'moaPopup';
var MOA_POPUP_HEIGHT = 415;

var MOVIE_EMBED_DIV_LARGE = 'moaFlashContentPopup';
var MOVIE_NAME_LARGE = 'moalarge';
var MOVIE_PATH_LARGE = '/swf/moa/HCP_playerLG.swf';
var CONFIG_PATH_LARGE = '/swf/moa/configurationLg.xml';

var MOVIE_EMBED_DIV_SMALL = 'flash-content';
var MOVIE_NAME_SMALL = 'moasmall';
var MOVIE_PATH_SMALL = '/swf/moa/HCP_playerSm.swf';
var CONFIG_PATH_SMALL = '/swf/moa/configurationSm.xml';

var t;								//timeout variable
var LARGE_MOVIE_DELAY = 1000; //delay in milliseconds before playing large swf.



//Embed small swf
var params = { wmode: "transparent", allowScriptAccess: "sameDomain" };
var attributes = { id: MOVIE_NAME_SMALL, name: MOVIE_NAME_SMALL };
var flashvars = { configPath: CONFIG_PATH_SMALL };
swfobject.embedSWF(MOVIE_PATH_SMALL, MOVIE_EMBED_DIV_SMALL, "470", "270", "8.0.0", "", flashvars, params, attributes);

//Initially embed large movie, so we can cache it.
embedLargeMovie(); 

//Re-embeds large swf.
function embedLargeMovie() {
	//Embed large swf
	attributes = { id: MOVIE_NAME_LARGE, name: MOVIE_NAME_LARGE };
	flashvars = { configPath: CONFIG_PATH_LARGE };
	swfobject.embedSWF(MOVIE_PATH_LARGE, MOVIE_EMBED_DIV_LARGE, "662", "414", "8.0.0", "", flashvars, params, attributes);
}

//Closes the div containing the large swf.
function closeMoa(pos){

	//Persist the frame number.
	saveFlashPosition(pos);

	//Call external interface function for small swf to advance small swf to correct frame.
	advanceTimeline(MOVIE_NAME_SMALL, pos);

	//Hide large movie
	$('#' + MOA_POPUP_CONTAINER_ID).hide();

}

//Wrapper function that calls Flash function via external interface
//pos: Flash movie frame number
function advanceTimeline(moviename, pos) {

	//alert('position is: ' + pos);

	//Call external interface function for small swf to advance small swf to correct frame.
	var objMovie = document.getElementById(moviename);
	if (objMovie) {
		//alert('found flash movie: ' + objMovie + ', calling external interface method...');
		objMovie.setFlashPosition(pos);
	}
	else {
		//alert('Could not find movie: ' + objMovie + '.');
	}

}


//Allows the large swf to be re-embedded.
function resetLargeDiv() {
	
	var innerhtml = '<div id="moaFlashContentPopup"><p>This site is best viewed using JavaScript and Flash. To view certain content, JavaScript must be enabled, and you must have the latest version of the Adobe Flash Player installed.</p><span><a href="/system_pages/SiteExit.aspx?url=http://www.adobe.com/products/flashplayer/&amp;type=1" alt="Get adobe Flash Player">Download the free Flash Player now</a>.</span></div>';
	
	document.getElementById('flashMoaContainerLarge').innerHTML = innerhtml;

}


//After the time delay (so the xml can load), advances the large movie's timeline.
function delayedAdvanceLargeMovie() {
	clearTimeout(t);
	if (!isNaN(MOA_CURRENT_FRAME)) {
		advanceTimeline(MOVIE_NAME_LARGE, MOA_CURRENT_FRAME);
	}
}

//Displays the div containing the large MOA player.
function enlargeMOA(pos) {   

	resetLargeDiv();
	embedLargeMovie();

	document.getElementById(MOA_POPUP_CONTAINER_ID).style.left="0px";
	$('#' + MOA_POPUP_CONTAINER_ID).show();
	$('#' + MOA_POPUP_CURTAIN_ID).click( closeMoa );
	initMoaPopupPosition();
	setMoaPopupHeight(MOA_POPUP_HEIGHT);
	centerMoaPopup(MOA_POPUP_CONTAINER_ID);
	saveFlashPosition(pos); //call on open and on close.
	
	
	//advanceTimeline(MOVIE_NAME_LARGE, pos);
	
	//Delay call to advance large movie's timeline, so xml can load.
	t = setTimeout("delayedAdvanceLargeMovie()", LARGE_MOVIE_DELAY);
}

//Saves the frame number locally.
function saveFlashPosition(pos) {
	MOA_CURRENT_FRAME = pos;
}

//Gets the frame number.
function getFlashPosition() {
	return MOA_CURRENT_FRAME;
}

//Initialize the moa popup
function initMoaPopupPosition() {
	document.getElementById(MOA_POPUP_CONTAINER_ID).style.left = '0px';
	document.getElementById(MOA_POPUP_CONTAINER_ID).style.top = '0px';
}

function setMoaPopupHeight(h) {
	var htop = 16; //60;
	var hfoot = 16;
	var p = document.getElementById(MOA_POPUP_INNER_ID);
	var total = htop + h + hfoot;
	p.style.height = total + 'px';
}

function centerMoaPopup(id) {
	var x = document.getElementById(id)

	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number') {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} 
	else if( document.documentElement 
		&& ( document.documentElement.clientWidth 
		|| document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} 
	else if( document.body 
		&& ( document.body.clientWidth 
		|| document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}

	//get scroll xy
	var arr = getScrollXY();
	var scrlX = arr[0];
	var scrlY = arr[1];

	x.style.left = ((myWidth/2) - (x.offsetWidth/2) + scrlX) + "px";
	//x.style.top = ((myHeight/2) - (x.offsetHeight/2) + scrlY) + "px";
	x.style.top = "23em";
}



/*** Flash external interface calls */

////Magnifying glass call:
//videoControls.enlargeBtn.onRelease = function() {
//    my_flvPb.pause();
//    ExternalInterface.call("enlargeMOA('"+my_flvPb.playheadTime+"')");
//    //trace("my_flvPb.playheadTime = "+my_flvPb.playheadTime);
//};

//videoControls.enlargeBtn.onRelease = function() {
//    my_flvPb.pause();
//    ExternalInterface.call("closeMoa('"+my_flvPb.playheadTime+"')");
//  

//videoControls.enlargeBtn.onRelease = function() {
//    my_flvPb.pause();
//    ExternalInterface.call("enlargeMOA('"+my_flvPb.playheadTime+"')");



/*** Flash external interface callbacks */

//ExternalInterface.addCallback("setFlashPosition",this,setVideo);

//function setVideo(arg){
//    if(arg == undefined){
//        my_flvPb.seek ;
//    } else {
//        my_flvPb.seek(arg);
//    }
//}









