var popupok = new Array();
$(function() {

    $("[href^=mailto]").each(function() {
        email = $(this).attr("href");
        html = $(this).html();
        email2 = email.replace(/\[kukac\]/, "@").replace(/\[pont\]/, ".");
        $(this).attr("href",email2);
        if (email == "mailto:"+html)
        {
            email3 = email2.replace(/mailto:/,"");
            $(this).html(email3);
        }
    });

    $("[id^=varosdot]").each(function() {
        $(this).tooltip({
            delay: 0,
            track: true,
            showURL: false, 
            bodyHandler: function() { 
                id = $(this).attr("id");
                
                if ($(this).hasClass("dot_sel"))
                {
                    varosok = $(this).attr("nev");
                    text = "<div class='tooltip_title'>"+varosok+"</div>";
                    text += "<ul class='tooltip_list'>";
                    text += "<li style='padding: 1px; font-weight: normal;'>Részletek a térkép alatt!</li>";
                    text +="</ul>";
                }
                else if (popupok[id])
                {
                    text = popupok[id];
                }
                else
                {
                    varosok = $(this).attr("nev");
                    text = "<div class='tooltip_title'>"+varosok+"</div>";
                    fszk = $(this).attr("rel").split(",");
                    likk = new Array();
                    likks = new Array('fesz_kiem','fesz_van','fesz_lesz','fesz_volt');
                    
                    text += "<ul class='tooltip_list'>";
                    for (x in fszk)
                    {
                        // Meghatározzuk az osztály típusát a fesztiválnak
                        for (y in likks)
                        {
                            if ($("#fesz_"+fszk[x]).hasClass(likks[y])) cls = likks[y];
                        }
                        likk[cls] = likk[cls] ? likk[cls] + 1 : 1;
                    }
                    for (x in likks)
                    {
                        if (likk[likks[x]])
                        {
                            if (likks[x] == 'fesz_kiem') ftex = " kiemelt fesztivál";
                            if (likks[x] == 'fesz_van')  ftex = " fesztivál van";
                            if (likks[x] == 'fesz_lesz') ftex = " fesztivál lesz";
                            if (likks[x] == 'fesz_volt') ftex = " fesztivál volt";
                            text += "<li class='"+likks[x]+"'>"+likk[likks[x]]+ftex+"</li>";
                        }
                    }
                    text += "<li style='padding: 1px; font-weight: normal;'>Kattints a részletekhez!</li>";
                    text +="</ul>";
                    
                    popupok[id] = text;
                }
                return text;                 
            }
        });
    }).click(function() {
        $("#fesztival_lista .forumnotice").hide();
        
        $("[id^=varosdot]").removeClass("dot_sel");
        $(this).addClass("dot_sel");
        
        var varos = $(this).attr("nev");
        $("#feszvaros").html(varos).show();
   
        var fszk = $(this).attr("rel").split(",");
        $("[id^=fesz_]").hide().removeClass("odd").removeClass("even");
        for (x in fszk)
        {
            $("#fesz_"+fszk[x]).show();
        }
        $("[id^=fesz_]:visible").each(function(idx) {
            if (idx%2) $(this).addClass("odd");
            else $(this).addClass("even");

            $(".av",this).each(function() {
                var av = $(this).attr("alt");
                if (av.match(/^http/i)) {
                    $(this).attr("alt","").load(function() {
                        $(this).show();
                    }).attr("src",av);
                }
            });
            $(".ct",this).click(function(e) {
                var ct = $(this).attr("alt");
                if (ct.match(/^http/i)) {
                    window.open(ct,"feszt_ad");
                }
            });
        });
        
        $("[id^=feszbody_]").each(function() {
            var id = $(this).attr("id").split("_")[1];
            var db = $("[id^=fesz_]:visible",this).size();
            $("#feszhead_"+id+" .fesz_info").html(db+" fesztivál");
            if (db == 0)
            {
                $("#feszhead_"+id).hide();
            }
            else
            {
                $("#feszhead_"+id).show();
            }
        });
        $(this).mouseover();
    
    }).css("cursor","pointer");

    if ($("#feszvaros").size() == 0)
    {
        $("[id^=fesz_]:visible").each(function(idx) {
            $(".av",this).each(function() {
                var av = $(this).attr("alt");
                if (av.match(/^http/i)) {
                    $(this).attr("alt","").load(function() {
                        $(this).show();
                    }).attr("src",av);
                }
            });
            $(".ct",this).click(function(e) {
                var ct = $(this).attr("alt");
                if (ct.match(/^http/i)) {
                    window.open(ct,"feszt_ad");
                }
            });
        });
    }


    $("[id^=fesz_]").mouseover(function() {
        $(this).addClass("fesz_selected");
    }).mouseout(function() {
        $(this).removeClass("fesz_selected");
    });

    $("#fesz_hsz_submit_btn").click(function() {
        $("#fesz_hsz_form").submit();
    });
    $("#send_ajanlo").click(function() {
        $("#fform_ajanlo").submit();
    });
    
    $("a.star_off").add("a.star_on").click(function() {
        url = $(this).attr("href")+"&ajax=1";
        var that = this;
        $(that).removeClass("star_on").removeClass("star_off").addClass("star_loading");
        $.get(url,function(data) {
            if (data == "off")
            {
                $(that).removeClass("star_loading").removeClass("star_on").addClass("star_off");
            }
            else if (data == "on")
            {
                $(that).removeClass("star_loading").removeClass("star_off").addClass("star_on");
            }
        });
        $(this).blur();
        return false;
    });


    function getDatum(type)
    {
        nap = parseInt($("[name=ajanlo_"+type+"_nap]").val());
        ev = parseInt($("[name=ajanlo_"+type+"_ev]").val());
        honap = parseInt($("[name=ajanlo_"+type+"_honap]").val().replace(/^0/,""))-1;
        return new Date(ev, honap, nap);
    }

    function testDatum(type)
    {
        nap = $("[name=ajanlo_"+type+"_nap]").val().replace(/[^0-9]/,"").substring(0,2);
        $("[name=ajanlo_"+type+"_nap]").val(nap);
        ev = parseInt($("[name=ajanlo_"+type+"_ev]").val());
        honap = parseInt($("[name=ajanlo_"+type+"_honap]").val().replace(/^0/,""))-1;
        date = new Date(ev, honap, nap);

        datum_ok = true;
        if (date.getDate() != nap) datum_ok = false;
        else if (date.getMonth() != honap) datum_ok = false;
        else if (date.getFullYear() != ev) datum_ok = false;
        if (!datum_ok)
        {
            $("#ajanlo_"+type+"_test").html("<span style='color: #d90000;'>Hibás dátum!</span>");
            type = "hiba";
        }
        else
        {
            hetnapok = new Array("vasárnap","hétf&#337;","kedd","szerda","csütörtök","péntek","szombat");
            $("#ajanlo_"+type+"_test").html(hetnapok[date.getDay()]);
        }

        switch (type)
        {
            case "start":dt = getDatum("end").getTime() - date.getTime();break;
            case "end":dt = date.getTime() - getDatum("start").getTime();break;
            case "hiba":dt = -1;break;
        }

        if (dt < 0)
        {
            $("#intervallum_test").html("<span style='color: #d90000;'>Hibás intervallum!</span>");
        }
        else
        {
            napok = Math.round(dt / (1000 * 60 * 60 * 24)) + 1;
            $("#intervallum_test").html("A fesztivál "+napok+" napig tart.");
        }
    }

    $("[name=ajanlo_end_nap]").add("[name=ajanlo_start_nap]").focus(function() {
        this.select();
    }).keyup(function() {
        type = $(this).attr("name").split("_")[1];
        testDatum(type);
    }).keyup();
    $("select[name^=ajanlo_end]").add("select[name^=ajanlo_start]").change(function() {
        type = $(this).attr("name").split("_")[1];
        testDatum(type);
    });

});
