$(function() {
 
  $('.scrollable').scrollable();
  var scrollApi = $(".scrollable").data("scrollable");

  $("#section").change(function() {
    if (!$(this).val())
      $(this).addClass("home");
    else
      $(this).removeClass("home");
    location.href = MAIN_URL+$(this).val();  
  });

  if ($("body.home").length) {
    $(window).load(function() {
      $('#slider')
        .show()
        .nivoSlider({
          effect: 'fade',
          startSlide: 0,
          directionNav: false,
          keyboardNav: false,
          captionOpacity: 0.8
        });
    });
  }

  if ($("body.portfolio").length) {
    $('#video').hide();

    $("#media_next a").click(function() {
      var nextItem = $('#media_nav li.selected').next();
      if ($('#media_nav li.selected').html() == $('#media_nav li').last().html()) nextItem = $('#media_nav li').first();
      nextItem.find('a').click();
      scrollApi.seekTo(nextItem.index());
      return false;
    });

    $("#media_prev a").click(function() {
        var prevItem = $('#media_nav li.selected').prev();
        if ($('#media_nav li.selected').html() == $('#media_nav li').first().html()) prevItem = $('#media_nav li').last();
        prevItem.find('a').click();
        scrollApi.seekTo(prevItem.index());
        return false;
     });

    var video = null;
    var placeholderImageSrc = $('.mainImg').attr("src");

    $("#media_nav a").click(function() {
      // check whether file is of image or video type
      if ($(this).attr('href').lastIndexOf(".mp4") != -1) {
        $('.mainImg').attr("src", placeholderImageSrc).hide();
        $('#sublime_videos').empty();
        $('#video')
          .empty()
          .show();

        video = $("<video/>")
                  .addClass("sublime")
                  .attr("width", 750)
                  .attr("height", 422)
                  .attr("preload", "none")
                  .attr("poster", videoPosters[$(this).attr('id')])
                  .append($("<source/>").attr("src", $(this).attr('href')));
        $("#video").append(video);

        // we'll assume that if this function exists it needs to be called
        // it's probably not calling for the first video but that's prepared by the Sublime JavaScript include
        if (typeof(sublimevideo.prepare) != "undefined") sublimevideo.prepare();

        var caption = $(this).find('img').attr('alt');
        $('#caption').html(caption);
        $('#media_nav li.selected').removeClass('selected');
        $(this).parent().addClass('selected');

      } else {
        if (typeof(sublimevideo.stop) != "undefined") sublimevideo.stop();
        $('#video')
          .hide()
          .empty();
        $('#sublime_videos').empty();

        var imgSrc  = $(this).attr('href');
        var caption = $(this).find('img').attr('alt');
  
        $('.mainImg').attr('src',imgSrc).show();
        $('#caption').html(caption);

        $('#media_nav li.selected').removeClass('selected');
        $(this).parent().addClass('selected');
      }
        return false;
    });

    // click the first entry to load the first image or video
    if ($("#media_nav a").length) {
      if ($("#media_nav a[href$=.mp4]").length && ($("#media_nav a:first").attr('href').lastIndexOf(".mp4") == -1)) {
        // if at least one entry has a video but not the first once we have to do a click hack so SublimeVideo loads properly
        $("#media_nav a[href$=.mp4]").first().click();
        setTimeout(function() { $("#media_nav a:first").click(); }, 250);
      } else {
        // otherwise just click the first entry
        $("#media_nav a:first").click();
      }
    }
  }
});

