(function(){
  if ( window.selikoff ) var selikoff = window.selikoff;
  
  var selikoff = window.selikoff = function() {return new selikoff.init();};
  
  selikoff.apply = function(o, c, defaults){
      if(defaults){selikoff.apply(o, defaults);}
      if(o && c && typeof c == 'object'){for(var p in c){o[p] = c[p];}}
      return o;
  };
  
  selikoff.apply(selikoff, {
    version: '1.0.0',
    activeImage: '',
    
    init: function() {
      return this.version;
    },
    
    initialize: function() {
      var theP = jQuery('#content .description p').eq(0);
      
      if (theP.length == 1 && theP.next().hasClass('gallery-main')) {
        if (theP.html() == '') {
          theP.remove();
        }
      }
      
      delete theP;
      
      var loader = jQuery('.gallery-loader');
      if (loader.length > 0) {
        var img = {};
        var containerHeight = loader.parent().height();
        var height = loader.find('.gallery-loader-content').height();
        
        loader.find('.gallery-loader-content').css('padding', ((containerHeight - height) / 2) + 'px 0px 0px 0px');
        
        var items = jQuery('.gallery-item');
        
        items.each(function(index){
          if (index == 0) {
            jQuery(this).find('a').addClass('selected');
          }
          
          jQuery(this).find('a').attr('id', 'image-' + index);
          
          img = jQuery('<img id="main-image-' + index + '" class="main-image" src="' + jQuery(this).find('a').attr('href') + '" width="' + WORK_IMAGE_WIDTH + '" alt="' + jQuery(this).find('a').attr('title') + '" />').appendTo('.gallery-items');
          
          if (index == 0) {
            img.fadeIn();
            selikoff.activeImage = 'image-' + index;
          }
          
          if (index == (items.length - 1)) {
            loader.fadeOut();
          }
        });
        
        items.find('a').click(function(event){
          jQuery('.gallery-item a').removeClass('selected');
          
          var a = jQuery(this).addClass('selected');
          
          jQuery('#main-' + selikoff.activeImage).fadeOut(function(){
            selikoff.activeImage = a.attr('id');
            jQuery('#main-' + selikoff.activeImage).fadeIn();
          });
          
          delete a;
          
          event.preventDefault();
        });
      }
    },
    
    noop: function() {}
  });
  
  selikoff.options = selikoff.options || {};
})();

jQuery(document).ready(function() {
  selikoff.initialize();
});

