TESCO.$("sites.UI").Product = (function() {

    var NODE = TESCO.system.DOM.node;

    function _constructor() {
        _constructor.base.constructor.apply(this, arguments);
        return this;
    }
    _constructor.extend(TESCO.sites.retail.UI.Product);
    
    _constructor.prototype.getLocation = function(element) {
        //	Check if this product was from a productlist, productdetails, or featuredspace

        var _form = NODE.getAncestorByAttributeRegExp(element, "method", new RegExp('^post'));
        var _formID = _form.id.split("-")[0];
        var _trace = {};

        if (_formID === "fBrowse") {
            _trace.name = "productList&view=";
            _trace.name += document.body.id.split("_")[1];
            _trace.name += "&filter=";
            var _breadCrumbContainer = document.getElementById("breadcrumbNav");
            if (_breadCrumbContainer) {
                var _breadcrumbs = _breadCrumbContainer.getElementsByTagName("li");
                for (var b = 0; b < _breadcrumbs.length; b++) {
                    if (_breadcrumbs[b].className.indexOf("hide") == -1) {
                        _trace.name += NODE.getTextValue(_breadcrumbs[b]) + "/";
                    }
                }
            }
        } else if (_form.id.split("-")[2] === "alt") {
            _trace.name = "productDetailsAlternative";
            _trace.name += "&originalProductID=";
            var _div = NODE.getDescendantsByAttributeRegExp(document.getElementById("header"), "div", "id", new RegExp('^p-'));
            var _baseProductID = _div[0].id.split("-")[1];
            _trace.name += _baseProductID;
        } else if (_formID === "fDetails") {
            _trace.name = "productDetails";
        } else if (_formID === "fComplementary") {
            _trace.name = "complementaryProduct";
            _trace.name += "&originalProductID=";
            var _div = NODE.getDescendantsByAttributeRegExp(document.getElementById("header"), "div", "id", new RegExp('^p-'));
            var _baseProductID = _div[0].id.split("-")[1];
            _trace.name += _baseProductID;
        } else if (_formID === "fFeaturedSpace") {
            _trace.name = "featuredSpace";
            _trace.name += "&featuredSpaceID=";
            _trace.name += _form.id.split("-")[2];
            _trace.name += "&candidateID=";
            _trace.name += _form.id.split("-")[3];
        }
        return _trace;
    }

    return _constructor;
})();


TESCO.sites.UI.ToggleNav = (function() {

    var NODE = TESCO.system.DOM.node;

    function _constructor(container) {
        TESCO.system.event.attach(container, "click", clickHandler);
        return this;
    }

    function clickHandler(e) {
        if (TESCO.system.DOM.node.hasClassName(e.target, "toggle")) {
            var _parentList = NODE.getAncestorByAttributeRegExp(e.target, "class", new RegExp('navList(\t)?'));
            if (_parentList) {
                if (NODE.hasClassName(_parentList, "open")) {
                    NODE.setTextValue(e.target, TESCO.locale.navigation.textMore);
                    NODE.removeClassName(_parentList, "open");
                } else {
                    NODE.setTextValue(e.target, TESCO.locale.navigation.textLess);
                    NODE.addClassName(_parentList, "open");
                }
            }
            e.prevent();
        }
    }

    return _constructor;
})();


TESCO.UI.Collapse.Section.KeyFacts = (function() {
	/*	Wrapper for collapse which sets up toggle key facts boxes.	*/
	
	var NODE = TESCO.system.DOM.node;

	_constructor.initialise = function() {
		var _products = NODE.getDescendantsByAttributeRegExp(document.body, "*", "id", new RegExp('^p-'));
		if (_products) {
			for (var i = 0, L = _products.length; i < L; i++) {
				var _node = _products[i];
				var _keyFacts = NODE.getElementsByClassAndTagName(_node, "keyFacts", "div")
				if (_keyFacts){
					var _toggleBtn = NODE.getElementsByClassAndTagName(_node, "expandInfo", "a")[0];
					new _constructor(_node, _toggleBtn, true, 
						{
							"collapseImg": TESCO.sites.Configuration.application.img.URI + "/Sites/Retail/" + TESCO.sites.Configuration.application.name + "/Online/Generic/showKeyFactsContract.gif",
							"expandImg": TESCO.sites.Configuration.application.img.URI + "/Sites/Retail/" + TESCO.sites.Configuration.application.name + "/Online/Generic/showKeyFactsExpand.gif",
							"collapseAlt": TESCO.locale.product.keyFactsText.showKeyFacts,
							"expandAlt": TESCO.locale.product.keyFactsText.hideKeyFacts,
							"preventDefault": true
						}
					);
				}
			}
		}
	}
	
	TESCO.system.event.manager.call(_constructor.event = {}, "complete");
	
	function _constructor(container, trigger, collapsed, attributes) {
		_constructor.base.constructor.apply(this, arguments);
		this.addEventListener("complete",
			function(e) {
				_constructor.event.dispatchEvent("complete", e);
			}
		);
		return this;
	}
	_constructor.extend(TESCO.UI.Collapse.Section);
	
	return _constructor;
})();
