jQuery.fn.tmnCoverflow = function(prefix) {
  prefix = prefix || '';
  var coverflow_tmn = jq('.' + prefix + 'coverflow_tmn');
  var coverflow_window = jq('.' + prefix + 'coverflow_window');
  var selector_next = jq('.' + prefix + 'coverflow_tmn a.next');
  var selector_prev = jq('.' + prefix + 'coverflow_tmn a.prev');
  var selector_pag_zon = jq('.' + prefix + 'pag_zone');
  var allImages = jq('.' + prefix + 'coverflow_window > a img');
  var style_active_page = prefix + 'active_pag';
  var currentIndex = 0;

  allImages.each( function(i) {allImages.eq(i).css( {position: 'absolute', left: 0, top: 0} );});

  coverflow_window.css({position:'relative',width: coverflow_tmn.width() - selector_next.width() - selector_prev.width(), height: jq(allImages[0]).height()});
  var margin_width = (coverflow_window.width() - jq(allImages).width()* 1.2) / 2;

  //posicoes left, center e right
  var coords = [
                {
                  left : margin_width,
                  height : jq(allImages[0]).height() * 0.9,
                  width : jq(allImages[0]).width() * 0.9,
                  top : jq(allImages[0]).height() * 0.1 / 2
                },
                {
                  left : (coverflow_window.width() - jq(allImages[0]).width()) / 2,
                  height : jq(allImages[0]).height(),
                  width : jq(allImages[0]).width(),
                  top : 0
                },
                {
                  left : coverflow_window.width() - jq(allImages[0]).width() * 0.9 - margin_width,
                  height : jq(allImages[0]).height() * 0.9,
                  width : jq(allImages[0]).width() * 0.9,
                  top : jq(allImages[0]).height() * 0.1 / 2
                }
               ];
  
  //z-index das posicoes
  var z_index = [1,2,1];
  var position_behind = (coverflow_window.width() - jq(allImages[0]).width() * 0.8) / 2;
  for(var i = 3; i<allImages.length; ++i) {
    coords[i] = {
        left : position_behind,
        height : jq(allImages[0]).height() * 0.8,
        width : jq(allImages[0]).width() * 0.8,
        top : jq(allImages[0]).height() * 0.2 / 2
        };
    z_index[i] = 0;
  }
  function circularIndex(ci) {
    if (ci >= allImages.length) {
      return ci - allImages.length;
    } else if (ci < 0) {
      return allImages.length + ci;
    } else {
      return ci;
    }
  }
  allImages.each( function(i) {
    allImages.eq(i).css(coords[circularIndex(i+1)]).css({position: 'absolute', 'z-index': z_index[circularIndex(i+1)]});
    selector_pag_zon.append('<a class="' + prefix + 'pag">&#9679;</a>');
  });
  selector_pag_zon.find('a').eq(0).addClass(style_active_page);

  function frontFromLeft() {
    selector_prev.trigger('click');
    return false;
  }
  function frontFromRight() {
    selector_next.trigger('click');
    return false;
  }
  function setImgClick() {
    allImages.unbind('click.to_front');
    allImages.eq(circularIndex(currentIndex - 1)).bind('click.to_front', frontFromLeft);
    allImages.eq(circularIndex(currentIndex + 1)).bind('click.to_front', frontFromRight);
  }
  selector_next.click(
      function() {
        selector_pag_zon.find('a').eq(currentIndex).removeClass(style_active_page);
        for(var i = 0; i < 4 && i < allImages.length; ++i) {
          jq(allImages.eq(circularIndex(currentIndex + i - 1))).animate(coords[circularIndex(i - 1)], 600, 'easeOutSine').css('z-index', z_index[circularIndex(i - 1)]);
        }
        
        currentIndex = circularIndex(currentIndex + 1);
        selector_pag_zon.find('a').eq(currentIndex).addClass(style_active_page);
        
        setImgClick();
      });
  selector_prev.click(
          function() {
            selector_pag_zon.find('a').eq(currentIndex).removeClass(style_active_page);
            for(var i = 0; i < 4 && i < allImages.length ; ++i) {
              jq(allImages.eq(circularIndex(currentIndex + 1 - i))).animate(coords[circularIndex(3 - i)], 600, 'easeOutSine').css('z-index', z_index[circularIndex(3 - i)]);
            }
            currentIndex = circularIndex(currentIndex - 1);
            selector_pag_zon.find('a').eq(currentIndex).addClass(style_active_page);
            
            setImgClick();
          });
  selector_pag_zon.find('a').click(
      function() {
        var ci = selector_pag_zon.find('a').index(this);
        for(var i = 0; i < allImages.length ; ++i) {
          jq(allImages.eq(i)).animate(coords[circularIndex(i - ci + 1)], 600, 'easeOutSine').css('z-index', z_index[circularIndex(i - ci + 1)]);
        }
        selector_pag_zon.find('a').eq(currentIndex).removeClass(style_active_page);
        currentIndex = circularIndex(ci);
        selector_pag_zon.find('a').eq(currentIndex).addClass(style_active_page);

        setImgClick();
      });
  setImgClick();
};
