// The sm_object function retrieves a handle to the specified Flash
// object, accomodating for different DOM methods supported by 
// different browsers.  This is the method recommended by Adobe.

function sm_object(ID) {
	return (navigator.appName.indexOf("Microsoft") != -1) ? window[ID] : document[ID];
}

// The querystring function retrieves and parses query string arguments
// passed to the current page, if any, storing them in an associative array

function querystring() {
	var returnData = new Array();
	var url = parent.document.location.href.split('?', 2);
	if(url.length != 2) return returnData;

	var arguments = url[1].split('&');

	for(var i = 0; i < arguments.length; i++) {
		var argument = arguments[i].split('=', 2);
		returnData[argument[0]] = argument[1];
	}

	return returnData;
}

// The onExternalInterfaceReady function is invoked by the StoryPlate when
// the JavaScript API has been initialized.  This event handler parses 
// the (optional) index from the query string and navigates to the
// corresponding record in the StoryPlate.

function onExternalInterfaceReady(ID) {
	// retrieve reference to StoryMaker project
	var flashID = sm_object('flashObj');

	// retrieve query string
	var query = querystring();

	// identify selected index specified via query string
	if(!isNaN(query['index'])) {
		// set active slide in the StoryMaker project
		flashID.setFocusByIndex(query['index'] - 1);
	}

	// send page load omniture event
}

// The onEndChangeFocus function is invoked by the StoryPlate when the
// user navigates from record to record.  This event handler currently 
// logs nav events to a text field beneath the slideshow, but the code
// could be modified to invoke Omniture tracking events.

function onEndChangeFocus(ID, record) {
	pageIncrement();
}



// The pageIncrement function coordinates the analytics page view call and
// the ad refresh call, so that they have time to complete and don't step on each other (IE bug)
function pageIncrement() {
	var pvTimeOut = window.setTimeout(ngsPageView, 100);
	var adTimeOut = window.setTimeout(RefreshAds, 500);
}


// The ngmPageView function is a wrapper that calls the standard adrefresh/pageView code, 
// but throttles it to not occur more frequently than 1 pageView per minute
var pvCnt		= 0; 		//placeholder
function ngmPageView(pgName,delay) 
{
	if (!delay) {
		pageIncrement();
	} else {

		var now = new Date();
		var now_ms = now.getTime();
		if (pvCnt == 0) 
		{
			//counter was not set previously, initialize
			pvCnt = now_ms;
			pageIncrement();
		} else {
			var diff_time = now_ms - pvCnt;
			
			//check if threshold has been met...
			if (diff_time/1000 >= 20) 
			{
				pageIncrement();
				//reset counter
				pvCnt = 0; 		
			}
		}
	}
}
