﻿/**
* 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 moodular effects by Sylvain Gougouzian http://sylvain.gougouzian.fr
*
* Version: 1.2
*
* 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.moodular.effects.init, {
        corner: function(moodular) {
            $.fn.corner.defaults.useNative = false;
            $('> ' + moodular.opts.item, moodular.e).corner("10px");
        },
        reflection: function(moodular) {
            $('> ' + moodular.opts.item, moodular.e).each(function() {
                $(this).height(parseInt($(this).height()) * 1.33);
            });
            moodular.e.parent().height(parseInt(moodular.e.parent().height()) * 1.33);
            $(moodular.opts.item + ' > img', moodular.e).load(function() {
                reflect(this);
            });
        },
        relief: function(moodular) {
            moodular.incrementPercent = parseInt(50 / (parseInt(moodular.opts.dispNumber) - 3));
            $('.moodular_item', moodular.e).each(function() {
                var size = get3Dsize(moodular, $(this).attr('rel'), 0);
                $('*', this).load(function() {
                    $(this).width(parseInt(moodular.block.width * (size / 100)) + 'px').height(parseInt(moodular.block.height * (size / 100)) + 'px');
                    $(this).parent().width(parseInt(moodular.block.width * (size / 100)) + 'px').height(parseInt(moodular.block.height * (size / 100)) + 'px');
                });
            });
        },
        align: function(moodular) {
            if (moodular.opts.mode == 'img') {
                $('.moodular_item img', moodular.e).load(function() {
                    alignItem($(this).parent(), moodular);
                });
            }
            else {
                for (var i = 0; i < moodular.nbItems; i++) {
                    alignItem($('.moodular_item', moodular.aItems.eq(i)), moodular);
                }
            }
        }
    });
    $.extend($.fn.moodular.effects.before, {
        fade: function(moodular) {
            $('.moodular_item', moodular.container).fadeTo(parseInt(moodular.opts.speed / 2), 0.5);
        },
        relief: function(moodular, direction) {
            var move = direction * (moodular.dep == -1 ? moodular.opts.scroll : moodular.dep);
            $('.moodular_item', moodular.container).each(function() {
                var size = get3Dsize(moodular, $(this).attr('rel'), move);
                var nWidth = parseInt(moodular.block.width * size / 100) + 'px';
                var nHeight = parseInt(moodular.block.height * size / 100) + 'px';
                $('.moodular_item, .moodular_item > ' + moodular.opts.mode, $(this).parent()).animate({
                    width: nWidth,
                    height: nHeight,
                    left: moodular._getAlignWidth(parseInt(nWidth)),
                    top: moodular._getAlignHeight(parseInt(nHeight))
                }, moodular.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.moodular.effects.after, {
        fade: function(moodular) {
            $('.moodular_item', moodular.container).fadeTo(parseInt(moodular.opts.speed / 2), 1.0);
        }
    });
    function get3Dsize(moodular, i, add) {
        var min = moodular.current + add;
        if (min < 0)
            min += (moodular.nbItems / 2);
        min = moodular._realpos(min);
        var max = parseInt(moodular.opts.dispNumber) + min - 1;
        max = moodular._realpos(max);
        var moy = parseInt(moodular.opts.dispNumber / 2) + min;
        moy = moodular._realpos(moy);
        var j = i;
        i = moodular._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) * moodular.incrementPercent;
                    }
                    else {
                        res = 50 - moodular.incrementPercent;
                    }
                }
                else {
                    if ((i < (moodular.nbItems / 2)) && (i > min)) {
                        if (moy == 0) {
                            moy = (moodular.nbItems / 2);
                        }
                        res = 50 + Math.abs(moy - i) * moodular.incrementPercent;
                    }
                    else {
                        if ((i >= 0) && (i < max)) {
                            if (moy == ((moodular.nbItems / 2) - 1)) {
                                moy = 1;
                            }
                            res = 50 + Math.abs(moy - i) * moodular.incrementPercent;
                        }
                        else {
                            res = 50 - moodular.incrementPercent;
                        }
                    }
                }
            }
        }
        moodular.aItems.eq(j).css('z-index', moodular.opts.dispNumber - Math.abs(moy - i));
        return res;
    }
    function alignItem(item, moodular) {
        item.css({
            'top': getAlignHeight(parseInt($(moodular.opts.mode, item).height()) + moodular.margin.height, moodular),
            'left': getAlignWidth(parseInt($(moodular.opts.mode, item).width()) + moodular.margin.width, moodular),
            'position': 'relative'
        });
    }
    function getAlignHeight(size, moodular) {
        var top = moodular.block.height - size;
        if (moodular.opts.align.indexOf('top') != -1) {
            return '0px';
        }
        if (moodular.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, moodular) {
        var left = moodular.block.width - size;
        if (moodular.opts.align.indexOf('left') != -1) {
            return '0px';
        }
        if (moodular.opts.align.indexOf('right') != -1) {
            return left + 'px';
        }
        if (moodular.opts.align.indexOf('center') != -1) {
            return parseInt(left / 2) + 'px';
        }
        return '0px';
    }
});



/******

INIT :

if (this.legend == "mouseover") {
$('.moodular_item', this.container).css('cursor', 'pointer').bind("mouseenter", function(evt){
$('div#' + self.id + '_legend').html($(this).attr('title'));
}).bind("mouseleave", function(evt){
$('div#' + self.id + '_legend').html("");
});
}
if (this.legend == "always") {
$('div#' + self.id + '_legend').html($('> ul > li', this.container).eq(this.realpos(this.current)).attr('title'));
}


moodular.container.parent().append("<div id='" + moodular.id + "_legend' class='moodular_legend'></div>");

AFTER :


if (this.legend == "always") {
$('div#' + this.id + "_legend").html($(this.opts.mode, this.container).eq(this.realpos(this.current)).attr('title'));
}


******/
