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(){
    $(".hidden").hide();
    $(".kata").click(function()
    {
    //    $(this).next("ul").slideToggle();
        return true;
    }
    );

  $("a.colorbox").colorbox({transition:"fade",maxHeight:"98%",
    current:"{current}. kép / {total}",
    previous:"&laquo;&nbsp;előző",
    next:"következő &raquo;",
    close:"Bezárás",
    speed:350,
    slideshowStop:"Diavetítés leállítása",
    slideshowStart:"Diavetítés",
    slideshow:true
  });

 $(".kats a").each(function()
 {
    var s = document.location.toString();
    var hr = $(this).attr("href");
    if(s.indexOf(hr) >=0  )
    {
        $(this).addClass("nyil");
    }
 });
 
 $(".tocart a").click(function(){
  $(".fin").remove();
  var ids = new String($(this).attr("id"));
  var idk = ids.split("_");
  var id = idk[1];
  $.get("index.php","mod=cart&add="+id+"&db=1",
  function(html)
  {
    $(html).appendTo("body").dialog(
    {
        title:"Kosár",
        modal:true,
        buttons:{
            'Tovább a kosárhoz':function(){
                document.location="/index.php?mod=cart";
            },
            'Bezárás':function(){
                $(this).dialog('close');
            }
        }
    });
//    $(".menu").append(html);
//    setTimeout(function(){$('div.fin').fadeOut("slow");},5000);
  },"html");
  return false;    
 });
/*
 $(".del").bind("click",function(){
    if (!window.confirm("Biztosan törli a terméket a kosárból?")){
        return false;
    }
    var ids = new String($(this).attr("id"));
    var idk = ids.split("_");
    var id = idk[1];
    $.get("index.php","mod=cart&del="+id,
    function(html)
    {
       $("#tr_"+id).fadeOut("slow");
       if(html=="E"){
        document.location=document.location;
       }
    },"html");
    return false;
 });*/

$(".cart tr:odd").css("background-color","#D2D8D8");
/*
    $(".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,
            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,function(json){
            $("#ar_"+id).html(json.ar+" Ft");
            $("#mind").html("<b>"+json.mind+" Ft</b>");
            $(fiz).html(tagol((price*db))+" Ft");
            
            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;
    });

});
