var ge;

$(document).ready(function(){
    
    var navObj = $("#nav");
    
    if(document.getElementById("nav")) {
        var navTop = navObj.position().top+navObj.parent().position().top+30;
        navObj.mousemove(function(e) {
            $("#menu_kez").css({"top":(e.pageY-navTop)+"px"});
        });
    }
    
    if(document.getElementById("mo-haz-bal1")) {
        //$("#mo-haz-bal1").animate({"top": "0px"}, 3000).animate({"top": "-500px"}, 5000);
        //$("#mo-haz-bal2").animate({"top": "-95px"}, 3000).animate({"left": "-100px"}, 1000);
        //$("#mo-haz-jobb").animate({"top": "40px"}, 3000).animate({"right": "-100px"}, 1000);
		var image1Ok = false;
		var image2Ok = false;
		var image3Ok = false;
		var image1 = new Image();
		image1.src = "/en/style/images/haz_bal1.png";
		image1.onload = function() {image1Ok = true;}
		var image2 = new Image();
		image2.src = "/en/style/images/haz_bal2.png";
		image2.onload = function() {image2Ok = true;}
		var image3 = new Image();
		image3.src = "/en/style/images/haz_jobb.png";
		image3.onload = function() {image3Ok = true;}
		varakozik = function () {
			if(image1Ok && image2Ok && image3Ok) {
				$("#mo-haz-bal1").animate({"top": "-500px"}, 5000);
				$("#mo-haz-bal2").animate({"left": "-100px"}, 1000);
				$("#mo-haz-jobb").animate({"right": "-100px"}, 1000);
			} else {
				setTimeout("varakozik()", 1000);
			}
		}
		setTimeout("varakozik()", 3000);
    }
    
    if(document.getElementById("mo-program-bal")) {
		//$("#mo-program-bal").animate({"top": "-10px"}, 3000).animate({"left": "10px"}, 2000);
        //$("#mo-program-jobb1").animate({"top": "-10px"}, 3000).animate({"top": "-150px"}, 3000);
        //$("#mo-program-jobb2").animate({"top": "-10px"}, 3000).animate({"right": "-20px"}, 2000);
		var image1Ok = false;
		var image2Ok = false;
		var image3Ok = false;
		var image1 = new Image();
		image1.src = "/en/style/images/program_bal.png";
		image1.onload = function() {image1Ok = true;}
		var image2 = new Image();
		image2.src = "/en/style/images/program_jobb1.png";
		image2.onload = function() {image2Ok = true;}
		var image3 = new Image();
		image3.src = "/en/style/images/program_jobb2.png";
		image3.onload = function() {image3Ok = true;}
		varakozik = function () {
			if(image1Ok && image2Ok && image3Ok) {
				$("#mo-program-bal").animate({"left": "10px"}, 2000);
				$("#mo-program-jobb1").animate({"top": "-150px"}, 3000);
				$("#mo-program-jobb2").animate({"right": "-20px"}, 2000);
			} else {
				setTimeout("varakozik()", 1000);
			}
		}
		setTimeout("varakozik()", 3000);
    }
    
    if(document.getElementById("mo-oo-betet")) {
        //$("#mo-oo-betet").animate({"top": "90px"}, 3000).effect("shake", {times:3, distance:10}, 100);
        //setTimeout(function() {
        //    $("#mo-oo-betet").animate({"top": "-150px", "right": "-200px"}, 3000)
        //}, 5000);
		var image1Ok = false;
		var image1 = new Image();
		image1.src = "/en/style/images/oo_betet.png";
		image1.onload = function() {image1Ok = true;}
		varakozik = function () {
			if(image1Ok) {
				$("#mo-oo-betet").effect("shake", {times:3, distance:10}, 100);
				setTimeout(function() {
				    $("#mo-oo-betet").animate({"top": "-150px", "right": "-200px"}, 3000)
				}, 1000);
			} else {
				setTimeout("varakozik()", 1000);
			}
		}
		setTimeout("varakozik()", 3000);
    }
    
    if(document.getElementById("mo-cafe-bal")) {
		var image1Ok = false;
		var image2Ok = false;
		var image1 = new Image();
		image1.src = "/en/style/images/cafe_bal.png";
		image1.onload = function() {image1Ok = true;}
		var image2 = new Image();
		image2.src = "/en/style/images/cafe_urhajo.png";
		image2.onload = function() {image2Ok = true;}
		varakozik = function () {
			if(image1Ok && image2Ok) {
				$("#mo-cafe-bal").animate({"left": "-60px"}, 3000);
				$("#mo-cafe-urhajo").animate({"top": "-5px", "right": "-50px"}, 1500);
				//$("#mo-cafe-bal").animate({"top": "-10px"}, 3000).animate({"left": "-60px"}, 3000);
				//$("#mo-cafe-urhajo").animate({"top": "70px"}, 3000).animate({"top": "-5px", "right": "-50px"}, 1500)
			} else {
				setTimeout("varakozik()", 1000);
			}
		}
		setTimeout("varakozik()", 3000);
    }
    
    if(document.getElementById("gmap")) {
        if (GBrowserIsCompatible()) {
            var map = new GMap2(document.getElementById("gmap"));
            map.addControl(new GSmallMapControl());
            map.setCenter(new GLatLng(47.504636,19.058604), 12);
            var marker = new GMarker(new GLatLng(47.504636,19.058604));
            map.addOverlay(marker);
            GEvent.addListener(marker, "click", function() {
                marker.openInfoWindowHtml('<p style="color:#000;text-align:center;margin:2px;padding:0;">Budapest, Nagymező utca 38.<br/><img src="http://www.instant.co.hu/bejarat.jpg" alt="" style="margin-top:5px;" /></p>');
            });
        }
    }
    
    if(document.getElementById("gearth")) {
        google.earth.createInstance('gearth', initCallback, failureCallback);
    }
    
    $("a[rel*='gallery_0']").lightBox();
    $("a[rel*='gallery_1']").lightBox();
    $("a[rel*='gallery_2']").lightBox();
    $("a[rel*='gallery_3']").lightBox();
    $("a[rel*='gallery_4']").lightBox();
    $("a[rel*='gallery_5']").lightBox();
    $("a[rel*='gallery_6']").lightBox();
    $("a[rel*='gallery_7']").lightBox();
    $("a[rel*='gallery_8']").lightBox();
    $("a[rel*='gallery_9']").lightBox();
    
    $("a[rel*='lightbox_1']").lightBox();
    $("a[rel*='lightbox_2']").lightBox();
    
});

function initCallback(instance) {
    ge = instance;
    ge.getWindow().setVisibility(true);
  
    // add a navigation control
    ge.getNavigationControl().setVisibility(ge.VISIBILITY_AUTO);
  
    // add some layers
    ge.getLayerRoot().enableLayerById(ge.LAYER_BORDERS, true);
    ge.getLayerRoot().enableLayerById(ge.LAYER_ROADS, true);
  
    // in this sample we will attempt
    // to fetch a  KML file and show it
  
    function finished(object) {
      if (!object) {
        // wrap alerts in API callbacks and event handlers
        // in a setTimeout to prevent deadlock in some browsers
        setTimeout(function() {
          alert('Bad or null KML.');
        }, 0);
        return;
      }
      ge.getFeatures().appendChild(object);
      var la = ge.createLookAt('');
      la.set(47.504646400032, 19.058618299769, 0, ge.ALTITUDE_RELATIVE_TO_GROUND, 0, 60, 1000);
      ge.getView().setAbstractView(la);
    }
  
    // fetch the KML
    var url = 'http://www.instant.co.hu/instant.kml';
    google.earth.fetchKml(ge, url, finished);
  }
  
  function failureCallback(errorCode) {
  }