(function ($) {
    $.fn.eShowcase = function (options) {
        var defaults = {
            block_class: 'e-showcase-block',
            link_class: 'e-showcase-link',
            filter_class: 'e-showcase-filter',
            showNav: false,
            nav_class: 'e-showcase-navigation',
            initial_block: 'block_2',
            animSpeed: 500,
            startCollection: 'photograpy',
            collection_id: 'showcase_items'
        };
        var s = $.extend({}, defaults, options);
        return this.each(function () {
            var t = $(this),
                block = t.find('.' + s.block_class),
                items;
            block.hide();
            showcase = {
                init: function () {
                    showcase.loading();
                    showcase.block.link();
                    showcase.collection.start();
                    showcase.collection.change();
                    if (s.showNav) {
                        showcase.nav.create();
                        showcase.nav.previous();
                        showcase.nav.next();
                        showcase.nav.close();
                    }
                },
                loading: function () {
                    var count = 0,
                        done = false,
                        all = t.find('img').length;
                    t.append($('<div />').addClass('loader'));
                    $('.' + s.block_class).each(function () {
                        var img = new Image,
                            old = $(this).find('img'),
                            src = old.attr('src');
                        count += old.length;
                        old.css({
                            visibility: 'hidden'
                        });
                        $(img).load(function () {
                            old.css({
                                visibility: 'visible'
                            });
                        }).attr('src', src)
                        done = count == all ? true : false;
                    });
                    var timer = setInterval(function () {
                        if (done == true) {
                            showcase.block.start();
                            t.find('.loader').fadeOut(function () {
                                $(this).remove();
                            });
                            clearInterval(timer);
                        }
                    }, 400);
                },
                block: {
                    start: function () {
                        block.filter('#' + s.initial_block).addClass('active').show();
                        $('#' + s.collection_id).find('a').filter(function () {
                            return $(this).attr('rel') == s.initial_block;
                        }).addClass('active');
                    },
                    change: function (block_id) {
                        var current = $('#' + block_id);
                        block.removeClass('active').fadeOut(s.animSpeed);
                        current.addClass('active').fadeIn(s.animSpeed, function () {
                            $.scrollTo(current, 500, {
                                axis: 'y'
                            });
                        });
                    },
                    link: function () {
                        $('.' + s.link_class).live('click', function () {
                            var a = $(this),
                                rel = a.attr('rel');
                            $('.' + s.link_class).removeClass('active');
                            a.addClass('active');
                            showcase.block.change(rel);
                            return false;
                        });
                    }
                },
                nav: {
                    create: function () {
                        t.after('<div class="e-showcase-navigation"><ul><li class="previous">Previous</li><li class="next">Next</li></ul>');
                    },
                    previous: function () {
                        $('.' + s.nav_class).find('.previous').live('click', function () {
                            var a = $('#' + s.collection_id).find('a').filter('.active'),
                                rel = a.attr('rel'),
                                prev = items[($.inArray(rel, items) - 1 + items.length) % items.length],
                                active = $('.' + s.link_class).filter(function () {
                                    return $(this).attr('rel') == prev;
                                });
                            a.removeClass('active');
                            showcase.block.change(prev);
                            active.addClass('active');
                            return false;
                        });
                    },
                    next: function () {
                        $('.' + s.nav_class).find('.next').live('click', function () {
                            var a = $('#' + s.collection_id).find('a').filter('.active'),
                                rel = a.attr('rel'),
                                next = items[($.inArray(rel, items) + 1) % items.length],
                                active = $('.' + s.link_class).filter(function () {
                                    return $(this).attr('rel') == next;
                                });
                            a.removeClass('active');
                            showcase.block.change(next);
                            active.addClass('active');
                            return false;
                        });
                    },
                    close: function () {
                        $('.' + s.nav_class).find('.close').live('click', function () {
                            var a = $(this),
                                h = block.filter('.active').height() || '465px';
                            if (block.is(':visible')) {
                                a.text('Open');
                                block.fadeOut();
                                t.animate({
                                    height: 0
                                });
                            } else {
                                block.filter('.active').fadeIn();
                                t.animate({
                                    height: h
                                });
                                a.text('Close');
                            }
                            return false;
                        });
                    }
                },
                collection: {
                    start: function () {
                        var start = s.startCollection,
                            li = $('#' + s.collection_id).find('li'),
                            collection = li.filter('.' + start),
                            a = $('.' + s.filter_class).find('a').filter(function () {
                                return $(this).attr('rel') == start;
                            });
                        if (start != 'all') {
                            li.hide();
                            collection.show();
                        }
                        a.addClass('active');
                        showcase.collection.arr();
                    },
                    change: function () {
                        $('.' + s.filter_class).find('a').click(function () {
                            var a = $(this),
                                rel = a.attr('rel'),
                                collection = $('#' + s.collection_id),
                                li = collection.find('li');
                            $('.' + s.filter_class).find('a').removeClass('active');
                            a.addClass('active');
                            collection.fadeOut(400, function () {
                                li.hide();
                                if (rel == 'all') li.show();
                                else li.filter('.' + rel).show();
                                collection.fadeIn();
                            });
                            setTimeout(function () {
                                showcase.collection.arr();
                            }, 1000);
                            return false;
                        });
                    },
                    arr: function () {
                        items = new Array, li = $('#' + s.collection_id).find('li'), filter = li.filter(function () {
                            return $(this).css('display') != 'none';
                        }), filter.each(function () {
                            var t = $(this),
                                anchor = t.find('.' + s.link_class).attr('rel');
                            items.push(anchor);
                        });
                    }
                }
            }
            showcase.init();
        });
    };
}(jQuery));
