// http://www.asual.com/jquery/address/docs/

function viewSection(selector) {

  var container = jQuery('#content');
  var element_position = Number(jQuery(selector).position().left);

  var navigation = jQuery('#navigation');
  var container_left = -element_position + 10;

  window.current_section = selector;

  if (selector.match('company')) {
    jQuery('.reel').hide('slow');
  } else {
    jQuery('.reel').show();
  }

  container.animate({
    left: container_left
  });
}

jQuery(document).ready(function($) {
  window.current_section = '#home';

  $("img.lazy").lazyload();

  $('.vj_section').jScrollPane();

  $(window).resize(function(){
    $('.vj_section').jScrollPane().reinitialize;
  });

  // Remove gallery from content area and stick it in under the feautered image
  if ($('#feature').length > 0) {
    var gallery = $('.gallery').detach();
    $('#gallery_container').html(gallery);
    // $('.gallery a').lightBox();
  }

  $("#slideshow ul li:first").addClass('first');
  $("#slideshow ul li:last").addClass('last');

  $("#slideshow ul").cycle({
    pager:'#nav',
    prev:'#nav-prev',
    next:'#nav-next',
    pagerAnchorBuilder: pager,
    pause: 1,
    before: function(current_slide, next_slide, options, forward_flag) {
      if($(next_slide).hasClass('first')) {
        $("#nav").css('left', 0);
        return;
      }
      if($("#nav .activeSlide").size() == 0) { return; }
      nav_width = $("#nav").width();
      pager_item = $("#nav .activeSlide").next();
      pager_item_width = pager_item.width();
      pager_item_left = pager_item.position().left
      shift_distance = nav_width - (pager_item_left + pager_item_width + 15)

      if(shift_distance < 0) {
        $("#nav").css('left', shift_distance);
      }
    }
  });
  $("a.lb-image").lightBox({width: "80%"});

  $("#details-slideshow ul").cycle({
    pager:'#nav',
    prev:'#nav-prev',
    next:'#nav-next',
    pagerAnchorBuilder: pagerdetails,
    pause: 1
      });

  function pager(index, slide) {
        return '<a href="#">' + $(slide).attr('rel') + '</a> ';
    };

  function pagerdetails(index, slide) {
        return '<a href="#">' + (index+1) + '</a> ';
    };

  $(document).keydown(function(e) {
    var keyCode = e.keyCode || e.which,
        arrow = {left: 37, up: 38, right: 39, down: 40 };

    switch (keyCode) {
      case arrow.left:
        $.address.path('previous');
      break;
      case arrow.right:
        $.address.path('next');
      break;
    }
  });

  $.address.init(function() {
    // container.css({left: 0});
  });

  $.address.change(function(event) {
    if (event.path.match(/next|previous/)) {
      var index = $(current_section).index('.vj_section');

      if (event.path.match(/next/)) {
        index += 1;
      } else {
        index -= 1;
      }

      if (index >= 0 && index < $('.vj_section').length) {
        var section = $($('.vj_section').get(index));
        $.address.path(section.attr('id'));
      } else {
        $.address.path(current_section.replace('#', '/'));
      }
    } else {
      var sectionID = $.address.path().replace(/^\//, '#');
      if (sectionID === '#'){
        sectionID = '#' + $('.vj_section:first').attr('id');
      }
      viewSection(sectionID);
    }

  });


  $('#left_arrow, #right_arrow').each(function() {
    var arrow = $(this).find('div');
    setTimeout(function() {
      arrow.fadeOut(1000);
    }, 1200);
  });

  $('#left_arrow, #right_arrow').hover(function() {
    $(this).find('div').fadeIn(400);
  }, function() {
    $(this).find('div').fadeOut(400);
    var middle = $(this).height() / 2 - 25;
    $(this).find('div').animate({top: middle}, 'slow');
  }).mousemove(function(event) {
    $(this).find('div').css({top: (Number(event.pageY) - 25) + 'px' });
  });


  //$('#work_carousel').barousel({});
  // $('.carousel').carousel({
  //   pagination: true,
  //   nextBtn: '<span>Next</span>',
  //   prevBtn: '<span>Previous</span>',
  //   nextBtnInsert: "insertAfter",
  //   prevBtnInsert: "insertAfter",
  //   btnsPosition: "outside"
  // });

  
  $('#work .item .thumb').mouseover(function() {
    $(this).fadeOut(200).parent().find('.thumb-over').fadeIn(200);
  });
  $('#work .item .thumb-over').mouseout(function() {
    $(this).fadeOut(200).parent().find('.thumb').fadeIn(200);
  });

  // Replace Feature video with source from thumbnail gallery
  $('.video_gallery a').click(function() {
    $('#feature').html($(this).next().html());
    return false;
  });


  // on the development site links go to vladimirjones.com since we share the db and it's configured for that domain
  // rewrite links to stay on the development domain
  // unfortunately the problem goes beyond just links, since all assets are still pulled from production, but this helps in some cases
  if (window.location.hostname === 'vj.voltagead.com') {
    $('a').each(function() {
      var new_link = $(this).attr('href').replace('vladimirjones.com', 'vj.voltagead.com');
      $(this).attr('href', new_link);
    });
  }

});
