//---------------------------------------------------------------------------------------------------------------------------
// gis_comm lib
// javascript functions for js-to-flash communications
//

//---------------------------------------------------
//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;

//---------------------------------------------------
// deep-link handling

// called from flash to set #hash-value
function updateHash( dl) {
//	window.status = "dl: "+dl;
//ausmarkiert um Fehlermeldungen zu verhindern Orginal in gis_comm3.js
}



function setCoord( txt) {
  var p = txt.split( "|");

  var x = Math.round( p[0] * 1000) / 1000;
  var y = Math.round( p[1] * 1000) / 1000;

//  alert( "gis: "+x+" , "+y);

  setGisX( x);
  setGisY( y);
  setGisZentroid( x, y);
/*
  var wl = top.location;

  top.location.replace( wl.protocol+'//'+wl.host+wl.pathname+"#x="+x+"&Y="+y);
*/
}

function openMap( uid, gis_x, gis_y, gis_q, ort, str, hn, rest) {
  var temp = uid+"|"+gis_x+"|"+gis_y+"|"+gis_q+"|";
  temp += ( ort != 'undefined') ? ort : '';
  temp += ( str != undefined) ? str : '';
  temp += ( hn != undefined) ? hn : '';
  if ( rest == undefined) rest = '';
  triggerVianovis( "/fileadmin/vianovis/"+mandant_prefix+"/regio.html?CID="+mandant_prefix+"&cid="+mandant_prefix, mandant_web_root+"|"+uid+"|"+gis_x+"|"+gis_y+"|"+gis_q+"|"+ort+"|"+str+"|"+hn+"|"+rest, '');
}

function openMapSafari() {
var t="toolbar=0,menubar=0,scrollbars=0,location=0,status=1,resizable=1,width=900,height=630,screenX=200,left=200,screenY=20,top=20";
    	var url="/fileadmin/vianovis/"+mandant_prefix+"/regio.html?CID="+mandant_prefix+"&cid="+mandant_prefix;
    	var wh = window.open( url, "viaSafari", t);
}

function openEdit( uid, gis_x, gis_y, gis_q, ort, str, hn) {
  var temp = uid+"|"+gis_x+"|"+gis_y+"|"+gis_q+"|";
  temp += ( ort != 'undefined') ? ort : '';
  temp += ( str != undefined) ? str : '';
  temp += ( hn != undefined) ? hn : '';
  triggerVianovis( "/fileadmin/vianovis/"+mandant_prefix+"/index.html?CID="+mandant_prefix+"&cid="+mandant_prefix, mandant_web_root+"|"+uid+"|"+gis_x+"|"+gis_y+"|"+gis_q+"|"+ort+"|"+str+"|"+hn, '_edit');
}

   // 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);
    }

    var isConnected = false;
    var isValid = false;
    var autoOpen = false;

    var timer = -1;
    var loopCount = 20;
    var loops = loopCount;
    var pmesg = '';
    var purl = '';
    var pwin_suff = '';

    function setValid() {
    	isConnected = false;
    	isValid = true;
//    	alert( "valid");
    	if ( autoOpen) triggerVianovis( "/fileadmin/vianovis/"+mandant_prefix+"/regio.html", '');
//	window.status = "viaEdit offline";
    }
    
    function setConnected() {
    	isConnected = true;
        isValid = true;
//        window.status = "viaEdit connected";
//    	alert( "connected");
    }
    
    function triggerVianovis( url, mesg, win_suff){
    
      if ( mesg == '') mesg = pmesg;
      if ( url == '') url = purl;
      if ( win_suff == '') win_suff = pwin_suff;
      
      if ( isValid || loops <= 0) {
//	alert( "open: "+isValid+" / "+isConnected+" / "+loops);
		
	// post message
	sendMessage(mesg);
      	if ( ! isConnected) via_open( url, win_suff);
      	
        clearInterval( timer);
        timer = -1;
        loops = loopCount;
        pmesg = '';
	purl = '';
	pwin_suff = '';
      } else {
	//window.status = "looping: "+loops;

        if ( timer == -1) {
          pmesg = mesg;
	  purl = url;
	  pwin_suff = win_suff;
          timer = setInterval( "triggerVianovis('"+url+"','"+mesg+"')", 500);
        }
		
        if ( loops <= 0) {
                clearInterval( timer);
                timer = -1;
                loops = loopCount;
                pmesg = '';
		purl = '';
		pwin_suff = '';
        }

        loops--;
      }
    }
	
    var fName = 'via_comm';
    
    function sendMessage( mesg) {
      if ( isIE()) window.document[ fName].SetVariable("_level0:externalParm", mesg);
      else window.document.embeds[ fName].SetVariable("_level0:externalParm", mesg);
    }
    
    function readValue( parm) {
      if ( isIE()) return window.document[ fName].GetVariable( parm);
      else return window.document.embeds[ fName].GetVariable( parm);
    }
    
    function via_open( url, win_suff) {
    	var t="toolbar=0,menubar=0,scrollbars=0,location=0,status=1,resizable=1,width=900,height=630,screenX=200,left=200,screenY=20,top=20";
    	// var url="/fileadmin/vianovis/"+mandant_prefix+"/regio.html";
    	var wh = window.open( url, 'via_'+mandant_prefix+win_suff, t);
        if ( wh == null) {
                alert( "Ihr Popup-Blocker ist aktiviert!\n\nBitte öffnen Sie die Anwendung per Click.");
        }
    }
    function via_openDesktop() {
    	var t="dependant,toolbar=0,menubar=0,scrollbars=0,location=0,status=1,resizable=1,width="+screen.width+",height="+screen.height+",screenX=0,left=200,screenY=0,top=5";
    	var url="/fileadmin/vianovis/"+mandant_prefix+"/desktop.html";
    	window.open( url, 'via_'+mandant_prefix, t);
    }
    function via_open_edit() {
    	var t="toolbar=0,menubar=0,scrollbars=0,location=0,status=1,resizable=1,width=900,height=700,screenX=200,left=200,screenY=200,top=200";
    	var url="/fileadmin/vianovis/"+mandant_prefix+"/index.html?CID="+mandant_prefix;
    	window.open( url, 'via_'+mandant_prefix+'_edit', t);
    }


    function createEmbed( obj, parmlist, w, h) {
	document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" id="via_comm" width="'+w+'" height="'+h+'" align="middle">');
	document.write('<param name="allowScriptAccess" value="sameDomain" />');
	document.write('<param name="movie" value="'+obj+'" />');
	document.write('<param name="play" value="false" />');
// document.write('<param name="menu" value="false" />');
	document.write('<param name="quality" value="high" />');
	
	document.write('<param name="allowFullScreen" value="true" />');
//	document.write('<param name="scale" value="all" />');
	document.write('<param name="bgcolor" value="#ffffff" />');
	document.write('<param name="FlashVars" value="'+parmlist+'" />');
	document.write('<embed src="'+obj+'" FlashVars="'+parmlist+'" play="false" menu="false" quality="high" allowFullScreen="true" scale="noscale" bgcolor="#ffffff" width="'+w+'" height="'+h+'" swLiveConnect=true name="via_comm" id="via_comm" name="regio" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />');
	document.write('</object>');
    }

    //-->

// script ends----------------------------------------------