var currentIndex=0;
var win = false;
var flash = false;

// retrieves a reference to the history frame
function getHistoryFrame() {
	if(document.all) {
		return frames['frmHistory'];									//ie
	} else {
		return document.getElementById('frmHistory').contentDocument;	//moz
	}
}

// event handler for flash.navigation.onNavigateResponse
function onFlashNavigate() {
	currentIndex++;
	var url = "historyDummy.asp?index="+currentIndex;
	getHistoryFrame().location.href = url;
}

// event handler for flash.navigation.onHistory
function onFlashHistory(direction) {
	if(win) {
		win = false;
	} else {
		flash = true;
		window.history.go(direction);
	}
}

// event handler for browser.history
function onWinHistory(newIndex) {
	if(document.getElementById('swf') != null) {
		var	steps = newIndex - currentIndex;
		currentIndex = newIndex;
		if(flash) {
			flash = false;
		} else {
			if(steps != 0) {
				win = true;
				flashHistoryGo(steps);
			}
		}
	}
}

// invokes pepsi.navigation.historyGo
function flashHistoryGo(steps) {
	var vars = "historySteps="+steps;
	setFlashVariables("swf", vars);
}

// checks for previous history and clears it (IE only)
function checkHistoryStatus() {
	var url = getHistoryFrame().location.href;
	//alert( url );
	var index = url.split('=')[1];
	if(index != undefined && index != currentIndex) {
		getHistoryFrame().location.href = "historyDummy.asp?index="+currentIndex;
	}
}

var ua        = navigator.userAgent.toLowerCase(); 
var is_pc_ie  = ( (ua.indexOf('msie') != -1 ) && ( ua.indexOf('win') != -1 ) && ( ua.indexOf('opera') == -1 ) && ( ua.indexOf('webtv') == -1 ) );

function setFlashVariables(movieid, flashquery){
	var i,values;
	if(is_pc_ie){
		var chunk = flashquery.split("&");
		for(i in chunk){
			values = chunk[i].split("=");
			document[movieid].SetVariable(values[0],values[1]);
		}
	}else{
		var divcontainer = "flash_setvariables_"+movieid;
		if(!document.getElementById(divcontainer)){
			var divholder = document.createElement("div");
			divholder.id = divcontainer;
			document.body.appendChild(divholder);
		}
		document.getElementById(divcontainer).innerHTML = "";
		var divinfo = "<embed src='history_gateway.swf' FlashVars='lc="+movieid+"&fq="+escape(flashquery)+"' width='0' height='0' type='application/x-shockwave-flash'></embed>";
		document.getElementById(divcontainer).innerHTML = divinfo;
	}
}