
function tagol(nStr){

    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? ',' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1))
        x1 = x1.replace(rgx, '$1' + '.' + '$2');
    return x1 + x2;
}

$(function(){

    $(".colorbox").live("click", function(){
        $(this).colorbox({
            maxHeight:"98%",
            open:true
        });
        return false;
    });
    
    $(".eger").mouseover(function(){
       $(this).css("background-color","rgb(250,235,245)"); 
    });
    
    $(".eger").mouseout(function(){
       $(this).css("background-color","white"); 
    });    
    
    $(function(){
        $("#keresoszo").autocomplete("search.php", {
            width: 260,
            selectFirst: false
        });         
    });    

    $(".online button").click(function(){
        $(".kell").css("border","1px solid silver");
        var ok = true;
        $(".online .kell").each(function(){
            if($(this).val()==''){
                $(this).css("border","1px dashed red");
                ok=false;
            }
        });
        return ok;
    });

    $(".menu a").each(function(){
        var s = document.location.toString();
        var hr = $(this).attr("href");
        if(hr!="/" && s.indexOf(hr) >=0 ){
            if(!$(this).hasClass('marka')){
                $(this).addClass("aktiv");
            }
        }
        if(s.indexOf("&tipus")>=0){
            $("#akcio").removeClass("aktiv");
        }
    });

    $(".tocart").bind("click",function(){
        var ids = new String($(this).attr("id"));
        var idk = ids.split("_");
        var id = idk[1];
        var ar = idk[2];
        var db = $("#db_"+id).val();

        $.get("index.php",
        "mod=cart&add="+id+"&db="+db+"&ar="+ar,
        function(html){
            $(html).appendTo("body").dialog({
                title:"Kosár",
                modal:true,
                buttons:{
                    'Tovább a kosárhoz':function(){
                        document.location="/index.php?mod=cart";
                    },
                    'Vissza az áruházba':function(){
                        $(this).dialog('close');
                    }
                }
            });
        },"html");
        return false;
    });

    $(".del").bind("click",function(){
        // var ids = new String($(this).parents("tr").find(".egyedi").val());
        // var idk = ids.split("_");
        // var id = idk[1];
        var ids = new String($(this).parents("tr").find(".egyedi").val());
        var idk = ids.split("_");
        var id = idk[1];
        var price = $(this).parents("tr").find(".price").val();
        var db =$(this).val();
        var fiz =$(this).parents("tr").find(".fiz");       

        $("<div style='text-align:left;line-height:32px;'>Biztosan törli a kosárból?</div>").appendTo("body").dialog({
            title:"Termék törlése - Megerősítés",
            modal:true,
            buttons:{
                'Igen, törlöm':function(){
                    $.get("index.php","mod=cart&del="+id+"&meret="+idk[2]+"&szin="+idk[3],
                    function(json){
                        $("#t_"+id).fadeOut("slow");
                        $("#mind").html("<b>"+json.mind+" Ft</b>");
                    },"json");
                    //document.location="/index.php?mod=cart";
                    $(this).dialog('close');
                    return false;
                },
                'Mégsem':function(){
                    $(this).dialog('close');
                }
            }
        });

        return false;
    });  

    $(".cart .db").bind("change",function(){
        var ids = new String($(this).parents("tr").find(".egyedi").val());
        var idk = ids.split("_");
        var id = idk[1];
        var price = $(this).parents("tr").find(".price").val();
        var db =$(this).val();
        var fiz =$(this).parents("tr").find(".fiz");

        if(db==0){
            $.get("index.php","mod=cart&del="+id+"&meret="+idk[2]+"&szin="+idk[3],
            function(json){
                $("#t_"+id).fadeOut("slow");
                $("#mind").html("<b>"+json.mind+" Ft</b>");
            },"json");
            return false;
        }
        $.get("index.php","mod=cart&set="+id+"&db="+db+"&meret="+idk[2]+"&szin="+idk[3],function(json){
            $("#ar_"+id).html(json.ar+" Ft");
            $(fiz).html(tagol((price*db))+" Ft");
            $("#mind").html("<b>"+json.mind+" Ft</b>");
            setTimeout(function(){$("#st_"+id+' div.fin').fadeOut("slow",function(){$("#st_"+id).hide();} );},5000);

        },"json");
        return false;
    });

    $("#send").click(function(){
        var ok=true;
        $(".form input").each(function(){
            $(this).removeClass("error");
            $(this).removeClass("rendben");
            if($(this).val()==""){
                $(this).addClass("error");
                ok=false;
            }else{
                $(this).addClass("rendben");
            }
        });
        if(($("#sok").attr("checked")) && ($("#snev").val().length==0) && ($("#scim").val().length==0) ){
            ok=true;
        }

        if(ok){
            if(($("#rf").attr("checked"))){
                ok=true;
            }else{
                ok=false;
            }
        }

        if(!ok){
            $("<div><img src='images/warning.png' style='margin-right:15px;float:left;' /><p style='text-align:center; color:#A80000; font-weight:bold;'>Nem adott meg minden adatot! <br /> Kérem ellenőrízze!</p></div>").appendTo("body").dialog({
                title:"<img src='images/warning.png' style='margin-right:5px;margin-left:-10px;float:left;' width='16' /> Hiányzó adatok",
                modal:true,
                resizable:false,
                buttons:{
                    'Bezárás':function(){
                        $(this).dialog("close");
                    }
                }
            });


            return false; }
        $.post("index.php?mod=cart",$("#cart").serialize(),function(html){
            $(html).appendTo("body").dialog({
                title:"Rendelés elküldése",
                modal:true,
                resizable:false,
                buttons:{
                    'Vissza a főoldalra':function(){
                        document.location="/";
                        //window.history.go(-1);
                    }
                }
            });
        },"html");
        return false;
    });
    
    $(".leiras").bind("click",function(){
        $(this).colorbox({
            maxHeight:"98%",
            open:true
        });
        return false;
    });
    
    $(".keres").click(function(){
        document.forms["keresoform"].submit();
        return false;
    });

});
