﻿
    var total = 0; 
$(document).ready(function() {

    $(".cItem").draggable({ helper: "clone", opacity: "0.5" });

    $(".cDropzone").droppable(
        {
            accept: ".cItem",
            hoverClass: "dropHover",
            drop: function(ev, ui) {
                
                var droppedItem = ui.draggable.clone().addClass("droppedItemStyle");       
                //Get length of all items in the dropzone and append it to id of dropped item.
                //This is to generate items with unique id's when same item is added more than once.
                 //var itemsInCart = $(".cDropzone div");
                 var itemsInCart = $(".cDroppedItems div");
                 
                 droppedItem[0].id=droppedItem[0].id+itemsInCart.length;
                                   
                var productDesc = droppedItem[0].attributes["desc"].nodeValue; 
                var productPrice = droppedItem[0].attributes["price"].nodeValue;
                             
                var removeLink = document.createElement("a"); 
                removeLink.innerHTML = "Remove"; 
                removeLink.className = "deleteLink";
                removeLink.href = "#";
                removeLink.onclick = function() 
                {
                    $(".cDroppedItems").children().remove("#"+droppedItem[0].id); 
                    updateTotal(productPrice * (-1)); 
                }        
                
                /*26th Feb, 09*/
                //$("#divDroppedItems").append("<div id='"+droppedItem[0].id+"' class='cDivCartItem'><div class='cDivCartDesc'>"+productDesc+"</div><div class='cDivCartPrice'>"+productPrice +"</div><div><a href='#' id='deleteLink' class='deleteLink'>Remove</a></div></div>");
                /*26th Feb, 09*/
        
                var divLink=document.createElement("div");
                divLink.className="deleteDiv";
                divLink.appendChild(removeLink);
                
                droppedItem[0].appendChild(divLink);
                               
                $(".cDroppedItems").append(droppedItem);        
                 updateTotal(productPrice);                  
            }
        }

    );
   
    
});


   // update the total!
    function updateTotal(price) {
        total += parseFloat(price);
        //alert(total);
        $("#total").html(total.toFixed(2));
        $(".shoppingCartTotal").effect("bounce");
    
    }
    
    function listSelectedItems() 
    {
        var itemsInShoppingCart = $(".cDroppedItems div");
    
        for(i=0;i<=itemsInShoppingCart.length-1;i++) 
        {
            alert("Code: "+itemsInShoppingCart[i].attributes["code"].nodeValue+'\n'+"Price: Rs."+itemsInShoppingCart[i].attributes["price"].nodeValue); 
        }
        
    }