function lazyModalLoading()
{
    if($j.loadingTimer) {
        clearTimeout($j.loadingTimer);
    }

    $j.loadingTimer = setTimeout('modalLoading()', 1000);
}

function modalLoading()
{
    $j.blockUI({
        message: $j('#loadingMessage'),
        css: {
            padding: 10,
            border: '0',
            backgroundColor: '#ffffff',
            border:         '1px solid #aaa'
        },

        baseZ: 8000
    });
}


$j(document).ready(function() {
    setTimeout('keepAlive()', 120000);

    $j(document).bind("ajaxSend", function(){
        lazyModalLoading();
    }).bind("ajaxComplete ajaxError", function(){
        //stop the loading timer
        if($j.loadingTimer) {
            clearTimeout($j.loadingTimer);
        }
        $j.unblockUI();
        Cufon.refresh();
        constructPapers();
    });

    $j("input[type=image]").bind("ajaxSend", function(){
        $j(this).addClass('ui-state-disabled');
        $j(this).attr('disabled', 'disabled');
        $j('body').css('cursor', 'progress');
    }).bind("ajaxComplete", function(){
        $j(this).removeClass('ui-state-disabled');
        $j(this).attr('disabled', false);
        $j('body').css('cursor', 'auto');
    });

    $j(window).bind("ajaxComplete", function(event, XMLHttpRequest, ajaxOptions) {
        if(XMLHttpRequest.responseText == 'loginRequired') {
            this.location.reload();
        }
    });

    $j('.closeCustomDialog').live('click', function() {
        $j(document).unbind('userLoggedIn');

        $j('.customDialog').fadeOut('normal', function() {
            $j(this).children('.popUpContainer').remove();
            $j(this).dialog('close').remove();
        });

        return false;
    });


    $j('.halfDisabled').css('opacity', 0.5);
});


function keepAlive()
{
    $j.ajax({
        type: 'get',
        url: '/default/index/keep-alive'
    });

    setTimeout('keepAlive()', 120000);
}

$j(document).ready(function() {
    
});



function scrollToTop()
{
    $j("html,body").stop().animate({
        scrollTop: 0
    }, 400, function(){
            //scroll complete function
    });
}


$j('.popupLink').live('click', function() {
    var url = $j(this).attr('href');

    window.open(url, "fbShare", "status = 1, height = 350, width = 600");

    return false;
});



$j(document).ready(function() {
    $j('.bogdanOverImg:not(.active)').live('mouseover mouseout', function(event) {
        if(event.type == 'mouseover') {
            this.src = this.src.replace('-off', '-over');
        } else {
            this.src = this.src.replace('-over', '-off');
        }
    });
    
    $j('.bogdanOverImg').each(function() {
        var overImg = this.src.replace('-off', '-over');
        $j('#preloadOvers').append('<span style="background:url(\'' + overImg + '\')"></span>');
    });
});


$j('.dynamicContentLink').live('click', function() {
    var url = $j(this).attr('href');
    $j('#dynamicForms').load(url);

    return false;
});


$j('#loginForm').live('submit', function() {
    sendForm($j(this), true, true, true, true, 'afterLoginSuccess');

    return false;
});

$j('#registerForm').live('submit', function() {
    sendForm($j(this), true, true, true, true, 'afterRegisterSuccess');

    return false;
});

$j('#forgotPasswordForm').live('submit', function() {
    sendForm($j(this), true, true, true, true, 'afterRecoverSuccess');

    return false;
});

$j('#changepassForm').live('submit', function() {
    sendForm($j(this), true, true, true, true, 'afterChangePasswordSuccess');

    return false;
});

$j('#editProfileForm').live('submit', function() {
    sendForm($j(this), true, true, true, true, 'afterEditProfileSuccess');

    return false;
});


$j('.logout').live('click', function() {
    var url = $j(this).attr('href');

    if(FB && hasFBSession == 1) {
        FB.logout(function() {
            $j.post(url, {}, function() {
                window.location.href = '/';
            });
        });
    } else {
        $j.post(url, {}, function() {
            window.location.href = '/';
        });
    }

    return false;
});
