// JavaScript Document

var gep;
function fixOverlay() {
    GEvent.addListener(map, "click", Timeout);
	GEvent.addListener(map, "maptypechanged", addEarthListener);
    /*map.getEarthInstance(function (pluginInstance) {
        gep = pluginInstance;
		if (gep){
            google.earth.addEventListener(gep.getGlobe(), 'click', Timeout);
		}
    });*/
}

//fügt Listner zu Earth hinzu 
function addEarthListener(){
	
   map.getEarthInstance(function (pluginInstance) {
        gep = pluginInstance;
		if (gep){
            google.earth.addEventListener(gep.getGlobe(), 'click', Timeout);
		}
    });
}
//initialisiert Overlay
function Timeout() {
    "use strict";
	
    setTimeout("initOverlay()", 500);
	
}
function initOverlay() {
    "use strict";
	
	var earthIsOn;
	var Ohref;
	var lay;
	var Ergebnis;
	$('a', '#iw_kml').addClass("iframe");
	lay = "&lay=" + $("#hiddenlay").val();
	//fügt Overlay zu allen Links in Balloons in Maps hinzu
    
	
	
    $(".iframe").fancybox({
		'width'	:	700,
		'height'	:	'90%',
		'type' : 'iframe',
		//schaltet falls google earth plugin eingestellt auf Satellit
		'onStart' : function () { 
			if (map.getCurrentMapType() === G_SATELLITE_3D_MAP) {
                map.setMapType(G_SATELLITE_MAP);
                earthIsOn = true;
            } else {
                earthIsOn = false;
            }
		},
		//schaltet zurück auf earth 
		'onCleanup' : function () { 
			if (earthIsOn) {
				map.setMapType(G_SATELLITE_3D_MAP);
			} 
		}
		
	});
    Ohref = $('.iframe').attr("href");
	
    Ergebnis = Ohref.match(/&lay=/);
	
    if (!Ergebnis) {
        Ohref = Ohref + lay;
    } 
	
    $('.iframe').attr("href", Ohref);
   
	
} 
