TESCO.$("sites.UI").Product = (function() {

    function _constructor() {
        _constructor.base.constructor.apply(this, arguments);
        return this;
    }
    _constructor.extend(TESCO.UI.Product);

    return _constructor;
})();

TESCO.$("sites.UI.Product").OverlayUnavailable = (function() {

    function _constructor() {
        TESCO.system.event.document.addEventListener("load",
            function() {
                //Get all unavailable products on the page
                var _content = document.getElementById("contentMain");
                if (_content) {
					var _prods = TESCO.system.DOM.node.getElementsByClassAndTagName(_content, "noStock", "li");
					if (_prods) {
						var background;
						for(var i=0;i<_prods.length;i++) {
							background = new TESCO.UI.effects.Background(_prods[i]);
							background.show();
	                        
							var _activeElements = TESCO.system.DOM.node.getElementsByClassName(_prods[i], "active");
							if (_activeElements) {
								for (var e=0;e<_activeElements.length;e++) {
									var _activeRect = new TESCO.system.DOM.Rectangle.Position(_activeElements[e]);
									_activeRect.raise();
								}
							}
	                        
							var _altProduct = TESCO.system.DOM.node.getElementsByClassName(_prods[i], "altContent");
							if (_altProduct) {
								var _altRect = new TESCO.system.DOM.Rectangle.Position(_altProduct[0]);
								_altRect.raise();
							}
						}
					}
				}
            }
         );
    }

    return new _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;
})();