$(function() {
    var menuPosLeft = 0;
    $("ul#menu li").each(function(i, item) {  
        if($(item).hasClass("active")) {
            $(item).css({
                left    : (menuPosLeft - 10) + "px",
                zIndex  : 10
            }); 
            menuPosLeft -= 20;
        } else {
            $(item).css({
                left    : menuPosLeft + "px",
                zIndex  : (5 - i)
            }); 
            menuPosLeft -= 10;
        }
    }).click(function() {
        var idx = $("ul#menu li").index($(this));
        switch(idx) {
            case 0: window.location.href = _HTTP_ROOT + "kezdolap";         break;
            case 1: window.location.href = _HTTP_ROOT + "alkoss";           break;
            case 2: window.location.href = _HTTP_ROOT + "kollekciok";       break;
            case 3: window.location.href = _HTTP_ROOT + "termekek";         break;
            case 4: window.location.href = _HTTP_ROOT + "nyeremenyjatek";   break;
        }
    });    
    
    $("#ratebox img").mouseover(function() {
        var starNum = $("#ratebox img").index(this) + 1;
        $("#ratebox img").each(function(){  $(this).attr("src", _HTTP_ROOT + "images/star.jpg") });
        $("#ratebox img").slice(0, starNum).attr("src", _HTTP_ROOT + "images/star_active.jpg");                                                    
    }).click(function(e) {
        if(e.target.id == "first") $(this).attr("src", _HTTP_ROOT + "images/star.jpg");
        $("input[name=rate_value]").val($("#ratebox img[src=" + _HTTP_ROOT + "images/star_active.jpg]").length);
        $.post(_HTTP_ROOT + "kollekcio/" + $(this).attr("name") + "/ertekel", { rate : $("input[name=rate_value]").val() }, function(r) {
            var useV = eval('(' + r + ')');
            $("#ratebox span").text(useV.message);
            $("input[name=rate_value]").val(useV.rate);
            selImg = $("input[name=rate_value]").val();
            $("#ratebox img").each(function(){  $(this).attr("src", _HTTP_ROOT + "images/star.jpg") });
            $("#ratebox img").slice(0, selImg).attr("src", _HTTP_ROOT + "images/star_active.jpg");
        });
    }).mouseout(function(){
        selImg = $("input[name=rate_value]").val();
        $("#ratebox img").each(function(){  $(this).attr("src", _HTTP_ROOT + "images/star.jpg") });
        $("#ratebox img").slice(0, selImg).attr("src", _HTTP_ROOT + "images/star_active.jpg");
    });
    
    $(".fg-tooltip").hide();
    $(".middle-bottom form input.input.error, .middle-bottom form input[type=checkbox].error, ").hover(
        function(){ $(this).prev(":hidden").fadeIn(); },
        function(){ $(this).prev(":visible").fadeOut(); }
    );
    
    $(".logmeinform input:not(input[type=submit])").click(function() {
        if($(this).attr("alt") == $(this).val()) {
            $(this).val("");
        }
    }).blur(function() {
        if($(this).val() == "") {
            $(this).val($(this).attr("alt"));
        }
    });

});

$.fn.clearForm = function() {
    return this.each(function() {
        $(':input', this).each(function() {
            var type = this.type, tag = this.tagName.toLowerCase();
            if(type == 'text' || type == 'password' || tag == 'textarea') {
                this.value = '';
            } else if(type == 'checkbox' || type == 'radio') {
                this.checked = false;
            } else if(tag == 'select') {
                this.selectedIndex = -1;
            }
        });
    });
};

function sendTo(collectionId) {
    $("#sendto").dialog({
        modal: true,
        width: 390,
        resizable: false,
        title: "Kollekció küldése e-mailben",
        open: function() { 
            $("#sendto form").clearForm();    
        },
        buttons: {
            "Küldés": function() {
                $(".ui-dialog .ui-dialog-buttonpane").prepend("<span id=\"sendmsg\">Küldés...</span>");
                $(".ui-dialog .ui-dialog-buttonpane button").css("visibility", "hidden");
                $.post(_HTTP_ROOT + "kollekcio/" + collectionId + "/kuldes/", {
                    data : $("#sendto form").serializeArray()
                }, function(r) {
                    $("#sendmsg").remove();
                    $(".ui-dialog .ui-dialog-buttonpane button").css("visibility", "visible");
                    if(r.length == 0) {
                        $("#progressbar").progressbar("destroy");
                        alert("Kollekció sikeresen elküldve a megadott e-mail címre!");
                        $("#sendto").dialog("close");     
                    } else {
                        $("#progressbar").progressbar("destroy");
                        alert(r);    
                    }
                });
            }, 
            "Mégsem": function() { 
                $(this).dialog("close"); 
            } 
        }
    });   
}

function printShopCart(collectionId) {
    window.open(_HTTP_ROOT + "bevasarlolista/" + collectionId, "", "scrollbars=1,width=400,height=800");    
}

function openPrivacyPolicy() {
    $("#privacypolicy").dialog({
        modal: true,
        width: 800,
        title: "Adatkezelési szabályzat"
    });
}
