/*
 * Title: jQuery Etalage plugin
 * Author: Berend de Jong, Frique
 * Author URI: http://www.frique.me/
 * Version: 1.01 (20110325.1)
 */
(function ($) {
    jQuery.fn.etalage = function (Z) {
        var o = $.extend({
            thumb_image_width: 340,
            thumb_image_height: 340,
            source_image_width: 800,
            source_image_height: 650,
            zoom_area_width: 605,
            zoom_area_height: 'justify',
            zoom_easing: true,
            small_thumbs: 4,
            smallthumb_inactive_opacity: 0.4,
            smallthumb_hide_single: true,
            magnifier_opacity: 0.5,
            magnifier_invert: true,
            show_icon: true,
            hide_cursor: false,
            show_hint: true,
            speed: 600,
            autoplay: true,
            autoplay_interval: 6000
        }, Z);
        $(this).each(function () {
            var h = $(this);
            if (h.is('ul') && h.find('> li').length > 0 && h.find('img.etalage_source_image').length > 0) {
                var i, j, src, thumb_id, magnifier_opacity, autotimer, faster = Math.floor(o.speed * 0.7),
                    zoom_follow_speed = Math.round(o.speed / 100),
                    st_moving = false,
                    ie6_iframe_fix = false,
                    preview = true,
                    zoom_move_timer = 0,
                    cur_zoomx = 0,
                    cur_zoomy = 0,
                    new_zoomx = 0,
                    new_zoomy = 0;
                if ($.browser.msie) {
                    preview = false;
                    if ($.browser.version < 7) {
                        ie6_iframe_fix = true
                    }
                }
                h.addClass('etalage').show();
                var k = h.find('li').addClass('etalage_thumb');
                k.first().show().addClass('etalage_thumb_active');
                var l = k.length;
                if (l < 2) {
                    o.autoplay = false
                }
                k.each(function (i) {
                    i++;
                    var a = $(this);
                    var b = a.find('.etalage_thumb_image').removeAttr('alt').show();
                    var c = a.find('.etalage_source_image');
                    a.data('id', i).addClass('thumb_' + i);
                    if (b.length < 1 && c.length > 0) {
                        a.prepend('<img class="etalage_thumb_image" src="' + c.attr('src') + '" />')
                    } else if (b.length < 1 && c.length < 0) {
                        a.remove()
                    }
                });
                var m = k.find('.etalage_thumb_image').css({
                    width: o.thumb_image_width,
                    height: o.thumb_image_height
                }).show();
                m.each(function () {
                    $(this).data('src', this.src)
                });
                var n = $('<li class="etalage_magnifier"><div><img /></div></li>').appendTo(h);
                var p = n.find('div');
                var q = p.find('img');
                var r = $('<li class="etalage_icon">&nbsp;</li>').appendTo(h);
                if (o.show_icon) {
                    r.show()
                }
                var s = $('<li class="etalage_hint">&nbsp;</li>').appendTo(h);
                if (o.show_hint) {
                    s.show()
                }
                var t = $('<li class="etalage_zoom_area"><div><img class="etalage_zoom_img" /></div></li>').appendTo(h);
                var u = t.find('div');
                if (preview) {
                    var v = $('<img class="etalage_zoom_preview" />').css({
                        width: o.source_image_width,
                        height: o.source_image_height,
                        opacity: 0.3
                    }).prependTo(u).show()
                }
                var w = u.find('.etalage_zoom_img').css({
                    width: o.source_image_width,
                    height: o.source_image_height
                });
                if (l > 1 || !o.smallthumb_hide_single) {
                    var x = $('<li class="etalage_small_thumbs"><ul></ul></li>').appendTo(h);
                    var y = x.find('ul');
                    m.each(function () {
                        var a = $(this);
                        src = a.data('src');
                        thumb_id = a.parents('.etalage_thumb').data('id');
                        $('<li><img class="etalage_small_thumb" src="' + src + '" /></li>').data('thumb_id', thumb_id).appendTo(y)
                    });
                    var z = x.find('li').animate({
                        opacity: o.smallthumb_inactive_opacity
                    }, 0);
                    if (o.small_thumbs < 3) {
                        o.small_thumbs = 3
                    }
                    if (l > o.small_thumbs) {
                        src = m.eq(l - 1).data('src');
                        thumb_id = k.eq(l - 1).data('id');
                        $('<li class="etalage_smallthumb_first"><img class="etalage_small_thumb" src="' + src + '" /></li>').data('src', src).data('thumb_id', thumb_id).prependTo(y).animate({
                            opacity: o.smallthumb_inactive_opacity
                        }, 0);
                        src = m.eq(0).data('src');
                        thumb_id = k.eq(0).data('id');
                        $('<li><img class="etalage_small_thumb" src="' + src + '" /></li>').data('src', src).data('thumb_id', thumb_id).appendTo(y).animate({
                            opacity: o.smallthumb_inactive_opacity
                        }, 0);
                        z = x.find('li');
                        z.eq(o.small_thumbs - 1).addClass('etalage_smallthumb_last');
                        z.eq(1).addClass('etalage_smallthumb_active').animate({
                            opacity: 1
                        }, 0)
                    } else {
                        z.eq(0).addClass('etalage_smallthumb_active').animate({
                            opacity: 1
                        }, 0)
                    }
                    i = 1;
                    z.each(function () {
                        $(this).data('id', i);
                        i++
                    });
                    var A = z.find('img');
                    var B = z.length
                }
                if (o.magnifier_invert) {
                    magnifier_opacity = 1
                } else {
                    magnifier_opacity = o.magnifier_opacity
                }
                var C = parseInt(k.css('borderLeftWidth'), 10) + parseInt(k.css('borderRightWidth'), 10) + parseInt(m.css('borderLeftWidth'), 10) + parseInt(m.css('borderRightWidth'), 10);
                var D = parseInt(k.css('marginLeft'), 10) + parseInt(k.css('marginRight'), 10);
                var E = parseInt(k.css('paddingLeft'), 10) + parseInt(k.css('paddingRight'), 10) + parseInt(m.css('marginLeft'), 10) + parseInt(m.css('marginRight'), 10) + parseInt(m.css('paddingLeft'), 10) + parseInt(m.css('paddingRight'), 10);
                var F = o.thumb_image_width + C + D + E,
                    thumb_outerheight = o.thumb_image_height + C + D + E;
                var G = 0,
                    smallthumb_margin = 0,
                    smallthumb_padding = 0,
                    smallthumb_width = 0,
                    smallthumb_height = 0,
                    smallthumb_outerwidth = 0,
                    smallthumb_outerheight = 0;
                if (l > 1 || !o.smallthumb_hide_single) {
                    G = parseInt(z.css('borderLeftWidth'), 10) + parseInt(z.css('borderRightWidth'), 10) + parseInt(A.css('borderLeftWidth'), 10) + parseInt(A.css('borderRightWidth'), 10);
                    smallthumb_margin = parseInt(z.css('marginTop'), 10);
                    smallthumb_padding = parseInt(z.css('paddingLeft'), 10) + parseInt(z.css('paddingRight'), 10) + parseInt(A.css('marginLeft'), 10) + parseInt(A.css('marginRight'), 10) + parseInt(A.css('paddingLeft'), 10) + parseInt(A.css('paddingRight'), 10);
                    smallthumb_width = Math.round((F - ((o.small_thumbs - 1) * smallthumb_margin)) / o.small_thumbs) - (G + smallthumb_padding);
                    smallthumb_height = Math.round((o.thumb_image_height * smallthumb_width) / o.thumb_image_width);
                    smallthumb_outerwidth = smallthumb_width + G + smallthumb_padding;
                    smallthumb_outerheight = smallthumb_height + G + smallthumb_padding
                }
                var H = parseInt(t.css('borderTopWidth'), 10);
                var I = parseInt(t.css('marginTop'), 10);
                var J = parseInt(t.css('paddingTop'), 10);
                if ((o.zoom_area_width - (H * 2) - (J * 2)) > o.source_image_width) {
                    o.zoom_area_width = o.source_image_width
                } else {
                    o.zoom_area_width = o.zoom_area_width - (H * 2) - (J * 2)
                }
                if (o.zoom_area_height == 'justify') {
                    o.zoom_area_height = (thumb_outerheight + smallthumb_margin + smallthumb_outerheight) - (H * 2) - (J * 2)
                } else {
                    o.zoom_area_height = o.zoom_area_height - (H * 2) - (J * 2)
                }
                if (o.zoom_area_height > o.source_image_height) {
                    o.zoom_area_height = o.source_image_height
                }
                if (ie6_iframe_fix) {
                    var K = $('<iframe marginwidth="0" marginheight="0" scrolling="no" frameborder="0" src="javascript:\'<html></html>\'"></iframe>').css({
                        position: 'absolute',
                        zIndex: 1
                    }).prependTo(t)
                }
                var L = parseInt(n.css('borderTopWidth'), 10);
                var M = parseInt(k.css('borderTopWidth'), 10) + parseInt(k.css('marginTop'), 10) + parseInt(k.css('paddingTop'), 10) + parseInt(m.css('borderTopWidth'), 10) + parseInt(m.css('marginTop'), 10) - L;
                var N = m.offset().left - h.offset().left - L;
                var O = Math.round(o.zoom_area_width * (o.thumb_image_width / o.source_image_width));
                var P = Math.round(o.zoom_area_height * (o.thumb_image_height / o.source_image_height));
                var Q = M + o.thumb_image_height - P;
                var R = N + o.thumb_image_width - O;
                var S = Math.round((O / 2));
                var T = Math.round((P / 2));
                if (o.show_hint) {
                    var U = parseInt(s.css('marginTop'), 10);
                    var V = parseInt(s.css('marginRight'), 10)
                }
                h.css({
                    width: F,
                    height: thumb_outerheight + smallthumb_margin + smallthumb_outerheight
                });
                if (o.show_icon) {
                    r.css({
                        top: thumb_outerheight - r.outerHeight(true)
                    })
                }
                if (o.show_hint) {
                    s.css({
                        margin: 0,
                        top: -U,
                        right: -V
                    })
                }
                q.css({
                    margin: 0,
                    padding: 0,
                    width: o.thumb_image_width,
                    height: o.thumb_image_height
                });
                p.css({
                    margin: 0,
                    padding: 0,
                    width: O,
                    height: P
                });
                n.css({
                    margin: 0,
                    padding: 0,
                    left: (R - N) / 2,
                    top: (Q - M) / 2
                }).hide();
                u.css({
                    width: o.zoom_area_width,
                    height: o.zoom_area_height
                });
                t.css({
                    margin: 0,
                    left: F + I
                }).animate({
                    opacity: 0
                }, 0).hide();
                if (l > 1 || !o.smallthumb_hide_single) {
                    x.css({
                        top: thumb_outerheight + smallthumb_margin,
                        width: F
                    });
                    y.css({
                        width: (smallthumb_outerwidth * B) + (B * smallthumb_margin)
                    });
                    A.css({
                        width: smallthumb_width,
                        height: smallthumb_height
                    }).attr('width', smallthumb_width);
                    z.css({
                        margin: 0,
                        marginRight: smallthumb_margin
                    });
                    var W = ((smallthumb_outerwidth * o.small_thumbs) + ((o.small_thumbs - 1) * smallthumb_margin)) - F;
                    if (W > 0) {
                        for (i = 1; i <= (B - 1); i = i + (o.small_thumbs - 1)) {
                            j = 1;
                            for (j; j <= W; j++) {
                                z.eq(i + j - 1).css({
                                    marginRight: (smallthumb_margin - 1)
                                })
                            }
                        }
                    } else if (W < 0) {
                        for (i = 1; i <= (B - 1); i = i + (o.small_thumbs - 1)) {
                            j = 1;
                            for (j; j <= (-W); j++) {
                                z.eq(i + j - 1).css({
                                    marginRight: (smallthumb_margin + 1)
                                });
                                y.css({
                                    width: parseInt(y.css('width'), 10) + 1
                                })
                            }
                        }
                    }
                }
                if (o.show_icon && !o.magnifier_invert) {
                    n.css({
                        background: n.css('background-color') + ' ' + r.css('background-image') + ' center no-repeat'
                    })
                }
                if (o.hide_cursor) {
                    n.add(r).css({
                        cursor: 'none'
                    })
                }
                if (ie6_iframe_fix) {
                    K.css({
                        width: u.css('width'),
                        height: u.css('height')
                    })
                }
                var X = k.first().find('.etalage_thumb_image');
                var Y = k.first().find('.etalage_source_image');
                if (o.magnifier_invert) {
                    q.attr('src', X.data('src')).show()
                }
                if (preview) {
                    v.attr('src', X.data('src'))
                }
                w.attr('src', Y.attr('src'));

                function stopAutoplay() {
                    if (autotimer) {
                        clearInterval(autotimer);
                        autotimer = false
                    }
                }
                function startAutoplay() {
                    if (autotimer) {
                        stopAutoplay()
                    }
                    autotimer = setInterval(function () {
                        var a = h.find('.etalage_smallthumb_active');
                        if (a.next().length > 0) {
                            a.next().click()
                        } else {
                            z.first().click()
                        }
                    }, o.autoplay_interval)
                }
                function st_click(a, b) {
                    var c = h.find('.etalage_smallthumb_active').removeClass('etalage_smallthumb_active');
                    a.addClass('etalage_smallthumb_active');
                    n.stop().hide();
                    t.stop().hide();
                    if (!b) {
                        c.stop(true, true).animate({
                            opacity: o.smallthumb_inactive_opacity
                        });
                        a.stop(true, true).animate({
                            opacity: 1
                        })
                    }
                    h.find('.etalage_thumb_active').removeClass('etalage_thumb_active').stop().animate({
                        opacity: 0
                    }, o.speed, function () {
                        $(this).hide()
                    });
                    var d = k.filter('.thumb_' + a.data('thumb_id')).addClass('etalage_thumb_active').show().stop().animate({
                        opacity: 0
                    }, 0).animate({
                        opacity: 1
                    }, o.speed);
                    X = d.find('.etalage_thumb_image');
                    Y = d.find('.etalage_source_image');
                    if (o.magnifier_invert) {
                        q.attr('src', X.data('src'))
                    }
                    if (preview) {
                        v.attr('src', X.data('src'))
                    }
                    w.attr('src', Y.attr('src'));
                    if (o.autoplay) {
                        stopAutoplay();
                        startAutoplay()
                    }
                };

                function st_move(b, c, d, e) {
                    z.each(function () {
                        var a = {
                            left: '-=' + b,
                            opacity: o.smallthumb_inactive_opacity
                        };
                        if ($(this).data('id') == e.data('id')) {
                            a.opacity = 1
                        }
                        $(this).animate(a, faster, 'swing', function () {
                            if (st_moving) {
                                c.addClass('etalage_smallthumb_first');
                                d.addClass('etalage_smallthumb_last');
                                e.addClass('etalage_smallthumb_active');
                                st_moving = false
                            }
                        })
                    });
                    st_click(e, true)
                };

                function zoom_move() {
                    var a = new_zoomx - cur_zoomx;
                    var b = new_zoomy - cur_zoomy;
                    var c = -a / zoom_follow_speed;
                    var d = -b / zoom_follow_speed;
                    cur_zoomx = cur_zoomx - c;
                    cur_zoomy = cur_zoomy - d;
                    if (a < 1 && a > -1) {
                        cur_zoomx = new_zoomx
                    }
                    if (b < 1 && b > -1) {
                        cur_zoomy = new_zoomy
                    }
                    w.css({
                        left: cur_zoomx,
                        top: cur_zoomy
                    });
                    if (preview) {
                        v.css({
                            left: cur_zoomx,
                            top: cur_zoomy
                        })
                    }
                    if (a > 1 || b > 1 || a < 1 || b < 1) {
                        zoom_move_timer = setTimeout(function () {
                            zoom_move()
                        }, 25)
                    }
                };
                if (o.show_hint) {
                    h.hover(function () {
                        s.hide()
                    })
                }
                k.add(n).add(r).mouseenter(function () {
                    n.stop().fadeTo(faster, magnifier_opacity);
                    r.stop().animate({
                        opacity: 0
                    }, faster);
                    t.stop().show().animate({
                        opacity: 1
                    }, faster);
                    if (o.magnifier_invert) {
                        X.stop().animate({
                            opacity: o.magnifier_opacity
                        }, faster)
                    }
                    if (o.autoplay) {
                        stopAutoplay()
                    }
                }).mouseleave(function () {
                    n.stop().fadeOut(o.speed);
                    r.stop().animate({
                        opacity: 1
                    }, o.speed);
                    t.stop().animate({
                        opacity: 0
                    }, o.speed, function () {
                        $(this).hide()
                    });
                    if (o.magnifier_invert) {
                        X.stop().animate({
                            opacity: 1
                        }, o.speed)
                    }
                    clearTimeout(zoom_move_timer);
                    if (o.autoplay) {
                        startAutoplay()
                    }
                });
                k.add(n).add(r).mousemove(function (e) {
                    var a = Math.round(e.pageX - X.offset().left + N);
                    var b = Math.round(e.pageY - X.offset().top + M);
                    var c = (a - S);
                    var d = (b - T);
                    if (c < N) {
                        c = N
                    }
                    if (c > R) {
                        c = R
                    }
                    if (d < M) {
                        d = M
                    }
                    if (d > Q) {
                        d = Q
                    }
                    n.css({
                        left: c,
                        top: d
                    });
                    if (o.magnifier_invert) {
                        var f = c - N;
                        var g = d - M;
                        q.css({
                            left: -f,
                            top: -g
                        })
                    }
                    new_zoomx = -((c - N) * (1 / (o.thumb_image_width / o.source_image_width)));
                    new_zoomy = -((d - M) * (1 / (o.thumb_image_height / o.source_image_height)));
                    if (o.zoom_easing) {
                        clearTimeout(zoom_move_timer);
                        zoom_move()
                    } else {
                        if (preview) {
                            v.css({
                                left: -c,
                                top: -d
                            })
                        }
                        w.css({
                            left: -c,
                            top: -d
                        })
                    }
                });
                if (l > 1 || !o.smallthumb_hide_single) {
                    h.find('.etalage_smallthumb_first').live('click', function () {
                        if (!st_moving) {
                            st_moving = true;
                            var a = $(this).removeClass('etalage_smallthumb_first');
                            var b = h.find('.etalage_smallthumb_last').removeClass('etalage_smallthumb_last');
                            var c, $next_last, $next_active;
                            if ($(this).prev().length > 0) {
                                c = a.prev();
                                $next_last = b.prev();
                                $next_active = a
                            } else {
                                c = z.eq(B - o.small_thumbs);
                                $next_last = z.eq(B - 1);
                                $next_active = $next_last.prev()
                            }
                            var d = c.position().left;
                            st_move(d, c, $next_last, $next_active)
                        }
                    });
                    h.find('.etalage_smallthumb_last').live('click', function () {
                        if (!st_moving) {
                            st_moving = true;
                            var a = h.find('.etalage_smallthumb_first').removeClass('etalage_smallthumb_first');
                            var b = $(this).removeClass('etalage_smallthumb_last');
                            var c, $next_last, $next_active;
                            if ($(this).next().length > 0) {
                                c = a.next();
                                $next_last = b.next();
                                $next_active = b
                            } else {
                                c = z.eq(0);
                                $next_last = z.eq(o.small_thumbs - 1);
                                $next_active = c.next()
                            }
                            var d = c.position().left;
                            st_move(d, c, $next_last, $next_active)
                        }
                    });
                    z.click(function () {
                        if (!$(this).hasClass('etalage_smallthumb_first') && !$(this).hasClass('etalage_smallthumb_last') && !$(this).hasClass('etalage_smallthumb_active') && !st_moving) {
                            st_click($(this), false)
                        }
                    });
                    $(window).bind('load', function () {
                        k.css({
                            'background-image': 'none'
                        });
                        t.css({
                            'background-image': 'none'
                        });
                        if (preview) {
                            preview = false;
                            v.remove()
                        }
                    })
                }
                if (o.autoplay) {
                    startAutoplay()
                }
            }
        })
    }
})(jQuery);
