/* RX-picGallery - 02/02/10
  images are resized & centerd to viewport,
  big images are taken from another folder with the same name as thumbnails
  opacity & slide transitions
*/
jQuery.fn.RX_picGallery = function(Q) {

  var nop = function(){};

  this.each(function() {
    var I = $(this),
        _busy = false,
        C = 'rx-',
        P_thumb = 0,
        P_pic = 0,

        pic_vis = false, // visible pic
        pic_hid = false, // hidden pic

        A_thumb = I.children(Q.w_thumb),
        A_pic = I.children(Q.w_pic),

        W_thumb = I.children(Q.c_thumb).children('strong'),
        R_thumb = W_thumb.children('img'),

        W_pic = I.children(Q.c_pic).children('strong'),

        width_elm = Q.v_w_thumb, // R_thumb.outerWidth(true),
        num_row = Q.num_row, // num of thumbs visible
        num_elm = R_thumb.size(); // real number of pics

    A_thumb.find(Q.a_prev).click(function() {
      var ax = Math.max(1, Math.min(P_thumb, num_row));

      if (P_thumb - ax >= 0) {
        W_thumb.animate({left : '+='+ width_elm * ax});
        P_thumb-=ax;
      }

      return false;
    });

    A_thumb.find(Q.a_next).click(function() {
      var ax = Math.max(1, Math.min(num_elm - num_row - P_thumb, num_row));

      if (P_thumb + ax <= num_elm - num_row) {
        W_thumb.animate({left : '-='+ width_elm * ax});
        P_thumb+=ax;
      }

      return false;
    });

    A_pic.find(Q.a_prev).click(function() {
      if (P_pic>0) {
        _transition('slide-prev', P_pic, P_pic-1);
      }
      return false;
    });

    A_pic.find(Q.a_next).click(function() {
      if (P_pic<num_elm-1) {
        _transition('slide-next', P_pic, P_pic+1);
      }

      return false;
    });

    W_thumb.click(function(e) {
      var I = $(e.target);
      if (I.is('img')) {
        var i = I.attr('class').match(/rx\-\d{1,2}/),
            j = parseFloat( i.toString().substr(3) );

        _transition('opacity', P_pic, j);

        return false;
      }
    });

    function init() {
      W_thumb.css('width', num_elm*width_elm + 1);
      var i=0;
      R_thumb.each(function() {
        $(this).addClass('rx-'+ i++);
      });

      $('<img />').
        attr({'alt' : '',
              'src' : _getFileName($(R_thumb[0])),
              'class' : 'rx-first'
            }).
        load(function() {
          pic_vis = $(this);
          W_pic.append(pic_vis);
          _resizePic(pic_vis);
        });
        pic_hid = $('-rx-nop-');
      }
    //

    function _getFileName(V) {
      var i = V.attr('src'),
          j = Q.folder+i.substr(1+i.lastIndexOf('/'));
      return j + '?rx='+Math.random(7979);
    }
    //

    function _resizePic(V) {
      var h = V.height(),
          w = V.width(),
          cx = h/Q.v_h,
          dx = w/Q.v_w,
          ax, bx;

      if (cx>=dx) {
        ax = Math.ceil( w / cx );
        bx = _padding(ax, Q.v_w);

        V.css({
          'height': Q.v_h,
          'width': ax,
          'padding-left': bx.i1,
          'padding-right': bx.i2
        });
      } else {
        ax = Math.ceil( h / dx );
        bx = _padding(ax, Q.v_h);

        V.css({
          'width': Q.v_w,
          'height': ax,
          'padding-top': bx.i1,
          'padding-bottom': bx.i2
        });
      }

    }
    //

    function _padding(V, M) {
      var i = M - V,
          ax = Math.floor(i/2);
      return {i1:ax, i2:(i-ax)}
    }
    //

    function _transition(V, old_id, new_id) {
      var _cleanup = function() {
        pic_hid.attr('class','rx-first');
        pic_hid.css({left:0, opacity:1});

        pic_vis.attr('class','rx-slide');
        pic_vis.css({left:0, opacity:1});

        var i = pic_vis;
        pic_vis = pic_hid;
        pic_hid = i;

        P_pic = new_id;
        _busy = false;
      }

      if (old_id == new_id) { return false; }
      if (_busy) { return false; }
      _busy = true;

      switch (V) {
        case 'opacity':

          pic_hid.remove();
          $('<img />').
            attr({ 'src': _getFileName($(R_thumb[new_id])),
                   'alt': '',
                   'class': 'rx-opacity' }).
            load(function() {
              pic_hid = $(this);
              W_pic.append(pic_hid);
              _resizePic(pic_hid);

              pic_vis.animate({opacity:0}, 'slow', _cleanup );
            });
          break;

        case 'slide-next':
          pic_hid.remove();
          $('<img />').
            attr({ 'src': _getFileName($(R_thumb[new_id])),
                   'alt': '',
                   'class': 'rx-slide' }).
            css({ 'left' : Q.v_w }).
            load(function() {
              pic_hid = $(this);
              W_pic.append(pic_hid);
              _resizePic(pic_hid);

              pic_vis.animate({left: '-='+Q.v_w }, 'normal');
              pic_hid.animate({left: '-='+Q.v_w }, 'normal', _cleanup );
            });
          break;

        case 'slide-prev':
          pic_hid.remove();
          $('<img />').
            attr({ 'src': _getFileName($(R_thumb[new_id])),
                   'alt': '',
                   'class': 'rx-slide' }).
            css({ 'left' : -Q.v_w }).
            load(function() {
              pic_hid = $(this);
              W_pic.append(pic_hid);
              _resizePic(pic_hid);

              pic_vis.animate({left: '+='+Q.v_w }, 'normal');
              pic_hid.animate({left: '+='+Q.v_w }, 'normal', _cleanup );
            });
          break;

        default:
          break;
      }
    }
    //

    init();

  });
  //

}
//


jQuery(document).ready(function($) {


	$('.rx-gallery').RX_picGallery({
    w_thumb : 'big', // wrapper for thumbnail controls
    w_pic : 'em', // wrapper for big image controls

		a_prev : '.prev', // controls
		a_next : '.next', // controls

    folder : 'http://youplusweevents.com/wp-content/plugins/imgsmanager/images/', // directory for pictures
		v_h : 422, // height of picture
		v_w : 641, // width of picture
    num_row : 4, // number of thumbs visible
    v_w_thumb : 163, //full width of thumbnail - can;t be calculated if display:none
		c_pic : 'p',
		c_thumb : 'del'
	});

});

