/* #############################################################################

NS: TESCODIRECT.lib.section
	static: yes

dependancies:
	TESCODIRECT.system.browser
	TESCODIRECT.lib.DOM.node
	TESCODIRECT.lib.event
	
############################################################################# */

// Variable used by choicestream
var showingRelated = false;

TESCODIRECT.lib.section = new function() {


    this.VERSION = "1.0.0";
    this.NAME = "TESCODIRECT.lib.section";
    this.comparePriceHeader;

    this.toggle = function(id, selectedIndex) {

        var selectedHeader = null;

        if (document.getElementById && !TESCODIRECT.system.browser.isMacIE) {
            var self = this;

            var o = document.getElementById(id);

            this.selectedIndex = (selectedIndex ? selectedIndex : 0);

            if (o) {
                o.className = (o.className + " toggle-section").trim();
                var oSections = o.getElementsByTagName("div");
                var iSectionCount = 0, oHeaders;
                if (oSections) {
                    var oHeader = TESCODIRECT.lib.DOM.node.create("div");
                    oHeader.className = "section-header";
                    var oSectionHead = null, oSectionHeadTitle = null, oSectionHeadTitleValue = null, oSectionHeadTitleValueText = null;
                    for (var i = 0; i < oSections.length; i++) {

                        if (oSections[i].className == "section") {
                            oHeaders = oSections[i].getElementsByTagName("h4");
                            if (oHeaders.length > 0) {
                                oSectionHead = TESCODIRECT.lib.DOM.node.create("div");
                                oSectionHead.id = ("section-header" + i);
                                oSectionHeadTitle = TESCODIRECT.lib.DOM.node.create("h4");
                                if (oHeaders[0].innerHTML == "Related Items") {
                                    oSectionHeadTitle.id = ("red");
                                }

                                if (oHeaders[0].innerHTML == "Related Items") {
                                    var oSectionHeadRelatedItems = TESCODIRECT.lib.DOM.node.create("img");
                                    oSectionHeadRelatedItems.id = ("tick");
                                    oSectionHeadRelatedItems.alt = "Related Items";
                                    oSectionHeadRelatedItems.src = "/direct/i/b/tick.gif";
                                    oSectionHeadTitle.appendChild(oSectionHeadRelatedItems);
                                }

                                oSectionHead.appendChild(oSectionHeadTitle);

                                oSectionHeadTitleValue = TESCODIRECT.lib.DOM.node.create("a");
                                if (oHeaders[0].innerHTML == "Related Items") {
                                    oSectionHeadTitleValue.id = ("red");
                                }

                                oSectionHeadTitleValue.href = "#";
                                oSectionHeadTitle.appendChild(oSectionHeadTitleValue);

                                oSectionHead.section = oSections[i];
                                oSectionHead.setAttribute("originalClassName", oSectionHead.className);

                                TESCODIRECT.lib.DOM.node.setTextValue(oSectionHeadTitleValue, oHeaders[0].innerHTML);
                                oHeader.appendChild(oSectionHead);


                                //set the initial header selected to be the first in the list
                                if (i == 0) {
                                    selectedHeader = oSectionHead;
                                }

                                TESCODIRECT.lib.event.attach(oSectionHead, "click",
									function(e) {
									    var oA = this.getElementsByTagName("a");
									    if (oA && oA.length > 0) {
									        // SJH 23/07/08 Cross Sell Up Sell
									        // Hide any carousels on any of the tabs
									        var allTabsWithCarousels = document.getElementsByClassName(document.body, 'sectionHolder');
									        if (allTabsWithCarousels) {
									            for (var idx = 0; idx < allTabsWithCarousels.length; idx++) {
									                ///allTabsWithCarousels[idx].style.display = "none";
									            }
									        }
									        // Show any carousels on the current tab

									        showingRelated = false;
									        var thisTabCarousels = document.getElementsByClassName(this.section, 'sectionHolder');
									        if (thisTabCarousels) {
									            showingRelated = true;
									            thisTabCarousels[0].style.display = "block";
									            // SJH 02/10/08 Choicestream Phase 1
									            var csAccess = document.getElementById('csAccessories');
									            if (csAccess != null) setTimeout("initCarousel('csAccessories')", 100);
									            // SJH 02/10/08 End of Choicestream Phase 1
									        }
									        // SJH End of Cross Sell Up Sell



									        oA[0].blur();
									    } else if (this.blur) {
									        this.blur();
									    }
									    if (selectedHeader) {
									        selectedHeader.className = selectedHeader.getAttribute("originalClassName");
									        selectedHeader.section.className = "section";
									        this.className = (this.className + " selected").trim();
									        selectedHeader = this;

									        this.section.className = "section display";
									    }
									    e.stopEvent();
									}
								);
                            }
                            if (iSectionCount == this.selectedIndex) {
                                selectedHeader = oSectionHead;
                                oSections[i].className = "section display";
                                if (oSectionHead) {
                                    oSectionHead.className = (oSectionHead.className + " selected").trim();
                                }
                            }
                            //oSections[i].removeChild(oHeaders[0]);
                            iSectionCount++;
                        }
                    }
                    o.insertBefore(oHeader, o.childNodes[0]);
                }
            }
        }
    };


    this.displayComparePrice = function() {
        var detailsAndSpec = document.getElementById("detailsAndSpec");

        var sectionHeaders = document.getElementsByClassName(detailsAndSpec, "section-header")[0];

        this.comparePriceHeader;


        //set the current tab to be selected
        for (var i = sectionHeaders.childNodes.length - 1; i >= 0; i--) {

            if (sectionHeaders.childNodes[i].nodeType == 1) {
                this.comparePriceHeader = sectionHeaders.childNodes[i];
                this.comparePriceHeader.className = "selected";
                this.comparePriceHeader.section.className = "section display";
                TESCODIRECT.lib.section.selectedHeader = this.comparePriceHeader;
                break;
            }
        }

        //set the other tabs to be deselected
        for (var i = sectionHeaders.childNodes.length - 1; i >= 0; i--) {

            if (sectionHeaders.childNodes[i] === this.comparePriceHeader) {
            } else {
                if (sectionHeaders.childNodes[i].nodeType == 1) {
                    sectionHeaders.childNodes[i].className = sectionHeaders.childNodes[i].getAttribute("originalClassName");
                    sectionHeaders.childNodes[i].section.className = "section";
                }
            }
        }
    }
}
