var lib = new Hash({
  basename: function(path){
    var sp = path.split('/');
    return sp[sp.length - 1];
  }
});

var photo_gallery = new Hash({
  mw: 0,
  ss: null,
  step: 100,
  margin: 100,
  json: null,
  prefix: null,
  adoptTo: null,
  es: new Object(),
  scroll: null,
  current: 0,
  preloadImage: null,
  preloadImageSrc: null,
  init: function(el, jsonS, prefix, adoptTo){
    photo_gallery.mw = 0;
    photo_gallery.ss = null;
    photo_gallery.step = 100;
    photo_gallery.margin = 100;
    photo_gallery.json = null;
    photo_gallery.prefix = null;
    photo_gallery.adoptTo = null;
    photo_gallery.es = new Object();
    photo_gallery.scroll = null;
    photo_gallery.current = 0;
    photo_gallery.preloadImage = null;
    photo_gallery.preloadImageSrc = null;
    photo_gallery.json = JSON.decode(jsonS);
    photo_gallery.prefix = prefix;
    photo_gallery.adoptTo = adoptTo;
    if(el.get('tag') == 'img'){
      photo_gallery.json.pf.each(function(i, k){
        if(lib.basename(el.get('src')) == 'prew_' + i.file){
          photo_gallery.current = k;
        }
      });
    } else {
      photo_gallery.current = 0;
    }
    photo_gallery.buildHTML();
    photo_gallery.setSize();
    window.addEvent('resize', function(){
      photo_gallery.setSize();
      photo_gallery.recenter();
    });
    photo_gallery.es.root.set('tween', {duration: 'short'});
    photo_gallery.es.root.tween('opacity', 0, 1);
    photo_gallery.moveTo(photo_gallery.current);
  },
  buildHTML: function(){
    photo_gallery.es.html = document.getElement('html');
    photo_gallery.ss = photo_gallery.es.html.getScroll();
    photo_gallery.es.html.addClass('gallery_open');
    photo_gallery.es.root = new Element('div#g_root');
    photo_gallery.adoptTo.adopt(photo_gallery.es.root, 'after');
    photo_gallery.es.root.adopt(new Element('span#g_close', {'html' : '<span>Закрыть</span>', 'events' : {'click' : function(){photo_gallery.close()}}}));
    photo_gallery.es.root.adopt(new Element('div#g_mini', {'html' : '<div id="g_mini_w"><div id="g_mini_s"></div></div>'}));
    photo_gallery.es.root.adopt(new Element('div#g_title', {'html' : photo_gallery.json.title}));
    photo_gallery.es.g_mini_w = $('g_mini_w');
    photo_gallery.es.g_mini_s = $('g_mini_s');
    photo_gallery.es.g_mini_s.set('tween', {duration: 50});
    photo_gallery.es.root.adopt(new Element('div#g_maxi'));
    photo_gallery.es.root.adopt(new Element('div#g_count'));
    photo_gallery.es.root.adopt(new Element('img#g_b_l', {'src' : '/images/arrow_l.png', 'width' : 40, 'height' : 64}));
    photo_gallery.es.root.adopt(new Element('img#g_b_r', {'src' : '/images/arrow_r.png', 'width' : 40, 'height' : 64}));
    photo_gallery.es.g_maxi = $('g_maxi');
    photo_gallery.json.pf.each(function(i, k){
      photo_gallery.es.g_mini_s.adopt(new Element('img#g_mini_img_' + k, {
        'class' : (k == photo_gallery.current ? 'current' : ''),
        'src' : photo_gallery.prefix + 'prew_50_' + i.file,
        'events' : {
          'click' : function(){
            if(photo_gallery.current == this.get('id').replace('g_mini_img_', '')){
              return;
            }
            photo_gallery.moveTo(this.get('id').replace('g_mini_img_', ''));
          }
        }
      }));
      photo_gallery.mw += (4 + i.s1.w);
    });
    photo_gallery.es.g_mini_s.setStyle('width', photo_gallery.mw);
    photo_gallery.es.root.addEvent('mousewheel', function(event) {
      if(event.wheel < 0){
        photo_gallery.moveTo(photo_gallery.current < photo_gallery.json.pf.length - 1 ? photo_gallery.current + 1 : 0);
      } else {
        photo_gallery.moveTo(photo_gallery.current > 0 ? photo_gallery.current - 1 : photo_gallery.json.pf.length - 1);
      }
      event.stop();
    });
    $('g_b_r').addEvent('click', function(){
      photo_gallery.moveTo(photo_gallery.current < photo_gallery.json.pf.length - 1 ? photo_gallery.current + 1 : 0);
    });
    $('g_b_l').addEvent('click', function(){
      photo_gallery.moveTo(photo_gallery.current > 0 ? photo_gallery.current - 1 : photo_gallery.json.pf.length - 1);
    });
    new Keyboard({
      defaultEventType: 'keydown',
      events: {
        'control+right': function(){photo_gallery.moveTo(photo_gallery.current < photo_gallery.json.pf.length - 1 ? photo_gallery.current + 1 : 0)},
        'control+left': function(){photo_gallery.moveTo(photo_gallery.current > 0 ? photo_gallery.current - 1 : photo_gallery.json.pf.length - 1)}
      }
    });
  },
  setSize: function(){
    var ws = window.getCoordinates();
    photo_gallery.es.root.setStyles({
      'width' : ws.width,
      'height' : ws.height
    });
    photo_gallery.es.g_mini_w.setStyle('width', ws.width);
    $('g_b_r').setStyle('top', Math.round(ws.height / 2) - 60);
    $('g_b_l').setStyle('top', Math.round(ws.height / 2) - 60);
  },
  moveTo: function(k){
    photo_gallery.es.g_maxi.set('html', '<img lang="' + photo_gallery.json.pf[k].max.w + 'x' + photo_gallery.json.pf[k].max.h + '" src="/images/n.gif" title="' + photo_gallery.json.pf[k].title + '" width="' + photo_gallery.json.pf[k].max.w + '" height="' + photo_gallery.json.pf[k].max.h + '" />');
    photo_gallery.preloadImageSrc = photo_gallery.json.pf[k].file;
    photo_gallery.current = parseInt(k);
    photo_gallery.wheel(true);
    photo_gallery.recenter();
    photo_gallery.preloadImage = Asset.image(photo_gallery.prefix + '/' + photo_gallery.json.pf[k].file, {
      onLoad: function(source){
        if(photo_gallery.preloadImageSrc == lib.basename(source.get('src'))){
          photo_gallery.es.g_maxi.getElement('img').set('src', source.get('src'));
        }
      }
    });
  },
  recenter: function(){
    var wc = window.getCoordinates();
    var img = photo_gallery.es.g_maxi.getElement('img');
    var img_l = img.get('lang').split('x');
    if(img_l[1] > wc.height - 120){
      var k = (wc.height - 120) / img_l[1];
      img.set('width', k * img_l[0]);
      img.set('height', k * img_l[1]);
    }
    var img_c = img.getCoordinates();
    photo_gallery.es.g_maxi.setStyles({
      'left' : Math.round(wc.width / 2) - Math.round(img_c.width / 2),
      'top' : Math.round(wc.height / 2) - Math.round(img_c.height / 2) - 15
    });
  },
  close: function(){
    photo_gallery.es.root.destroy();
    photo_gallery.es.html.removeClass('gallery_open');
    photo_gallery.es.html.scrollTo(0, photo_gallery.ss.y);
  },
  wheel: function(fx){
    photo_gallery.es.g_mini_s.getElements('img').each(function(i){
      i.removeClass('current');
    });
    $('g_mini_img_' + photo_gallery.current).addClass('current');
    var cs = photo_gallery.es.g_mini_s.getCoordinates();
    var mix = $('g_mini_img_' + photo_gallery.current).getCoordinates(photo_gallery.es.g_mini_s);
    var ws = window.getCoordinates();
    var to = Math.round(ws.width / 2) - Math.round(mix.width / 2) - mix.left;
    $('g_count').setStyles({
      'left' : to + mix.left
    });
    
    $('g_count').set('html', (1 + photo_gallery.current) + ' из ' + photo_gallery.json.pf.length);
    if(fx){
      photo_gallery.es.g_mini_s.tween('left', cs.left, to);
    }
    else{
      photo_gallery.es.g_mini_s.setStyle('left', to);
    }
  }
});
