$(function() {
    $('#jqm-window').jqm({
        ajax: '@href',
        ajaxText: '<div class="loading"><img src="/images/layout/indicator.gif" /></div>',
        target: '#jqm-content',
        closeClass: 'jqm-close',
        onShow: function (hash) {
            hash.o.fadeIn('slow');
            hash.w.show();
        },
        onHide: function (hash) {
            hash.w.fadeOut('fast');
            hash.o.fadeOut('fast');
            hash.o.remove();
        }
    });

    $('a[rel=modal]').live('click', function() {
    	$('#jqm-window').jqmShow(this);
    	return false;
    });

    $('#news-alert').jqm({
        target: '#jqm-content',
        closeClass: 'jqm-close',
        onShow: function (hash) {
            hash.o.fadeIn('slow');
            hash.w.show();
        },
        onHide: function (hash) {
            hash.w.fadeOut('fast');
            hash.o.fadeOut('fast');
            hash.o.remove();

            // Don't show it again.
            $.get('/news/hide-news-alert');
        }
    });

    $('#vip-modal').jqm({
        target: '#jqm-content',
        closeClass: 'jqm-close',
        onShow: function (hash) {
            hash.o.fadeIn('slow');
            hash.w.show();

            // Store number of views.
            $.get('/site/t?t-type=user&t-action=vip-modal.view');

            if (typeof pageTracker !== 'undefined') {
            	pageTracker._trackEvent('vip-modal', 'view', 'version-2010-11-04');
            }
        },
        onHide: function (hash) {
            hash.w.fadeOut('fast');
            hash.o.fadeOut('fast');
            hash.o.remove();

            // Store number of times the dialog is closed.
            $.get('/site/t?t-type=user&t-action=vip-modal.close');

            if (typeof pageTracker !== 'undefined') {
            	pageTracker._trackEvent('vip-modal', 'close', 'version-2010-11-04');
            }
        }
    });

    $('#vip-modal-banner').click(function() {
        if (typeof pageTracker !== 'undefined') {
        	pageTracker._trackEvent('vip-modal', 'click-upgrade-now', 'version-2010-11-04');
        }
    });

    var delay = 400;

    function hideMenu() {
        if (!$('.custom_button').data('in') && !$('.hover_menu').data('in') && !$('.hover_menu').data('hidden')) {
            $('.hover_menu').fadeOut('fast', function(){
                $('.custom_button').removeClass('active');
                $('.hover_menu').data('hidden', true);
            });
        }
    }

    $('.custom_button, .hover_menu').mouseenter(function() {
        $('.hover_menu').fadeIn('fast');
        $('.custom_button').addClass('active');
        $(this).data('in', true);
        $('.hover_menu').data('hidden', false);
    }).mouseleave(function() {
        $(this).data('in', false);
        setTimeout(hideMenu, delay);
    });

    //
    $('#flash-message .close').click(function(){
        $('#flash-message').fadeOut(250);
    });
});
