$(document).ready(function(){

  $('a[href^="http://"]:not([href*='+document.domain+'])').attr("target", "_blank");

  // slideshow
  setTimeout(function() {
    if ($("#gallery").length > 0) {
      $("#gallery").cycle({
        cleartype:  1,
        timeout: 0,
        speed: 500,
        before: onBefore,
        after: onAfter,
        //next: "#right-arrow",
        //prev: "#left-arrow",
        pager:  '#pagination',
        pagerEvent: 'mouseover',
        pagerClick: function(zeroBasedSlideIndex, slideElement) {
          //window.location = $(slideElement).find("a").attr("href");
        },
        pagerAnchorBuilder: function(idx, slide) {
          // return selector string for existing anchor
          return '#pagination li:eq(' + idx + ')';
        }
      });
    }
  }, 7000);
  function onAfter(curr, next, opts) {
    $(next).find('.caption').fadeIn(400);
  }
  function onBefore(curr, next, opts) {
    // $(curr).find('.caption').fadeOut(400);
  }
  setTimeout(function(){
    $("#slideshow-start").fadeOut(400);
  },6000);

  // current
  function filterPath(string) {
    return string
      .replace(/^\//,'')
      .replace(/(index|default).[a-zA-Z]{3,4}$/,'')
      .replace(/\/$/,'');
  }
  $("#nav a").filter(function() {
    return filterPath(this.pathname) == filterPath(location.pathname);
  }).parent().addClass('current');

  // swap value
  var swapValues = [];
  $(".swap").each(function(i){
    swapValues[i] = $(this).val();
    $(this).focus(function(){
      if ($(this).val() == swapValues[i]) {
        $(this).val("");
      }
    }).blur(function(){
      if ($.trim($(this).val()) == "") {
        $(this).val(swapValues[i]);
      }
    });
  });

  $("#side-menu li:last").addClass("last");

  // hovers
  $("#nav a .spanbg").hide();
  $("#nav a").hover(function(){
    $(this).find(".spanbg").stop(true,true).fadeIn(50);
  },function(){
    $(this).find(".spanbg").stop(true,true).fadeOut(200);
  });
  $("#nav li.current a .spanbg").fadeIn(50);

  $("#pagination a").hover(function(){
    $(this).find("img").stop().animate({
      opacity: "1"
    }, 100);
  },function(){
    $(this).find("img").stop().animate({
      opacity: ".6"
    }, 500);
  });

  $("#side-menu a").click(function(e){
    e.preventDefault();
    $("#side-menu li").removeClass("current");
    $(this).parent().addClass("current");
    var toload = $(this).attr('href');
    $("#main li:visible").fadeOut(200,function(){
      $(toload).fadeIn(200);
    });
  });

  function goToFilm() {
    var h = window.location.hash.toString();
    if (h != "" && $(h).length > 0) {
      $("#main li:visible").hide();
      $(h).parent().parent().show();
//      $.scrollTo($(h), 100);
    }
  } goToFilm();

  $("#search-results a").each(function(){
    var link = parseUri($(this).attr('href')).path;
    $(this).attr('href',link.split("/")[1]);
  });

  // swish
  $("#main a").swish();
  $("#nav a").swish({hoverclass:"navhover"});

  // viz hacks
  $("#what-our-customers-are-saying h3").each(function(){
    var $this = $(this);
    $(this).parent().append($this);
  });

  $("#our-team .film").each(function(){
    var href = $(this).find("h3").next().attr('href');
    $(this).find("h3").wrapInner($("<a href='"+href+"'></a>"));
  });

  // $("#our-facility #films").masonry();

});

