﻿///<reference path="/TESCO.js" />
///<reference path="basket.js" />
///<reference path="../basket.js" />
///<reference path="../basket.shelf.js" />
///<reference path="../basket.row.js" />

TESCO.$("sites.UI.Basket.Row").Product = (function() {

	var SHELF = TESCO.UI.Basket.Shelf;
	var NODE = TESCO.system.DOM.node;
    
	//	private static
	//#region
    function _setPrice(price, container) {	//	update price
        var _node = NODE.getElementsByClassAndTagName(container, "price", "p")[0];
        _node.innerHTML = price; //	uses innerhtml because £ symbol produces funny symbol
    }
    //#endregion
    
    //  private instance
    //#region 
	function _move(shelf, tr, product) {
	
		var _defaultShelf = SHELF.Default.get();
		var _oldShelfNode = NODE.getAncestorByAttributeRegExp(this.tr.parentNode, "class", new RegExp('^shelf'));
		var _oldShelf = SHELF.getByType(SHELF.getTypeFromNode(_oldShelfNode));
		var _expand = false;
		
		if (shelf !== _defaultShelf) {	//	check the new shelf is not the default shelf
			_expand = shelf.isEmpty();	//	record whether to expand it after the new tr is inserted
			if (shelf.type.toLowerCase().indexOf("excluded") > -1) {
				this.product.action = TESCO.UI.entities.Product.actions.exclude;
			}
		}
		if (_oldShelf !== _defaultShelf) {	//	check the old shelf not the default shelf
			this.hide();
			if (_oldShelf.isEmpty()) 
				_oldShelf.collapse();
		}
		
        this.product = product;
        this.colour = new TESCO.UI.effects.Colour(tr);
        this.remove();  //  destroy old tr
        this.tr = tr;
        this.insert(shelf.tbody());
        
		if (_expand) {	//	check it's not the default shelf
			shelf.expand();
		}
    }
    //#endregion 
    
    //  constructor
    function _constructor(basket, product, tr) {
        _constructor.base.constructor.apply(this, arguments);
        return this;
    }
    _constructor.extend(TESCO.sites.retail.UI.Basket.Row.Product);

    //  public instance
    //#region 
    _constructor.prototype.NAME = "TESCO.sites.UI.Basket.Row.Product";

    _constructor.prototype.update = function(shelf, tr, product, move) {	//	augment the base
		if (move) {	
			//	row exists in this basket, but in another shelf
			_move.apply(this, arguments);
		} else {
			_constructor.base.update.call(this, shelf, tr, product);
		}		
    }
    
    _constructor.prototype.setQuantity = function(quantity) {
        _constructor.base.setQuantity.call(this, quantity, this.tr);
    }
    
    _constructor.prototype.updateMessage = function() { //  override base
        this.setQuantity(this.product.getFormattedQty(this.basket.id));
        _setPrice(this.product.getFormattedBasketPrice(this.basket.id), this.tr);
    }
    
    _constructor.prototype.create = function(basket) {
        var row = document.createElement("tr");
        row.setAttribute("id", "tr-" + this.product.getId());

		//	decrease quantity
        var cell = document.createElement("td");
        NODE.addClassName(cell, "quantityDecrease first");
        var alink = document.createElement("a");
        NODE.addClassName(alink, "decreaseAmount");
        alink.setAttribute("href", "?action=IncreaseDecrease&amp;ProductId=" + this.product.getId() + "&amp;qty=-1");
        var image = document.createElement("img");
        NODE.addClassName(image, "bd");
        image.setAttribute("src", TESCO.locale.basket.srcDecreaseLocked);
        image.setAttribute("alt", TESCO.locale.basket.altDecrease);
        alink.appendChild(image);
        cell.appendChild(alink);
        row.appendChild(cell);
        
        //	quantity
        cell = document.createElement("td");
        NODE.addClassName(cell, "quantityAmount");
        var span = document.createElement("span");
        NODE.addClassName(span, "basketItemQuantity");
        var text = document.createTextNode(this.product.getFormattedQty(basket.id));
        span.appendChild(text);
        cell.appendChild(span);
        row.appendChild(cell);
		//	increase quantity
        cell = document.createElement("td");
        NODE.addClassName(cell, "quantityIncrease");
        alink = document.createElement("a");

        NODE.addClassName(alink, "increaseAmount");
        alink.setAttribute("href", "?action=IncreaseDecrease&amp;ProductId=" + this.product.getId() + "&amp;qty=1");
        image = document.createElement("img");
        NODE.addClassName(image, "bi");
        image.setAttribute("src", TESCO.locale.basket.srcIncreaseLocked);
        image.setAttribute("alt", TESCO.locale.basket.altIncrease);
        alink.appendChild(image);
        cell.appendChild(alink);
        row.appendChild(cell);
     	
		//	add product image
        if (basket.view === TESCO.UI.Basket.views.maxi) {
            cell = document.createElement("td");
            NODE.addClassName(cell, "productImage");
            image = document.createElement("img");
            image.setAttribute("src", this.product.getImgURL());
            image.setAttribute("alt", this.product.getName());
            cell.appendChild(image);
            row.appendChild(cell);
        }

		//	product name
        var cellHeader = document.createElement("th");
        cellHeader.setAttribute("scope", "row");
        alink = document.createElement("a");
        alink.setAttribute("href", TESCO.sites.Configuration.application.path + "/Product/Details/?id=" + this.product.getId());
        text = document.createTextNode(this.product.getName());
        alink.appendChild(text);
        cellHeader.appendChild(alink);
        row.appendChild(cellHeader);

		//	price
        cell = document.createElement("td");
        NODE.addClassName(cell, "price");
        para = document.createElement("p");
        NODE.addClassName(para, "price");
        para.innerHTML = this.product.getFormattedBasketPrice(basket.id);
        cell.appendChild(para);
        row.appendChild(cell);

		//	remove produst
        cell = document.createElement("td");
        NODE.addClassName(cell, "last");
        alink = document.createElement("a");
        NODE.addClassName(alink, "Remove Item");
        alink.setAttribute("href", "?action=remove-basket-item&amp;ProductId=" + this.product.getId());

        if (basket.view !== TESCO.UI.Basket.views.maxi) {	//	add remove image
            image = document.createElement("img");
            NODE.addClassName(image, "br");
            image.setAttribute("src", TESCO.locale.basket.srcRemoveItemLocked);
            image.setAttribute("alt", TESCO.locale.basket.altRemoveItem);
			alink.appendChild(image);
        } else {
            alink.appendChild(document.createTextNode(TESCO.locale.basket.removeLinkValue));
        }
        cell.appendChild(alink);
        row.appendChild(cell);

        return row;
    }
    //#endregion
    
    //  return _constructor as function pointer
    return _constructor;
})();
