
var flashProxy;
var htmlImagesLoaded = false;
var flashLoaded = false;
var flashCalled = false;

// window.onload = function() { ini(); }

function ini() {
	//setTimeout( "reportHTMLimagesLoad()", 3000 ); // timeout. used instead of real JS call when HTML images are loaded

	setTimeout( "reportHTMLimagesLoadLocal()", 3000 ); // added to make flash working even with "1 item remaining"

	// setup for javascript-flash integration kit
	var lcId = new Date().getTime();
	flashProxy = new FlashProxy( lcId, "/stc/refresh/js/JavaScriptFlashGateway.swf" );

	// ini FlashObject for pagePromo
	var swf1 = new SWFObject( "/stc/refresh/pagePromo.swf", "pagePromoFlash", "498", "230", "7", "#FFFFFF", true );
	swf1.addParam( "scale", "noscale" );
	swf1.addParam( "quality", "best" );
	swf1.addParam( "wmode", "opaque" );
	swf1.addVariable( "promoXMLpath", "shared/xml/pagePromo/0,,,00.xml");
	swf1.addVariable( "lcId", lcId  );
	swf1.write( "flashPromo" ); // write flash object
	
	// ini FlashObject for pageVideo
	var swf2 = new SWFObject( "/stc/refresh/pageVideo.swf", "pageVideoFlash", "285", "230", "7", "#FFFFFF", true );
	swf2.addParam( "scale", "noscale" );
	swf2.addParam( "quality", "best" );
	swf2.addParam( "wmode", "opaque" );
	swf2.addVariable( "videoXMLpath", "shared/xml/homeVideo/0,,,00.xml");
	swf2.write( "flashVideo" ); // write flash object
	
	// parse DOM for image tags
	imageArray = document.getElementsByTagName('img'); 
	
	timerCheck = setInterval("CheckImagesLoad()", 10);
}


//----------------------------------------
// calls from Flash
//----------------------------------------
function callFromFlash( obj ) {

	switch( obj.event ) {
		case "onFlashLoaded": // flash loaded
			//document.getElementById( "debug" ).innerHTML += "Flash loaded...<br>"; 
			// continue loading flash images. Timeout for now
			flashLoaded = true; // flash loaded = true
			callFromJavaScriptContinueLoading(); // tell flash to continue image loading
			break;
		case "video": // call from video thumbnail click
			// obj.id - video id
			// obj.playerURL - player URL
			// obj.prop - Omniture property name
			// obj.trackingString - Omniture tracking string
			iv_doTracking( obj.trackingString, '', obj.prop ); // track video in Omniture
			
			// here you can open video player using obj properties
            nbc_launchVideo(obj.id, obj.trackingString);

			//for ( var key in obj ) { // loop through obj properties - debug
				// document.getElementById( "debug" ).innerHTML += "Call from video thumbnail: " + key + ": " + obj[key] + "<br>";
			//}
			break;
		case "viewall": // call from view all button click
			// obj.prop - Omniture property name
			// obj.trackingString  - Omniture tracking string
			// obj.url - view all URL
			iv_doTracking( obj.trackingString, '', obj.prop ); // track view all in Omniture

			// here you can open view all URL
            nbc_launchVideo(obj.url, obj.trackingString);
			
			//for ( var key in obj ) { // loop through obj properties -  debug
				// document.getElementById( "debug" ).innerHTML += "Call from ViewAll:: " + key + ": " + obj[key] + "<br>";
			//}
			break;
	}

}


function callFromJavaScript( obj ) { // not used
	flashProxy.call( "callFromJavaScript", obj );	
}

 
//----------------------------------------
// call from JS to Flash -> 
// continue loading Flash images etc.
//----------------------------------------
function callFromJavaScriptContinueLoading() { // call from JS to Flash -> continue loading Flash images etc.
	
	// debug
	//document.getElementById( "debug" ).innerHTML += "Flash loaded: " + flashLoaded + " --- HTML images loaded: " + htmlImagesLoaded + "<br>"; 

	if ( flashCalled || (htmlImagesLoaded && flashLoaded) ) { // if html images and flash are loaded -> tell flash to conitnue image loading
		flashProxy.call( "callFromJavaScriptContinueLoading", {} );	
	}
}


//----------------------------------------
// called from JS image preloader
// sets html images loading flag to true
//----------------------------------------
function reportHTMLimagesLoad() {
	
	htmlImagesLoaded = true; // images loaded = true
	callFromJavaScriptContinueLoading(); // tell flash to continue image loading
}

function reportHTMLimagesLoadLocal() {
	
	htmlImagesLoaded = true; // images loaded = true
	flashCalled = true;
	callFromJavaScriptContinueLoading(); // tell flash to continue image loading
}

//----------------------------------------
// Simulation of Omniture call
//----------------------------------------
function iv_doTrackingivr( trackStr, url, prop, linkType, gs ) {
	// debug
	//document.getElementById( "debug" ).innerHTML += "Omniture call: trackStr: " + trackStr + " url: " + url + " prop: " + prop + "<br>";
}


//----------------------------------------
// Check if all images are loaded
//----------------------------------------
function CheckImagesLoad() {

	temp = 0;
	// clear debug window
	//document.getElementById( "debug" ).innerHTML = '';
	
	for (var i=0; i<imageArray.length-2; i++) {
		if (imageArray[i].complete) {
			temp++;
			//document.getElementById( "debug" ).innerHTML += imageArray[i].src + "<br />";
		}
	}

	if (temp == imageArray.length-2) {
		// clear timer
		clearInterval(timerCheck);
		// call flash to load child clips
		reportHTMLimagesLoad();
	}
}