if (typeof curvyCorners != 'function') {
  var curvyCorners = function() {};
}


$(document).ready(function() {
  if ($('#carousel').length > 0) {
    initCarousel();  
  }
  
  if ($('.project #page .images .thumbs').length > 0) {
    $('.project #page .images .thumbs a').click(function() {
      $('.project #page .images .large .current').removeClass('current');
      $($(this).attr('href')).addClass('current');
      $(this).parents('ul').find('.current').removeClass('current');
      $(this).parents('li').addClass('current');
      
      return false;
    });
  }
  
  if ($('.filters').length > 0) {
    $('.filters .current').removeClass('current');
    $('.filters .field select').hide();
    $('.filters input:checked').siblings('label').addClass('current').siblings('select').show();
    
    $('.filters input[type="radio"]').click(function() {
      $('.filters .current').removeClass('current');
      $('.filters .field select').hide();
      $(this).siblings('label').addClass('current').siblings('select').show();
    });
  }
  
  $('#page .title').each(function() {
    curvyCorners({
      tl: { radius: 5 },
      tr: { radius: 5 },
      antiAlias: true
    }, this);
  });
  
  $('#page .body').each(function() {
    curvyCorners({
      bl: { radius: 5 },
      br: { radius: 5 },
      antiAlias: true
    }, this);
  });
  
  $('#comments').each(function() {
    curvyCorners({
      tl: { radius: 5 },
      tr: { radius: 5 },
      bl: { radius: 5 },
      br: { radius: 5 },
      antiAlias: true
    }, this);
  });
  
  $('.projects #page .filters').each(function() {
    curvyCorners({
      tl: { radius: 5 },
      tr: { radius: 5 },
      antiAlias: true
    }, this);
  });
  
  $('.contact #page .side .map').each(function() {
    curvyCorners({
      tl: { radius: 3 },
      tr: { radius: 3 },
      bl: { radius: 3 },
      br: { radius: 3 },
      antiAlias: true
    }, this);
  });
  
  $('#flickr a').lightBox({fixedNavigation:true});
  $('#imagespark a').lightBox({fixedNavigation:true});
});

var carouselTimer;
var carouselAnimating = false;
var carouselDelay = 5000;

var initCarousel = function() {
  $('#carousel .items a').click(function() {
    if (!carouselAnimating &! $(this).parent().hasClass('current')) {
      carouselAnimating = true;
      
      var cname = $(this).parent().attr('class');
      var id = $(this).attr('href');
      
      $('#carousel .overlay').removeClass('top middle bottom').addClass('overlay '+cname);
      
      $(id).show();
      
      $('#carousel .main .current').fadeOut('slow', function() {
        $(this).removeClass('current');
        $(id).addClass('current');
        
        carouselAnimating = false;
      });
      
      $('#carousel .items .current').removeClass('current');
      $(id+'_list').addClass('current');
    } else if ($(this).parent().hasClass('current')) {
      var id = $(this).attr('href');
      
      window.location.href = $(id).children('a').attr('href'); 
    }
    
    return false;
  });
  
  $('#carousel .main li').hover(function() {
    if (!carouselAnimating) {
      $(this).find('img').animate({
        top: '0px',
        left: '0px',
        width: '650px',
        height: '160px'
      });
      
      var top = 233 - $(this).find('.info').outerHeight();
      
      $(this).find('.info').animate({
        top: top + 'px'
      });
    }
  }, function() {
    if (!carouselAnimating) {
      $(this).find('img').animate({
        top: '0px',
        left: '-132px',
        width: '914px',
        height: '225px'
      });
      
      $(this).find('.info').animate({
        top: '224px'
      });
    }
  });
  
  $('#carousel').hover(function() {
    clearTimeout(carouselTimer);
  }, function() {
    carouselTimer = setTimeout('autoCarousel()', carouselDelay);
  });
  
  $('#carousel .navigation .prev').click(function() {
    prevCarousel();
    
    return false;
  });
  
  $('#carousel .navigation .next').click(function() {
    nextCarousel();
    
    return false;
  });
  
  carouselTimer = setTimeout('autoCarousel()', carouselDelay);
};

var prevCarousel = function() {
  if (!carouselAnimating) {
    carouselAnimating = true;
    
    var current = $('#carousel .items .current');
    var top = $('#carousel .items .top');
    var middle = $('#carousel .items .middle');
    var bottom = $('#carousel .items .bottom');
    var id = null;
    
    if (current.prev('li').length == 0) {
      $('#carousel .items li:last').addClass('current');
    } else {
      current.prev('li').addClass('current');     
    }
    
    if (top.prev('li').length == 0) {
      $('#carousel .items li:last').addClass('top');
    } else {
      top.prev('li').addClass('top');
    }
    
    current.removeClass('current');
    top.removeClass('top').addClass('middle');
    middle.removeClass('middle').addClass('bottom');
    bottom.removeClass('bottom');
    
    var id = $('#carousel .items .current a').attr('href').substring(1, $('#carousel .items .current a').attr('href').length);
    var currentImg = $('#carousel .main .current');
    var prevImg = $('#'+id);
    
    prevImg.show();
    currentImg.fadeOut('slow', function() {
      prevImg.addClass('current');
      currentImg.removeClass('current');
      
      carouselAnimating = false;
    });
  }
};

var nextCarousel = function() {
  if (!carouselAnimating) {
    carouselAnimating = true;
    
    var current = $('#carousel .items .current');
    var top = $('#carousel .items .top');
    var middle = $('#carousel .items .middle');
    var bottom = $('#carousel .items .bottom');
    
    if (current.next('li').length == 0) {
      $('#carousel .items li:first').addClass('current');
    } else {
      current.next('li').addClass('current');
    }
    
    if (bottom.next('li').length == 0) {
      $('#carousel .items li:first').addClass('bottom');
    } else {
      bottom.next('li').addClass('bottom');
    }
    
    current.removeClass('current');
    top.removeClass('top');
    middle.removeClass('middle').addClass('top');
    bottom.removeClass('bottom').addClass('middle');
    
    var id = $('#carousel .items .current a').attr('href').substring(1, $('#carousel .items .current a').attr('href').length);
    var currentImg = $('#carousel .main .current');
    var nextImg = $('#'+id);
    
    nextImg.show();
    currentImg.fadeOut('slow', function() {
      nextImg.addClass('current');
      currentImg.removeClass('current');
      
      carouselAnimating = false;
    });
  }
};

var autoCarousel = function() {
  nextCarousel();
  
  carouselTimer = setTimeout('autoCarousel()', carouselDelay);
};


