/** 
 * Copyright (c) 2009 Sylvain Gougouzian (sylvain@gougouzian.fr)
 * MIT (http://www.opensource.org/licenses/mit-license.php) licensed.
 * GNU GPL (http://www.gnu.org/licenses/gpl.html) licensed.
 *
 * jQuery carrousel effects by Sylvain Gougouzian http://sylvain.gougouzian.fr
 *
 * Version: 1.1.1
 *
 * Requires: jQuery 1.3.2+ 	// http://www.jquery.com
 *			jQuery corner plugin 2.01 // http://jquery.malsup.com/corner/
 *
 * Compatible : Internet Explorer 6+, Firefox 1.5+, Safari 3+, Opera 9+, Chrome 0.9+
 */

jQuery(function($) {
 $.extend($.fn.carrousel.effects.init,{
  corner: function (carrousel) {
   $.fn.corner.defaults.useNative = false;
   $('.carrousel_item',carrousel.container).corner("10px");
  },
  reflection: function (carrousel) {
   if (carrousel.vertical) {
    carrousel.block.height *= 1.33;
    $('.carrousel_item', carrousel.container).parent().height($('.carrousel_item', carrousel.container).parent().height() * 1.33);
   }
   carrousel.container.height(parseInt(carrousel.container.height()) * 1.33);
   $('.carrousel_item > img').load(function () {
    reflect(this);
   });
  },
  relief: function (carrousel){
   carrousel.incrementPercent = parseInt(50 / (parseInt(carrousel.opts.dispNumber) - 3));
   $('.carrousel_item', carrousel.container).each(function () {
    var size = get3Dsize(carrousel, $(this).attr('rel'), 0);
    $('> ' + carrousel.opts.mode, this).load(function (){
     $(this).width(parseInt(carrousel.block.width * (size / 100)) + 'px').height(parseInt(carrousel.block.height * (size / 100)) + 'px');
     $(this).parent().width(parseInt(carrousel.block.width * (size / 100)) + 'px').height(parseInt(carrousel.block.height * (size / 100)) + 'px');
    });
   });
  },
  align: function (carrousel) {
   if (carrousel.opts.mode == 'img') {
    $('.carrousel_item img', carrousel.container).load(function(){
     alignItem($(this).parent(), carrousel);
    });
   }
   else {
    for (var i = 0; i < carrousel.nbItems; i++) {
     alignItem($('.carrousel_item', carrousel.aItems.eq(i)), carrousel);
    }
   }
  }
 });
 $.extend($.fn.carrousel.effects.before,{
  fade: function (carrousel) {
   $('.carrousel_item',carrousel.container).fadeTo(parseInt(carrousel.opts.speed / 2), 0.5);
  },
  relief: function (carrousel, direction) {
   var move = direction * (carrousel.dep == -1 ? carrousel.opts.scroll : carrousel.dep);
   $('.carrousel_item', carrousel.container).each(function () {
    var size = get3Dsize(carrousel, $(this).attr('rel'), move);
    var nWidth = parseInt(carrousel.block.width * size / 100) + 'px';
    var nHeight = parseInt(carrousel.block.height * size / 100) + 'px';
    $('.carrousel_item, .carrousel_item > ' + carrousel.opts.mode, $(this).parent()).animate({
     width: nWidth,
     height: nHeight,
     left: carrousel._getAlignWidth(parseInt(nWidth)),
     top: carrousel._getAlignHeight(parseInt(nHeight))
    }, carrousel.opts.speed);
   });
  }
 });
 function reflect(img) {
  var $this = $(img);
  var w = parseInt($this.width());
  var h = parseInt($this.height());
  var r;
  if ($.browser.msie) {
   r = $("<img />").attr('src', $this.attr('src')).css({
    'width': w,
    'height': h,
    'margin-bottom': h - Math.floor(h * 0.33),
    'filter': "flipv progid:DXImageTransform.Microsoft.Alpha(opacity=50, style=1, finishOpacity=0, startx=0, starty=0, finishx=0, finishy=33)"
   })[0];
  }
  else {
   r = $("<canvas />")[0];
   if (!r.getContext) { return; }
   var f = r.getContext("2d");
   try {
    $(r).attr({
     'width': w,
     'height': Math.floor(h * 0.33)
    });
    f.save();
    f.translate(0, h - 1);
    f.scale(1, -1);
    f.drawImage(img, 0, 0, w, h);
    f.restore();
    f.globalCompositeOperation="destination-out";
    var i = f.createLinearGradient(0, 0, 0, Math.floor(h * 0.33));
    i.addColorStop(0, "rgba(255, 255, 255, 0.5)");
    i.addColorStop(1, "rgba(255, 255, 255, 1.0)");
    f.fillStyle = i;
    f.rect(0, 0, w, Math.floor(h * 0.33));
    f.fill();
   }
   catch (e) {
    return;
   }
  }
  $(r).css('display', 'block');
  $this.parent().css({
   width: w,
   height: h + Math.floor(h * 0.33),
   overflow: "hidden"
  }).append($(r));
  return false;
 }	
 function unreflect($this) {
  var html = $this.parent().html();
  $this.parent().parent().html(html);
  return false;
 }
 $.extend($.fn.carrousel.effects.after,{
  fade: function (carrousel) {
   $('.carrousel_item',carrousel.container).fadeTo(parseInt(carrousel.opts.speed / 2), 1.0);
  }
 });
	function get3Dsize(carrousel, i, add){
  var min = carrousel.current + add;
  if (min < 0)
   min += (carrousel.nbItems / 2);
  min = carrousel._realpos(min);
  var max = parseInt(carrousel.opts.dispNumber) + min - 1;
  max = carrousel._realpos(max);
  var moy = parseInt(carrousel.opts.dispNumber / 2) + min;
  moy = carrousel._realpos(moy);
  var j = i;
  i = carrousel._realpos(i);
  var res = 0;
  if ((i == min) || (i == max)) {
   res = 50;
  }
  else {
   if (i == moy) {
    res = 100;
   }
   else {
    if (min < max) {
    if ((i > min) && (i < max)) {
     res = 50 + Math.abs(moy - i) * carrousel.incrementPercent;
    }
    else {
     res = 50 - carrousel.incrementPercent;
    }
   }
   else {
    if ((i < (carrousel.nbItems / 2)) && (i > min)) {
     if (moy == 0) {
      moy = (carrousel.nbItems / 2);
     }
     res = 50 + Math.abs(moy - i) * carrousel.incrementPercent;
    }
    else {
     if ((i >= 0) && (i < max)) {
      if (moy == ((carrousel.nbItems / 2) - 1)) {
       moy = 1;
      }
      res = 50 + Math.abs(moy - i) * carrousel.incrementPercent;
     }
     else {
      res = 50 - carrousel.incrementPercent;
     }
    } 
   }
  }
 }
 carrousel.aItems.eq(j).css('z-index', carrousel.opts.dispNumber - Math.abs(moy - i));
  return res;
 }
 function alignItem(item, carrousel){
  item.css({
   'top': getAlignHeight(parseInt($(carrousel.opts.mode, item).height()) + carrousel.margin.height, carrousel),
   'left': getAlignWidth(parseInt($(carrousel.opts.mode, item).width()) + carrousel.margin.width, carrousel),
   'position': 'relative'
  });
 }
 function getAlignHeight(size, carrousel){
  var top = carrousel.block.height - size;
  if (carrousel.opts.align.indexOf('top') != -1) {
   return '0px';
  }
  if (carrousel.opts.align.indexOf('bottom') != -1) {
   return top + 'px';
  }
  if (this.opts.align.indexOf('center') != -1) {
   return parseInt(top / 2) + 'px';
  }
  return '0px';
 }
 function getAlignWidth(size, carrousel){
  var left = carrousel.block.width - size;
  if (carrousel.opts.align.indexOf('left') != -1) {
   return '0px';
  }
  if (carrousel.opts.align.indexOf('right') != -1) {
   return left + 'px';
  }
  if (carrousel.opts.align.indexOf('center') != -1) {
   return parseInt(left / 2) + 'px';
  }
  return '0px';
 }
});
