$(function(){

  function getUrlVars()
  {
      var vars = [], hash;
      var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
      for(var i = 0; i < hashes.length; i++)
      {
          hash = hashes[i].split('=');
          vars.push(hash[0]);
          vars[hash[0]] = hash[1];
      }
      return vars;
  }

  $.ajaxSetup({
    data: {lang: getUrlVars()['lang']}
  });

  $(".langsel").click(function(){
    window.location.href = window.location.pathname + '?lang=' + $(this).attr('data-lang');
  });

  $(".menu a").click(function(){
    $(this).closest("ul").find("a").removeClass("selected");
    $(this).addClass("selected");

    var href = $("#header_menu a.selected").attr("href");
    var lang = $(".langsel.selected").attr("data-lang");
    if(href.indexOf("php") == -1)
    {
       href = "content.php?f=" + href;
    }

    $menu = $(this);
    $("#content").load(href, function(){

     $(".markdown p:has(img)").addClass("image_p");
     if($menu.hasClass("automenu"))
     {
       $("#content img").load(function(){
         var height = 0;
         $("#content").children().each(function(){
           height = height + $(this).outerHeight(true);
         });
         $("#content").animate({height: height}, 200);
       });
     }      
       
     if($menu.attr("id") == "menu_home") 
     {
       $("#intro_slideshow").cycle();
       //$("#content").animate({height: "800px"});
     }
     if($menu.attr("id") == "menu_gallery") 
     {
       $("#content").animate({height: "500px"}, 200, function(){
         $("#gallery").galleria({height: 500, width: 800});
       });
     }
     if($menu.attr("id") == "menu_contact")
     {
	  $("#contact-form input[type='text'], #contact-form textarea").each(function(i,e){
	    $(e).data("label", $(e).attr("value"));
	  });

	  $("#contact-form input[type='text'], #contact-form textarea").blur(function(){
	    if($(this).attr("value") == "")
	    {
	      $(this).attr("value", $(this).data("label"));
	    }
	  });

	  $("#contact-form input[type='text'], #contact-form textarea").focus(function(){
	    if($(this).attr("value") == $(this).data("label"))
	    {
	      $(this).attr("value", "");
	    }
	  });
     }
     if($menu.attr("id") == "menu_panorama")
     {
       $("#content").animate({height: "500px"}, 200, function(){
         $('.panorama-view').panorama360({
           bind_resize: false,
           is360: false,
         }); 
       });

     }


    });
    return false;
  });

  $("#header_menu a").click(function(){

    return false;
  });




  $("#menu_home").click();

});

