
$(document).ready(function() {

  // Section one
  $(".section_one_portfolio_scroller").scrollable({
    clickable: false,
    globalNav: true,
    items: ".vitems_one",
    next: ".vnav_one_down",
    prev: ".vnav_one_up",
    size: 1,
    onStart: function(e, n) {
      // Set the huge numbers
      $('#slidecounter').removeClass().addClass('slide_' + (parseInt(n, 10) + 1) + '_of_' + this.getSize());
    },
    vertical: true
    }).navigator({
      navi: 'div.vnav_one'
    });

  $(".section_one_project_one").scrollable({clickable: false, globalNav: true, items: ".hitems_one_one", next: '#section_one_next_one', prev: '#section_one_prev_one', size:1}); //.circular();

  $(".section_one_project_two").scrollable({clickable: false, globalNav: true, items: ".hitems_one_two", next: '#section_one_next_two', prev: '#section_one_prev_two', size:1});

  $(".section_one_project_three").scrollable({clickable: false, globalNav: true, items: ".hitems_one_three", next:'#section_one_next_three', prev:'#section_one_prev_three', size:1});

  $(".section_one_project_four").scrollable({clickable: false, globalNav: true, items: ".hitems_one_four", next:'#section_one_next_four', prev:'#section_one_prev_four', size:1});

  $(".section_one_project_five").scrollable({clickable: false, globalNav: true, items: ".hitems_one_five", next:'#section_one_next_five', prev:'#section_one_prev_five', size:1});
  
  $(".section_one_project_six").scrollable({clickable: false, globalNav: true, items: ".hitems_one_six", next:'#section_one_next_six', prev:'#section_one_prev_six', size:1});
    // end section one


  // Section two
  $(".section_two_portfolio_scroller").scrollable({
    clickable: false,
    globalNav: true,
    items: ".vitems_two",
    next: ".vnav_two_down",
    prev: ".vnav_two_up",
    size: 1,
    onStart: function(e, n) {
      // Set the huge numbers
      $('#slidecounter').removeClass().addClass('slide_' + (parseInt(n, 10) + 1) + '_of_' + this.getSize());
    },
    vertical: true
  }).navigator({
    navi: 'div.vnav_two'
  });

  $(".section_two_project_one").scrollable({clickable: false, globalNav: true, items: ".hitems_two_one", next:'#section_two_next_one', prev:'#section_two_prev_one', size: 1});

  $(".section_two_project_two").scrollable({clickable: false, globalNav: true, items: ".hitems_two_two", next:'#section_two_next_two', prev:'#section_two_prev_two', size: 1});

  $(".section_two_project_three").scrollable({clickable: false, globalNav: true, items: ".hitems_two_three", next:'#section_two_next_three', prev:'#section_two_prev_three', size: 1 });

  $(".section_two_project_four").scrollable({clickable: false, globalNav: true, items: ".hitems_two_four", size: 1});
    // end section two


  // Section three
  $(".section_three_portfolio_scroller").scrollable({
    clickable: false,
    globalNav: true,
    items: ".vitems_three",
    next: ".vnav_three_down",
    prev: ".vnav_three_up",
    size: 1,
    onStart: function(e, n) {
      // Set the huge numbers
      $('#slidecounter').removeClass().addClass('slide_' + (parseInt(n, 10) + 1));
    },
    vertical: true
    }).navigator({
      navi: 'div.vnav_three'
    });

  $(".section_three_project_one").scrollable({clickable: false, globalNav: true, items: ".hitems_three_one", size: 1});
    // end section three


  var VISIBLE_SECTION = '',
  fade_in_section = function(name) {
    if (VISIBLE_SECTION === '') {
      return;
    }

    // bold the nav
    $('#nav_to_' + name).css('color', '#fff');

    // fade in the nav
    $('#nav_for_' + name).fadeIn();

    // fade in the content
    $('#' + name).fadeIn();
  },
  transition_to_section = function(new_section) {
    var prev_section = VISIBLE_SECTION;
    
    if (VISIBLE_SECTION === '') {
      VISIBLE_SECTION = new_section;
      fade_in_section(VISIBLE_SECTION);
      $('body').addClass('bg_' + VISIBLE_SECTION);
      return;
    }

    // un-bold the current nav
    $('#nav_to_' + VISIBLE_SECTION).css('color', '#5b595a');

    // Fade out the current sub-nav
    $('#nav_for_' + VISIBLE_SECTION).fadeOut('fast');

    $('#slidecounter').fadeOut('fast');

    // fade out the current portfolio
    $('#' + VISIBLE_SECTION).fadeOut('fast', function() {
      // Once complete, swap the global to the new section name
      VISIBLE_SECTION = new_section;
  
      // Switch backgrounds
      $('body').removeClass('bg_' + prev_section).addClass('bg_' + new_section);
  
      if ((new_section === "section_one") || (new_section === "section_two")) {
        // Reset the huge numbers
        try {
          var scrlr = $('.' + new_section + '_portfolio_scroller').scrollable(),
            n = parseInt(scrlr.getPageIndex(), 10) + 1;
          $('#slidecounter').removeClass().addClass('slide_' + n + '_of_' + scrlr.getSize());
          $('#slidecounter').fadeIn('fast');
        } catch (err) {
        }
      }

      fade_in_section(VISIBLE_SECTION);
    });
  },
  show_section_one = function() {
    if (VISIBLE_SECTION === 'section_one') {
      return;
    }
    transition_to_section('section_one');
  },
  show_section_two = function() {
    if (VISIBLE_SECTION === 'section_two') {
      return;
    }
    transition_to_section('section_two');
  },
  show_section_three = function() {
    if (VISIBLE_SECTION === 'section_three') {
      return;
    }
    transition_to_section('section_three');
  },
  //show_section_four = function() {
  //  if (VISIBLE_SECTION === 'section_four') {
  //    return;
  //  }
  //  transition_to_section('section_four');
  //},
  show_section_five = function() {
    if (VISIBLE_SECTION === 'section_five') {
      return;
    }
    transition_to_section('section_five');
  };

 $('#nav_to_section_one').click(show_section_one);
 $('#nav_to_section_two').click(show_section_two);
 $('#nav_to_section_three').click(show_section_three);
 //$('#nav_to_section_four').click(show_section_four);
 $('#nav_to_section_five').click(show_section_five);

 show_section_one();

 // Wire-up the mobile quote loading
 $('#smalltalk_trigger').click(function() {
  $.get("http://" + document.location.host + "/smalltalk/sayings.cgi", {id: "0"}, function(data) {
    $('#smalltalk_quote').html(data);
  });
 });

});

