(function($){

var ns = 'Archive';

window[ns] = function () {
  this.ajax_url = AJAX_BASE + "archive";
  this.hiding = false;
  
  this.catID = false;
};

window[ns].prototype = {
  
  prepare: function () {
    this.wrap = $('<div>').css('opacity',1).appendTo('#content');
    
    if ( site.page == 'category' ) {
      this.catID = getCatID( site.slug );
    }
  },
  
  load: function () {
    this.wrap.load( this.ajax_url, $.proxy( this.done, this ) );
  },
  
  done: function () {
    this.root = this.wrap.find('.archive');
    
    var options = { filter: this.catID };
    if ( site.page != 'home' && site.page != 'category' ) {
      options.hidden = true;
    }
    this.floatingInstance = new Floatingize( this.root, options );

    this.buttons = this.wrap.find('.absolute').find('.project-button');
    
    this.observe();
    
    site.loadComplete('archive');
  },
  
  observe: function () {
    this.buttons.click( $.proxy( this.clickProject, this ) );
  },
  
  clickProject: function ( event ) {
    var me       = event.currentTarget;
    var id       = me.getAttribute('projectid');
    var category = me.getAttribute('category');
    
    $(me).addClass('visited');
    site.set('project', getProjectSlug( id ), category, getProjectTitle( id ) );
    this.loadProject( id );
  },
  
  loadProject: function ( id ) {
    this.hide();
    this.project = new Project( id, {
        closeCallback: $.proxy( this.closeProject, this )
      });
  },
  
  closeProject: function () {
    this.show();
    
    var filter = this.floatingInstance.options.filter;
    
    if ( ! filter ) {
      site.set('home');
    } else {
      site.set('category', getCategorySlug( filter ), getCategoryTitle( filter ) );
    }
    
    this.floatingInstance.show();
  },
  
  show: function () {
    this.hiding = false;
    if ( $.browser.msie )
      this.wrap.fadeIn();
    else
      this.wrap.animate({opacity:1.0});
  },
  
  hide: function () {
    this.hiding = true;
    if ( $.browser.msie )
      this.wrap.fadeOut();
    else
      this.wrap.animate({opacity:0.0});
  },
  
  /**
   * when clicking categories button
   */
  switchCategory: function ( event ) {

    if ( this.hiding ) {
      if ( site.page == 'project' ) {
        this.project.exit();
      } else if ( site.page == 'page' ) {
        page.exit();
      }
      this.show();
    }
    
    var target = $(event.currentTarget);
    this.floatingInstance.switchFilter( target );

  }
  
}

})(jQuery);
