﻿Type.registerNamespace('Shopping.Controls');

Shopping.Controls.ShoppingCart = function(element) {
	Shopping.Controls.ShoppingCart.initializeBase(this, [element]);
	this._products = [];
	this._productQuantity = [];
	this._sessionID = null;
}

Shopping.Controls.ShoppingCart.prototype = 
{
	initialize: function() {
		Shopping.Controls.ShoppingCart.callBaseMethod(this, 'initialize');
		this.render();
	},
	// Page Size
	get_sessionID: function() { return this._sessionID; },
	set_sessionID: function(value) { this._sessionID = value; }, 
	
    // Add a product
	addProduct: function(product) 
	{ 
	    // See if the product exists
        var found = false;
	    for (var i = 0; i < this._products.length; i++) 
	    {
	        if (this._products[i].ProductId === product.ProductId)
	        {
	            // Update the qty
	             this._products[i].Qty = this._products[i].Qty + 1;
	             Shopping.Services.CatalogService.updateProductToBasket(product.ProductId,this._sessionID,this._products[i].Qty );

	            
    	        found = true;
	        }
	    }
	
	    if (found == false)
	    {
	        // Add the qty; Add the product to the collection
	        Shopping.Services.CatalogService.addProductToBasket(product.ProductId,this._sessionID);
	        product.Qty = 1;
	        this._products.push(product);
	    }

	    this.render();
	},
	
	 // Products
	get_products: function() { return this._products; },
	set_products: function(value) { this._products = value; }, 
    
    dataBind: function()
	{
	    this.render();
	},
	
	render: function()
	{
	
	    // Get a reference to the passed in element
		var element = this.get_element();
		
        // Clear content from the element
		element.innerHTML = '';
        
        
        /*    <div class="cartBook">
               <strong class="bookNameCart">דודי ואודי מצילים את הכפר</strong>
               <img class="CartImg"src="App_Themes/superSefer/images/noBook.jpg" />
                <input id="Text1" type="text" class="checkOutFormsField" />
                <img src="images/delete.jpg" />
            </div>*/
            
         var div =  document.createElement('div');
         Sys.UI.DomElement.addCssClass(div, 'cartTotal');
        
         
//         div.innerHTML = "סל קניות";
//         div.appendChild(document.createElement('br'));
//         //תמונה לקופה
        
         var basketImg = document.createElement('img');
         basketImg.setAttribute("src", "../App_Themes/superSefer/images/cartgoBasket.jpg" );
         basketImg.setAttribute("style", "float:right; border:0" );
         basketImg.setAttribute("border","0");
         var  a = document.createElement('a');
         a.setAttribute("href", "basket.aspx");
         a.appendChild(basketImg);
        
         div.appendChild(a);
         element.appendChild(div);
         //Show no items in cart, if empty, otherwise, show subtotal
            if (this._products.length == 0)
                this.showEmpty(div);
            else
                this.showSubTotal(div);
       
     
     
        // Iterate through the items in the collection
		for (var i = 0; i < this._products.length; i++) 
		{
		    //שם ספר
		    subDiv =  document.createElement('div');
		    Sys.UI.DomElement.addCssClass(subDiv, 'cartBook');
		    var kot = document.createElement('strong');
		    Sys.UI.DomElement.addCssClass(kot, 'bookNameCart');
		    kot.innerHTML = this._products[i].ProductName;
		    subDiv.appendChild(kot);	
		    element.appendChild(subDiv);
		    //תמונה
	       var img = document.createElement('img');
           img.setAttribute("src", "../files/" + this._products[i].Picture);
           img.className="CartImg";
           subDiv.appendChild(img);
		    //טקסט של כמות
		    // Add the textbox
            var inp = document.createElement('input');
            inp.type = "text";
            inp.size = "2";
            Sys.UI.DomElement.addCssClass(inp, 'checkOutFormsField');
            inp.value = this._products[i].Qty;
            $addHandler(inp, 'change', Function.createCallback(Function.createDelegate(this, this.itemQtyUpdated), i));
            subDiv.appendChild(inp);	
		  
		    //אפשרות מחיקה
		    img = document.createElement('img');
            img.setAttribute("src", "../images/delete.jpg");
            Sys.UI.DomElement.addCssClass(img, 'blImg');
            img.alt = "מחק";
            subDiv.appendChild(img);
             subDiv.appendChild(document.createElement('br'));
            var small = document.createElement('small');
            small.innerHTML = this._products[i].Price + "&nbsp; ש''ח" ;
            subDiv.appendChild(small);
            $addHandler(img, 'click', Function.createCallback(Function.createDelegate(this, this.itemDeleteClicked), i));
           
        
        }
        
          

	},
	
	itemDeleteClicked: function(sender, index)
	{
	    // Remove the item from the array
	     Shopping.Services.CatalogService.deleteProductFromBasket(this._products[index].ProductId,this._sessionID );
	    this._products.splice(index, 1);
	    this.render();
	},
	
	showBigBookClicked:function(sender, index)
	{
	alert('aaaaa');
	},
	
	itemQtyUpdated: function(sender, index)
	{
	    // Get the new qty
	    var qty = parseInt(sender.target.value);
	    if (qty == 0 )
	    {
	    
	        Shopping.Services.CatalogService.deleteProductFromBasket(this._products[index].ProductId,this._sessionID );
	        this._products.splice(index, 1);
        }
	    else if (qty >0)
	    {
	        this._products[index].Qty = qty;
    	    Shopping.Services.CatalogService.updateProductToBasket(this._products[index].ProductId,this._sessionID,  this._products[index].Qty );
    	
    	}
    	    
	    this.render();
	},
	
	showEmpty: function(parentDiv)
	{
        var ctrl = document.createElement('div');
        Sys.UI.DomElement.addCssClass(ctrl, 'readOnlyField');
        ctrl.innerHTML = "(סל הקניות שלך ריק)";
        parentDiv.appendChild(ctrl);	
	},
	
	showSubTotal: function(parentDiv)
	{
    
	    var bld = document.createElement('strong');
	  //  bld.setAttribute("style", "paddingtop");
	    bld.innerHTML = "סך הכל: " + this.calculateSubtotal() + " ש''ח";
	    parentDiv.appendChild(bld);
	   // parentDiv.appendChild(document.createElement('br')); //Spacer
	},
	
	calculateSubtotal: function()
	{
	    var subTotal = 0;
	    
	    for (var i = 0; i < this._products.length; i++) 
		{
		    subTotal += (this._products[i].Qty * this._products[i].Price);
		}
		
		return subTotal;
	}
}
	
Shopping.Controls.ShoppingCart.registerClass('Shopping.Controls.ShoppingCart', Sys.UI.Control);
Sys.Application.notifyScriptLoaded();

