var currentSlide = 0;
var previousSlide = 0;
var sliderWidth;
var slideCount;
  
$(document).ready(function() {


  // Intro Animation
  
  $('body.home.intro').queue(function() {
    
    var vCenter = $('body').height() / 2;
    var hCenter = $('body').width() / 2;
    $('#ilogo').css({top: vCenter - 18, left: hCenter - 320});    
    
    $('#ilogo').fadeIn(3000, 'easeInCirc').animate({top: vCenter - 70}, 700, function() {
      $('#logo').show();
      $(this).hide();
      $('#main').fadeIn(1000, 'easeInCirc').animate({height: 77}, 800, 'easeOutCirc', function() {
        $('#menu-bg').slideDown(600, function() {
          $('#galleries').fadeIn(700, function() {
            $('#bio').fadeIn(700, function() {
              $('#contact').fadeIn(700, function() {
                $('#info').fadeIn(700, function() {
                   $('.external, #copyright').fadeIn(1700, 'easeInCirc');
                });
              });
            });
          });
        });
      });
    });
  
  });
 

  // Main Nav

  $('#menu .item').mouseenter(function() {
    if(!$(this).children('.active').is(':visible')) {
      $(this).children('.active').addClass('entered');
      $(this).children('.active').stop(true, true).fadeIn(300);
    }
  }).mouseleave(function() {
    if($(this).children('.active').hasClass('entered')) {
      $(this).children('.active').stop(true, true).fadeOut(300);
    }
  });

  $('#galleries').mouseenter(function() {
    if(!$('#galleries-menu').is(':visible')) {
      $('#galleries-menu').addClass('entered');
      $('#galleries-menu').stop(true, true).slideDown(300);
    }
  }).mouseleave(function() {
    if($('#galleries-menu').hasClass('entered')) {
      $('#galleries-menu').stop(true, true).slideUp(300);
    }
  });

  $('#info').mouseenter(function() {
    if(!$('#info-menu').is(':visible')) {
      $('#info-menu').addClass('entered');
      $('#info-menu').stop(true, true).slideDown(300);
    }
  }).mouseleave(function() {
    if($('#info-menu').hasClass('entered')) {
      $('#info-menu').stop(true, true).slideUp(300);
    }
  });


  // External Links

  $('#twitter').mouseenter(function() {
    $('#twitter-active').stop().animate({left: '0'}, 200);
  }).mouseleave(function() {
    $('#twitter-active').stop().animate({left: '-21px'}, 200);
  });

  $('#facebook').mouseenter(function() {
    $('#facebook-active').stop().animate({left: '0'}, 200);
  }).mouseleave(function() {
    $('#facebook-active').stop().animate({left: '-21px'}, 200);
  });

  $('#blog').mouseenter(function() {
    $('#blog-active').stop().animate({top: '0'}, 200);
  }).mouseleave(function() {
    $('#blog-active').stop().animate({top: '-20px'}, 200);
  });

  $('#client-area').mouseenter(function() {
    $('#client-area-active').stop().animate({top: '0'}, 200);
  }).mouseleave(function() {
    $('#client-area-active').stop().animate({top: '-20px'}, 200);
  });

  $('#hotd').mouseenter(function() {
    $('#hotd-active').stop().animate({right: '0'}, 200);
  }).mouseleave(function() {
    $('#hotd-active').stop().animate({right: '-135px'}, 200);
  });
  
  
  // Gallery

  $('#fade').animate({bottom: '+=500px'}, 6000, 'easeOutCirc');

  $('#gallery').queue(function() {
    var title = $('#image-' + (currentSlide + 1)).attr('file').substr(3).replace('-','').replace(/.jpg/i,'');
    $('#image-title').html(title);
  });

  slideCount = $('#frame .image').size();
  sliderWidth = slideCount * 36;
  
  $('#slider-content').width(sliderWidth);

  $('#bottom-left').click(function() {
    $('#slider').animate({scrollLeft: '-=150px'});
  });

  $('#bottom-right').click(function() {
    $('#slider').animate({scrollLeft: '+=150px'});
  });

  $('#top-left').click(function() {
    prevSlide()
  });

  $('#top-right').click(function() {
    nextSlide();
  });

  $('.image').click(function() {
    nextSlide();
  });

  $('.index').click(function() {
    var id = parseInt($(this).html());
    showSlide(id - 1);
  });
  

  // Default form entry handling

  $('textarea.js').focus(function() {
    if($(this).text() == $(this).attr('default')) {
      $(this).text('');
    }
  });

  if($('textarea.js').text() == '') {
    $('textarea.js').text($('textarea.js').attr('default'));
  }

  $('input.js').focus(function() {
    if($(this).val() == $(this).attr('default')) {
      $(this).val('');
    }
  }).blur(function() {
    if($(this).val() == '') {
      $(this).val($(this).attr('default'));
    }
  });

  $('input.js').each(function() {
    if($(this).val() == '') {
      $(this).val($(this).attr('default'));
    }  
  });

});


function showSlide(index) {    
  previousSlide = currentSlide;
  currentSlide = index;
  
  if (currentSlide != previousSlide) {
    $('#image-' + (previousSlide + 1)).fadeOut(900);
    $('#image-' + (currentSlide + 1)).fadeIn(900);
    $('#index-' + (previousSlide + 1)).css({fontWeight: 'normal', fontSize: '12px'});
    $('#index-' + (currentSlide + 1)).css({fontWeight: 'bold', fontSize: '13px'});
  }

  var w = $('#slider-content').width();
  var move = (w - 542) * (index / slideCount);
  $('#slider').animate({scrollLeft: move}, 'slow');
  
  var title = $('#image-' + (currentSlide + 1)).attr('file').substr(3).replace('-','').replace(/.jpg/i,'');
  $('#image-title').html(title);
  $('#number').html(index + 1);
}

function nextSlide() {
  var index = 0;
  
  if(currentSlide < slideCount - 1) {
    index = currentSlide + 1;
  } else {
    index = 0;
  }

  showSlide(index);
}

function prevSlide() {
  var index = 0;
  
  if(currentSlide > 0) {
    index = currentSlide - 1;
  } else {
    index = slideCount - 1;
  }

  showSlide(index);
}

