//---------------------------------------------------------------------------------------------------------------------------
// gis_comm V3
// javascript functions for js-to-flash communications
// deeplink/browser-history support added
//

//---------------------------------------------------
//get a hook to the onload event

function startComm() {
	// todo: trigger embedded flash object to search for map/edit application

	window.status = "parm: "+readValue("_level0:isConnected");
	sendMessage( 'connect');

	if ( go_onload != undefined) go_onload();
}

var go_onload = window.onload;
// window.onload = startComm;

if ( isOpera()) history.navigationMode = 'compatible';

//----------------------------------------------------
// deep-link handling

// called from flash to set #hash-value
function updateHash( dl) {
//	window.status = "dl: "+dl;

	if ( dl != window.location.hash) {
		lastLoco = '#'+dl;
		makeAnchor( dl);
		window.location.hash = dl;
		createHistoryHTML( dl);
//  		window.status = "sX: "+dl+" / "+window.location.hash;
	}
}

// making a named anchor
var lastLink = '';
function makeAnchor(str) {
	if ( isIE()) return;

	if (str != null && str.length > 0 && lastLink != str) {
		var view_anchor = document.createElement('A');
		view_anchor.setAttribute('id', str);
		view_anchor.setAttribute('name', str);
		view_anchor.innerHTML = str;
		var hf = window.frames[ 'hist_frame'];
		var container = hf.document.body;
		if (container != null) {
			container.appendChild(view_anchor);
			container.appendChild( document.createElement('br'));
		}
		lastLink = str;
	}
}

// creating a frame with a javascript function
function createHistoryHTML( newHash) {
	if ( !isIE()) return;

	var histFrame = document.frames[ 'hist_frame'];
	histFrame.document.open('text/html');
	histFrame.document.write('<html><head></head><body onLoad="window.parent.histLink( \'#'+newHash+'\');">'+newHash+'</body></html>');
	histFrame.document.close();
}

// method used by ff to check for a changed #hash
var lastLoco = window.location.hash;
function checkHash() {
	if ( window.location.hash != lastLoco) {
		lastLoco = window.location.hash;
		setDeepLink( lastLoco.substr( 1));
	}

	setTimeout( checkHash, 200);
}

// method used by IE to set dl-position
function histLink( lnk) {
	if ( window.location.hash == lnk) return;

	lastLoco = lnk;
	window.location.hash = lnk;
	setDeepLink( lnk.substr( 1));
}

// unified function to send flash to a deeplink
function setDeepLink( lnk) {
//	alert( "lnk: "+lnk);
	sendMessage( '_level1.deepLink', lnk);
	window.status = "deepLink: "+lnk;
}

//---------------------------------------------------

   // added fix for opera
function isIE() {
//      alert( "app: "+navigator.appName+"ua:  "+navigator.userAgent);
	return (navigator.appName.indexOf("Microsoft") != -1) && (navigator.userAgent.indexOf("Opera") == -1) && (navigator.userAgent.indexOf("Safari") == -1);
}
function isOpera() {
//      alert( "app: "+navigator.appName+"ua:  "+navigator.userAgent);
	return (navigator.appName.indexOf("Microsoft") != -1) && (navigator.userAgent.indexOf("Opera") != -1);
}
function isSafari() {
//      alert( "app: "+navigator.appName+"ua:  "+navigator.userAgent);
	return ( navigator.userAgent.indexOf("Safari") != -1);
}

// tell flash to use getURL or fscommand
function jsCommMode() {
	// opera doesn't support fscommand

	return 'jsMode='+( isOpera() ? 'GET' : 'FSC');
}

//--------------------------------------------------
// communication with flash

var fName = 'regio';

function sendMessage( fvar, mesg) {
	if ( isIE()) window.document[ fName].SetVariable( fvar, mesg);
	else {
		if ( isSafari()) {
			var histFrame = document.frames[ 'safari_frame'];
			histFrame.document.open('text/html');
			histFrame.document.write('<html><head></head><body>');
			histFrame.document.write( getEmbed( 'viacom_post.swf', 'vcpost', 'cid=local&'+document.location.hash.substr( 1), '80', '30'));
			histFrame.document.write('</body></html>');
			histFrame.document.close();

		} else window.document.embeds[ fName].SetVariable( fvar, mesg);
	}
}

function readValue( parm) {
	if ( isIE()) return window.document[ fName].GetVariable( parm);
	else return window.document.embeds[ fName].GetVariable( parm);
}

//---------------------------------------------------------------------------
// handling flash embedding

// write to actual page
function createEmbed( obj, fName, parmlist, w, h) {
	document.write( getEmbed( obj, fName, parmlist, w, h));
}

function getEmbed( obj, fName, parmlist, w, h) {
	var temp = '';
	if (navigator.plugins && navigator.mimeTypes && navigator.mimeTypes.length) {
          temp += '<embed src="'+obj+'" FlashVars="'+parmlist+'" play="false" menu="false" quality="high" allowFullScreen="true" scale="noscale" bgcolor="#ffffff" width="'+w+'" height="'+h+'" swLiveConnect=true name="'+fName+'" id="'+fName+'" name="'+fName+'" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
	} else {
          temp += '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" id="'+fName+'" width="'+w+'" height="'+h+'" align="middle">';
          temp += '<param name="allowScriptAccess" value="sameDomain" />';
          temp += '<param name="movie" value="'+obj+'" />';
          temp += '<param name="play" value="false" />';
          temp += '<param name="menu" value="false" />';
          temp += '<param name="quality" value="high" />';
          temp += '<param name="allowFullScreen" value="true" />';
          temp += '<param name="scale" value="noscale" />';
          temp += '<param name="bgcolor" value="#ffffff" />';
          temp += '<param name="FlashVars" value="'+parmlist+'" />';
          temp += '</object>';
	}

	return temp;
}

function writeEmbed( elementId, obj, fName, parmlist, w, h, relocateURL){
	var rc =  versionIsValid( getPlayerVersion(), '7.0.0');
	if( rc){
		var n = (typeof elementId == 'string') ? document.getElementById(elementId) : elementId;
		n.innerHTML = getEmbed( obj, fName, parmlist, w, h);
		return true;
	} else {
//		alert( "rc: "+rc);
		if ( relocateURL != null) document.location.replace( relocateURL);
	}
	return false;
}

/* ---- detection functions ---- */
 function getPlayerVersion(){
	var PlayerVersion = '0.0.0';
	if(navigator.plugins && navigator.mimeTypes.length){
		var x = navigator.plugins["Shockwave Flash"];
		if( x && x.description) {
			PlayerVersion = x.description.replace(/([a-zA-Z]|\s)+/, "").replace(/(\s+r|\s+b[0-9]+)/, ".");
		}
	}else{
		// do minor version lookup in IE, but avoid fp6 crashing issues
		// see http://blog.deconcept.com/2006/01/11/getvariable-setvariable-crash-internet-explorer-flash-6/
		try{
			var axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
		}catch(e){
			try {
				var axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");
				PlayerVersion = '6.0.21';
				axo.AllowScriptAccess = "always"; // throws if player version < 6.0.47 (thanks to Michael Williams @ Adobe for this code)
			} catch(e) {
				if ( getMajor( PlayerVersion) == 6) {
					return PlayerVersion;
				}
			}
			try {
				axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
			} catch(e) {}
		}
		if (axo != null) {
			PlayerVersion = axo.GetVariable("$version").split(" ")[1].replace(/,/g, ".");
		}
	}
	return PlayerVersion;
}

function getMajor( vstr) {
	var f = vstr.split('.');

	return f[0];
}

function versionIsValid( fv, ck) {
	var f = fv.split('.');
	var c = ck.split('.');

//	alert( "cmp-ing versions:\n"+f.join('#')+"\n"+c.join('#'));

	if ( parseInt( f[0]) < parseInt( c[0])) return false;
	if ( parseInt( f[0]) > parseInt( c[0])) return true;
	if ( parseInt( f[1]) < parseInt( c[1])) return false;
	if ( parseInt( f[1]) > parseInt( c[1])) return true;
	if ( parseInt( f[2]) < parseInt( c[2])) return false;

	return true;
}
    //-->

// script ends----------------------------------------------
