var categories = {
  shown: false,
  
  init: function(){
    $('#show-categories').bind('click', function(e){
      e.preventDefault();
      if(categories.shown){
        $('#show-categories').removeClass('selected');
        categories.shown = false;
        $('#categories-list').slideUp();
      }else{
        $('#show-categories').addClass('selected');
        categories.shown = true;
        $('#categories-list').slideDown();        
      }
    });
  }
}

var views = {
  view: 'full-view',
  
  init: function(){
    $('.change-view')
      .each(function(){
        if($(this).hasClass('selected')){
          views.view = $(this).attr('id');
          views.setView();
        }
      })
      .bind('click', function(e){
        e.preventDefault();
        
        $.ajax({
          url: '/change-view.php?archies-view=' + $(this).attr('id')
        });
        
        $('.change-view').removeClass('selected');
        $(this).addClass('selected');
        
        views.view = $(this).attr('id');
        views.setView();
      });
  },
  
  setView: function(view){
       
    $('#articles').attr('class', views.view);
    
    $('#articles .media').each(function(){
      
      $(this).find('img').each(function(index){        
        if(!$(this).data('width')) $(this).data('width', $(this).attr('width'));
        if(!$(this).data('height')) $(this).data('height', $(this).attr('height'));
        if(!$(this).data('ratio')) $(this).data('ratio', $(this).data('height') / $(this).data('width'));
        
        var newWidth = (views.view == 'full-view' ? 460 : 205 );
        var newHeight = Math.round(newWidth * $(this).data('ratio'));
        
        if($(this).data('width') > newWidth){
          $(this).attr('width', newWidth);
          $(this).attr('height', newHeight);
        }else{
          $(this).attr('width', $(this).data('width'));
          $(this).attr('height', $(this).data('height'));
        }
      });
      
      $(this).find('object').each(function(index){
      
        if(!$(this).attr('original-width')) $(this).attr('original-width', $(this).attr('width'));
        if(!$(this).attr('original-height')) $(this).attr('original-height', $(this).attr('height'));
        
        if(views.view == 'full-view'){
          $(this).attr('width', $(this).attr('original-width'));
          $(this).attr('height', $(this).attr('original-height'));          
        }else{
          $(this).attr('width', 205);
          $(this).attr('height', 170);
        }
      });
      
      $(this).find('embed').each(function(index){
      
        if(!$(this).attr('original-width')) $(this).attr('original-width', $(this).attr('width'));
        if(!$(this).attr('original-height')) $(this).attr('original-height', $(this).attr('height'));
        
        if(views.view == 'full-view'){
          $(this).attr('width', $(this).attr('original-width'));
          $(this).attr('height', $(this).attr('original-height'));          
        }else{
          $(this).attr('width', 205);
          $(this).attr('height', 170);
        }
      });
      
    });
    
  }
}

$(document).ready(function(){
  categories.init();
  views.init();
});