$(document).ready( function() {
  
  /*********** Slideshow ************/
  
  function changeSlideshowImage(idx) {    
	$("#slideshow .image").attr("data-idx",idx);
    $("#slideshow .bkg").css( "opacity", "1" );
	$("#slideshow .bkg").css("background-image", "url('images/image" + idx + ".jpg')");
	$("#slideshow .image").fadeTo( 2500, 0.0, function() {
	  $("#slideshow .image").css("background-image", "url('images/image" + idx + ".jpg')");
  	  $("#slideshow .bkg").css( "opacity", "0.0" );
	  $("#slideshow .image").css("opacity", "1");
	});  
  }
  
  /*  $("#slideshow .image").hover(
    function () { $(this).fadeTo( 500, 1.0 ); }, 
    function () { $(this).fadeTo( 500, 0.75 ); }
  );*/
  
  $(document).everyTime( 4500, "slide", function() {
	var idx = parseInt($("#slideshow .image").attr("data-idx")) + 1;
    if (idx > 11) idx = 1;
    changeSlideshowImage(idx);
  });
  
  /*********** Fade In ************/
  
  $(window).load(function () {
	  $("#header").fadeTo( 500, 1.0, function() {
		$("#slideshow").fadeTo( 500, 1.0, function() {
		  $("#menu").fadeTo( 500, 1.0, function() {
			$("#footer").fadeTo( 500, 1.0 );
		  });
		});
	  });
	});	

	/*********** Impressum ************/

  function revisibleImpressum() {
    var opacity = parseFloat( $("#impressum").css("opacity"));
	if ( opacity == 0.0 ) {
	  $("#impressum").show();
  	  $("#impressum").fadeTo( 1000, 0.9 );
	} else {
	  $("#impressum").fadeTo( 1000, 0.0, function(){
	    $("#impressum").hide();
	  });
	}
  }
  
  $("#impressum .close-btn").click( function(event){
    revisibleImpressum();
	return false;
  });

  $("#footer .impressum-btn").click( function(event){
    revisibleImpressum();
	return false;
  });
   
  $("#impressum").hide();
  
  /*********** Termine ************/
  
  function revisibleTermine() {
    var opacity = parseFloat( $("#termine").css("opacity"));
	if ( opacity == 0.0 ) {
	  $("#termine").show();
  	  $("#termine").fadeTo( 1000, 0.9 );
	} else {
	  $("#termine").fadeTo( 1000, 0.0, function(){
	    $("#termine").hide();
	  });
	}
  }
  
  $("#termine .close-btn").click( function(event){
    revisibleTermine();
	return false;
  });
  
  $(".termine-btn").click( function(event){
    revisibleTermine();
	return false;
  });
  
  $("#termine").hide();

	/*********** Socials ************/

  $("#social .facebook").click( function(event){
    window.open( 'http://www.facebook.com/sharer.php?u=http://www.die-basis.org&t=Die Basis - Kirche für diese Generation', '', 'width=450, height=300, left='+(screen.availWidth/2-225)+', top='+(screen.availHeight/2-150));
    return false;
  });

  $("#social .twitter").click( function(event){
    window.open( 'http://twitter.com/home?status=Die Basis - Kirche für diese Generation - http://www.die-basis.org', '', 'width=450, height=300, left='+(screen.availWidth/2-225)+', top='+(screen.availHeight/2-150));
    return false;
  });

  $("#social .google").click( function(event){
    window.open( 'https://m.google.com/app/plus/x/?v=compose&content=Die Basis - Kirche für diese Generation | http://www.die-basis.org', '', 'width=450, height=300, left='+(screen.availWidth/2-225)+', top='+(screen.availHeight/2-150));
    return false;
  });

	/*********** Menu ************/

  $("#menu li.why").hover(
    function () { $("#menu li.why .gray").fadeTo( 500, 0.0 ); }, 
    function () { $("#menu li.why .gray").fadeTo( 500, 0.9 ); }
  );

  $("#menu li.facebook").hover(
    function () { $("#menu li.facebook .gray").fadeTo( 500, 0.0 ); }, 
    function () { $("#menu li.facebook .gray").fadeTo( 500, 0.9 ); }
  );


  $("#menu li.about").hover(
    function () { $("#menu li.about .gray").fadeTo( 500, 0.0 ); }, 
    function () { $("#menu li.about .gray").fadeTo( 500, 0.9 ); }
  );

  $("#header .session").hover(
    function () { $(this).css( 'border-color', 'white' ); }, 
    function () { $(this).css( 'border-color', '#333333' ); }
  );

  $("#menu .why-button").click( function(event){
    if ( !$("#content .why").is(":visible")) {
	  $("#content .about").slideToggle("slow", function() {
	    $("#content .why").slideToggle("slow");
	  });	  
	}
	return false;
  });
});
