

initMenu = function() {
if ($("#fomenu").size() > 0)
    {
        var menu_on = $("#fomenu .on").attr("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 a").add(".almenu2").not("#"+menu_on+"_menu").each(function() {
            var menuid = $(this).attr("id").split("_")[0];
            $(this).data("menuid",menuid);
            menu_to[menuid] = null;
        }).mouseover(function() {
            var menuid = $(this).data("menuid");
            menuOver(menuid);
        }).mouseout(function() {
            var menuid = $(this).data("menuid");
            menuOut(menuid);
        });
    }
};

var diab_to = null;
$.fn.addNyilbubi = function(text,mleft) {
    return $(this).each(function() {
        $(this).attr("title","").mouseover(function() {
            window.clearTimeout(diab_to);
            $(".nyilbubi").show().insertBefore(this);
            if (!mleft) mleft = 0;
            $(".nyilbubi_top").html(text);
            h = $(".nyilbubi").height();
            w = 116;
            $(".nyilbubi").css({
                'z-index': 1000,
                'margin-top': -h+3,
                'margin-left': Math.round((45-w)/2) + mleft
            });
        }).mouseout(function() {
            diab_to = window.setTimeout(function() {
                $(".nyilbubi").hide();
            },50);
        });
    });
}


$.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['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];

$.fn.init_kbtn = function(name) {
    return $(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");
    });
}

$(function() {

    $("#koponyeg_help_bezar").click(function() {
        $("#koponyeg_help").hide();
    });

    $("#telepules_valaszto_gomb").data("over",false).mouseover(function() {
        $(this).data("over",true).css("background-position","0px -38px");
    }).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).html();
            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).html();
            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+"index.php?q="+t+"&foreca="+foreca_search;
                }
                else
                {
                    window.location = _url+"index.php?q="+t;
                }
                /*
                url = _url+"jelenido.php?ajax=place_exists&t="+t;
                $.get(url,function(data) {
                    if (data == "no")
                    {
                        $("#telepules_valaszto_mezo input").removeClass("loading");
                        $("#telepules_valaszto_mezo input").blur();
                        alert("Sajnáljuk, de a nincs előrejelzés a keresett településre!");
                    }
                    else
                    {
                        window.location = _url+"index.php?"+data;
                    }
                });*/

            },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;
            }
        });
	};

    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;
    $("#telepules_valaszto_mezo input").keyup(function(evt) {
        if (evt.keyCode == 13)
        {
            /*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).html();
                tt = talalat.split("_");
                if (tt[0] == "foreca")
                {
                    talalat = tt[2];
                    foreca_search = tt[1];
                }
                $("#telepules_valaszto_mezo input").val(talalat);
                $("#telepules_valaszto_gomb").click();
            }
            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).html();
                $("#telepules_valaszto_mezo input").val(talalat);
                $("#telepules_valaszto_gomb").click();
            } */

            $("#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);
            var 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() {
                    url = _url+"jelenido.php?ajax=lista&q="+part+"&type=2";
                    $("#telepules_valaszto input").addClass("loading");
                    search_req = $.get(url,function(data) {
                        $("#telepules_valaszto input").removeClass("loading");
                        $("#default_list_hu").hide();
                        $("#talalat_hu").html(data).show();
                        selected_index = -1;
                        /*if ($("#talalat_hu .talalat_start").size() > 0 && $("#talalat_hu .talalat_end").size() > 0)
                        {
                            talalat_start = $("#talalat_hu .talalat_start").html();
                            talalat_end = $("#talalat_hu .talalat_end").html();
                            sel_text = talalat_start+talalat_end;
                            sel_start = talalat_start.length;
                            sel_end = sel_text.length;
                            $("#telepules_valaszto input").val(sel_text).selectRange(sel_start,sel_end);
                            
                            last_search = talalat_start;
                        }*/
                    });
                },500);
            }
            else if (part.length == 0)
            {
                selected_index = -1;
                $("#default_list_hu").show();
                $("#talalat_hu").html("").hide();
            }
        }
        return true;
    }).mouseover(function() {
        show_menu_to = window.setTimeout(function() {
            showDefaultMenu();
        },500);
    }).mouseout(function() {
        window.clearTimeout(show_menu_to);
    }).focus(function() {
        $("#telepules_valaszto").data("focus",true);
        $(this).data("oldval",$(this).val()).val("");
        showDefaultMenu();
    }).blur(function() {
        var that = this;
        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);
    });

    $("#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();

    if ($.fn.tooltip)
    {
        $("#koptip_hold").tooltip({
            delay: 0,
            track: true,
            showURL: false,
            bodyHandler: function() {
                return $("#koptipd_hold").html();
            }
        });

        $("#bevalasi_esely").tooltip({
            delay: 0,
            track: true,
            showURL: false,
            bodyHandler: function() {
                return $("#bevalas_tooltip").html();
            }
        });
    }
    
    $(".hirdetes_c a").each(function() {
        var text = $(this).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);
    });
    $(".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,6);
    });
    $(".bar .datum").each(function() {
        var text = $(".datum_nevnap",this).html();
        if (text) $(this).addNyilbubi(text);
    });


    $(".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ő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ő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();}

});

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);
    }
};


/* *** PP *** */

$.fn.init_pp_form = function() {
    return $(this).each(function() {
        var that = this;

        $("#pp_close_btn",this).init_kbtn("tovabb_btn").click(function() {
            $("#whale").hide();
            $("#whale_popup").hide();
        });

        $("#pp_submit_btn",this).init_kbtn("kuldes_btn").click(function() {
            kuldes_ok = true;
            if ($("[name=pp_nev]",that).val().replace(/\s/,"") == "") kuldes_ok = false;
            if ($("[name=pp_telepules]",that).val().replace(/\s/,"") == "") kuldes_ok = false;
            if ($("#fenykep_bekuld").is(":visible") && $("#pp_photo_name_field").val() == "")
            {
                kuldes_ok = false;
                alert("Még nem töltötte fel a fényképet!");
            }
            // $("#pp_form").submit(); return true;
            if (kuldes_ok)
            {
                qs = $("#pp_form").serialize();
                url = $("#pp_form").attr("action");
                openAjaxWhale(url,qs,function() {
                    $("#pp_form").init_pp_form();
                });
            }
            return false;
        });

        $("[name=pp_telepules]",this).focus(function() {
            $(this).val("");
            $("#pp_telepules_ok").hide();
        }).autocomplete("jelenido.php", {
            minChars: 2,
            extraParams: {'ajax': "lista"},
            matchSubset: 0,
            matchContains: 1,
            selectFirst: 1,
            selectOnly: 1,
            onItemSelect: function(li) {
                varos = $(li).html();
                $("#pp_telepules_ok").show();
            }
        });

        $("[name=pp_nev]",this).blur(function() {
            if ($(this).val().replace(/\s/,"") != "") $("#pp_nev_ok").show();
            else $("#pp_nev_ok").hide();
        });

        $("[name=pp_video_link]",this).blur(function() {
            if ($(this).val().replace(/\s/,"") != "") $("#pp_video_link_ok").show();
            else $("#pp_video_link_ok").hide();
        });

        $("[name=pp_video_title]",this).blur(function() {
            if ($(this).val().replace(/\s/,"") != "") $("#pp_video_title_ok").show();
            else $("#pp_video_title_ok").hide();
        });

        $("[name=pp_photo_title]",this).blur(function() {
            if ($(this).val().replace(/\s/,"") != "") $("#pp_photo_title_ok").show();
            else $("#pp_photo_title_ok").hide();
        });

        $("[name=pp_text]",this).keyup(function() {
            remain = 140-$(this).val().length;
            if (remain < 10) $("#pp_text_counter").css({'color': "#ff0000"});
            if (remain < 0)
            {
                visszavagva = $(this).val().substr(0, 140);
                $(this).val(visszavagva);
                remain = 0;
            }
            $("#pp_text_counter").html(remain);
        });

        $("#pp_tipus_valaszto",this).click(function() {
            showid = $("input:checked",this).val();
            $("[id$=_bekuld]").hide();
            $("#"+showid+"_bekuld").show();
            $("#pp_submit_btn").show();
            $("#pp_tipus_info").hide();
        });
    });
}

var rt_sum = 0;
var rt_callback = null;
var rt_to = null;
$.fn.startRefreshTimer = function(times,delay,callback) {
    rt_sum = times*delay;
    rt_callback = callback;
    $(this).refreshTimer(times,delay);
}
$.fn.refreshTimer = function(times,delay) {
    var that = this;
    prec = Math.round((times * delay) / rt_sum * 10000) / 100;
    $(this).css("width",prec+"%");
    rt_to = window.setTimeout(function() {
        next_times = times-1;
        if (next_times == 0) rt_callback();
        else $(that).refreshTimer(next_times,delay);
    },delay);
}

$.fn.refreshPercrolPercre = function() {
    window.clearTimeout(rt_to);
    window.clearTimeout(pp_photo_to);
    pp_refresh_in_progress = true;

    var that = this;
    url = _url+"pp.php?action=pp_refresh&id="+pp_id;
    $.get(url,function(data) {

        if (data.match(/HIBA: Inak/i))
        {
            $("#pp_main_container").hide();
            pp_photo_counter = 0;
            pp_refresh_in_progress = false;
        }
        else
        {

            $("#pp_text_scroll_frame .pp_new_scroll").remove();
            $("#pp_photo_frame .pp_new_photo").remove();
            $("#pp_closed_frame .pp_new_closed").remove();

            $(that).html(data);
            $("#pp_text_scroll_frame").append($(".pp_new_scroll",that));
            $("#pp_photo_frame").append($(".pp_new_photo",that));
            $("#pp_closed_frame").append($(".pp_new_closed",that));

            $("#pp_refresh_bar").startRefreshTimer(300,100,function() {
                $("#pp_refresh_frame").refreshPercrolPercre();
            });
            pp_photo_counter = 0;
            $("[id^=pp_photo_]",$("#pp_photo_frame")).rotatePhotos();
            $("a[rel^='prettyPhoto']",$("#pp_photo_frame")).prettyPhoto({ 'allow_resize': true, 'overlay_gallery': false });

            pp_refresh_in_progress = false;
        }
    });
}

$.fn.rotatePhotos = function() {
    var that = this;
    $(this).hide().eq(pp_photo_counter).show();
    pp_photo_counter++;
    if (pp_photo_counter >= $(this).size()) pp_photo_counter = 0;
    pp_photo_to = window.setTimeout(function() {
        $(that).rotatePhotos();
    },4000);
}

var pp_refresh_in_progress = false;
var pp_photo_counter = 0;
var pp_photo_to = null;
var pp_id = 0;
$(function() {
    $("#new_pp_post").click(function() {
        openAjaxWhale(_url+"pp.php?action=new_post_frame",null,function() {
            $("#pp_form").init_pp_form();
        });
    });

    $("#pp_open").click(function() {
        $("#pp_closed_part").hide();
        $("#pp_open_part").show();
        $.get(_url+"pp.php?action=set_user_open");
    });

    $("#pp_close").click(function() {
        $("#pp_closed_part").show();
        $("#pp_open_part").hide();
        $.get(_url+"pp.php?action=set_user_closed");
    });

    $("#pp_refresh").click(function() {
        if (!pp_refresh_in_progress) $("#pp_refresh_frame").refreshPercrolPercre();
    });

    if ($("#pp_id").size() > 0)
    {
        pp_id = $("#pp_id").val();
        pp_photo_counter = 0;
        $("#pp_refresh_bar").startRefreshTimer(300,100,function() {
            $("#pp_refresh_frame").refreshPercrolPercre();
        });
        $("[id^=pp_photo_]",$("#pp_photo_frame")).rotatePhotos();
    }

    $("#pp_ugras").click(function() {
        window.location = _url+"pp.php?id="+$("#pp_older").val();
    });

    if ($("#pp_refresh_all").size() > 0)
    {
        $("#pp_refresh_all").click(function() {window.location.reload();});
        $("#pp_refresh_bar").startRefreshTimer(500,100,function() {
            window.location.reload();
        });
    }
});


/* *** 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();
    });
});