var canvas = {
  init: function(){
    $('#resize-window').live('click', function(e){
      e.preventDefault();
      canvas.setSize();
    });
    
    canvas.checkSize();
  },
  
  checkSize: function(){
    if($(window).height() < 580 || $(window).width() < 960){
      if(!$('#overlay').length){
        $('body').append('<div id="overlay" style="width: ' + $(window).width() + 'px; height: ' + $(window).height() + 'px;"><div id="screensize-message">Your window isn\'t big enough.<br/>Please <a href="#" id="resize-window">click here</a> to adjust!</div></div>');
      }else{
        $('#overlay').css({
          'width': $(window).width() + 'px',
          'height': $(window).height() + 'px'
        });
      }
    }else{
      $('#overlay').remove();
    }
  },
  
  setSize: function(){
    window.resizeTo(1000,750);
  }
};

var menu = {
  init: function(){
    
    $('#submenu .year.active').next().show();
    $('#submenu .year').live('click', function(e){
      e.preventDefault();
      
      next = $(this).next();
      
      $('#submenu .year').removeClass('active');
      $(this).addClass('active');
      $('#submenu .months').not(next).slideUp();
      
      next.slideDown();
    });
  }
};

var everydayProject = {
  photos: new Object(),
  animate: true,
  currentPhoto: null,
  prevMonth: null,
  nextMonth: null,
  month: null,
  time: null,
  
  init: function(){
    
    setTimeout(function() { window.scrollTo(0, 1); }, 100);
    
    if($('#gallery, #thumbs').length){
      
      everydayProject.getMonth($('.current', '#gallery').attr('title'));
      
      $('.prev', '#gallery').live('click', function(e){
        e.preventDefault();
        
        everydayProject.animateGallery('prev', $(this).attr('title'));
      });
      
      $('.next', '#gallery').live('click', function(e){
        e.preventDefault();
        
        everydayProject.animateGallery('next', $(this).attr('title'));
      });
      
    };
  },
  
  animateGallery: function(direction, photo){
        
    if(everydayProject.animate){
      everydayProject.animate = false;
                
      if(everydayProject.photos[photo.substr(0, 7)][photo]){
        
        $('.photo', '#photos').removeClass('current');
        
        if('prev' == direction){
          $('.photo:eq(0)', '#photos')
            .animate({
              'top': -20,
              'width': 1,
              'height': 1,
              'opacity': 0
            }, function(){
              $(this).remove()
            });
          $('.photo:eq(1)', '#photos')
            .animate({
              'top': 0,
              'width': 67,
              'height': 49,
              'opacity': 0.3
            });
          $('.photo:eq(2)', '#photos')
            .animate({
              'top': 95,
              'width': 135,
              'height': 100,
              'opacity': 0.6
            })
            .find('.info').hide();
          $('.photo:eq(3)', '#photos')
            .animate({
              'top': 241,
              'width': 270,
              'height': 200,
              'opacity': 1
            })
            .addClass('current');
          $('.photo:eq(4)', '#photos')
            .animate({
              'top': 487,
              'width': 135,
              'height': 100,
              'opacity': 0.6
            });
          $('<div class="photo"></div>')
            .appendTo($('#photos'))
            .css({
              'top': 704,
              'width': 4,
              'height': 3,
            })
            .animate({
              'top': 633,
              'width': 67,
              'height': 49,
              'opacity': 0.3              
            }, function(){
              everydayProject.animate = true;
            });
        }else{
          $('.photo:eq(0)', '#photos')
            .animate({
              'top': 95,
              'width': 135,
              'height': 115,
              'opacity': 0.6
            });
          $('.photo:eq(1)', '#photos')
            .animate({
              'top': 241,
              'width': 270,
              'height': 230,
              'opacity': 1
            })
            .addClass('current');
          $('.photo:eq(2)', '#photos')
            .animate({
              'top': 487,
              'width': 135,
              'height': 115,
              'opacity': 0.6
            })
            .find('.info').hide();
          $('.photo:eq(3)', '#photos')
            .animate({
              'top': 633,
              'width': 67,
              'height': 54,
              'opacity': 0.3
            });
          $('.photo:eq(4)', '#photos')
            .animate({
              'top': 718,
              'width': 1,
              'height': 1,
              'opacity': 0
            }, function(){
              $(this).remove()
            });
          $('<div class="photo"></div>')
            .prependTo($('#photos'))
            .css({
              'top': -10,
              'width': 1,
              'height': 1,
              'opacity': 0
            })
            .animate({
              'top': 0,
              'width': 67,
              'height': 54,
              'opacity': 0.3              
            }, function(){
              everydayProject.animate = true;
            });
        };
        
        $('#photo-info').fadeOut('fast', function(){
          $(this).html($('.current .info', '#gallery').html()).fadeIn();
        });
        
      }
    }else{
      everydayProject.getMonth(photo);
    }
  },
  
  getMonth: function(day){
    everydayProject.time = new Date(day.substr(0, 4), day.substr(5, 2), day.substr(8, 2));
    $.getJSON(everydayProjectUrl + '/' + everydayProject.time.getFullYear() + '/' + everydayProject.time.getMonth() + '.json', function(data){
      everydayProject.prevMonth = data.prevMonth;
      everydayProject.nextMonth = data.nextMonth;
      everydayProject.month = data.month;
      
      everydayProject.photos[data.month] = new Object();
      for(photo in data.photos){
        everydayProject.photos[data.month][data.photos[photo].date] = data.photos[photo];
      }
    });
  }
}

var form = {
  init: function(){
    $('#contactForm').bind('submit', function(e){
      e.preventDefault();
      
      $.post($(this).attr('action'), $(this).serialize(), function(data){
        alert(data);
      });
    });
  }
}

$(document).ready(function(){
  $('body').addClass('js');
  menu.init();
  form.init();
//  everydayProject.init();
  
  $('input, textarea').fhInputClear();
});
