// Video Sync Popup 

function VideoSynchronizationWindow() {

	// reference to new browser win
	this.windowHandle;
	this.windowHandleName = "syncVideoPopup";


	// transient variables
	// user triggered
	this.videoXML;
	this.slideIndex;
	this.templateType; // image or SWF


	// triggered from AS->JS processor
	this.commandSlideChange = function ( idx ) {
		// slide 0 indicates that before first slide
		if ( idx > 0 ) {
			this.slideIndex = idx;
			var fn = this.windowHandle.eventTriggerShowSlide;
			if ( fn != undefined ) {
				try {
					fn ( idx );    
				} catch (e) {
					//ignore
				}
			}
			// the window reference is not yet ready ?
			else {
			

			}
		}
	}

	// triggered from AS->JS processor
	this.commandClose = function () {
		try {
			this.close();
		} catch (e) {
		//	alert ( "commandClose " + e );
		}
	}

	// triggered from AS->JS processor
	// given a metadata XML URL and slide index, build presentation
	this.commandLoad = function ( xml, template, idx ) {
		this.videoXML = xml;
		this.templateType = template;
		this.slideIndex = idx;


		// 1. popup win if necessary
		if ( ! this.checkIfOpened() ) {
			this.open();
			// launch new slide

		}


		// usual case on subsequent slides is to reuse window and trigger a slide change
		else {
			// and valid slide
			if ( this.windowHandle && idx > 0) {
				D("fnx?" + this.windowHandle.eventTriggerShowSlide );
				this.windowHandle.eventTriggerShowSlide ( idx );
			 }
		}

		// is window available in an open state
		return this.checkIfOpened();
	}

	// event handler

	// page framework loaded
	this.onWindowLoadedEvent = function ( win ) {
		this.render();
	}

	// view created
	this.onViewLoadedEvent = function ( win ) {


		// is there a slide index desired ?
		if ( this.slideIndex ) {
			this.commandSlideChange ( this.slideIndex );
		}

	}

	// window is closed by user
	this.onWindowClosedEvent = function ( win ) {
		// notify UI of closure
		_JS_F( "videoSynchronizationWindowOnCloseEvent", [ this.slideIndex ] );
	}


	// load up a video XML into the template
	this.render = function () {
		this.windowHandle.setTemplateType ( this.templateType );
		this.windowHandle.changeXMLSource( this.videoXML );
	}


	// AS->JS trigger
	this.open = function () {

		// don't open twice
		if ( this.checkIfOpened() ) {
			return false;
		}


		var metadata = com.cisco.dms.vp.ui.videoPortalConfigurationMetadata;

		var URL = metadata.configuration.resources.videoSynchronizationURL;
		// dimension of the image view 
		var w = metadata.interface.dimension.synchronization.standalone.width;
		var h = metadata.interface.dimension.synchronization.standalone.height;
		// add on window elements, thumbnails, padding
		w += 290;
		h += 100;


		var screenWidth = ( screen.availWidth || screen.width ); // whatever the browser supports
		var screenHeight = ( screen.availHeight || screen.height ); // whatever the browser supports

		var windowPositionLeft = ( window.screenLeft || window.screenX );
		var windowPositionTop = ( window.screenTop || window.screenY );

		// var topPos= ( screenHeight - h ) / 2;
		var topPos= ( windowPositionTop + 50 );

                var videoDefaultWidth = metadata.interface.dimension.video.window.width;
		// try and place popup to right of the video overlay
		var leftPos = windowPositionLeft + 50 + videoDefaultWidth;


		// center it then, if it overflows
		if ( ( leftPos + w ) > screenWidth ) {
			leftPos = ( screenWidth - w ) /2;
		}

		this.windowHandle = window.open( URL , this.windowHandleName, "toolbar=no,scrollbars=no,location=no,status=no,directories=no,resizable=yes," + 'width=' + w + ',height=' + h + ',left=' + leftPos + ',top=' + topPos );
		this.windowHandle.focus();


		// cascade close to children
		if (isIE) {
			window.attachEvent("onunload", _alsoCloseSyncVideoChildPopup); 
		} else {
			window.addEventListener("unload", _alsoCloseSyncVideoChildPopup, false);
		}

		// tell me, is there an open window now? popup blocker detection
		return this.checkIfOpened();
	}


	// is the popup already available?
	this.checkIfOpened = function () {
		return( this.windowHandle != undefined && this.windowHandle && ( !this.windowHandle.closed) )
	}



	// shut down window
	this.close = function() {
		if ( this.windowHandle != undefined && this.windowHandle != null ) {
			if ( ! this.windowHandle.closed ) {
				this.windowHandle.close();
			}
		}

	}

}

// auto close popup
function _alsoCloseSyncVideoChildPopup() {
	videoSyncManager.close();
}


// created later in initializeServiceManagers()
var videoSyncManager;
