$(document).ready(function() {
    // Fix logos png for IE
    $('.top-search-pic').pngFix();
    $('.top-search-left').pngFix();
    //$('.top-search-content .search-line').pngFix();

    // Search button
    $('#search-find').click(function() {
        $('#search-form').submit();
    });

    // Search checkbox
    $('.top-search-content .search-subline .checkbox-text').click(function() {
        $('input:checkbox[name=search-type]').attr('checked', !$('input:checkbox[name=search-type]').attr('checked'));
    });

    // Show\hide left block content
    $('.block-content .header .pic').click(function() {
        if ($(this).hasClass('pos')) {
            $(this).removeClass('pos');
            $(this).addClass('neg');
        } else {
            $(this).removeClass('neg');
            $(this).addClass('pos');
        }
        $(this).parent().parent().next().slideToggle('fast');
    });

    // Search input
    $('.top-search-content input:text').focus(function() {
        $('.top-search-content .search-line').removeClass('transparency');
    });
    $('.top-search-content input:text').blur(function() {
        $('.top-search-content .search-line').addClass('transparency');
    });

    // Top links submenu
    $('.toplinks .menu .link-all').bind('click', servicesShow);
    $('.toplinks .menu .link-all .submenu').mouseenter(function() {
        clearTimeout(servicesTimeout);
    });
    $('.toplinks .menu .link-all .submenu').mouseleave(function() {
        servicesTimeout = setTimeout("servicesHide();", 3000);
    });

    // Main page links hover property
    $('.content-column .block-content .preview .descr .buts .buttons li').hover(function() {
        $(this).css('text-decoration', 'none');
    }, function() {
        $(this).css('text-decoration', 'underline');
    });

    checkIcq();
});

function checkIcq() {
    $.post('/ajax/checkicq', {}, function(data) {
        if (data.status == 1) {
            $('.block#librarian li.icq').addClass('online');
        } else {
            $('.block#librarian li.icq').removeClass('online');
        }
    }, 'json');
    setTimeout('checkIcq();', 60000);
}

var servicesTimeout;

function servicesShow() {
    var submenu = $('.toplinks .menu .link-all .submenu:hidden');
    if (submenu) {
        $(submenu).show();
        $('.toplinks .menu .link-all').unbind('click');
        $('.toplinks .menu .link-all').bind('click', servicesHide);
    }
}

function servicesHide() {
    $('.toplinks .menu .link-all .submenu').hide();
    $('.toplinks .menu .link-all').unbind('click');
    $('.toplinks .menu .link-all').bind('click', servicesShow);
}


function addMessage(text, type) {
    var time = new Date();
    msg = $("<div class='message" + type + "'></div>").html("[" + time.toLocaleTimeString() + "] " + text);
    $(msg).prependTo('#messageBox');
    $(msg).hide().fadeIn(500);
    $(msg).click(function() {
        $(this).fadeOut(500, function() { $(this).remove(); });
    });

    msgId = Math.round(Math.random() * 100000);
    if ($("#messageBox div").length > 4) {
        $("#messageBox div:last").fadeOut(500, function() { $(this).remove(); });
    }
    while ($("div#message" + msgId).length > 0) {
        msgId = Math.round(Math.random() * 100000);
    }
    $(msg).attr('id', 'message' + msgId);
    setTimeout("$('div#message" + msgId + "').fadeOut(500,function(){$('div#message" + msgId + "').remove();});", 5000);
    return msgId;
}

function addBookmark(document, page) {
    $.post('/ajax/bookmarkadd', { 'docid': document, 'page': page, 'title': $('.bookmark-form input:text').val() }, function(data) {
        if (data.result) {
            addMessage(data.resultMessage, 'Success');
            $('ul.bookmarks').append('<li id="bookmark' + data.key + '"><a href="#" class="icon" onclick="return removeBookmark(' + data.key + ');"></a><a href="/library/' + data.document + '/' + data.page + '" title="' + data.alt + '">' + data.title + '</a></li>');
            $('.bookmark-form input:text').val('');
        } else {
            addMessage(data.resultMessage, 'Error');
        }
    }, 'json');
    return false;
}

function removeBookmark(id) {
    $.post('/ajax/bookmarkremove', { 'id': id }, function(data) {
        if (data.result) {
            addMessage(data.resultMessage, 'Success');
            $('ul.bookmarks li#bookmark' + id).remove();
        } else {
            addMessage(data.resultMessage, 'Error');
        }
    }, 'json');
    return false;
}
