

initMenu = function() {
if ($("#fomenu").size() > 0)
    {
        var menu_on = "";
        if ($("#fomenu .on").size() > 0)
        {
            var menu_on_id = $("#fomenu .on").attr("id");
            if (menu_on_id) menu_on = menu_on_id.split("_")[0];
        }
        var menu_to = new Array();

        var menuOver = function(menuid) {
            window.clearTimeout(menu_to[menuid]);
            $("#"+menuid+"_menu").addClass("on");
            $(".almenu2").hide();
            $("#"+menuid+"_almenu").show();
            amw = $("#"+menuid+"_almenu").width();
            fmw = $("#"+menuid+"_menu").width() + 20;
            if (amw < fmw) $("#"+menuid+"_almenu").width(fmw);
        }

        var menuOut = function(menuid) {
            menu_to[menuid] = window.setTimeout(function() {
                if (menu_on != menuid) $("#"+menuid+"_menu").removeClass("on");
                $("#"+menuid+"_almenu").hide();
            },50);
        }

        $("#fomenu .fomenuitem").add(".almenu2").not("#"+menu_on+"_menu").each(function() {
            var menuid = $(this).attr("id");
            if (menuid) {
                menuid = menuid.split("_")[0];
                menu_to[menuid] = null;
                $(this).mouseenter(function() {
                    menuOver(menuid);
                }).mouseleave(function() {
                    menuOut(menuid);
                });
            }
        });
    }
};




$.fn.rotateElements = function() {
    var elements = this;
    $(elements).eq(0).fadeIn(500, function() {
        if ($(elements).size() > 1)
        {
            window.setTimeout(function() {
                $(elements).eq(0).fadeOut(500, function() {
                    $(this).appendTo($(this).parent());
                    $(this).parent().children().rotateElements();
                });
            },4000);
        }
    });
}

var kbtn = new Array();
kbtn['elozo_het_btn'] = [-452,22];
kbtn['kovetkezo_het_btn'] = [-578,22];
kbtn['kuldes_btn'] = [-1125,22];
kbtn['uj_hozzaszolas_btn'] = [-784,22];
kbtn['keres_btn'] = [-80,28];
kbtn['uj_eszleles_bekuldese_btn'] = [0,38];
kbtn['szerkeszt_btn'] = [-1205,22];
kbtn['jelentem_btn'] = [-160,22];
kbtn['valasz_btn'] = [-704,22];
kbtn['ugras_btn'] = [-104,22];
kbtn['vissza_btn'] = [-1295,22];
kbtn['megnyit_btn'] = [-1375,22];
kbtn['tovabb_btn'] = [-1455,22];
kbtn['first_btn'] = [0,22];
kbtn['prev_btn'] = [-26,22];
kbtn['next_btn'] = [-52,22];
kbtn['last_btn'] = [-78,22];
kbtn['close_btn'] = [-1535,22];
kbtn['ajanlas_kuldes_btn'] = [-995,22];
kbtn['feliratkozom_btn'] = [-1561,22];
kbtn['leiratkozom_btn'] = [-1684,22];
kbtn['uj_eszleles_btn'] = [-1807,22];
kbtn['pp_refresh_btn'] = [-1973,22];
kbtn['pp_close_btn'] = [-1999,22];
kbtn['pp_open_btn'] = [-2025,22];
kbtn['reg_torles_btn'] = [-2051,22];
kbtn['jatszom_btn'] = [-2210,22];
kbtn['uj_hozzaszolas_btn'] = [-2284,22];
kbtn['uj_jelentes_btn'] = [-2406,22];

$.fn.init_kbtn = function(name) {
    return $(this).each(function() {
        $(this).data("bg_x_pos",kbtn[name][0]).data("bg_h",kbtn[name][1]).data("over",false).mouseover(function() {
            bg_x_pos = $(this).data("bg_x_pos");
            bg_h = $(this).data("bg_h");
            $(this).data("over",true).css("background-position",""+bg_x_pos+"px -"+bg_h+"px");
        }).mouseout(function() {
            bg_x_pos = $(this).data("bg_x_pos");
            $(this).data("over",false).css("background-position",""+bg_x_pos+"px 0px");
        }).mousedown(function() {
            bg_x_pos = $(this).data("bg_x_pos");
            bg_h = $(this).data("bg_h");
            $(this).css("background-position",""+bg_x_pos+"px -"+(bg_h*2)+"px");
        }).mouseup(function() {
            over = $(this).data("over");
            bg_x_pos = $(this).data("bg_x_pos");
            bg_h = $(this).data("bg_h");
            if (over) $(this).css("background-position",""+bg_x_pos+"px -"+bg_h+"px");
            else $(this).css("background-position",""+bg_x_pos+"px 0px");
        });
    });
}

var show_menu_to = null;
var hide_menu_to = null;
var search_to = null;
var last_search = "";
var selected_index = -1;
var search_req = null;
var foreca_search = 0;
var enter_pressed = false;

$(function() {

    $("#telepules_valaszto_prefill").click(function() {
        $("#telepules_valaszto_mezo input").focus();
    }).show();

    $("#telepules_valaszto_gomb").data("over",false).mouseover(function() {
        $(this).data("over",true).css("background-position","0px -38px");
        
        var f = $("#telepules_valaszto").data("focus");
        if (!f)
        {
            show_menu_to = window.setTimeout(function() {
                showDefaultMenu();
            },300);
        }
        
    }).mouseout(function() {
        $(this).data("over",false).css("background-position","0px 0px");
    }).mousedown(function() {
        $(this).css("background-position","0px -76px");
    }).mouseup(function() {
        over = $(this).data("over");
        if (over) $(this).css("background-position","0px -38px");
        else $(this).css("background-position","0px 0px");
    }).click(function() {

        if (selected_index == -1) selected_index = 0;
        if ($("#talalat_hu .talalat_go").size() > 0)
        {
            foreca_search = 0;
            window.clearTimeout(search_to);
            talalat = $("#talalat_hu .talalat_go").eq(selected_index).val();
            tt = talalat.split("_");
            if (tt[0] == "foreca")
            {
                talalat = tt[2];
                foreca_search = tt[1];
            }
            $("#telepules_valaszto_mezo input").val(talalat);
            t = talalat;
        }
        else if ($("#default_list_hu").is(":visible") && $("#telepules_valaszto_mezo input").val() == "")
        {
            window.clearTimeout(search_to);
            talalat = $("#default_list_hu .talalat_go").eq(selected_index).val();
            tt = talalat.split("_");
            if (tt[0] == "foreca")
            {
                talalat = tt[2];
                foreca_search = tt[1];
            }
            $("#telepules_valaszto_mezo input").val(talalat);
            t = talalat;
        }
        if (t)
        {
            $("#telepules_valaszto_mezo input").val(t).addClass("loading");
            window.setTimeout(function() {

                if (foreca_search)
                {
                    window.location = _url+"f/"+foreca_search+"/"+t;
                }
                else
                {
                    window.location = _url+"t/"+t;
                }

            },1000);
        }
    });

    function showDefaultMenu()
    {
        $("#telepules_valaszto_mezo").addClass("aktiv");
        $("#telepules_valaszto_menu").show();
        $("#default_list_hu").show();
        $("#talalat_hu").html("").hide();
    }

    function hideDefaultMenu()
    {
        $("#telepules_valaszto_mezo").removeClass("aktiv");
        $("#telepules_valaszto_menu").fadeOut(200,function() {$(this).hide()});
    }

    $.fn.selectRange = function(start, end) {
        return $(this).each(function() {
            if (this.createTextRange)
            {
                var selRange = this.createTextRange();
                selRange.collapse(true);
                selRange.moveStart("character", start);
                selRange.moveEnd("character", end);
                selRange.select();
            }
            else if (this.setSelectionRange )
            {
                this.setSelectionRange(start, end);
            }
            else if (this.selectionStart)
            {
                this.selectionStart = start;
                this.selectionEnd = end;
            }
        });
	};

    $("#telepules_valaszto_mezo input").keyup(function(evt) {
        var part;

        enter_pressed = false;
        if (evt.keyCode == 13)
        {
            enter_pressed = true;

            part = $(this).val().replace(/^\s*/,"").replace(/\s*$/,"");
            if (part == last_search) $("#telepules_valaszto_gomb").click();
            return false;
        }
        else if (evt.keyCode == 27)
        {
            window.clearTimeout(search_to);
            $(this).blur();
        }
        else if (evt.keyCode == 40) // Down
        {
            window.clearTimeout(search_to);
            if ($("#talalat_hu li").size() > 0)
            {
                selected_index++;
                if (selected_index == $("#talalat_hu a").size()) selected_index = 0;
                $("#talalat_hu li").removeClass("over").eq(selected_index).addClass("over");
            }
            else if ($("#default_list_hu").is(":visible"))
            {
                selected_index++;
                if (selected_index == $("#default_list_hu a").size()) selected_index = 0;
                $("#default_list_hu li").removeClass("over").eq(selected_index).addClass("over");
            }
            return false;
        }
        else if (evt.keyCode == 38) // Up
        {
            window.clearTimeout(search_to);
            if (selected_index == -1) selected_index = 0;
            if ($("#talalat_hu li").size() > 0)
            {
                selected_index--;
                if (selected_index == -1) selected_index = $("#talalat_hu a").size()-1;
                $("#talalat_hu li").removeClass("over").eq(selected_index).addClass("over");
            }
            else if ($("#default_list_hu").is(":visible"))
            {
                selected_index--;
                if (selected_index == -1) selected_index = $("#default_list_hu a").size()-1;
                $("#default_list_hu li").removeClass("over").eq(selected_index).addClass("over");
            }
            return false;
        }
        else if (evt.keyCode > 46 || evt.keyCode == 8 || evt.keyCode == 46 || evt.keyCode == 13)
        {
            window.clearTimeout(search_to);
            part = $(this).val().replace(/^\s*/,"").replace(/\s*$/,"");
            if (part.length > 2 && part != last_search)
            {
                if (search_req) {
                    search_req.abort();
                    search_req = null;
                }
                search_to = window.setTimeout(function() {
                    $("#telepules_valaszto input").addClass("loading");
                    search_req = terkepTelepules2(part,function(data) {
                        $("#telepules_valaszto input").removeClass("loading");
                        $("#default_list_hu").hide();
                        $("#talalat_hu").html(data).show();
                        selected_index = -1;
                        last_search = part;

                        if (enter_pressed)
                        {
                            $("#telepules_valaszto_gomb").click();
                        }
                    });
                },500);
            }
            else if (part.length == 0)
            {
                selected_index = -1;
                $("#default_list_hu").show();
                $("#talalat_hu").html("").hide();
            }
        }
        return true;
    }).mouseover(function() {
        var f = $("#telepules_valaszto").data("focus");
        if (!f)
        {
            show_menu_to = window.setTimeout(function() {
                showDefaultMenu();
            },500);
        }
    }).mouseout(function() {
        window.clearTimeout(show_menu_to);
    }).focus(function() {
        enter_pressed = false;
        last_search = "";

        $("#telepules_valaszto_prefill").hide();
        $("#telepules_valaszto").data("focus",true);
        $(this).data("oldval",$(this).val()).val("");
        showDefaultMenu();
    }).blur(function() {
        var that = this;
        enter_pressed = false;

        window.clearTimeout(search_to);
        window.setTimeout(function() {
            $("#telepules_valaszto").data("focus",false);
            if ($(that).val().replace(/\s/,"") == "") $(that).val($(that).data("oldval"));
            hideDefaultMenu();
        },300);
    }).blur();
    $("#telepules_valaszto").data("focus",false).mouseout(function() {
        if (!$(this).data("focus"))
        {
            hide_menu_to = window.setTimeout(function() {
                hideDefaultMenu();
            },100);
        }
    }).mouseover(function() {
        window.clearTimeout(hide_menu_to);
    });

    $("#telepules_valaszto_menu_bezar").mouseover(function() {
        $(this).css("text-decoration","underline");
    }).mouseout(function() {
        $(this).css("text-decoration","none");
    }).click(function() {
        if ($("#telepules_valaszto").data("focus"))
        {
            $("#telepules_valaszto_mezo input").blur();
        }
        else
        {
            hideDefaultMenu();
        }
    });
    
    initMenu();
    
    $(".diahird img").each(function() {
        var text = $(this).parent().attr("title");
        $(this).parent().attr("title","");
        if (text) $(this).addNyilbubi(text);
    });
    $(".egkep_c img").each(function() {
        var text = $(this).attr("alt");
        if (text) $(this).addNyilbubi(text);
    });
    $(".rovid_ikon img").each(function() {
        var text = $(this).attr("alt");
        if (text) $(this).addNyilbubi(text);
    });
    $(".rovid_szel img").each(function() {
        var text = $(this).attr("alt");
        if (text) $(this).addNyilbubi(text);
    });
    $(".foreca_ikon img").each(function() {
        var text = $(this).attr("alt");
        if (text) $(this).addNyilbubi(text);
    });
    $(".foreca_szel img").each(function() {
        var text = $(this).attr("alt");
        if (text) $(this).addNyilbubi(text);
    });
    $(".en_wind_mezo img").each(function() {
        var text = $(this).attr("alt");
        if (text) $(this).addNyilbubi(text);
    });
    $(".en_idokep_mezo img").each(function() {
        var text = $(this).attr("alt");
        if (text) $(this).addNyilbubi(text);
    });
    $(".header_egkep img").each(function() {
        var text = $(this).attr("alt");
        if (text) $(this).addNyilbubi(text);
    });
    $(".diaegkep").each(function() {
        var text = $(".egkep_nev",this).html();
        if (text) $(this).addNyilbubi(text);
    });
    $(".bar .datum").add(".diadatum").each(function() {
        var text = $(".datum_nevnap",this).html();
        if (text) $(this).addNyilbubi(text);
    });
    $("#holdfazis").each(function() {
        var text = $("div",this).html();
        if (text) $(this).addNyilbubi(text);
    });
    $(".bevalas_perc").each(function() {
        var text = $("#bevalas_tooltip").html();
        if (text) $(this).addNyilbubi(text);
    });

    $(".snow_flake").each(function() {
        $(this).addNyilbubi("1 cm hó<br />1 mm es&#337;nek<br />felel meg",-12);
    });


    $(".d3i").hide().rotateElements();

    if ($.fn.prettyPhoto)
    {
        // PrettyPhoto képek aktiválása
        $("a[rel^='prettyPhoto']").prettyPhoto({'allow_resize': true, 'overlay_gallery': false});
    }
    
    for (x in kbtn) $("."+x).init_kbtn(x);
    
    $("#forum_kereses_gomb").click(function() {$("#forum_kereses_form").submit();});
    $("#forum_kereses_form").submit(function() {
        if ($("#forum_kereses_mezo input").val().replace(/\s/,"") != "")
        {
            $("#forum_kereses_mezo input").addClass("loading");
            return true;
        }
        return false;
    });
    
    
    $("#forum_ugras_btn").click(function() {
        if ($("#forum_aktlap").val().replace(/\s/,"") != "") $("#forum_ugras_form").submit();
    });
    $("#blog_ugras_btn").click(function() {$("#blog_ugras_form").submit();});
    
    $("#forum_login_submit_btn").click(function() {$('#forum_login_form').submit();});
    $('#forum_login_form').submit(function() {
        $("#forum_error_frame").hide();
        login_ok = true;
        if ($("[name=email]",this).val().replace(/\s/,"") == "") login_ok = false;
        if ($("[name=jelszo]",this).val().replace(/\s/,"") == "") login_ok = false;
        if (login_ok) return true;
        else
        {
            $("#forum_error_frame").html("A belépéshez mind a két mez&#337;t ki kell töltened!").show();
            return false;
        }
    });

    $("#forum_emlekezteto_submit_btn").click(function() {$('#forum_emlekezteto_form').submit();});
    $('#forum_emlekezteto_form').submit(function() {
        $("#forum_error_frame").hide();
        emlek_ok = false;
        if ($("[name=email]",this).val().replace(/\s/,"") != "") emlek_ok = true;
        if ($("[name=lognick]",this).val().replace(/\s/,"") != "") emlek_ok = true;
        if (emlek_ok) return true;
        else
        {
            $("#forum_error_frame").html("Az egyik mez&#337;t legalább ki kell töltened!<br />Enélkül nem tudjuk elküldeni a jelszavadat.").show();
            return false;
        }
    });

    $("#forum_tragar_submit_btn").click(function() {$('#forum_tragar_form').submit();});

    $("#forum_hsz_submit_btn").click(function() {$('#forum_hsz_form').submit();});

    if ($("#redirect_link").size() > 0) {redirectLink();}

    var iA = "fe", iD = "ah", iT = "id", iU = "_"; $("["+iT+"^="+iA+"b"+iD+"o"+iU+"]").each(function() { var iF = this, iV = $(iF).attr(iT).split(iU)[1];
        var eGs = $(".egg").size()+1; if (eGs>1) { var eGi = Math.floor(Math.random()*eGs); $("#degu").each(function() { $("[name=b]",this).val(iV); }); $(".egg").eq(eGi).show().click(function() { $("#degu").submit(); }); }
    });

});

redirectLink = function() {
    var counter = parseInt($("#redirect_counter").html());
    if (counter == 0)
    {
        $("#redirect_counter").html("átirányítás...");
        window.location = $("#redirect_link").attr("href");
    }
    else
    {
        window.setTimeout(function() {
            $("#redirect_counter").html(counter-1);
            redirectLink();
        },1000);
    }
};

/* *** WHALE *** */
$.fn.showWhale = function() {
    return $(this).show().adjustWhale();
}

$.fn.showWhalePopup = function(data) {
    return $(this).show().html(data).adjustWhalePopup();
}

$.fn.adjustWhale = function() {
    return $(this).css({
       'top': $(window).scrollTop()-100,
       'left': 0,
       'width': $(window).width(),
       'height': $(window).height()+200,
       'opacity': 0.5
    });
};

$.fn.adjustWhalePopup = function() {
    var wpp_w = $(this).width();
    var wpp_h = $(this).height();
    return $(this).css({
       'top': $(window).scrollTop(),
       'left': 0,
       'margin-top': Math.round(($(window).height()-wpp_h)/2),
       'margin-left': Math.round(($(window).width()-wpp_w)/2)
    });
};

openHtmlWhale = function(data,callback) {
    $("#whale").showWhale();
    $("#whale_popup").showWhalePopup(data).each(function() {
        $(".close_btn",this).click(function() {
            $("#whale").hide();
            $("#whale_popup").hide();
        });
    });
    if (callback) callback();
}

openAjaxWhale = function(url,data,callback) {
    $("#whale").showWhale();
    $("#whale_popup").hide();
    $.ajax({
        'type': "POST",
        'url': url,
        'data': data,
        'error': function() {$("#whale").hide();},
        'success': function(data) {
            $("#whale_popup").showWhalePopup(data).each(function() {
                $(".close_btn",this).init_kbtn("close_btn").click(function() {
                    $("#whale").hide();
                    $("#whale_popup").hide();
                });
            });
            if (callback) callback();
        }
    });
}

$(function() {
    $(window).scroll(function() {
        $("#whale").adjustWhale();
        $("#whale_popup").adjustWhalePopup();
    }).resize(function() {
        $("#whale").adjustWhale();
        $("#whale_popup").adjustWhalePopup();
    });
});
