/*<![CDATA[*/
$(document).ready(function(){

//  $("#header h1").click(function() {
//      location.href = "/";
//  });

  if($("#blog").length > 0) {
    activeNav = $("a[href*='/posts']").parent();
    activeNav.addClass("active");
  };

  // Setup Language Selectors
  function changeLang(lang) {
    var path = window.location.pathname.split("/");

    if (path[0] == "") { path.shift(); } // WTF

    var fragment;
    if (path[0] == "en") {
      fragment = path.slice(1, path.length);
    } else {
      fragment = path;
    }
    fragment = fragment.join("/");

    if (path[0] == "blog" || path[0] == "trips") {
      return false;
    }

    var currentLang = (path[0] == "en" ? "en" : "de");
    if (currentLang != lang) {
      var newLang = (lang == "en") ? "/en" : "";
      var newPath = "" + newLang + "/" + fragment;

      if (newPath[newPath.length - 1] == "/") {
        newPath = newPath.substring(0, newPath.length - 1);
      }

      window.location.pathname = newPath;
    }
  };

  $("#language-selector a").click(function(event){
    var lang = $(event.target).parent('a').attr("lang");
    changeLang(lang);
    return false
  });


  // Scroll testimonials
  $(".testimonials").serialScroll({
    items: 'li',
    duration: 1000,
    cycle: true,
    force: true,
    start: 0,
    stop: true,
    axis: 'x',
    easing: 'linear',
    interval: 14500,
    step: 1
  });

  // Big Targer for clickable items
  // clashes with booking button on item-teasers
  /*
  $(".item-teaser a").bigTarget({
      clickZone : 'li:eq(0)'
  });

  $(".item-teaser-big a").bigTarget({
      clickZone : 'li:not(.nobigclick):eq(0)'
  });

  $(".people-teaser a").bigTarget({
      clickZone : 'li:eq(0)'
  });

  $(".panel-item-alternatives a").bigTarget({
      clickZone : 'div:eq(0)'
  });
  */

  $("#site-navigation ul li a").each(function(e, v) {
    p = $(v).parent();
    $("span img", p).width($(v).width() + 32)
  });


  $("ul.subnav").hide();

  $("#site-navigation ul li").hoverIntent({
      over: function(){
              p = $(this);

              if($("ul.subnav", p).length > 0) {
                $("ul.subnav.active").hide();
                $("ul.subnav", p).show();
              };

              $("span img", p).animate({
                  top: "0px"
              }, {
                  queue: false,
                  duration: 600
              });
            },
      timeout: 500, // number = milliseconds delay before onMouseOut
      sensitivity: 1,
      interval: 250,
      out: function(){
            p = $(this);

            if($("ul.subnav", p).length > 0) {
              $("ul.subnav:not(.active)", p).hide();
              $("ul.subnav.active").show();
            };

            if (!p.hasClass("active")) {
                $("span img", p).animate({
                    top: "-25px"
                }, {
                    queue: false,
                    duration: 600
                });
            }
   }}
  );

  // Panorama Helpers
  $("#images-handler").click(function(){

      if ($('#panorama-player').length) {
        $('#panorama-player').hide();
        $("#panoramas-handler").removeClass('active-gallery-mode');
      }

      if ($('#item-images').length) {
        $('#item-images').show();
      } else {
        $('.gallery-container').show();
      }


      $(this).addClass('active-gallery-mode');

  });

  $("#panoramas-handler").click(function(){


      if ($('#item-images').length) {
        $('#item-images').hide();
      }

      if ($('.gallery-container').length) {
        $('.gallery-container').hide();
      }


      if (!$('#panorama-player').length) {

        $('#panorama-player').show();

        if ($('#item-images').length) {
          initPanos($('#pano-xml').html(), "detail");
        }

        if ($('.gallery-container').length) {
          initPanos($('#pano-xml').html(), "overview");
        }

      } else {
        $('#panorama-player').show();
      }

      $("#images-handler").removeClass('active-gallery-mode');
      $(this).addClass('active-gallery-mode');

  });

});

function initPanos(pano_xml, mode) {

  if (!mode) {
    var mode = "detail";
  }

  var flashvars = {};

  var params = {
    menu: "false",
    allowFullScreen: "true",
    allowScriptAccess: "sameDomain"
  };

  var attributes = {
    id: "panorama-player"
  };

  if (mode == "detail") {

    swfobject.embedSWF(
    "/swf/panosalado/ModuleLoader.swf?xml=/swf/panosalado/" + pano_xml,
    "item-panos",
    "630",
    "320",
    "9.0.0",
    "/swf/expressInstall.swf",
    flashvars,
    params,
    attributes
    );
  };

  if (mode == "overview") {

    swfobject.embedSWF(
    "/swf/panosalado/ModuleLoader.swf?xml=/swf/panosalado/" + pano_xml,
    "item-panos",
    "450",
    "300",
    "9.0.0",
    "/swf/expressInstall.swf",
    flashvars,
    params,
    attributes
    );
  };

  if (mode == "lightbox") {

    var attributes = {
      id: "lightbox-panorama-player"
    };

    swfobject.embedSWF(
    "/swf/panosalado/ModuleLoader.swf?xml=/swf/panosalado/" + pano_xml,
    "lightbox-pano",
    "630",
    "320",
    "9.0.0",
    "/swf/expressInstall.swf",
    flashvars,
    params,
    attributes
    );
  };



};


function jsonFlickrApi(rsp) {
   if (rsp.stat != "ok"){
    // If this executes, something broke!
    return;
   }

   //variable "s" is going to contain
   //all the markup that is generated by the loop below
   var s = "";

   //this loop runs through every item and creates HTML
   for (var i=0; i < rsp.photos.photo.length; i++) {
    photo = rsp.photos.photo[ i ];

    //notice that "t.jpg" is where you change the
    //size of the image
    t_url = "http://farm" + photo.farm +
    ".static.flickr.com/" + photo.server + "/" +
    photo.id + "_" + photo.secret + "_" + "s.jpg";

    p_url = "http://farm" + photo.farm +
    ".static.flickr.com/" + photo.server + "/" +
    photo.id + "_" + photo.secret + ".jpg";

    s +=  '<li><a rel="gallery" href="' + p_url + '">' + '<img alt="'+
    photo.title + '"src="' + t_url + '"/>' + '</a></li>';
  }


 var thumbList = "<ul>" + s + "</ul>";
 document.writeln(thumbList);

}

/*]]>*/

