/* #############################################################################

NS: TESCODIRECT.app.product
	static: yes

dependancies:

NS: TESCODIRECT.app.product.images
	static: no

dependancies:

NS: TESCODIRECT.app.product.compare
	static: no

dependancies:
	TESCODIRECT.lib.event
	TESCODIRECT.lib.cookies
	TESCODIRECT.lib.DOM.node
	
############################################################################# */

TESCODIRECT.app.product = new function() {
    this.VERSION = "1.0.0";
    this.NAME = "TESCODIRECT.app.product";

    // TODO: code for the product view images page - load the thumbnail without refreshing the page...
    this.images = function() {
    }

    this.compare = function(maxProducts, numberOfProducts) {
        var _self = this;

        var _maxProducts = maxProducts;
        var _numberOfProducts = numberOfProducts;
        var _items = [];
        var _cookie;
        var _compareIdPrefix = "compare-";

        function init() {
            _cookie = TESCODIRECT.lib.cookies.get("c");
            _cookie.domain = document.location.hostname;
            var itemList = _cookie.getValue();
            if (itemList) {
                var items = itemList.split("+");
                for (var i = 0; i < items.length; i++) {
                    _items[items[i]] = "compare";
                }
            }
        }

        init();

        function buildItemList(o) {
            var itemList = "";
            var iCount = 0;
            for (var item in _items) {
                if (_items[item] == "compare") {
                    itemList += ((iCount > 0 ? "+" : "") + item);
                    iCount++;
                }
            }
            o.count = iCount;
            o.itemList = itemList;
        }

        this.insertInstructions = function(id) {
            var oInfo = document.getElementById(id);
            if (oInfo) {
                oInfo.className = "navigation comp";
                var oCopy = TESCODIRECT.lib.DOM.node.create("p");
                TESCODIRECT.lib.DOM.node.setTextValue(oCopy, "To compare up to " + _maxProducts + " products, tick the boxes &");
                var oCompare = TESCODIRECT.lib.DOM.node.create("img");
                oCompare.alt = "Compare";
                oCompare.src = "/direct/i/b/compare.gif";
                TESCODIRECT.lib.event.attach(oCompare, "click",
					function(e) {
					    var o = {};
					    buildItemList(o);

					    if (o.count < 2) {
					        alert("You need to select at least 2 products to compare");
					    } else if (o.count > _maxProducts) {
					        alert("You have selected " + o.count + " products to compare. Please select only " + _maxProducts + ".");
					    } else {
					        var href = "/compare/?c=" + o.itemList + "&f=" + (window.location.pathname + window.location.search).URLEncode();

					        for (var item in _items) {
					            if (_items[item] == "compare") {
					                var o = document.getElementById(_compareIdPrefix + item);
					                if (o) {
					                    o.parentNode.removeChild(o);
					                }
					            }
					        }

					        TESCODIRECT.lib.cookies.remove(_cookie);
					        window.top.location.href = href;
					    }
					    e.stopEvent();
					}
				);
                oCopy.appendChild(oCompare);
                oInfo.appendChild(oCopy);
            }
        }

        this.checkOption = function(checkbox) {
            if (checkbox.checked) {
                _items[checkbox.getAttribute("catNo")] = "compare";
            } else {
                delete _items[checkbox.getAttribute("catNo")];
            }
            var o = {};
            buildItemList(o);
            _cookie.setValue(o.itemList);
            TESCODIRECT.lib.cookies.set(_cookie);
        }

        this.insertOptions = function(id) {
            var oOptions = document.getElementById("o-" + id);
            if (oOptions) {
                var oCheckbox = TESCODIRECT.lib.DOM.node.create("input");
                oCheckbox.type = "checkbox";
                oCheckbox.className = "compare";
                oCheckbox.id = _compareIdPrefix + id;
                oCheckbox.setAttribute("catNo", id);
                TESCODIRECT.lib.event.attach(oCheckbox, "click",
					function(e) {
					    _self.checkOption(this);
					}
				);

                var oLabel = TESCODIRECT.lib.DOM.node.create("label");
                oLabel.className = "compare";
                // Fix for labels in IE....
                oLabel.setAttribute("forcontrol", oCheckbox.id);
                TESCODIRECT.lib.event.attach(oLabel, "click",
					function(e) {
					    var o = document.getElementById(this.getAttribute("forcontrol"));
					    if (o) {
					        o.checked = !o.checked;
					    }
					    _self.checkOption(o);
					}
				);

                var oLabelCompare = TESCODIRECT.lib.DOM.node.create("label");
                TESCODIRECT.lib.DOM.node.setTextValue(oLabelCompare, "Compare");
                oLabel.appendChild(oLabelCompare);
                //Checking if reevoo image is present 
                if (oOptions.childNodes[1] != undefined) {
                    oOptions.insertBefore(oCheckbox, oOptions.childNodes[1]);
                    oOptions.insertBefore(oLabel, oOptions.childNodes[1]);
                }
                //If reevoo image is not present then display in first position
                else {
                    oOptions.insertBefore(oCheckbox, oOptions.childNodes[0]);
                    oOptions.insertBefore(oLabel, oOptions.childNodes[0]);
                }

                oCheckbox.checked = (_items[id] ? true : false);
            }
        }
    }
}

