/*
 * Kapos.hu menu extension
 * Keeping the top level menu item selected when hovering over a sub-menu
 * and add :hover support for IE6.
 */
$(document).ready(function() {
    $("ul.menu ul").each(function(i) {
        $(this).hover(function() {
            $(this).parent().find("a").slice(0,1).addClass("active");
        },function() {
            $(this).parent().find("a").slice(0,1).removeClass("active");
        });
    });

    if($.browser.msie && ($.browser.version < 7)) {
        $("ul.menu").each(function(i) {
            $(this).find("li").hover(function() {
                $(this).addClass("hover");
            },function() { 
                $(this).removeClass("hover"); 
            });
        });
    }
});


/*
 * Kapos.hu tab changer
 */
$(document).ready(function(){
    $(".tabBoxHead ul li").each(function(i) {
        $(this).click(function(){
            var index = $(this).parent('ul').children('li').index($(this)); 

            $(this).parents('ul').children('li').removeClass('active');
            $(this).addClass("active");
            $(this).parents('div.tabBox').find('.tabBoxBody').children('div').hide();
            $(this).parents('div.tabBox').find('.tabBoxBody').children('div').eq(index).show();

            return false;
        });
    });
});


/*
 * Submit vote by AJAX query
 */
function Vote(poll_id) {
    var option_id = $("input[name='poll-option-" + poll_id + "']:checked").val();
    var human_key = $("input[name='human_key']").val();
    var human_input = $("input[name='human_input']").val();

    $.ajax({
        url: '/szavazas/' + poll_id + '/ajax_szavazas.html',
        global: false,
        type: "POST",
        data: ({ poll_id: poll_id, option_id: option_id, human_key: human_key, human_input: human_input }),
        dataType: "html",
        success: function(result) {
            if (result == 'error:1') {
                jQuery.facebox('<h1>Hiba</h1><p>A kért szavazás nem található.</p>');
            }
            else if (result == 'error:2') {
                jQuery.facebox('<h1>Hiba</h1><p>A kiválasztott opció nem elérhető.</p>');
            }
            else if (result == 'error:3') {
                jQuery.facebox('<h1>Hiba</h1><p>Elfelejtette kiválasztani az opciót, amelyre szavazni szeretett volna.</p>');
            }
            else if (result == 'error:4') {
                jQuery.facebox('<h1>Hiba</h1><p>Az Ön IP címéről már szavaztak.</p><p>Egy IP címről, 10 percen belül, csak 1, azaz egy darab szavazatot tudunk elfogadni.</p>');
            }
            else if (result == 'error:5') {
                jQuery.facebox('<h1>Hiba</h1><p>Hibás ellenőrző kód!</p><p>Kérjük, adja meg a képen látható 3 karaktert.</p>');
            }
            else {
                $('#poll-' + poll_id).html(result);
            }

            if (typeof human_key != 'undefined') {
                $("a.image").fancybox({
                    'transitionIn': 'none',
                    'transitionOut': 'none',
                    'titleShow': true,
                    'titlePosition': 'over',
                });
            }
        }
    });
}


/*
 * Submit comment by AJAX query
 */
function newComment(content_type, content_id) {
    $('div.content input[title], div.content textarea[title]').qtip("disable");

    var realname = $("#newComment input[name='realname']").val();
    var email = $("#newComment input[name='email']").val();
    var message = $("#newComment textarea[name='message']").val();
    var aggree = $("#newComment input[name='aggree']:checked").val();
    var human_key = $("#newComment input[name='human_key']").val();
    var human_input = $("#newComment input[name='human_input']").val();
    var cat = $("#newComment input[name='cat']").val();
    var date = $("#newComment input[name='date']").val();
    var id = $("#newComment input[name='id']").val();

    $.ajax({
        url: '/common/cgi/AJAX_newComment.cgi',
        global: false,
        type: "POST",
        data: ({ content_type: content_type, content_id: content_id, realname: realname, email: email, message: message, aggree: aggree, human_key: human_key, human_input: human_input, cat: cat, date: date, id: id }),
        dataType: "html",
        success: function(result) {
            if (result == 'error:1') {
                jQuery.facebox('<h1>Hiba</h1><p>Kérjük, adja meg nevét! A név min. 3 max. 32 karakteres lehet.</p>');
            }
            else if (result == 'error:2') {
                jQuery.facebox('<h1>Hiba</h1><p>Kérjük, adjon meg érvényes e-mail címet!</p>');
            }
            else if (result == 'error:3') {
                jQuery.facebox('<h1>Hiba</h1><p>Kérjük, adja meg üzenetét! Az üzenet min. 3 max. 32000 karakteres lehet.</p>');
            }
            else if (result == 'error:4') {
                jQuery.facebox('<h1>Hiba</h1><p>Az üzenet elküldéséhez el kell fogadnia adatvédelmi és moderálási irányelveinket.</p>');
            }
            else if (result == 'error:5') {
                jQuery.facebox('<h1>Hiba</h1><p>Hibás ellenőrző kód!</p><p>Kérjük, adja meg a képen látható 3 karaktert.</p>');
            }
            else if (result == 'error:6') {
                jQuery.facebox('<h1>Hiba</h1><p><p>Hozzászólása olyan szófordulatot tartalmazott, amelyet szerkesztőségünk a megjelenítés előtt át kell, hogy nézzen.<br /><br />Ez a hozzászólások mennyiségétől függően több-kevesebb időbe is telhet. Kérjük, legyen türelemmel.</p></p>');
                $("#newComment textarea[name='message']").val('');
                $("#newComment input[name='human_input']").val('');
            }
            else if (result == 'error:7') {
                jQuery.facebox('<h1>Hiba</h1><p>Üzenetét már rögzítettük, ne küldje el többször.</p>');
            }
            else {
                $('#newComment').html(result);
                getComments(content_type, content_id, cat, date, id);
                location.href = '#comments';
                jQuery.facebox('<h1>Hozzászólás</h1><p>Köszönjük, üzenetét rögzítettük.</p>');
            }
        }
    });
}


/*
 * Get comments by AJAX query
 */
function getComments(content_type, content_id, cat, date, id) {
    $.ajax({
        url: '/common/cgi/AJAX_getComments.cgi',
        global: false,
        type: "POST",
        data: ({ content_type: content_type, content_id: content_id, cat: cat, date: date, id: id }),
        dataType: "html",
        success: function(result) {
            if (result == 'error:1') {
                jQuery.facebox('<h1>Hiba</h1><p>A hozzászólások lekérdezése nem sikerült.</p>');
            }
            else {
                $('.commentList:eq(0)').html(result);
            }
        }
    });
}

