var lastsel;
var lastsel_prod;


//подсветка строк в таблице
$(document).ready(function() { 
initprod(); //init product lister
initcart();
$("#cart_sum").load("/catopt/cart_sum/"); 
$("#root").treeview({collapsed: true});
$("#root span").hover(
    function() {$(this).addClass("hover");},
    function() {$(this).removeClass("hover");}
);
//init_goods();
});

function test_details()
    {
    $("#details_form").css("visibility","visible");
    } 

function details(id)
{
window.open('/catopt/details/'+id+'.htm',id, "width=400, height=400,' toolbar=no, scrollbars=yes,resizable=yes,status=no', left=1,top=1")
}    
    
function list_view()
    {
    $("#list_container").css("display","block");
    $("#ico_container").css("display","none");
    }

function prod_select(id)
    {
    $(".prod_selected").removeClass("prod_selected"); 
    $("#p_"+id).addClass("prod_selected");
    }    
    
function ico_view()
    {
    $("#list_container").css("display","none");
    $("#ico_container").css("display","block");
    }

 
function init_goods()
    {
$(".goods tr").hover(function() {
     $(this).addClass("active");
   },function(){
     $(this).removeClass("active");
   });

$(".goods tr").dblclick(function(){
    alert("bugaga");
});

    };
 
function updateprice(id,price)
{
el = document.getElementById("sum"+id);
inp = document.getElementById("pr"+id);
el.innerHTML = inp.value*price;
}; 

function codefind()
    {
    var cod = $("#codesearch").val();
    jQuery("#prod_container").setUrl('/catopt/codesearch/?id='+cod);
    jQuery("#prod_container").setPage(1);
    jQuery("#prod_container").trigger('reloadGrid'); 

    };

function getcat(id)
    {
    
    prod = document.getElementById("products");
    $.get("/catopt/products/"+id+".htm",
        function(data)
            {
            prod.innerHTML = data;
            init_goods();
            }
        )
    };
 
function setHash(category,product)
{
    if (product == undefined) 
    {
        location.hash = category;
    }
    else 
    {
    location.hash = category + ":" + product;
    }
}

function getHashCat()
{
    return location.hash.slice(1);
}


    
    function getproducts(id)
    {
        catsel(id);
        setHash(id);
        jQuery("#ico_container").html("");
        jQuery("#ico_container").addClass("ajax_loading");
        jQuery("#prod_container").setUrl('/catopt/products/'+id+'.htm');
        jQuery("#prod_container").setPage(1);
        jQuery("#prod_container").trigger('reloadGrid');
        jQuery("#ico_container").load('/catopt/products_icons/'+id+'.htm', 
            function()
            {
                jQuery("#ico_container").removeClass("ajax_loading"); 
                $(".product_box_ico").picpv(); 
            }
        );
        
    };

function delcart()
    {
    $.get("/catopt/delcart/"+lastsel+".htm?seed="+Math.random(),getcart);
    //jQuery("#cart_container").trigger('reloadGrid'); 
    }
    
    function delitem(id)
    {
        $.get("/catopt/delcart/"+id+".htm?seed="+Math.random(),getcart);
    //jQuery("#cart_container").trigger('reloadGrid'); 
    }
     
    
function getcart(data)
    {
jQuery("#cart_container").setPage(1); 
jQuery("#cart_container").trigger('reloadGrid');
$("#cart_sum").load("/catopt/cart_sum/"); 
    };    
    
function addcart(proid)
    {
    $.get("/catopt/addcart/"+proid+".htm?seed=" + Math.random(),getcart);
    };    

function addcount(id,count)
    {
    var cnt = 1;
    if (count > 0) cnt = count;
    $.get("/catopt/addcart/"+id+".htm?qty="+cnt+"&seed="+Math.random(),getcart);
    };    

function ajaxlinks()
{
    $("a[target]").bind("click", 
        function(e)
        {
            var destination = $(e.target).attr("target");
            $(destination).addClass("ajax_loading");
            $(destination).load($(e.target).attr("href"));
            $(destination).removeClass("ajax_loading");
            return false;
        }
    );
}
    
function initprod()
    {
    
    var hashcat = getHashCat();
    if (hashcat == undefined || hashcat == "") hashcat = 1;
    
    var cat = '/catopt/products/'+hashcat+'.htm';
    
    jQuery("#prod_container").jqGrid({        
    url: cat,
    height: 200,
    datatype: "xml",
    loadtext: "Загрузка...",
    ondblClickRow: addcart,
    colNames:['Артикул','Наименование', 'Цена','Количество','Добавить'],
    colModel:[
        {name:'product_code',index:'product_code', width:100},
        {name:'name',index:'name', width:300},
        {name:'Price',index:'Price', width:80, align:"right"},
        {name:'qty',index:'qty', width:80, align:"right",editable:true},
        {name:'add',index:'add', width:80, align:"left"}
        
    ],
    imgpath: '/images/jqgrid/',
    onSelectRow: function(id){
        if(id && id!==lastsel_prod){
            jQuery('#prod_container').restoreRow(lastsel_prod);
            jQuery('#prod_container').editRow(id,true);
            lastsel_prod=id;
        }
    },
    onSave: function()
        {
        getcart(); 
        },
    editurl: "/catopt/addcart/",

    viewrecords: true
});

        jQuery("#ico_container").load('/catopt/products_icons/'+hashcat+'.htm', 
            function()
            {
                jQuery("#ico_container").removeClass("ajax_loading"); 
                $(".product_box_ico").picpv(); 
            }
        );


    };
    
function initcart()
    {
 
    jQuery("#cart_container").jqGrid({        
    url:'/catopt/cart/',
    datatype: "xml",
    loadtext: "Загрузка...",
    colNames:['Артикул','Наименование','Цена','Количество','Сумма','X'],
    colModel:[
        {name:'product_code',index:'product_code', width:100},
        {name:'name',index:'name', width:300},
        {name:'product_price',index:'product_price', width:80,align:"right"},
        {name:'qty',index:'qty', width:80, align:"right",editable:true},
       {name:'product_sum',index:'product_sum', width:80,align:"right"},
       {name:'cart_del',index:'cart_del', width:20,align:"right"}

    ],
    imgpath: '/images/jqgrid/',
    onSelectRow: function(id){
        if(id && id!==lastsel){
            jQuery('#cart_container').restoreRow(lastsel);
            jQuery('#cart_container').editRow(id,true);
            lastsel=id;
        }
    },
    onSave: function()
        {
        getcart(); 
        },
    editurl: "/catopt/cart_qty/",
    viewrecords: true
});
    };

    
catsel = function(catid)
{
    //$("#root li span.active").removeClass("active");
    $("#"+catid).css("color", "#909090");
}    
    
$.fn.picpv = function()
{
    var img = document.createElement("img");
    if (!document.getElementById("picpvimg"))
    {
        img.id = "picpvimg";
        $(img).css({display: "block", position: "absolute"});
        $(img).addClass("ajax_loading");
        $(img).hide();
        $("body").append(img);
    }
    
    $().mousemove(
        function(e)
        {
            
            $('#picpvimg').css({top: e.pageY + 5 , left: e.pageX - 355});
        }
    );

    var getEnlarged = function(url)
    {
        var parts = url.split("/regular/");
        return parts[0]+"/"+parts[1];
    }
    
    this.each(
        function()
        {
            var element = this;
                $(element).bind("mouseover",
                    function(e)
                    {
                        $("#picpvimg").show();
                        $("#picpvimg").attr("src",getEnlarged($(e.target).attr("src")));
                    }
                );
                $(element).bind("mouseout",function(){ $("#picpvimg").attr("src","/images/transparent.gif"); $("#picpvimg").hide(); });
        }
    );

}
   
