function jump_words() {
    $('#words').focus();
}

function loadTemplate(templateId) {
    return $('#template_'+templateId).html();
}

function showCommentForm (itemId, db, docId, commentText, tagsText) {
    if ($('#comments_form_'+itemId).html().length == 0) {
        renderCommentForm (itemId, db, docId, commentText, tagsText);
    }
    $('#comments_form_'+itemId).show();
    hideComment(itemId);
    return false;
}

function hideCommentForm (itemId) {
    $('#comments_form_'+itemId).hide();
    showComment(itemId);
    return false;
}

function toggleCommentForm (itemId, db, docId, commentText, tagsText) {
    return $('#comments_form_'+itemId).is(':visible') ? hideCommentForm (itemId) : showCommentForm (itemId, db, docId, commentText, tagsText);
}

function renderCommentForm (itemId, db, docId, commentText, tagsText) {
    var form_template = loadTemplate('comments_form');
    form_template = form_template.replace(/__id__/g, itemId);
    form_template = form_template.replace(/__db__/g, db);
    form_template = form_template.replace(/__docid__/g, docId);
    form_template = form_template.replace(/__comment__/g, $('<div/>').text(commentText).html());
    $('#comments_form_'+itemId).html(form_template);
    $('#tags_text_'+itemId).attr('value', tagsText);
    $('#form_'+itemId).submit(function() {
        return updateComment(itemId
                    , db
                    , docId
                    , $('#comment_text_'+itemId).val()
                    , $('#tags_text_'+itemId).val()
        );
    });
    return false;
}

function showComment (itemId) {
    $('#comments_'+itemId).show();
    return false;
}

function hideComment (itemId) {
    $('#comments_'+itemId).hide();
    return false;
}

function toggleComment (itemId, commentText, tagsText) {
    return $('#comments_'+itemId).is(':visible') ? hideComment (itemId) : showComment (itemId);
}

function renderComment (itemId, commentText, tagsText) {
    var form_template = loadTemplate('comments');
    if (commentText.length || tagsText.length) {
        form_template = form_template.replace(/__comment__/g, $('<div/>').text(commentText).html());
        form_template = form_template.replace(/__tags__/g, $('<div/>').text(tagsText).html());
    } else {
        form_template = '';
    }
    $('#comments_'+itemId).html(form_template);
    return false;
}

function updateComment (itemId, db, docId, commentText, tagsText) {
    hideCommentForm(itemId);
    renderComment (itemId, commentText, tagsText);
    showComment(itemId);
    prependSavedObj(itemId, commentText, tagsText, db, docId);
    $.get('/a_saveComment.shtml',{id: docId
                                , db: db
                                , comment: commentText
                                , tags: tagsText
        }
    );
    return false;
}

function removeComment ( itemId, db, docId ) {
    hideCommentForm(itemId);
    hideComment(itemId);
    $('#save_link_'+itemId).text('добавить в избранное');
    $('#save_link_'+itemId).attr('onclick', '');
    $('#save_link_'+itemId).unbind('click');
    $('#save_link_'+itemId).click(function() { return saveDocument(itemId, db, docId,'',''); });
    $('#favorite_'+itemId).remove();

    $.get('/a_removeComment.shtml',{id: docId
                                , db: db
        }
    );
    return false;
}

function saveDocument (itemId, db, docId, commentText, tagsText) {
    if ($('#comments_form_'+itemId).length && $('#comments_form_'+itemId).html().length == 0) {
        renderCommentForm (itemId, db, docId, commentText, tagsText);
    }
    updateComment(itemId, db, docId, commentText, tagsText);
    $('#save_link_'+itemId).text('удалить из избранного');
    //! !!!!!!!!!!
    $('#save_link_'+itemId).attr('onclick', '').click(function() { return removeComment(itemId, db, docId); });
    return false;
}

function notifyDocClick(dbID, docKeyAscii, object, url, query_id) {
    $(object).parent().find('.last_click').text('(Просмотрено только что)');
    $.get('/a_notifyClick.shtml', {id: docKeyAscii
                                 , db: dbID
                                 , url: url
                                 , query_id: query_id
        }
    );
    return true;
}

function notifyDocClickOnPartner(url, dbID, docKeyAscii, object, query_id) {
    return notifyDocClick(dbID, docKeyAscii, object, url, query_id);
}

var popupsDisabled = '';

/* FIXME */
function showpopup(object, dbID, docKeyAscii, url, msgID) {
    return notifyDocClickOnPartner(url, dbID, docKeyAscii, object, '');
}


// Add to cookie hidden element
function setCookieHidden(name) {
    var hidden = getCookie('hidden');

    if (hidden) {
        if (hidden.indexOf(name) == -1) {
            setCookie('hidden', hidden + "|" + name, new Date('Mon, 01-Jan-2020 00:00:00 GMT'), '/');
        }
    } else {
        setCookie('hidden', "|" + name, new Date('Mon, 01-Jan-2020 00:00:00 GMT'), '/');
    }
}

// Remove from Cookie hidden element
function setCookieVisible(name) {
    var hidden = getCookie('hidden');
    if (hidden) {
        setCookie('hidden', hidden.replace("|"+name, ''), new Date('Mon, 01-Jan-2020 00:00:00 GMT'), '/');
    }
}

//05.02.09

function prependSavedObj(itemId, comment, tags, db, docId) {
    var vac_title = $( $('#r' + itemId ).find('a').get(0) ).text();
    var vac_href  = $( $('#r' + itemId ).find('a').get(0) ).attr('href');
    var vac_info  = $('#r' + itemId ).find('.vacinfo').text();
    var additional = "" ;
    if ( tags.length ) {
        additional += "[" + $('<div/>').text(tags).html() + "]";
    }
    if ( comment.length ) {
        additional += $('<div/>').text(comment).html();
    }
    if ( additional.length ) {
        additional = "<br />" + additional;
    }
    if ( $('#favorite_' + itemId ).length ) {
         $('#favorite_' + itemId ).remove();
    }

    var object = "<li id='favorite_" + itemId +  "'><a href='" + vac_href + "'>" + vac_title + "</a>" + '&nbsp;(<a href="" style="text-decoration:none;color:red" onclick=" return removeComment(\'' + itemId + '\', \''  + db +  '\', \'' + docId + '\')">удалить</a>)<br>' + vac_info + additional + "</li>";
    $('#saved_stuff' ).prepend ( object );
}

function removeQuery(mode, key, id){
    $('#my_q_' + id).hide();
    $.get('/a_removeQuery.shtml', {mode: mode
                                 , key: key
        }
    );
    return false;
}

function doSubmit()  {
    if (typeof(suggest) == 'undefined' || suggest.dis) {
        $('#qipsearchform FORM').submit();
        return;
    }
    if (suggest.s.style.display == 'none' || suggest.sug_select == -1) {
        $('#qipsearchform INPUT[name=from]').attr('value', suggest.sel_pr ? 'presug' : (suggest.hc ? 'suggest' : ''));
        $('#qipsearchform FORM').submit();
    }
    if (suggest.sug_select >= 0) {
        suggest.setSuggest(suggest.s.childNodes[suggest.sug_select]);
        if (suggest.s.style.display != 'none') {
            suggest.hide();
            return;
        }
    }
}

function sample(el, track_url){
    $('#search_form_text').attr('value', $(el).text());
    $.ajax({
       type     : 'jsonp',
       url      : track_url,
       success  : function (data) {}
    });
    return false;
}

$( function(){
    $('.bhvr-show-filters').click(function(e){
        e.preventDefault();
        $(this).parent().siblings().show();
        $(this).hide();
    });
    $('.bhvr-show-hide-block').click(function(e){
        e.preventDefault();
        $(this).parent().next().toggle();
        $(this).toggleClass('open').toggleClass('close');
        if ( $(this).hasClass('open') ) {
            setCookieVisible($(this).attr('id'));
        } else {
            setCookieHidden($(this).attr('id'));
        }
    });
    
    $('.bhvr-clear').click(function(e){
        e.preventDefault();
        zz = eval( "(" + $(this).attr('rel') + ")" );
        $.get('/a_cleanBlock.shtml', zz );
        $(this).parent().prevAll().hide();
        if ( zz.hide_parent ) {
            $(this).parents('.leftbox').hide();
        }
    });
    
    $('#qipsearchtab A').click(function(e){
        e.preventDefault();
        $('UL.bookmark LI.current').removeClass('current')
        $('#qipsearchtab').addClass('current');
        $('#jobsearchform').hide();
        $('#qipsearchform').show();
        $('#qipsearchwords').focus();
    });
    
    $('UL.bookmark LI.current').find('A').click(function(e){
        e.preventDefault();
        $(this).parent().parent().addClass('current');
        $('#qipsearchtab').removeClass('current');
        $('#jobsearchform').show();
        $('#qipsearchform').hide();
    });
    
    if (typeof(suggest) != 'undefined') {
        suggest.pos = function() {
            var input = $('#search_form_text');
            var suggestions = $('#suggestions');
            var position = input.position();
            suggestions.width(input.width());
            suggestions.css('top', (position.top + input.height()) + 'px');
            suggestions.css('left', (position.left + 3) + 'px');
        };
        suggest.init('suggestions', 'search_form_text', 'qipsearchform', 'qipsearchbutton', 'http://search.qip.ru/suggest?q=');
        suggest.oldCb = suggest.cb;
        suggest.cb = function(json) {
            suggest.oldCb(json);
            $('#suggestions .sclose').text('Закрыть');
        }
        suggest.goSearch = doSubmit;
    }
    
    //! restore element states:
    var hidden = getCookie('hidden');
    if ( hidden ) {
        var hidden_blocks = hidden.split("|");
        for (key in hidden_blocks) {
            if ( hidden_blocks [ key ].length > 0 ) {
                $('#' + hidden_blocks [ key ] ).click();
            }
        }
    }
});