﻿$(function() {

  Cufon.replace('#header div a, #sections a', { fontFamily : 'Copperplate Gothic Bold', hover: true });
  Cufon.replace('h1, strong', { fontFamily : 'Copperplate Gothic Bold' });

  var page = $('body').attr('id').split('_')[1];
  var section_id = $('#sections a.current').attr('id').substr(2);
  var photo_id = 0;
  
  if (page == 'portfolio') {

    var thumb_width = 0;  
    var thumb_limit = 0;
    var thumb_left = 0;
    
    function load_photo() {
      var el = $('#section div img.current');
      var w = el.attr('alt').split('_')[0];
      var h = el.attr('alt').split('_')[1];
      var id = el.attr('id').substr(2);
      photo_id = id;
      $("#photo img").remove();
      $("#loading").css('display', '');
      $('#photo').animate({
        "width" : w,
        "height" : h
      }, "normal", function() {
        var i = new Image();
        $(i).load(function() {
          $('#photo').append($(this)),
            $('#loading').css('display', 'none');
            $('#photo img#lp_' + id).fadeIn("fast");
        }).attr('src', '/_/images/section_photos/' + section_id + '_' + id + '.jpg').attr('id', 'lp_' + id).css('display', 'none');  
      });
    }
    
    function thumbs() {
      var thumb = $('#section div.current img:last');
      thumb_width = thumb.outerWidth();  
      thumb_limit = (thumb.position().left * - 1) + (thumb_width * 5);
      thumb_left = 0;    
      $('#section div img.current').removeClass('current');
      $('#section div.current img:first').addClass('current');
      load_photo();      
    }
    
    $('#section div img').click(function() {
      var new_photo_id = $(this).attr('id').substr(2);
      if (new_photo_id != photo_id) {
        $('#section div img.current').removeClass('current');
        $(this).addClass('current');
        load_photo();
      }
    });
       
    function thumb_arrows() {
      var left = 1;
      var right = 1;
      if (thumb_left == 0) {
        left = .25;
      }
      if (thumb_left <= thumb_limit) {
        right = .25;
      }
      $('#left').css('opacity', left);      
      $('#right').css('opacity', right);
    }
    
    function thumb_slide(inc) {
      var el = $('#section div.current');
      var new_left = thumb_left + (thumb_width * inc);
      if (new_left >= thumb_limit && new_left <= 0) {
        thumb_left = new_left;
        el.animate({ left: thumb_left });
        thumb_arrows();
      }  
    }
    $('#left').click(function() { thumb_slide(1) });
    $('#right').click(function() { thumb_slide(-1) });    
  
    $('#photos img').hover(function() {
      $(this).addClass('on');
    }, function() {
      $(this).removeClass('on');
    });
    
  }
  
  $('#sections a').click(function() {
    if (!$(this).hasClass('current')) {
      var section = $(this).attr('href').substr(1);
      var el = $('#s_' + section);
      section_id = $(this).attr('id').substr(2);
      $('#section div.current').css('display', 'none');
      $('#sections a.current, #section div.current').removeClass('current');
      $(this).addClass('current');
      el.addClass('current');
      el.css('display', '');
      if (page == 'portfolio') {
        thumbs();
      }
    }
  });
  /*
  var foo = $('#sections a[href="' + document.location.hash + '"]');
  if (foo.length != 0) {
    foo.click();
  }*/
  if (page == 'portfolio') {
    thumbs();  
    thumb_arrows();
  }
});
