var clickbusy=false;
(function($) {
$(document).ready(
    function(){
        $('a.itemsubmit').click(
        function(){
            if(!clickbusy){

            clickbusy=true;
            var id = $(this).attr('data-itemid');
            var includestand = $('#chk-'+id).is(":checked");
            var qty = $('#qty-'+id).val();
                    $.ajax({'type':'POST',
                        'url':'index.php?r=category/ajax',
                        'dataType':'json',
                        'data':{'id':id,'qty':qty,"includestand":includestand},
                        'success':function(json){drawOrder(json);clickbusy=false;}
                        }
                );
            }
        }
        );
        $('a.weightchange').click(
        function(){
            var id = $(this).attr('data-itemid');
            var catid = $("#hdncategoryid").val();
            var weight = $('#txtweight-'+id).val();
            $.ajax(
            {'type':'POST',
            'url':'index.php?r=category/changeweight',
            'data':{'id':id,'category':catid,'weight':weight},
            'success':function(id){$(id).show();}
            }
            );
        }
    );
        $('a.archive_link').click(function(){
            var id = $(this).attr('data-orderid');
            $(this).text(($(this).text()=="Archive Order")?"Remove Order From Archive":"Archive Order");
            $.ajax(
                {'type':'POST','url':'index.php?r=admin/ajax','data':{'id':id}}
            );
        });
        applyItemRemove();

        //Initialise the order on page load
        $.ajax({'type':'POST',
                        'url':'index.php?r=category/ajax',
                        'dataType':'json',
                        'data':{'initialise':true},
                        'success':function(json){drawOrder(json);}
                        }
                );
    }
);

function applyItemRemove(){

   $('a.itemremove').click(
        function(){
            var id = $(this).attr('data-itemid');
                    $.ajax({'type':'POST',
                        'url':'index.php?r=category/ajax',
                        'dataType':'json',
                        'data':{'id':id},
                        'success':function(json){drawOrder(json);}
                        }
                );

            }
        );
}
function drawOrder(obj)
{
    count = 0;
    total = 0;
    items = obj.items;
    $("a.pink").text("Hire Now");
    $("a.orange").text("Buy Now");
    output="";
    for(i=0;i<items.length;i++)
        {
            type = (items[i].sale=="1")?"sell":"hire";//this is wrong
            $("#"+type+"-"+items[i].id).text("Remove");
            output+= returnOrderRow(items[i],obj.imageurl);
            total = total +(items[i].price*items[i].qty);
            if(items[i].includestand){
                total=total+(items[i].includecost*items[i].qty);//Include the cost of the stand multiplied by the number of items
            }
            count++;
        }
        output+="<div class='orderrow total'><div class='label'>Total:</div><div class='line_total'>R "+total.toFixed(2)+"</div><div class='remove'></div></div>";
        heading = "<div class='orderrow orderrowhead'><div class='name'>Item Name</div><div class='qty'>Quantity</div><div class='line_total'>Line Total</div><div class='remove'>[X]</div></div>";
        output = heading+output;
        if($('#hdnCheckout').val())
        {
            $('#GrandTotal_holder').hover(function(){$(this).css('background-color', '#E7B669');},
                                                function(){$(this).css('background-color', '#FFF');});
            $("#CheckoutOrder").html(output);
            $('#hdnCheckout').val(total);
            setGrandTotal();
            $('.transport select').change(function(){
                setGrandTotal();

            });
        }

        $("#TheOrder").html(output);
        $("#ItemsNumber").text(count);
        $("#ItemsTotal").text("R "+total.toFixed(2));

        applyItemRemove();
        $('.orderrow').hover(function(){$(this).css('background-color', '#ABDF7A');},
                                                function(){$(this).css('background-color', '#FFF');});
}
function setGrandTotal(){
    deliver_price=$('#Order_deliver').val();
                collect_price=$('#Order_collect').val();
                for(var i=0; i<travel_costs.length; i++)
                {
                    if(travel_costs[i].id==deliver_price){deliver_price=travel_costs[i].price;}
                    if(travel_costs[i].id==collect_price){collect_price=travel_costs[i].price;}
                }
                var grand_total = parseInt($('#hdnCheckout').val()) +parseInt(deliver_price) + parseInt(collect_price);
                $('#GrandTotal').text("R "+grand_total.toFixed(2));
}
function returnOrderRow(item,imageurl)
{
    name = "<div class='name'><a href='"+imageurl+item.image+"' target='_blank'>"+item.name+"</a></div>";
    qty = "<div class='qty'>X "+item.qty+"</div>";
    line_total = "<div class='line_total'>R "+(item.price * item.qty).toFixed(2)+"</div>";
    remove = "<div class='remove'><a href='javascript:void(0);' class='itemremove' data-itemid='"+item.id+"'>[X]</a></div>";
    row = name+qty+line_total+remove;
    div = "<div class='orderrow'>"+row+"</div>";
    if(item.includestand){
        div+="<div class='orderrow'><div class='includestand'>"+item.includetext+" x"+item.qty+"</div><div class='line_total'>R "+(item.includecost*item.qty).toFixed(2)+"</div><div class='remove'></div></div>";
    }
    return div;
}

function confirmCheckout()
{
    //function(){$('#order-checkout-form').submit(function(){return true;});}
    $(':submit').click(function(){$('#buttontype').val($(this).val());$('#order-checkout-form').submit();});
}
})(jQuery)
