var NightCry = {
    portfolio : {
        init : function() {
            $('.thumb_list li').each(function(i) {
                $(this).attr('number', i+1);
            });
            NightCry.portfolio.visited.check();
            $('.thumb_list li').click(function(e) {
                e.preventDefault();
                NightCry.portfolio.thumbFade($(this).find('a.img span'), 'out', 'default');
            });
            this.overlay.init();
        },
        thumbs : null,
        labels : null,
        thumbFade : function(elm, state, rate) {
            var parentLi = elm.parents('li');
            if (state == 'out' && !parentLi.hasClass("visited")) {
                parentLi.addClass("visited");
                NightCry.portfolio.visited.add(parentLi.attr('number'));
            }
        },
        visited : {
            add : function(num) {
                if (num) {
                    var cookie = $.cookie('NCG-Portfolio');
                    if (cookie) {
                        var lis = cookie.split('|');
                        if ($.inArray(num, lis) == -1) {
                            $.cookie('NCG-Portfolio', cookie+'|'+num);
                        }
                    }
                    else {
                        $.cookie('NCG-Portfolio', num);
                    }
                }
            },
            sortFunction : function(a, b){
                return (a - b); //causes an array to be sorted numerically and ascending
            },
            check : function() {
                if ($.cookie('NCG-Portfolio')) {
                    var cookie = $.cookie('NCG-Portfolio');
                    if (cookie.match(/|/g)) {
                        var lis = cookie.split('|');
                        lis.sort(NightCry.portfolio.visited.sortFunction);
                        $('.thumb_list li').each(function(i) {
                            if ($(this).attr('number')) {
                                var numToFade = $(this).attr('number');
                                if ($.inArray(numToFade, lis) > -1) {
                                    NightCry.portfolio.thumbFade($(this).find('a.img span'), 'out', 0);
                                }
                            }
                        });
                    }
                    else {
                        $('.thumb_list li').each(function(i) {
                            if ($(this).attr('number')) {
                                if (cookie == $(this).attr('number')) {
                                    NightCry.portfolio.thumbFade($(this).find('a.img span'), 'out', 0);
                                }
                            }
                        });
                    }
                }
            }
        },
        overlay : {
            init : function() {
                $('body').append('<div id="overlay"><div class="slideshow"></div></div>');
                $('a[rel="#overlay"]').overlay(this.config);
            },
            config : {
                mask: {
                    color: '#000',
                    loadSpeed: 200,
                    opacity: 0.3
                },
                onBeforeLoad: function() {
                    var wrap = this.getOverlay().find(".slideshow");
                    wrap.load(this.getTrigger().attr("href"));
                },
                onClose: function() {
                    $(".slideshow").html('');
                },
                fixed: false
            }
        },
        slideShow : {
            init : function(elm) {
                elm.tabs(".images > img", {
                    effect: 'fade',
                    fadeOutSpeed: "slow",
                    rotate: true
                }).slideshow();
            }

        }
    },
    externalLinks : function() {
        $('a[rel*="external"]').click(function(e) {
            var newWindow = window.open(this.href);
            newWindow.focus();
            e.preventDefault();
        });
    }
};
$(document).ready(function() {
    NightCry.externalLinks();
    if ($('body#portfolio').length) {
        NightCry.portfolio.init();
    }
});
