function ms_updateFrame(value, skipURL) {
	if(value.toString().indexOf('=') != -1 ) {
		value = value.substr(value.indexOf('=') + 1);
	}

	document.getElementById("history").src = "/media/history.html?chapter=" + value;

	if(!skipURL) {
		ms_updateURL(value);
	}
}

function ms_updateURL(value) {
	var ms_init = ms_curHash != null;

	if(location.hash.substr(location.hash.toString().length - 1, 1) == value) {
		return;
	}

	var wsValue = ms_getWsValue(location.hash);
	if(wsValue == null || wsValue == value) {
		location.hash = "chapter=" + value;
		ms_curHash = "#chapter=" + value;
		document.title = ms_docTitle;
	}

	if(!ms_init) {
		if(document.all && !window.opera) {
			ms_updateFrame(value, true);
			setTimeout("ms_checkFrameChange()", 500);
		} else {
			setTimeout("ms_checkURLChange()", 500);
		}
		if(window.ms_stopFlash) {
			ms_stopFlash();
		}
	}
}

function ms_update(value) {
	if(isNaN(value) && isNaN(value.toString().substr(0, 1))) {
		return;
	}
	if(document.all && !window.opera) {
		ms_updateFrame(value, false);
	} else {
		ms_updateURL(value);
	}
}


var ms_curHash;
function ms_checkFrameChange() {
	if(location.hash.length && ms_curHash != location.hash) {
		ms_curHash = location.hash;
		ms_updateFrame(ms_curHash, true);
		ms_setFlashParam("chapter", ms_curHash);
	} else {
		var curURL = document.getElementById("history").contentWindow.document.getElementById("historyfield").value;
		if(curURL.length) {
			curURL = '#' + curURL;
			if(curURL != ms_curHash) {
				ms_updateURL(curURL.replace(/[^=]+=/, ''));
				ms_setFlashParam("chapter", curURL.substr(1));
			}
		}
	}
	setTimeout("ms_checkFrameChange()", 500);
}


function ms_checkURLChange() {
	var curURL = location.hash;
	if(curURL != ms_curHash) {
		ms_curHash = curURL;
		ms_setFlashParam("chapter", curURL.substr(1));
	}
	setTimeout("ms_checkURLChange()", 500);
}


function ms_setFlashParam(name, value, isBool) {
	if(!isBool) {
		if(value.indexOf("=") == -1) {
			return;
		}
		value = value.substr(value.indexOf('=') + 1);
		if(!value.length) {
			return;
		}

		var wsValue = ms_getWsValue(value);
		if(wsValue != null) {
			value = wsValue;
			setFlashVar("denyWs", true);
		}
	} else if(!value || value.toString() == "undefined") {
		return;
	}

	setFlashVar(name, value);

	ms_updateURL(value);
}

function ms_getWsValue(value) {
	var separator = value.indexOf('_ws');
	if(separator != -1) {
		return value.substr(separator - 1, 1);
	}
	return null;
}

var ms_docTitle = "";
// This function is initially called from product_entry_page.swf
function ms_initPESFlash() {
	if(document.all && !window.opera) {
		if(getLayer("embflashie").PercentLoaded() != 100) {
			setTimeout("ms_initPESFlash()", 500);
			return;
		}
	} else {		
		if(getLayer("embflash").PercentLoaded() != 100) {
			setTimeout("ms_initPESFlash()", 500);
			return;
		}
	}

	var curChapt = 1;
	ms_docTitle = document.title;
	
	setFlashVar("isInit", true);
	if(document.all && !window.opera) {
		if(location.hash.toString().length) {
			ms_setFlashParam("chapter", location.hash.substr(1));
		} else {
			if(location.href.indexOf(".chapter") != -1) {
				curChapt = location.href.substr(location.href.indexOf(".chapter") + 8, 1);
			}
			setFlashVar("chapter", curChapt);
			ms_updateURL(curChapt);
		}
	} 
	else 
	{
		if(location.hash.toString().length) {
			ms_setFlashParam("chapter", location.hash.substr(1));
		} else {
			if(location.href.indexOf(".chapter") != -1) {
				curChapt = location.href.substr(location.href.indexOf(".chapter") + 8, 1);
			}
			setFlashVar("chapter", curChapt);
			ms_updateURL(curChapt);
		}
	}
}

function setFlashVar(name, value)
{
	var layer;
	var source;
	if(document.all && !window.opera) 
	{
		layer = "embflashie";
		source = getLayer(layer).movie;
	} 
	else 
	{
		layer = "embflash";
		source = getLayer(layer).src;
	}
	
	if (source.indexOf("fl10") > 0)
	{
		getLayer(layer).setVariableFl10(name, value);
	}
	else
	{
		getLayer(layer).SetVariable(name, value);
	}
}

function ms_filterFlashCalls(type, value) {
	if(type == null || value == null) {
		return;
	}
	if(type == "track") {
		Instadia_sendFlashEvent(value);
	} else {
		ms_update(value);
	}
}

