///<reference path="yui/yahoo/yahoo.js" />
///<reference path="yui/dom/dom.js" />
///<reference path="yui/event/event.js" />

(function() {

    //Local references to YUI
    var _DOM = YAHOO.util.Dom,
        _EVENT = YAHOO.util.Event;

    // Fix the Read Review link.
    POWERREVIEWS.display.registerCallback(function() {
        var activateInterval, activateCount = 0;

        function activateLink() {
            activateCount++;

            _DOM.getElementsByClassName("prSnippetReadReviews", "div", "product_container", function(el) {
                var readReviewLink = _DOM.getElementsByClassName("prSnippetLink", "a", el);
                if (readReviewLink.length != null && readReviewLink.length == 1) {
                    activateCount = 999;
                    readReviewLink[0].href = window.location.href + "#ReviewHeader";
                    _DOM.setStyle(readReviewLink[0], "visibility", "visible");
                }
            });

            if (activateCount > 20) {
                window.clearInterval(activateInterval);
            }
        }

        activateLink();
        if (activateCount == 1) {
            activateInterval = window.setInterval(activateLink, 500);
        }
    });


    //This variable will be set to true when the user clicks on the "Add To Cart" button
    //in order to prevent errors if the user double-clicked on the button.  
    var wasAddToCartPressed = false;

    _EVENT.on("linkIntShipping", "click", function(ev) {
        _DOM.removeClass("intShippingPanel", "hidden");
        BBE.intShippingPanel = new YAHOO.widget.Panel(
            "intShippingPanel", {
                width: "450px",
                visible: false,
                constraintoviewport: true,
                fixedcenter: true
            });
        BBE.intShippingPanel.render(document.body);
        BBE.intShippingPanel.show();
        BBE.intShippingPanel.cfg.setProperty("fixedcenter", false);
        _EVENT.preventDefault(ev);
    });

    _EVENT.on("linkSizeChart", "click", function(ev) {
        _DOM.removeClass("sizeChartPanel", "hidden");
        BBE.sizeChartPanel = new YAHOO.widget.Panel(
            "sizeChartPanel", {
                width: "780px",
                visible: false,
                constraintoviewport: true,
                fixedcenter: true
            });
        BBE.sizeChartPanel.render(document.body);
        BBE.sizeChartPanel.show();
        BBE.sizeChartPanel.cfg.setProperty("fixedcenter", false);

        BBE.trackPageEvent("Item Size Chart", "View-Size-Chart", BBE.product.name);

        _EVENT.preventDefault(ev);
    });

    _EVENT.on("zoom-image", "click", function(ev) {
        BBE.trackPageEvent("Item Enlarge", "View-Enlarge", BBE.product.name);
    });

    _EVENT.on("prev-arrow", "click", function(ev) {
        if (this.src.indexOf("disabled") == -1 || _EVENT.isIE) {
            BBE.trackPageEvent("Item Color Options", "Arrow-Left", BBE.product.name);
        }
    });

    _EVENT.on("next-arrow", "click", function(ev) {
        if (this.src.indexOf("disabled") == -1 || _EVENT.isIE) {
            BBE.trackPageEvent("Item Color Options", "Arrow-Right", BBE.product.name);
        }
    });


    _EVENT.onDOMReady(function() {
        var addToCartButtons = _DOM.getElementsByClassName("add-to-cart-button", "input", "product_container");

        _EVENT.on(addToCartButtons, "mouseover", function(ev) {
            window.status = "Add this product to your shopping cart.";
            return true;
        });

        _EVENT.on(addToCartButtons, "mouseout", function(ev) {
            window.status = "";
            return true;
        });

        _EVENT.on(addToCartButtons, "click", function(ev) {
            var attributeElements = document.getElementsByName("cboAttribute");
            var attributeIndex;

            //Ignore double-click on the "Add To Cart" button.            
            if (wasAddToCartPressed === false) {

                //Signal that the "Add To Cart" button was pressed.
                wasAddToCartPressed = true;

                for (attributeIndex = 0; attributeIndex < attributeElements.length; attributeIndex++) {
                    if (attributeElements[attributeIndex].selectedIndex == 0) {
                        alert("Please select Size, Color and Lens Option below before proceeding.");
                        _EVENT.preventDefault(ev);
                        wasAddToCartPressed = false;
                        return false;
                    }
                }
            } else {
                _EVENT.preventDefault(ev);
            }
        });


        //Track clicks on the "Write Reviews" link.
        var WriteReviewContainer = _DOM.getElementsByClassName("prSnippetWriteReview", "div", "product_container");
        if (WriteReviewContainer != null && WriteReviewContainer.length == 1) {
            _EVENT.on(WriteReviewContainer[0].getElementsByTagName("a"), "click", function(ev) {
                BBE.trackPageEvent("Item Review", "Write Review Click", BBE.product.name);
            });            
        }


        //Detect when the Rich Relevance recs are available and register Google Analytics event tracking.
        var RecContainers = _DOM.getElementsByClassName("recommandations", "div", document.body);
        var RecInterval = window.setInterval(function() {
            var isLoaded = true;

            _DOM.batch(RecContainers, function(recContainer) {
                if (recContainer.getElementsByTagName("a") == null) {
                    isLoaded = false;
                }
            });

            if (isLoaded) {
                window.clearInterval(RecInterval);

                _DOM.batch(RecContainers, function(recContainer) {
                    _EVENT.on(recContainer.getElementsByTagName("a"), "click", function(ev) {
                        BBE.trackPageEvent("Item Relevance", "Click-" + this.title.replace(/\s/g, "-"), BBE.product.name);
                    });
                });
            }
        }, 500);

    });
    // End OnDomReady
})();
  