﻿///<reference path="/Web/Js/TESCO.js" />
///<reference path="/Web/Js/system/event.js" />
///<reference path="/Web/Js/system/event.manager.js" />
///<reference path="/Web/Js/system/exception.js" />
///<reference path="/Web/Js/system/DOM.node.js" />
///<reference path="/Web/Js/UI/effects.js" />
///<reference path="basket.js" />
///<reference path="entities.js" />

TESCO.$("sites.retail.UI.Basket").Row = (function() {

    var NODE = TESCO.system.DOM.node;

	//  constructor
	function _constructor(basket, tr) {
		this.basket = basket;
		this.tr = tr;
		return this;
	}
	
	//  public instance
	//#region 
	_constructor.prototype.NAME = "TESCO.sites.retail.UI.Basket.Row";

	_constructor.prototype.show = function() {  
		this.tr.style.display = "";
	}

	_constructor.prototype.hide = function() {
		this.tr.style.display = "none";
	}

	_constructor.prototype.remove = function() {
		return this.tr.parentNode.removeChild(this.tr);
	}

	_constructor.prototype.insert = function(tbody) {
		tbody.insertBefore(this.tr, tbody.firstChild);
	}

	_constructor.prototype.update = function(shelf, tr) {
	    shelf.tbody().replaceChild(tr, this.tr);
		this.tr = tr;
	}
	//#endregion 
	
	return _constructor;
})();

TESCO.$("sites.retail.UI.Basket.Row").Product = (function() {

	//	constants
	//#region
	var NODE = TESCO.system.DOM.node;
    var ENTITIES = TESCO.UI.entities;
    //#endregion

    //  private instance
    //#region
    function _toggle(key) {
		var _img = NODE.getElementsByClassAndTagName(this.tr, "actuator", "img");
		if (_img) {
			for (var i = 0, L = _img.length; i < L; i++) {
				if (NODE.hasClassName(_img[i], "bi")) {
					_img[i].src = TESCO.locale.basket["srcIncrease" + key];
				} else if (NODE.hasClassName(_img[i], "bd")) {
					_img[i].src = TESCO.locale.basket["srcDecrease" + key];
				} else if (NODE.hasClassName(_img[i], "br")) {
					_img[i].src = TESCO.locale.basket["srcRemoveItem" + key];
				} 
			}
		}
    }
 
    function _unlock() {
		NODE.removeClassName(this.tr, "locked");
		_toggle.call(this, "");
    }
    //#endregion

    //  constructor
    function _constructor(basket, product, tr) {
        this.product = product;
        this.tr = tr || this.create(basket);
        this.colour = new TESCO.UI.effects.Colour(this.tr);
        _constructor.base.constructor.call(this, basket, this.tr);
        return this;
    }
    _constructor.extend(TESCO.sites.retail.UI.Basket.Row);

    //  public instance
    //#region 
    _constructor.prototype.NAME = "TESCO.sites.retail.UI.Basket.Row.Product";
    
    _constructor.prototype.warn = function() {
        this.updateMessage();
        switch (this.product.action) {
            case ENTITIES.Product.actions.remove:
                this.show();
                break;
            default:
                _unlock.call(this);
        }
        this.colour.warn();
    }

    _constructor.prototype.start = function() {
        switch (this.product.action) {
            case ENTITIES.Product.actions.remove:
                this.hide();
                break;
            default:
                this.updateMessage();
                this.show();
        }
    }

	_constructor.prototype.setQuantity = function(quantity, container) {
        var _node = NODE.getElementsByClassAndTagName(container, "basketItemQuantity", "span")[0];
		NODE.setTextValue(_node, quantity);
    }

    _constructor.prototype.lock = function() {
        NODE.addClassName(this.tr, "locked");
		_toggle.call(this, "Locked");
    }

    _constructor.prototype.update = function(shelf, tr, product) {
        this.product = product;
        this.colour = new TESCO.UI.effects.Colour(tr);
		_constructor.base.update.call(this, shelf, tr);
    }

    _constructor.prototype.confirm = function() {
        this.colour.confirm();
    }
    //#endregion

    //  return _constructor as function pointer
    return _constructor;
})();