jQuery(function($) {

    $.l = function(txt) {
        if (console != undefined) {
            console.log(txt);
        }
    }

    // Enable contact form
    /*$.onPlayerComplete = function() {
        if ($('#contact-wrapper form').length > 0) {
            $('#contact-wrapper').fadeIn('fast');
        }
    };
    $.onPlayerPlaying = function() {
        $('#contact-wrapper').hide();
    };*/

    // Enable dropdown submenu
    $('#subnav').children('li').children('a').click(function(event) {
        var li = $(this).closest('li');
        if (!li.hasClass('active')) {
            li.addClass('active');
            event.preventDefault();
        } else {
            li.removeClass('active');
            event.preventDefault();
        }
        return false;
    });
    // Close dropdown on click anywhere in the document
    $('#subnav').click(function(event) {
        $('#subnav').data('clickedInside', true);
    });
    $(document).click(function(event) {
        if ($('#subnav').data('clickedInside') != true) {
            $('#subnav').children('li').removeClass('active');
        }
        $('#subnav').data('clickedInside', false);
    });

    $.fn.fadeToHi = function(speed, callback)
    {
        var _this = $(this);
        _this.find('.hi').stop(true);
        _this.find('.lo').stop(true);
        _this.find('.hi').fadeTo(0, 0);
        _this.find('.lo').fadeTo(0, 1);
        _this.find('.lo').fadeTo(speed == null ? 'normal' : speed, 0, function() {
            _this.find('.hi').fadeTo(speed == null ? 'slow' : speed, 1, callback);
        });
    };

    $.fn.fadeToLo = function(speed, callback)
    {
        var _this = $(this);
        /*_this.find('.hi').stop(true);
        _this.find('.lo').stop(true);*/
        _this.find('.hi').fadeTo(speed == null ? 'normal' : speed, 0);
        _this.find('.lo').fadeTo(speed == null ? 'normal' : speed, 1, callback);
    };

    if ($('.media-item.matrix-6x6').length > 0) {
        var autoFade = function() 
        {
            if (!$('#matrix_carousel').data('mouseEnter')) {
                var items = $('.media-item');
                var randomItem = $(items[Math.floor(Math.random() * items.length)]);
                if (!randomItem.data('autoFading')) {
                    randomItem.data('autoFading', true);
                    randomItem.fadeToHi(4000, function() {
                        $(this).closest('.media-item').fadeToLo(4000, function() {
                            randomItem.data('autoFading', false);
                        });
                    });
                }
            }
            startAutoFade();
        };
        var startAutoFade = function(time) {
            setTimeout(autoFade, time == undefined ? 1000 / $('.media-item').length * 5 : time);
        };

        $('#matrix_carousel').data('mouseEnter', false);
        $('#matrix_carousel').mouseenter(function() {
            $(this).data('mouseEnter', true);
            $('.media-item').each(function() {
                _this = $(this);
                _this.data('autoFading', false);
                if (!_this.data('mouseEnter')) {
                    _this.find('.hi').stop(true);
                    _this.find('.lo').stop(true);
                    _this.fadeToLo('fast');
                }
            });
        });
        $('#matrix_carousel').mouseleave(function() {
            $(this).data('mouseEnter', false);
        });
        startAutoFade(2000);
        

        var appearElements = $('.appear');
        var rows = 6;
        var itemsPerRow = (appearElements.length / rows);
        for (var i = 0; i < rows; i++) {
                var delay = Math.round(10 + Math.random() * 1000);
                for (var j = 0; j < itemsPerRow; j++) {
                        $(appearElements[i + j * rows]).delay(delay + j * 100).fadeIn('slow');
                }
        }
    } else {
        $('.appear').each(function(index) {
            $(this).delay((index+10) * 75).fadeIn('slow');
        });
    }

    $('#matrix_carousel').mouseleave(function() {
        if ($('#media-info').data('prev-html')) {
            $('#media-info').html($('#media-info').data('prev-html'));
        }
    })

    $('.media-item').each(function(index) {
        if ($(this).find('img').length == 0) return; 
        $(this).data('index', index);
        $(this).mouseenter(function(event) {
            if (!$('#media-info').data('prev-html')) {
                $('#media-info').data('prev-html', $('#media-info').html());
            }
            var _this = $(this);
            _this.data('mouseEnter', true);
            _this.fadeToHi();
            $('.media-item').each(function() {
                if ($(this).find('img').length == 0) return; 
                var inactive = $(this).find('.inactive');
                inactive.stop(); 
                if ($(this).data('index') != index) {
                    inactive.fadeTo('fast', .75);
                } else {
                    inactive.fadeTo('fast', 0);
                }
            });

            var desc = $(this).find('.description').clone();
            $('#media-info').html(desc);
        });
        $(this).mouseleave(function(event) {
            var _this = $(this);
            _this.data('mouseEnter', false);
            _this.fadeToLo();
            $('.media-item').each(function() {
                var inactive = $(this).find('.inactive');
                inactive.stop();
                inactive.fadeTo('fast', 0).delay();
            });
        });
    });

/*  $('.media-item.matrix-3x3').each(function() {
        $(this).mouseenter(function(event) {
            var info = $(this).find('.info');
            if (!info.data('originalHeight')) {
                info.data('originalHeight', info.css('height'));
            }
            info.css('height', info.data('originalHeight'));
            $(this).find('.info').stop();
            $(this).find('.info').css('opacity', .75);
            $(this).find('.info').slideDown('fast');
        });
        $(this).mouseleave(function(event) {
            $(this).find('.info').stop();
            $(this).find('.info').slideUp('fast');
        });
    });*/

    $('.appear-first').fadeIn('normal');
});
